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, $wgInputEncoding;
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( do_html_entity_decode( str_replace(' ', '_', $parts[1] ),
167 $wgInputEncoding ) );
168 $replacearray = array(
172 $u = $parts[0] . '#' .
173 str_replace( array_keys( $replacearray ),
174 array_values( $replacearray ),
178 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
180 wfProfileOut( $fname );
182 } elseif ( $nt->isAlwaysKnown() ) {
183 # Image links, special page links and self-links with fragements are always known.
184 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
186 wfProfileIn( $fname.'-immediate' );
187 # Work out link colour immediately
188 $aid = $nt->getArticleID() ;
190 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
192 $threshold = $wgUser->getOption('stubthreshold') ;
193 if ( $threshold > 0 ) {
194 $dbr =& wfGetDB( DB_SLAVE
);
195 $s = $dbr->selectRow(
199 'page_is_redirect' ),
200 array( 'page_id' => $aid ), $fname ) ;
201 if ( $s !== false ) {
202 $size = $s->page_len
;
203 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
204 $size = $threshold*2 ; # Really big
207 $size = $threshold*2 ; # Really big
212 if ( $size < $threshold ) {
213 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
215 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
218 wfProfileOut( $fname.'-immediate' );
220 wfProfileOut( $fname );
225 * Pass a title object, not a title string
227 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
228 global $wgOut, $wgTitle, $wgInputEncoding;
230 $fname = 'Linker::makeKnownLinkObj';
231 wfProfileIn( $fname );
233 if ( !is_object( $nt ) ) {
234 wfProfileOut( $fname );
238 $u = $nt->escapeLocalURL( $query );
239 if ( '' != $nt->getFragment() ) {
240 if( $nt->getPrefixedDbkey() == '' ) {
243 $text = htmlspecialchars( $nt->getFragment() );
246 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT
, $wgInputEncoding ) );
247 $replacearray = array(
251 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
254 $text = htmlspecialchars( $nt->getPrefixedText() );
256 $style = $this->getInternalLinkAttributesObj( $nt, $text );
258 list( $inside, $trail ) = Linker
::splitTrail( $trail );
259 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
260 wfProfileOut( $fname );
265 * Pass a title object, not a title string
267 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
269 if ( ! isset($nt) ) {
270 # wfDebugDieBacktrace();
271 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
274 $fname = 'Linker::makeBrokenLinkObj';
275 wfProfileIn( $fname );
277 if ( '' == $query ) {
280 $q = 'action=edit&'.$query;
282 $u = $nt->escapeLocalURL( $q );
285 $text = htmlspecialchars( $nt->getPrefixedText() );
287 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
289 list( $inside, $trail ) = Linker
::splitTrail( $trail );
290 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
292 wfProfileOut( $fname );
297 * Pass a title object, not a title string
299 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
300 $link = $nt->getPrefixedURL();
302 $u = $nt->escapeLocalURL( $query );
305 $text = htmlspecialchars( $nt->getPrefixedText() );
307 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
309 list( $inside, $trail ) = Linker
::splitTrail( $trail );
310 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
314 /** @todo document */
315 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
316 $u = $nt->escapeLocalURL( $query );
318 $text = htmlspecialchars( $nt->getPrefixedText() );
320 list( $inside, $trail ) = Linker
::splitTrail( $trail );
321 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
324 /** @todo document */
325 function fnamePart( $url ) {
326 $basename = strrchr( $url, '/' );
327 if ( false === $basename ) {
330 $basename = substr( $basename, 1 );
332 return htmlspecialchars( $basename );
335 /** Obsolete alias */
336 function makeImage( $url, $alt = '' ) {
337 return $this->makeExternalImage( $url, $alt );
340 /** @todo document */
341 function makeExternalImage( $url, $alt = '' ) {
344 $alt = $this->fnamePart( $url );
346 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
350 /** @todo document */
351 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
352 $thumb = false, $manual_thumb = '' )
354 global $wgContLang, $wgUser, $wgThumbLimits;
356 $img = new Image( $nt );
357 $url = $img->getViewURL();
358 $prefix = $postfix = '';
360 if ( 'center' == $align )
362 $prefix = '<div class="center">';
367 if ( $thumb ||
$framed ) {
369 # Create a thumbnail. Alignment depends on language
370 # writing direction, # right aligned for left-to-right-
371 # languages ("Western languages"), left-aligned
372 # for right-to-left-languages ("Semitic languages")
374 # If thumbnail width has not been provided, it is set
375 # to the default user option as specified in Language*.php
376 if ( $align == '' ) {
377 $align = $wgContLang->isRTL() ?
'left' : 'right';
381 if ( $width === false ) {
382 $wopt = $wgUser->getOption( 'thumbsize' );
384 if( !isset( $wgThumbLimits[$wopt] ) ) {
385 $wopt = User
::getDefaultOption( 'thumbsize' );
388 $width = $wgThumbLimits[$wopt];
391 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
393 } elseif ( $width ) {
395 # Create a resized image, without the additional thumbnail
398 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
399 $width = $img->getWidth() * $height / $img->getHeight();
401 if ( '' == $manual_thumb ) {
402 $url = $img->createThumb( $width );
406 $u = $nt->escapeLocalURL();
408 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
409 //$s .= "<br />{$alt}<br />{$url}<br />\n";
411 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
412 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
414 if ( '' != $align ) {
415 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
417 return str_replace("\n", ' ',$prefix.$s.$postfix);
421 * Make HTML for a thumbnail including image, border and caption
422 * $img is an Image object
424 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
425 global $wgStylePath, $wgContLang;
426 $url = $img->getViewURL();
428 $width = $height = 0;
429 if ( $img->exists() )
431 $width = $img->getWidth();
432 $height = $img->getHeight();
434 if ( 0 == $width ||
0 == $height )
436 $width = $height = 200;
438 if ( $boxwidth == 0 )
444 // Use image dimensions, don't scale
446 $oboxwidth = $boxwidth +
2;
447 $boxheight = $height;
450 $h = intval( $height/($width/$boxwidth) );
451 $oboxwidth = $boxwidth +
2;
452 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
454 $boxwidth *= $boxheight/$h;
458 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
461 if ( $manual_thumb != '' ) # Use manually specified thumbnail
463 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
464 $manual_img = new Image( $manual_title );
465 $thumbUrl = $manual_img->getViewURL();
466 if ( $manual_img->exists() )
468 $width = $manual_img->getWidth();
469 $height = $manual_img->getHeight();
471 $boxheight = $height ;
472 $oboxwidth = $boxwidth +
2 ;
476 $u = $img->getEscapeLocalURL();
478 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
479 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
480 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
482 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
483 if ( $thumbUrl == '' ) {
484 $s .= $this->makeBrokenImageLinkObj( $img->getTitle
);
487 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
488 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
489 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
490 'longdesc="'.$u.'" /></a>';
494 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
495 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
496 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
497 'width="15" height="11" alt="'.$more.'" /></a></div>';
500 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
501 return str_replace("\n", ' ', $s);
505 * Pass a title object, not a title string
507 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
509 if ( ! isset($nt) ) {
510 # wfDebugDieBacktrace();
511 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
514 $fname = 'Linker::makeBrokenImageLinkObj';
515 wfProfileIn( $fname );
517 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
518 if ( '' != $query ) {
521 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
522 $url = $uploadTitle->escapeLocalURL( $q );
525 $text = htmlspecialchars( $nt->getPrefixedText() );
527 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
528 list( $inside, $trail ) = Linker
::splitTrail( $trail );
529 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
531 wfProfileOut( $fname );
535 /** @todo document */
536 function makeMediaLink( $name, $url, $alt = '' ) {
537 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
538 return $this->makeMediaLinkObj( $nt, $alt );
542 * Create a direct link to a given uploaded file.
544 * @param Title $title
545 * @param string $text pre-sanitized HTML
546 * @param bool $nourl Mask absolute URLs, so the parser doesn't
547 * linkify them (it is currently not context-aware)
548 * @return string HTML
551 * @todo Handle invalid or missing images better.
553 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
554 if( is_null( $title ) ) {
555 ### HOTFIX. Instead of breaking, return empty string.
558 $name = $title->getDBKey();
559 $img = new Image( $title );
560 $url = $img->getURL();
562 $url = str_replace( "http://", "http-noparse://", $url );
564 $alt = htmlspecialchars( $title->getText() );
568 $u = htmlspecialchars( $url );
569 return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";
573 /** @todo document */
574 function specialLink( $name, $key = '' ) {
577 if ( '' == $key ) { $key = strtolower( $name ); }
578 $pn = $wgContLang->ucfirst( $name );
579 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
583 /** @todo document */
584 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
585 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
586 global $wgNoFollowLinks;
587 if( $wgNoFollowLinks ) {
588 $style .= ' rel="nofollow"';
590 $url = htmlspecialchars( $url );
592 $text = htmlspecialchars( $text );
594 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
598 * This function is called by all recent changes variants, by the page history,
599 * and by the user contributions list. It is responsible for formatting edit
600 * comments. It escapes any HTML in the comment, but adds some CSS to format
601 * auto-generated comments (from section editing) and formats [[wikilinks]].
603 * The &$title parameter must be a title OBJECT. It is used to generate a
604 * direct link to the section in the autocomment.
605 * @author Erik Moeller <moeller@scireview.de>
607 * Note: there's not always a title to pass to this function.
608 * Since you can't set a default parameter for a reference, I've turned it
609 * temporarily to a value pass. Should be adjusted further. --brion
611 function formatComment($comment, $title = NULL) {
612 $fname = 'Linker::formatComment';
613 wfProfileIn( $fname );
616 $comment = str_replace( "\n", " ", $comment );
617 $comment = htmlspecialchars( $comment );
619 # The pattern for autogen comments is / * foo * /, which makes for
621 # We look for all comments, match any text before and after the comment,
622 # add a separator where needed and format the comment itself with CSS
623 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
631 # This is hackish but should work in most cases.
632 $section=str_replace('[[','',$section);
633 $section=str_replace(']]','',$section);
634 $title->mFragment
=$section;
635 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
639 if($pre) { $auto = $sep.' '.$auto; }
640 if($post) { $auto .= ' '.$sep; }
641 $auto='<span class="autocomment">'.$auto.'</span>';
642 $comment=$pre.$auto.$post;
645 # format regular and media links - all other wiki formatting
647 $medians = $wgContLang->getNsText( NS_MEDIA
) . ':';
648 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
649 # Handle link renaming [[foo|text]] will show link as "text"
650 if( "" != $match[3] ) {
655 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
656 # Media link; trail not supported.
657 $linkRegexp = '/\[\[(.*?)\]\]/';
658 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
661 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
662 $trail = $submatch[1];
666 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
667 if ($match[1][0] == ':')
668 $match[1] = substr($match[1], 1);
669 $thelink = $this->makeLink( $match[1], $text, "", $trail );
671 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
673 wfProfileOut( $fname );
678 * Wrap a comment in standard punctuation and formatting if
679 * it's non-empty, otherwise return empty string.
681 * @param string $comment
682 * @param Title $title
686 function commentBlock( $comment, $title = NULL ) {
687 if( $comment == '' ||
$comment == '*' ) {
690 $formatted = $this->formatComment( $comment, $title );
691 return " <span class='comment'>($formatted)</span>";
695 /** @todo document */
696 function tocIndent() {
700 /** @todo document */
701 function tocUnindent($level) {
702 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
706 * parameter level defines if we are on an indentation level
708 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
709 return "\n<li class='toclevel-$level'><a href=\"#" .
710 $anchor . '"><span class="tocnumber">' .
711 $tocnumber . '</span> <span class="toctext">' .
712 $tocline . '</span></a>';
715 /** @todo document */
716 function tocLineEnd() {
720 /** @todo document */
721 function tocList($toc) {
722 global $wgJsMimeType;
723 return "<table id='toc' class='toc'><tr><td>"
724 . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
726 . "</ul>\n</td></tr></table>\n"
727 . '<script type="'.$wgJsMimeType.'">'
728 . ' if (window.showTocToggle) {'
729 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
730 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
731 . ' showTocToggle();'
736 /** @todo document */
737 function editSectionLinkForOther( $title, $section ) {
741 $title = Title
::newFromText($title);
742 $editurl = '§ion='.$section;
743 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
745 if( $wgContLang->isRTL() ) {
752 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
756 /** @todo document */
757 function editSectionLink( $nt, $section ) {
761 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
762 # Section edit links would be out of sync on an old page.
763 # But, if we're diffing to the current page, they'll be
768 $editurl = '§ion='.$section;
769 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
771 if( $wgContLang->isRTL() ) {
778 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
782 * Split a link trail, return the "inside" portion and the remainder of the trail
783 * as a two-element array
787 function splitTrail( $trail ) {
788 static $regex = false;
789 if ( $regex === false ) {
791 $regex = $wgContLang->linkTrail();
794 if ( '' != $trail ) {
795 if ( preg_match( $regex, $trail, $m ) ) {
800 return array( $inside, $trail );