53deb39f2bb3a509663cad93d8d5e0295cbfc236
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='' ) {
31 $link = htmlspecialchars( $link );
33 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
35 $r .= " title=\"{$link}\"";
39 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
42 $same = ($link == $text);
43 $link = urldecode( $link );
44 $link = $wgContLang->checkTitleEncoding( $link );
45 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
46 $link = htmlspecialchars( $link );
48 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
50 $r .= " title=\"{$link}\"";
55 function getInternalLinkAttributes( $link, $text, $broken = false ) {
56 $link = urldecode( $link );
57 $link = str_replace( '_', ' ', $link );
58 $link = htmlspecialchars( $link );
60 if( $broken == 'stub' ) {
62 } else if ( $broken == 'yes' ) {
68 $r .= " title=\"{$link}\"";
73 * @param $nt Title object.
74 * @param $text String: FIXME
75 * @param $broken Boolean: FIXME, default 'false'.
77 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
78 if( $broken == 'stub' ) {
80 } else if ( $broken == 'yes' ) {
86 $r .= ' title="' . $nt->getEscapedText() . '"';
91 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
92 * it if you already have a title object handy. See makeLinkObj for further documentation.
94 * @param $title String: the text of the title
95 * @param $text String: link text
96 * @param $query String: optional query part
97 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
98 * be included in the link text. Other characters will be appended after
99 * the end of the link.
101 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
102 wfProfileIn( 'Linker::makeLink' );
103 $nt = Title
::newFromText( $title );
105 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
107 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
108 $result = $text == "" ?
$title : $text;
111 wfProfileOut( 'Linker::makeLink' );
116 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
117 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
119 * @param $title String: the text of the title
120 * @param $text String: link text
121 * @param $query String: optional query part
122 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
123 * be included in the link text. Other characters will be appended after
124 * the end of the link.
126 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
127 $nt = Title
::newFromText( $title );
129 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
131 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
132 return $text == '' ?
$title : $text;
137 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
138 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
140 * @param string $title The text of the title
141 * @param string $text Link text
142 * @param string $query Optional query part
143 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
144 * be included in the link text. Other characters will be appended after
145 * the end of the link.
147 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
148 $nt = Title
::newFromText( $title );
150 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
152 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
153 return $text == '' ?
$title : $text;
158 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
159 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
161 * @param $title String: the text of the title
162 * @param $text String: link text
163 * @param $query String: optional query part
164 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
165 * be included in the link text. Other characters will be appended after
166 * the end of the link.
168 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
169 $nt = Title
::newFromText( $title );
171 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
173 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
174 return $text == '' ?
$title : $text;
179 * Make a link for a title which may or may not be in the database. If you need to
180 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
181 * call to this will result in a DB query.
183 * @param $title String: the text of the title
184 * @param $text String: link text
185 * @param $query String: optional query part
186 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
187 * be included in the link text. Other characters will be appended after
188 * the end of the link.
190 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
192 $fname = 'Linker::makeLinkObj';
193 wfProfileIn( $fname );
196 if ( ! is_object($nt) ) {
197 # throw new MWException();
198 wfProfileOut( $fname );
199 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
202 $ns = $nt->getNamespace();
203 $dbkey = $nt->getDBkey();
204 if ( $nt->isExternal() ) {
205 $u = $nt->getFullURL();
206 $link = $nt->getPrefixedURL();
207 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
208 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
211 if ( '' != $trail ) {
212 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
218 # Check for anchors, normalize the anchor
220 $parts = explode( '#', $u, 2 );
221 if ( count( $parts ) == 2 ) {
222 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
223 $replacearray = array(
227 $u = $parts[0] . '#' .
228 str_replace( array_keys( $replacearray ),
229 array_values( $replacearray ),
233 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
235 wfProfileOut( $fname );
237 } elseif ( $nt->isAlwaysKnown() ) {
238 # Image links, special page links and self-links with fragements are always known.
239 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
241 wfProfileIn( $fname.'-immediate' );
242 # Work out link colour immediately
243 $aid = $nt->getArticleID() ;
245 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
247 $threshold = $wgUser->getOption('stubthreshold') ;
248 if ( $threshold > 0 ) {
249 $dbr =& wfGetDB( DB_SLAVE
);
250 $s = $dbr->selectRow(
254 'page_is_redirect' ),
255 array( 'page_id' => $aid ), $fname ) ;
256 if ( $s !== false ) {
257 $size = $s->page_len
;
258 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
259 $size = $threshold*2 ; # Really big
262 $size = $threshold*2 ; # Really big
267 if ( $size < $threshold ) {
268 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
270 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
273 wfProfileOut( $fname.'-immediate' );
275 wfProfileOut( $fname );
280 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
281 * it doesn't have to do a database query. It's also valid for interwiki titles and special
284 * @param $nt Title object of target page
285 * @param $text String: text to replace the title
286 * @param $query String: link target
287 * @param $trail String: text after link
288 * @param $prefix String: text before link text
289 * @param $aprops String: extra attributes to the a-element
290 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
291 * @return the a-element
293 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
295 $fname = 'Linker::makeKnownLinkObj';
296 wfProfileIn( $fname );
298 if ( !is_object( $nt ) ) {
299 wfProfileOut( $fname );
303 $u = $nt->escapeLocalURL( $query );
304 if ( $nt->getFragment() != '' ) {
305 if( $nt->getPrefixedDbkey() == '' ) {
308 $text = htmlspecialchars( $nt->getFragment() );
311 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
312 $replacearray = array(
316 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
319 $text = htmlspecialchars( $nt->getPrefixedText() );
321 if ( $style == '' ) {
322 $style = $this->getInternalLinkAttributesObj( $nt, $text );
325 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
327 list( $inside, $trail ) = Linker
::splitTrail( $trail );
328 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
329 wfProfileOut( $fname );
334 * Make a red link to the edit page of a given title.
336 * @param $title String: The text of the title
337 * @param $text String: Link text
338 * @param $query String: Optional query part
339 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
340 * be included in the link text. Other characters will be appended after
341 * the end of the link.
343 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
345 if ( ! isset($nt) ) {
346 # throw new MWException();
347 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
350 $fname = 'Linker::makeBrokenLinkObj';
351 wfProfileIn( $fname );
353 if ( '' == $query ) {
356 $q = 'action=edit&'.$query;
358 $u = $nt->escapeLocalURL( $q );
361 $text = htmlspecialchars( $nt->getPrefixedText() );
363 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
365 list( $inside, $trail ) = Linker
::splitTrail( $trail );
366 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
368 wfProfileOut( $fname );
373 * Make a brown link to a short article.
375 * @param $title String: the text of the title
376 * @param $text String: link text
377 * @param $query String: optional query part
378 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
379 * be included in the link text. Other characters will be appended after
380 * the end of the link.
382 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
383 $link = $nt->getPrefixedURL();
385 $u = $nt->escapeLocalURL( $query );
388 $text = htmlspecialchars( $nt->getPrefixedText() );
390 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
392 list( $inside, $trail ) = Linker
::splitTrail( $trail );
393 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
398 * Generate either a normal exists-style link or a stub link, depending
399 * on the given page size.
401 * @param $size Integer
402 * @param $nt Title object.
403 * @param $text String
404 * @param $query String
405 * @param $trail String
406 * @param $prefix String
407 * @return string HTML of link
409 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
411 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
412 if( $size < $threshold ) {
413 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
415 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
420 * Make appropriate markup for a link to the current article. This is currently rendered
421 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
422 * despite $query not being used.
424 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
426 $text = htmlspecialchars( $nt->getPrefixedText() );
428 list( $inside, $trail ) = Linker
::splitTrail( $trail );
429 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
432 /** @todo document */
433 function fnamePart( $url ) {
434 $basename = strrchr( $url, '/' );
435 if ( false === $basename ) {
438 $basename = substr( $basename, 1 );
440 return htmlspecialchars( $basename );
443 /** Obsolete alias */
444 function makeImage( $url, $alt = '' ) {
445 return $this->makeExternalImage( $url, $alt );
448 /** @todo document */
449 function makeExternalImage( $url, $alt = '' ) {
451 $alt = $this->fnamePart( $url );
453 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
457 /** @todo document */
458 function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
459 $thumb = false, $manual_thumb = '', $page = null )
461 global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
463 $img = new Image( $nt );
465 if ( ! is_null( $page ) ) {
466 $img->selectPage( $page );
469 if ( !$img->allowInlineDisplay() && $img->exists() ) {
470 return $this->makeKnownLinkObj( $nt );
473 $url = $img->getViewURL();
474 $error = $prefix = $postfix = '';
476 wfDebug( "makeImageLinkObj: '$width'x'$height', \"$label\"\n" );
478 if ( 'center' == $align )
480 $prefix = '<div class="center">';
485 if ( $thumb ||
$framed ) {
487 # Create a thumbnail. Alignment depends on language
488 # writing direction, # right aligned for left-to-right-
489 # languages ("Western languages"), left-aligned
490 # for right-to-left-languages ("Semitic languages")
492 # If thumbnail width has not been provided, it is set
493 # to the default user option as specified in Language*.php
494 if ( $align == '' ) {
495 $align = $wgContLang->isRTL() ?
'left' : 'right';
499 if ( $width === false ) {
500 $wopt = $wgUser->getOption( 'thumbsize' );
502 if( !isset( $wgThumbLimits[$wopt] ) ) {
503 $wopt = User
::getDefaultOption( 'thumbsize' );
506 $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
509 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
512 if ( $width && $img->exists() ) {
514 # Create a resized image, without the additional thumbnail
517 if ( $height == false )
519 if ( $manual_thumb == '') {
520 $thumb = $img->getThumbnail( $width, $height, $wgGenerateThumbnailOnParse );
522 // In most cases, $width = $thumb->width or $height = $thumb->height.
523 // If not, we're scaling the image larger than it can be scaled,
524 // so we send to the browser a smaller thumbnail, and let the client do the scaling.
526 if ($height != -1 && $width > $thumb->width
* $height / $thumb->height
) {
527 // $height is the limiting factor, not $width
528 // set $width to the largest it can be, such that the resulting
529 // scaled height is at most $height
530 $width = floor($thumb->width
* $height / $thumb->height
);
532 $height = round($thumb->height
* $width / $thumb->width
);
534 wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" );
535 $url = $thumb->getUrl();
537 $error = htmlspecialchars( $img->getLastError() );
541 $width = $img->width
;
542 $height = $img->height
;
545 wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
546 $u = $nt->escapeLocalURL();
549 } elseif ( $url == '' ) {
550 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
551 //$s .= "<br />{$alt}<br />{$url}<br />\n";
553 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
554 '<img src="'.$url.'" alt="'.$alt.'" ' .
556 ?
( 'width="'.$width.'" height="'.$height.'" ' )
558 'longdesc="'.$u.'" /></a>';
560 if ( '' != $align ) {
561 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
563 return str_replace("\n", ' ',$prefix.$s.$postfix);
567 * Make HTML for a thumbnail including image, border and caption
568 * $img is an Image object
570 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
571 global $wgStylePath, $wgContLang, $wgGenerateThumbnailOnParse;
575 $width = $height = 0;
576 if ( $img->exists() ) {
577 $width = $img->getWidth();
578 $height = $img->getHeight();
580 if ( 0 == $width ||
0 == $height ) {
581 $width = $height = 180;
583 if ( $boxwidth == 0 ) {
587 // Use image dimensions, don't scale
589 $boxheight = $height;
590 $thumbUrl = $img->getViewURL();
592 if ( $boxheight === false )
594 if ( '' == $manual_thumb ) {
595 $thumb = $img->getThumbnail( $boxwidth, $boxheight, $wgGenerateThumbnailOnParse );
597 $thumbUrl = $thumb->getUrl();
598 $boxwidth = $thumb->width
;
599 $boxheight = $thumb->height
;
601 $error = $img->getLastError();
605 $oboxwidth = $boxwidth +
2;
607 if ( $manual_thumb != '' ) # Use manually specified thumbnail
609 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
610 if( $manual_title ) {
611 $manual_img = new Image( $manual_title );
612 $thumbUrl = $manual_img->getViewURL();
613 if ( $manual_img->exists() )
615 $width = $manual_img->getWidth();
616 $height = $manual_img->getHeight();
618 $boxheight = $height ;
619 $oboxwidth = $boxwidth +
2 ;
624 $u = $img->getEscapeLocalURL();
626 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
627 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
628 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
630 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
631 if( $thumbUrl == '' ) {
632 // Couldn't generate thumbnail? Scale the image client-side.
633 $thumbUrl = $img->getViewURL();
636 $s .= htmlspecialchars( $error );
638 } elseif( !$img->exists() ) {
639 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
642 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
643 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
644 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
645 'longdesc="'.$u.'" /></a>';
649 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
650 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
651 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
652 'width="15" height="11" alt="'.$more.'" /></a></div>';
655 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
656 return str_replace("\n", ' ', $s);
660 * Pass a title object, not a title string
662 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
664 if ( ! isset($nt) ) {
665 # throw new MWException();
666 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
669 $fname = 'Linker::makeBrokenImageLinkObj';
670 wfProfileIn( $fname );
672 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
673 if ( '' != $query ) {
676 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
677 $url = $uploadTitle->escapeLocalURL( $q );
680 $text = htmlspecialchars( $nt->getPrefixedText() );
682 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
683 list( $inside, $trail ) = Linker
::splitTrail( $trail );
684 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
686 wfProfileOut( $fname );
690 /** @todo document */
691 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
692 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
693 return $this->makeMediaLinkObj( $nt, $alt );
697 * Create a direct link to a given uploaded file.
699 * @param $title Title object.
700 * @param $text String: pre-sanitized HTML
701 * @param $nourl Boolean: Mask absolute URLs, so the parser doesn't
702 * linkify them (it is currently not context-aware)
703 * @return string HTML
706 * @todo Handle invalid or missing images better.
708 function makeMediaLinkObj( $title, $text = '' ) {
709 if( is_null( $title ) ) {
710 ### HOTFIX. Instead of breaking, return empty string.
713 $name = $title->getDBKey();
714 $img = new Image( $title );
715 if( $img->exists() ) {
716 $url = $img->getURL();
719 $upload = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
720 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
723 $alt = htmlspecialchars( $title->getText() );
727 $u = htmlspecialchars( $url );
728 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
732 /** @todo document */
733 function specialLink( $name, $key = '' ) {
736 if ( '' == $key ) { $key = strtolower( $name ); }
737 $pn = $wgContLang->ucfirst( $name );
738 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
742 /** @todo document */
743 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
744 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
745 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
746 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
747 $style .= ' rel="nofollow"';
749 $url = htmlspecialchars( $url );
751 $text = htmlspecialchars( $text );
753 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
757 * Make user link (or user contributions for unregistered users)
758 * @param $userId Integer: user id in database.
759 * @param $userText String: user name in database
760 * @return string HTML fragment
763 function userLink( $userId, $userText ) {
764 $encName = htmlspecialchars( $userText );
766 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
767 return $this->makeKnownLinkObj( $contribsPage,
768 $encName, 'target=' . urlencode( $userText ) );
770 $userPage = Title
::makeTitle( NS_USER
, $userText );
771 return $this->makeLinkObj( $userPage, $encName );
776 * @param $userId Integer: user id in database.
777 * @param $userText String: user name in database.
778 * @return string HTML fragment with talk and/or block links
781 function userToolLinks( $userId, $userText ) {
782 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
783 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
784 $blockable = ( $wgSysopUserBans ||
0 == $userId );
788 $items[] = $this->userTalkLink( $userId, $userText );
791 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
792 $items[] = $this->makeKnownLinkObj( $contribsPage,
793 wfMsgHtml( 'contribslink' ), 'target=' . urlencode( $userText ) );
795 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
796 $items[] = $this->blockLink( $userId, $userText );
800 return ' (' . implode( ' | ', $items ) . ')';
807 * @param $userId Integer: user id in database.
808 * @param $userText String: user name in database.
809 * @return string HTML fragment with user talk link
812 function userTalkLink( $userId, $userText ) {
814 $talkname = $wgLang->getNsText( NS_TALK
); # use the shorter name
816 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
817 $userTalkLink = $this->makeLinkObj( $userTalkPage, $talkname );
818 return $userTalkLink;
822 * @param $userId Integer: userid
823 * @param $userText String: user name in database.
824 * @return string HTML fragment with block link
827 function blockLink( $userId, $userText ) {
828 $blockPage = Title
::makeTitle( NS_SPECIAL
, 'Blockip' );
829 $blockLink = $this->makeKnownLinkObj( $blockPage,
830 wfMsgHtml( 'blocklink' ), 'ip=' . urlencode( $userText ) );
835 * Generate a user link if the current user is allowed to view it
836 * @param $rev Revision object.
837 * @return string HTML
839 function revUserLink( $rev ) {
840 if( $rev->userCan( Revision
::DELETED_USER
) ) {
841 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
843 $link = wfMsgHtml( 'rev-deleted-user' );
845 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
846 return '<span class="history-deleted">' . $link . '</span>';
852 * Generate a user tool link cluster if the current user is allowed to view it
853 * @param $rev Revision object.
854 * @return string HTML
856 function revUserTools( $rev ) {
857 if( $rev->userCan( Revision
::DELETED_USER
) ) {
858 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
860 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
862 $link = wfMsgHtml( 'rev-deleted-user' );
864 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
865 return '<span class="history-deleted">' . $link . '</span>';
871 * This function is called by all recent changes variants, by the page history,
872 * and by the user contributions list. It is responsible for formatting edit
873 * comments. It escapes any HTML in the comment, but adds some CSS to format
874 * auto-generated comments (from section editing) and formats [[wikilinks]].
876 * The $title parameter must be a title OBJECT. It is used to generate a
877 * direct link to the section in the autocomment.
878 * @author Erik Moeller <moeller@scireview.de>
880 * Note: there's not always a title to pass to this function.
881 * Since you can't set a default parameter for a reference, I've turned it
882 * temporarily to a value pass. Should be adjusted further. --brion
884 function formatComment($comment, $title = NULL) {
885 $fname = 'Linker::formatComment';
886 wfProfileIn( $fname );
889 $comment = str_replace( "\n", " ", $comment );
890 $comment = htmlspecialchars( $comment );
892 # The pattern for autogen comments is / * foo * /, which makes for
894 # We look for all comments, match any text before and after the comment,
895 # add a separator where needed and format the comment itself with CSS
896 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
904 # Generate a valid anchor name from the section title.
905 # Hackish, but should generally work - we strip wiki
906 # syntax, including the magic [[: that is used to
907 # "link rather than show" in case of images and
908 # interlanguage links.
909 $section = str_replace( '[[:', '', $section );
910 $section = str_replace( '[[', '', $section );
911 $section = str_replace( ']]', '', $section );
912 $sectionTitle = wfClone( $title );
913 $sectionTitle->mFragment
= $section;
914 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
918 if($pre) { $auto = $sep.' '.$auto; }
919 if($post) { $auto .= ' '.$sep; }
920 $auto='<span class="autocomment">'.$auto.'</span>';
921 $comment=$pre.$auto.$post;
924 # format regular and media links - all other wiki formatting
926 $medians = $wgContLang->getNsText( NS_MEDIA
) . ':';
927 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
928 # Handle link renaming [[foo|text]] will show link as "text"
929 if( "" != $match[3] ) {
934 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
935 # Media link; trail not supported.
936 $linkRegexp = '/\[\[(.*?)\]\]/';
937 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
940 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
941 $trail = $submatch[1];
945 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
946 if ($match[1][0] == ':')
947 $match[1] = substr($match[1], 1);
948 $thelink = $this->makeLink( $match[1], $text, "", $trail );
950 $comment = preg_replace( $linkRegexp, wfRegexReplacement( $thelink ), $comment, 1 );
952 wfProfileOut( $fname );
957 * Wrap a comment in standard punctuation and formatting if
958 * it's non-empty, otherwise return empty string.
960 * @param $comment String: the comment.
961 * @param $title Title object.
965 function commentBlock( $comment, $title = NULL ) {
966 // '*' used to be the comment inserted by the software way back
967 // in antiquity in case none was provided, here for backwards
968 // compatability, acc. to brion -ævar
969 if( $comment == '' ||
$comment == '*' ) {
972 $formatted = $this->formatComment( $comment, $title );
973 return " <span class=\"comment\">($formatted)</span>";
978 * Wrap and format the given revision's comment block, if the current
979 * user is allowed to view it.
980 * @param $rev Revision object.
981 * @return string HTML
983 function revComment( $rev ) {
984 if( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
985 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle() );
987 $block = " <span class=\"comment\">" .
988 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
990 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
991 return " <span class=\"history-deleted\">$block</span>";
996 /** @todo document */
997 function tocIndent() {
1001 /** @todo document */
1002 function tocUnindent($level) {
1003 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
1007 * parameter level defines if we are on an indentation level
1009 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1010 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1011 $anchor . '"><span class="tocnumber">' .
1012 $tocnumber . '</span> <span class="toctext">' .
1013 $tocline . '</span></a>';
1016 /** @todo document */
1017 function tocLineEnd() {
1021 /** @todo document */
1022 function tocList($toc) {
1023 global $wgJsMimeType;
1024 $title = wfMsgForContent('toc') ;
1026 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1027 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1029 # no trailing newline, script should not be wrapped in a
1031 . "</ul>\n</td></tr></table>"
1032 . '<script type="' . $wgJsMimeType . '">'
1033 . ' if (window.showTocToggle) {'
1034 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
1035 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
1036 . ' showTocToggle();'
1041 /** @todo document */
1042 function editSectionLinkForOther( $title, $section ) {
1045 $title = Title
::newFromText( $title );
1046 $editurl = '§ion='.$section;
1047 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
1049 return "<span class=\"editsection\">[".$url."]</span>";
1054 * @param $title Title object.
1055 * @param $section Integer: section number.
1056 * @param $hint Link String: title, or default if omitted or empty
1058 function editSectionLink( $nt, $section, $hint='' ) {
1061 $editurl = '§ion='.$section;
1062 $hint = ( $hint=='' ) ?
'' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
1063 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '', $hint );
1065 return "<span class=\"editsection\">[".$url."]</span>";
1069 * Split a link trail, return the "inside" portion and the remainder of the trail
1070 * as a two-element array
1074 static function splitTrail( $trail ) {
1075 static $regex = false;
1076 if ( $regex === false ) {
1078 $regex = $wgContLang->linkTrail();
1081 if ( '' != $trail ) {
1082 if ( preg_match( $regex, $trail, $m ) ) {
1087 return array( $inside, $trail );