3 * Split off some of the internal bits from Skin.php.
4 * These functions are used for primarily page content:
5 * links, embedded images, table of contents. Links are
6 * also used in the skin.
11 * For the moment, Skin is a descendent class of Linker.
12 * In the future, it should probably be further split
13 * so that ever other bit of the wiki doesn't have to
14 * go loading up Skin to get at it.
25 function postParseLinkColour( $s = NULL ) {
30 function getExternalLinkAttributes( $link, $text, $class='' ) {
33 $same = ($link == $text);
34 $link = urldecode( $link );
35 $link = $wgContLang->checkTitleEncoding( $link );
36 $link = preg_replace( '/[\\x00-\\x1f_]/', ' ', $link );
37 $link = htmlspecialchars( $link );
39 $r = ($class != '') ?
" class='$class'" : " class='external'";
41 $r .= " title=\"{$link}\"";
46 function getInternalLinkAttributes( $link, $text, $broken = false ) {
47 $link = urldecode( $link );
48 $link = str_replace( '_', ' ', $link );
49 $link = htmlspecialchars( $link );
51 if( $broken == 'stub' ) {
53 } else if ( $broken == 'yes' ) {
59 $r .= " title=\"{$link}\"";
66 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
67 if( $broken == 'stub' ) {
69 } else if ( $broken == 'yes' ) {
75 $r .= ' title="' . $nt->getEscapedText() . '"';
80 * Note: This function MUST call getArticleID() on the link,
81 * otherwise the cache won't get updated properly. See LINKCACHE.DOC.
83 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
84 wfProfileIn( 'Linker::makeLink' );
85 $nt = Title
::newFromText( $title );
87 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
89 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
90 $result = $text == "" ?
$title : $text;
93 wfProfileOut( 'Linker::makeLink' );
98 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
99 $nt = Title
::newFromText( $title );
101 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
103 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
104 return $text == '' ?
$title : $text;
108 /** @todo document */
109 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
110 $nt = Title
::newFromText( $title );
112 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
114 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
115 return $text == '' ?
$title : $text;
119 /** @todo document */
120 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
121 $nt = Title
::newFromText( $title );
123 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
125 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
126 return $text == '' ?
$title : $text;
131 * Pass a title object, not a title string
133 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
134 global $wgOut, $wgUser;
135 $fname = 'Linker::makeLinkObj';
136 wfProfileIn( $fname );
139 if ( ! is_object($nt) ) {
140 # wfDebugDieBacktrace();
141 wfProfileOut( $fname );
142 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
145 $ns = $nt->getNamespace();
146 $dbkey = $nt->getDBkey();
147 if ( $nt->isExternal() ) {
148 $u = $nt->getFullURL();
149 $link = $nt->getPrefixedURL();
150 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
151 $style = $this->getExternalLinkAttributes( $link, $text, 'extiw' );
154 if ( '' != $trail ) {
155 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
161 # Check for anchors, normalize the anchor
163 $parts = explode( '#', $u, 2 );
164 if ( count( $parts ) == 2 ) {
165 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
166 $replacearray = array(
170 $u = $parts[0] . '#' .
171 str_replace( array_keys( $replacearray ),
172 array_values( $replacearray ),
176 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
178 wfProfileOut( $fname );
180 } elseif ( $nt->isAlwaysKnown() ) {
181 # Image links, special page links and self-links with fragements are always known.
182 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
184 wfProfileIn( $fname.'-immediate' );
185 # Work out link colour immediately
186 $aid = $nt->getArticleID() ;
188 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
190 $threshold = $wgUser->getOption('stubthreshold') ;
191 if ( $threshold > 0 ) {
192 $dbr =& wfGetDB( DB_SLAVE
);
193 $s = $dbr->selectRow(
197 'page_is_redirect' ),
198 array( 'page_id' => $aid ), $fname ) ;
199 if ( $s !== false ) {
200 $size = $s->page_len
;
201 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
202 $size = $threshold*2 ; # Really big
205 $size = $threshold*2 ; # Really big
210 if ( $size < $threshold ) {
211 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
213 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
216 wfProfileOut( $fname.'-immediate' );
218 wfProfileOut( $fname );
223 * Pass a title object, not a title string
225 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
226 global $wgOut, $wgTitle;
228 $fname = 'Linker::makeKnownLinkObj';
229 wfProfileIn( $fname );
231 if ( !is_object( $nt ) ) {
232 wfProfileOut( $fname );
236 $u = $nt->escapeLocalURL( $query );
237 if ( '' != $nt->getFragment() ) {
238 if( $nt->getPrefixedDbkey() == '' ) {
241 $text = htmlspecialchars( $nt->getFragment() );
244 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
245 $replacearray = array(
249 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
252 $text = htmlspecialchars( $nt->getPrefixedText() );
254 $style = $this->getInternalLinkAttributesObj( $nt, $text );
256 list( $inside, $trail ) = Linker
::splitTrail( $trail );
257 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
258 wfProfileOut( $fname );
263 * Pass a title object, not a title string
265 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
267 if ( ! isset($nt) ) {
268 # wfDebugDieBacktrace();
269 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
272 $fname = 'Linker::makeBrokenLinkObj';
273 wfProfileIn( $fname );
275 if ( '' == $query ) {
278 $q = 'action=edit&'.$query;
280 $u = $nt->escapeLocalURL( $q );
283 $text = htmlspecialchars( $nt->getPrefixedText() );
285 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
287 list( $inside, $trail ) = Linker
::splitTrail( $trail );
288 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
290 wfProfileOut( $fname );
295 * Pass a title object, not a title string
297 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
298 $link = $nt->getPrefixedURL();
300 $u = $nt->escapeLocalURL( $query );
303 $text = htmlspecialchars( $nt->getPrefixedText() );
305 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
307 list( $inside, $trail ) = Linker
::splitTrail( $trail );
308 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
313 * Generate either a normal exists-style link or a stub link, depending
314 * on the given page size.
318 * @param string $text
319 * @param string $query
320 * @param string $trail
321 * @param string $prefix
322 * @return string HTML of link
324 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
326 $threshold = IntVal( $wgUser->getOption( 'stubthreshold' ) );
327 if( $size < $threshold ) {
328 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
330 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
334 /** @todo document */
335 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
336 $u = $nt->escapeLocalURL( $query );
338 $text = htmlspecialchars( $nt->getPrefixedText() );
340 list( $inside, $trail ) = Linker
::splitTrail( $trail );
341 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
344 /** @todo document */
345 function fnamePart( $url ) {
346 $basename = strrchr( $url, '/' );
347 if ( false === $basename ) {
350 $basename = substr( $basename, 1 );
352 return htmlspecialchars( $basename );
355 /** Obsolete alias */
356 function makeImage( $url, $alt = '' ) {
357 return $this->makeExternalImage( $url, $alt );
360 /** @todo document */
361 function makeExternalImage( $url, $alt = '' ) {
364 $alt = $this->fnamePart( $url );
366 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
370 /** @todo document */
371 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
372 $thumb = false, $manual_thumb = '' )
374 global $wgContLang, $wgUser, $wgThumbLimits;
376 $img = new Image( $nt );
377 $url = $img->getViewURL();
378 $prefix = $postfix = '';
380 if ( 'center' == $align )
382 $prefix = '<div class="center">';
387 if ( $thumb ||
$framed ) {
389 # Create a thumbnail. Alignment depends on language
390 # writing direction, # right aligned for left-to-right-
391 # languages ("Western languages"), left-aligned
392 # for right-to-left-languages ("Semitic languages")
394 # If thumbnail width has not been provided, it is set
395 # to the default user option as specified in Language*.php
396 if ( $align == '' ) {
397 $align = $wgContLang->isRTL() ?
'left' : 'right';
401 if ( $width === false ) {
402 $wopt = $wgUser->getOption( 'thumbsize' );
404 if( !isset( $wgThumbLimits[$wopt] ) ) {
405 $wopt = User
::getDefaultOption( 'thumbsize' );
408 $width = $wgThumbLimits[$wopt];
411 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
413 } elseif ( $width ) {
415 # Create a resized image, without the additional thumbnail
418 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
419 $width = $img->getWidth() * $height / $img->getHeight();
421 if ( '' == $manual_thumb ) {
422 $thumb = $img->getThumbnail( $width );
424 $height = $thumb->height
;
425 $url = $thumb->getUrl( );
430 $u = $nt->escapeLocalURL();
432 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
433 //$s .= "<br />{$alt}<br />{$url}<br />\n";
435 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
436 '<img src="'.$url.'" alt="'.$alt.'" ' .
437 'width="'.$width.'" height="'.$height.'" ' .
438 'longdesc="'.$u.'" /></a>';
440 if ( '' != $align ) {
441 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
443 return str_replace("\n", ' ',$prefix.$s.$postfix);
447 * Make HTML for a thumbnail including image, border and caption
448 * $img is an Image object
450 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
451 global $wgStylePath, $wgContLang;
452 $url = $img->getViewURL();
454 $width = $height = 0;
455 if ( $img->exists() )
457 $width = $img->getWidth();
458 $height = $img->getHeight();
460 if ( 0 == $width ||
0 == $height )
462 $width = $height = 200;
464 if ( $boxwidth == 0 )
470 // Use image dimensions, don't scale
472 $oboxwidth = $boxwidth +
2;
473 $boxheight = $height;
476 $h = intval( $height/($width/$boxwidth) );
477 $oboxwidth = $boxwidth +
2;
478 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
480 $boxwidth *= $boxheight/$h;
484 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
487 if ( $manual_thumb != '' ) # Use manually specified thumbnail
489 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
490 $manual_img = new Image( $manual_title );
491 $thumbUrl = $manual_img->getViewURL();
492 if ( $manual_img->exists() )
494 $width = $manual_img->getWidth();
495 $height = $manual_img->getHeight();
497 $boxheight = $height ;
498 $oboxwidth = $boxwidth +
2 ;
502 $u = $img->getEscapeLocalURL();
504 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
505 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
506 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
508 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
509 if ( $thumbUrl == '' ) {
510 $s .= $this->makeBrokenImageLinkObj( $img->getTitle
);
513 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
514 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
515 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
516 'longdesc="'.$u.'" /></a>';
520 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
521 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
522 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
523 'width="15" height="11" alt="'.$more.'" /></a></div>';
526 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
527 return str_replace("\n", ' ', $s);
531 * Pass a title object, not a title string
533 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
535 if ( ! isset($nt) ) {
536 # wfDebugDieBacktrace();
537 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
540 $fname = 'Linker::makeBrokenImageLinkObj';
541 wfProfileIn( $fname );
543 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
544 if ( '' != $query ) {
547 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
548 $url = $uploadTitle->escapeLocalURL( $q );
551 $text = htmlspecialchars( $nt->getPrefixedText() );
553 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
554 list( $inside, $trail ) = Linker
::splitTrail( $trail );
555 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
557 wfProfileOut( $fname );
561 /** @todo document */
562 function makeMediaLink( $name, $url, $alt = '' ) {
563 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
564 return $this->makeMediaLinkObj( $nt, $alt );
568 * Create a direct link to a given uploaded file.
570 * @param Title $title
571 * @param string $text pre-sanitized HTML
572 * @param bool $nourl Mask absolute URLs, so the parser doesn't
573 * linkify them (it is currently not context-aware)
574 * @return string HTML
577 * @todo Handle invalid or missing images better.
579 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
580 if( is_null( $title ) ) {
581 ### HOTFIX. Instead of breaking, return empty string.
584 $name = $title->getDBKey();
585 $img = new Image( $title );
586 if( $img->exists() ) {
587 $url = $img->getURL();
589 $url = str_replace( "http://", "http-noparse://", $url );
593 $upload = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
594 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
597 $alt = htmlspecialchars( $title->getText() );
601 $u = htmlspecialchars( $url );
602 return "<a href=\"{$u}\" class='$class' title=\"{$alt}\">{$text}</a>";
606 /** @todo document */
607 function specialLink( $name, $key = '' ) {
610 if ( '' == $key ) { $key = strtolower( $name ); }
611 $pn = $wgContLang->ucfirst( $name );
612 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
616 /** @todo document */
617 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
618 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
619 global $wgNoFollowLinks;
620 if( $wgNoFollowLinks ) {
621 $style .= ' rel="nofollow"';
623 $url = htmlspecialchars( $url );
625 $text = htmlspecialchars( $text );
627 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
631 * This function is called by all recent changes variants, by the page history,
632 * and by the user contributions list. It is responsible for formatting edit
633 * comments. It escapes any HTML in the comment, but adds some CSS to format
634 * auto-generated comments (from section editing) and formats [[wikilinks]].
636 * The &$title parameter must be a title OBJECT. It is used to generate a
637 * direct link to the section in the autocomment.
638 * @author Erik Moeller <moeller@scireview.de>
640 * Note: there's not always a title to pass to this function.
641 * Since you can't set a default parameter for a reference, I've turned it
642 * temporarily to a value pass. Should be adjusted further. --brion
644 function formatComment($comment, $title = NULL) {
645 $fname = 'Linker::formatComment';
646 wfProfileIn( $fname );
649 $comment = str_replace( "\n", " ", $comment );
650 $comment = htmlspecialchars( $comment );
652 # The pattern for autogen comments is / * foo * /, which makes for
654 # We look for all comments, match any text before and after the comment,
655 # add a separator where needed and format the comment itself with CSS
656 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
664 # This is hackish but should work in most cases.
665 $section=str_replace('[[','',$section);
666 $section=str_replace(']]','',$section);
667 $title->mFragment
=$section;
668 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
672 if($pre) { $auto = $sep.' '.$auto; }
673 if($post) { $auto .= ' '.$sep; }
674 $auto='<span class="autocomment">'.$auto.'</span>';
675 $comment=$pre.$auto.$post;
678 # format regular and media links - all other wiki formatting
680 $medians = $wgContLang->getNsText( NS_MEDIA
) . ':';
681 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
682 # Handle link renaming [[foo|text]] will show link as "text"
683 if( "" != $match[3] ) {
688 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
689 # Media link; trail not supported.
690 $linkRegexp = '/\[\[(.*?)\]\]/';
691 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
694 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
695 $trail = $submatch[1];
699 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
700 if ($match[1][0] == ':')
701 $match[1] = substr($match[1], 1);
702 $thelink = $this->makeLink( $match[1], $text, "", $trail );
704 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
706 wfProfileOut( $fname );
711 * Wrap a comment in standard punctuation and formatting if
712 * it's non-empty, otherwise return empty string.
714 * @param string $comment
715 * @param Title $title
719 function commentBlock( $comment, $title = NULL ) {
720 if( $comment == '' ||
$comment == '*' ) {
723 $formatted = $this->formatComment( $comment, $title );
724 return " <span class='comment'>($formatted)</span>";
728 /** @todo document */
729 function tocIndent() {
733 /** @todo document */
734 function tocUnindent($level) {
735 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
739 * parameter level defines if we are on an indentation level
741 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
742 return "\n<li class='toclevel-$level'><a href=\"#" .
743 $anchor . '"><span class="tocnumber">' .
744 $tocnumber . '</span> <span class="toctext">' .
745 $tocline . '</span></a>';
748 /** @todo document */
749 function tocLineEnd() {
753 /** @todo document */
754 function tocList($toc) {
755 global $wgJsMimeType;
756 return "<table id='toc' class='toc'><tr><td>"
757 . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
759 . "</ul>\n</td></tr></table>\n"
760 . '<script type="'.$wgJsMimeType.'">'
761 . ' if (window.showTocToggle) {'
762 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
763 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
764 . ' showTocToggle();'
769 /** @todo document */
770 function editSectionLinkForOther( $title, $section ) {
774 $title = Title
::newFromText($title);
775 $editurl = '§ion='.$section;
776 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
778 if( $wgContLang->isRTL() ) {
785 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
789 /** @todo document */
790 function editSectionLink( $nt, $section ) {
794 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
795 # Section edit links would be out of sync on an old page.
796 # But, if we're diffing to the current page, they'll be
801 $editurl = '§ion='.$section;
802 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
804 if( $wgContLang->isRTL() ) {
811 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
815 * Split a link trail, return the "inside" portion and the remainder of the trail
816 * as a two-element array
820 function splitTrail( $trail ) {
821 static $regex = false;
822 if ( $regex === false ) {
824 $regex = $wgContLang->linkTrail();
827 if ( '' != $trail ) {
828 if ( preg_match( $regex, $trail, $m ) ) {
833 return array( $inside, $trail );