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.
24 function postParseLinkColour( $s = NULL ) {
29 function getExternalLinkAttributes( $link, $text, $class='' ) {
30 $link = htmlspecialchars( $link );
32 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
34 $r .= " title=\"{$link}\"";
38 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
41 $link = urldecode( $link );
42 $link = $wgContLang->checkTitleEncoding( $link );
43 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
44 $link = htmlspecialchars( $link );
46 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
48 $r .= " title=\"{$link}\"";
53 function getInternalLinkAttributes( $link, $text, $broken = false ) {
54 $link = urldecode( $link );
55 $link = str_replace( '_', ' ', $link );
56 $link = htmlspecialchars( $link );
58 if( $broken == 'stub' ) {
60 } else if ( $broken == 'yes' ) {
66 $r .= " title=\"{$link}\"";
71 * @param $nt Title object.
72 * @param $text String: FIXME
73 * @param $broken Boolean: FIXME, default 'false'.
75 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
76 if( $broken == 'stub' ) {
78 } else if ( $broken == 'yes' ) {
84 $r .= ' title="' . $nt->getEscapedText() . '"';
89 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
90 * it if you already have a title object handy. See makeLinkObj for further documentation.
92 * @param $title String: the text of the title
93 * @param $text String: link text
94 * @param $query String: optional query part
95 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
96 * be included in the link text. Other characters will be appended after
97 * the end of the link.
99 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
100 wfProfileIn( 'Linker::makeLink' );
101 $nt = Title
::newFromText( $title );
103 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
105 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
106 $result = $text == "" ?
$title : $text;
109 wfProfileOut( 'Linker::makeLink' );
114 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
115 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
117 * @param $title String: the text of the title
118 * @param $text String: link text
119 * @param $query String: optional query part
120 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
121 * be included in the link text. Other characters will be appended after
122 * the end of the link.
124 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
125 $nt = Title
::newFromText( $title );
127 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
129 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
130 return $text == '' ?
$title : $text;
135 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
136 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
138 * @param string $title The text of the title
139 * @param string $text Link text
140 * @param string $query Optional query part
141 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
142 * be included in the link text. Other characters will be appended after
143 * the end of the link.
145 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
146 $nt = Title
::newFromText( $title );
148 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
150 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
151 return $text == '' ?
$title : $text;
156 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
157 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
159 * @param $title String: the text of the title
160 * @param $text String: link text
161 * @param $query String: optional query part
162 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
163 * be included in the link text. Other characters will be appended after
164 * the end of the link.
166 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
167 $nt = Title
::newFromText( $title );
169 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
171 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
172 return $text == '' ?
$title : $text;
177 * Make a link for a title which may or may not be in the database. If you need to
178 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
179 * call to this will result in a DB query.
181 * @param $nt Title: the title object to make the link from, e.g. from
182 * Title::newFromText.
183 * @param $text String: link text
184 * @param $query String: optional query part
185 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
186 * be included in the link text. Other characters will be appended after
187 * the end of the link.
188 * @param $prefix String: optional prefix. As trail, only before instead of after.
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 if ( $nt->isExternal() ) {
203 $u = $nt->getFullURL();
204 $link = $nt->getPrefixedURL();
205 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
206 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
209 if ( '' != $trail ) {
211 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
216 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
218 wfProfileOut( $fname );
220 } elseif ( $nt->isAlwaysKnown() ) {
221 # Image links, special page links and self-links with fragements are always known.
222 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
224 wfProfileIn( $fname.'-immediate' );
225 # Work out link colour immediately
226 $aid = $nt->getArticleID() ;
228 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
230 $threshold = $wgUser->getOption('stubthreshold') ;
231 if ( $threshold > 0 ) {
232 $dbr =& wfGetDB( DB_SLAVE
);
233 $s = $dbr->selectRow(
237 'page_is_redirect' ),
238 array( 'page_id' => $aid ), $fname ) ;
239 if ( $s !== false ) {
240 $size = $s->page_len
;
241 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
242 $size = $threshold*2 ; # Really big
245 $size = $threshold*2 ; # Really big
250 if ( $size < $threshold ) {
251 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
253 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
256 wfProfileOut( $fname.'-immediate' );
258 wfProfileOut( $fname );
263 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
264 * it doesn't have to do a database query. It's also valid for interwiki titles and special
267 * @param $nt Title object of target page
268 * @param $text String: text to replace the title
269 * @param $query String: link target
270 * @param $trail String: text after link
271 * @param $prefix String: text before link text
272 * @param $aprops String: extra attributes to the a-element
273 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
274 * @return the a-element
276 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
278 $fname = 'Linker::makeKnownLinkObj';
279 wfProfileIn( $fname );
281 if ( !is_object( $nt ) ) {
282 wfProfileOut( $fname );
286 $u = $nt->escapeLocalURL( $query );
287 if ( $nt->getFragment() != '' ) {
288 if( $nt->getPrefixedDbkey() == '' ) {
291 $text = htmlspecialchars( $nt->getFragment() );
294 $u .= $nt->getFragmentForURL();
297 $text = htmlspecialchars( $nt->getPrefixedText() );
299 if ( $style == '' ) {
300 $style = $this->getInternalLinkAttributesObj( $nt, $text );
303 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
305 list( $inside, $trail ) = Linker
::splitTrail( $trail );
306 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
307 wfProfileOut( $fname );
312 * Make a red link to the edit page of a given title.
314 * @param $title String: The text of the title
315 * @param $text String: Link text
316 * @param $query String: Optional query part
317 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
318 * be included in the link text. Other characters will be appended after
319 * the end of the link.
321 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
323 if ( ! isset($nt) ) {
324 # throw new MWException();
325 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
328 $fname = 'Linker::makeBrokenLinkObj';
329 wfProfileIn( $fname );
331 if ( '' == $query ) {
334 $q = 'action=edit&'.$query;
336 $u = $nt->escapeLocalURL( $q );
339 $text = htmlspecialchars( $nt->getPrefixedText() );
341 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
343 list( $inside, $trail ) = Linker
::splitTrail( $trail );
344 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
346 wfProfileOut( $fname );
351 * Make a brown link to a short article.
353 * @param $title String: the text of the title
354 * @param $text String: link text
355 * @param $query String: optional query part
356 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
357 * be included in the link text. Other characters will be appended after
358 * the end of the link.
360 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
361 $u = $nt->escapeLocalURL( $query );
364 $text = htmlspecialchars( $nt->getPrefixedText() );
366 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
368 list( $inside, $trail ) = Linker
::splitTrail( $trail );
369 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
374 * Generate either a normal exists-style link or a stub link, depending
375 * on the given page size.
377 * @param $size Integer
378 * @param $nt Title object.
379 * @param $text String
380 * @param $query String
381 * @param $trail String
382 * @param $prefix String
383 * @return string HTML of link
385 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
387 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
388 if( $size < $threshold ) {
389 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
391 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
396 * Make appropriate markup for a link to the current article. This is currently rendered
397 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
398 * despite $query not being used.
400 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
402 $text = htmlspecialchars( $nt->getPrefixedText() );
404 list( $inside, $trail ) = Linker
::splitTrail( $trail );
405 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
408 /** @todo document */
409 function fnamePart( $url ) {
410 $basename = strrchr( $url, '/' );
411 if ( false === $basename ) {
414 $basename = substr( $basename, 1 );
416 return htmlspecialchars( $basename );
419 /** Obsolete alias */
420 function makeImage( $url, $alt = '' ) {
421 return $this->makeExternalImage( $url, $alt );
424 /** @todo document */
425 function makeExternalImage( $url, $alt = '' ) {
427 $alt = $this->fnamePart( $url );
429 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
433 /** @todo document */
434 function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
435 $thumb = false, $manual_thumb = '', $page = null )
437 global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
439 $img = new Image( $nt );
441 if ( ! is_null( $page ) ) {
442 $img->selectPage( $page );
445 if ( !$img->allowInlineDisplay() && $img->exists() ) {
446 return $this->makeKnownLinkObj( $nt );
449 $url = $img->getViewURL();
450 $error = $prefix = $postfix = '';
452 wfDebug( "makeImageLinkObj: '$width'x'$height', \"$label\"\n" );
454 if ( 'center' == $align )
456 $prefix = '<div class="center">';
461 if ( $thumb ||
$framed ) {
463 # Create a thumbnail. Alignment depends on language
464 # writing direction, # right aligned for left-to-right-
465 # languages ("Western languages"), left-aligned
466 # for right-to-left-languages ("Semitic languages")
468 # If thumbnail width has not been provided, it is set
469 # to the default user option as specified in Language*.php
470 if ( $align == '' ) {
471 $align = $wgContLang->isRTL() ?
'left' : 'right';
475 if ( $width === false ) {
476 $wopt = $wgUser->getOption( 'thumbsize' );
478 if( !isset( $wgThumbLimits[$wopt] ) ) {
479 $wopt = User
::getDefaultOption( 'thumbsize' );
482 $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
485 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
488 if ( $width && $img->exists() ) {
490 # Create a resized image, without the additional thumbnail
493 if ( $height == false )
495 if ( $manual_thumb == '') {
496 $thumb = $img->getThumbnail( $width, $height, $wgGenerateThumbnailOnParse );
498 // In most cases, $width = $thumb->width or $height = $thumb->height.
499 // If not, we're scaling the image larger than it can be scaled,
500 // so we send to the browser a smaller thumbnail, and let the client do the scaling.
502 if ($height != -1 && $width > $thumb->width
* $height / $thumb->height
) {
503 // $height is the limiting factor, not $width
504 // set $width to the largest it can be, such that the resulting
505 // scaled height is at most $height
506 $width = floor($thumb->width
* $height / $thumb->height
);
508 $height = round($thumb->height
* $width / $thumb->width
);
510 wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" );
511 $url = $thumb->getUrl();
513 $error = htmlspecialchars( $img->getLastError() );
514 // Do client-side scaling...
515 $height = intval( $img->getHeight() * $width / $img->getWidth() );
519 $width = $img->width
;
520 $height = $img->height
;
523 wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
524 $u = $nt->escapeLocalURL();
527 } elseif ( $url == '' ) {
528 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
529 //$s .= "<br />{$alt}<br />{$url}<br />\n";
531 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
532 '<img src="'.$url.'" alt="'.$alt.'" ' .
534 ?
( 'width="'.$width.'" height="'.$height.'" ' )
536 'longdesc="'.$u.'" /></a>';
538 if ( '' != $align ) {
539 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
541 return str_replace("\n", ' ',$prefix.$s.$postfix);
545 * Make HTML for a thumbnail including image, border and caption
546 * $img is an Image object
548 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
549 global $wgStylePath, $wgContLang, $wgGenerateThumbnailOnParse;
553 $width = $height = 0;
554 if ( $img->exists() ) {
555 $width = $img->getWidth();
556 $height = $img->getHeight();
558 if ( 0 == $width ||
0 == $height ) {
559 $width = $height = 180;
561 if ( $boxwidth == 0 ) {
565 // Use image dimensions, don't scale
567 $boxheight = $height;
568 $thumbUrl = $img->getViewURL();
570 if ( $boxheight === false )
572 if ( '' == $manual_thumb ) {
573 $thumb = $img->getThumbnail( $boxwidth, $boxheight, $wgGenerateThumbnailOnParse );
575 $thumbUrl = $thumb->getUrl();
576 $boxwidth = $thumb->width
;
577 $boxheight = $thumb->height
;
579 $error = $img->getLastError();
583 $oboxwidth = $boxwidth +
2;
585 if ( $manual_thumb != '' ) # Use manually specified thumbnail
587 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
588 if( $manual_title ) {
589 $manual_img = new Image( $manual_title );
590 $thumbUrl = $manual_img->getViewURL();
591 if ( $manual_img->exists() )
593 $width = $manual_img->getWidth();
594 $height = $manual_img->getHeight();
596 $boxheight = $height ;
597 $oboxwidth = $boxwidth +
2 ;
602 $u = $img->getEscapeLocalURL();
604 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
605 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
606 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
608 $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$oboxwidth}px;\">";
609 if( $thumbUrl == '' ) {
610 // Couldn't generate thumbnail? Scale the image client-side.
611 $thumbUrl = $img->getViewURL();
612 if( $boxheight == -1 ) {
614 $boxheight = intval( $height * $boxwidth / $width );
618 $s .= htmlspecialchars( $error );
620 } elseif( !$img->exists() ) {
621 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
624 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
625 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
626 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
627 'longdesc="'.$u.'" class="thumbimage" /></a>';
631 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
632 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
633 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
634 'width="15" height="11" alt="" /></a></div>';
637 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
638 return str_replace("\n", ' ', $s);
642 * Pass a title object, not a title string
644 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
646 if ( ! isset($nt) ) {
647 # throw new MWException();
648 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
651 $fname = 'Linker::makeBrokenImageLinkObj';
652 wfProfileIn( $fname );
654 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
655 if ( '' != $query ) {
658 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
659 $url = $uploadTitle->escapeLocalURL( $q );
662 $text = htmlspecialchars( $nt->getPrefixedText() );
664 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
665 list( $inside, $trail ) = Linker
::splitTrail( $trail );
666 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
668 wfProfileOut( $fname );
672 /** @todo document */
673 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
674 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
675 return $this->makeMediaLinkObj( $nt, $alt );
679 * Create a direct link to a given uploaded file.
681 * @param $title Title object.
682 * @param $text String: pre-sanitized HTML
683 * @param $nourl Boolean: Mask absolute URLs, so the parser doesn't
684 * linkify them (it is currently not context-aware)
685 * @return string HTML
688 * @todo Handle invalid or missing images better.
690 function makeMediaLinkObj( $title, $text = '' ) {
691 if( is_null( $title ) ) {
692 ### HOTFIX. Instead of breaking, return empty string.
695 $img = new Image( $title );
696 if( $img->exists() ) {
697 $url = $img->getURL();
700 $upload = SpecialPage
::getTitleFor( 'Upload' );
701 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
704 $alt = htmlspecialchars( $title->getText() );
708 $u = htmlspecialchars( $url );
709 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
713 /** @todo document */
714 function specialLink( $name, $key = '' ) {
717 if ( '' == $key ) { $key = strtolower( $name ); }
718 $pn = $wgContLang->ucfirst( $name );
719 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
723 /** @todo document */
724 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
725 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
726 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
727 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
728 $style .= ' rel="nofollow"';
730 $url = htmlspecialchars( $url );
732 $text = htmlspecialchars( $text );
734 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
738 * Make user link (or user contributions for unregistered users)
739 * @param $userId Integer: user id in database.
740 * @param $userText String: user name in database
741 * @return string HTML fragment
744 function userLink( $userId, $userText ) {
745 $encName = htmlspecialchars( $userText );
747 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
748 return $this->makeKnownLinkObj( $contribsPage,
751 $userPage = Title
::makeTitle( NS_USER
, $userText );
752 return $this->makeLinkObj( $userPage, $encName );
757 * @param $userId Integer: user id in database.
758 * @param $userText String: user name in database.
759 * @return string HTML fragment with talk and/or block links
762 function userToolLinks( $userId, $userText ) {
763 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
764 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
765 $blockable = ( $wgSysopUserBans ||
0 == $userId );
769 $items[] = $this->userTalkLink( $userId, $userText );
772 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
773 $items[] = $this->makeKnownLinkObj( $contribsPage ,
774 wfMsgHtml( 'contribslink' ) );
776 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
777 $items[] = $this->blockLink( $userId, $userText );
781 return ' (' . implode( ' | ', $items ) . ')';
788 * @param $userId Integer: user id in database.
789 * @param $userText String: user name in database.
790 * @return string HTML fragment with user talk link
793 function userTalkLink( $userId, $userText ) {
795 $talkname = $wgLang->getNsText( NS_TALK
); # use the shorter name
797 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
798 $userTalkLink = $this->makeLinkObj( $userTalkPage, $talkname );
799 return $userTalkLink;
803 * @param $userId Integer: userid
804 * @param $userText String: user name in database.
805 * @return string HTML fragment with block link
808 function blockLink( $userId, $userText ) {
809 $blockPage = SpecialPage
::getTitleFor( 'Blockip', $userText );
810 $blockLink = $this->makeKnownLinkObj( $blockPage,
811 wfMsgHtml( 'blocklink' ) );
816 * Generate a user link if the current user is allowed to view it
817 * @param $rev Revision object.
818 * @return string HTML
820 function revUserLink( $rev ) {
821 if( $rev->userCan( Revision
::DELETED_USER
) ) {
822 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
824 $link = wfMsgHtml( 'rev-deleted-user' );
826 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
827 return '<span class="history-deleted">' . $link . '</span>';
833 * Generate a user tool link cluster if the current user is allowed to view it
834 * @param $rev Revision object.
835 * @return string HTML
837 function revUserTools( $rev ) {
838 if( $rev->userCan( Revision
::DELETED_USER
) ) {
839 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
841 $this->userToolLinks( $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 * This function is called by all recent changes variants, by the page history,
853 * and by the user contributions list. It is responsible for formatting edit
854 * comments. It escapes any HTML in the comment, but adds some CSS to format
855 * auto-generated comments (from section editing) and formats [[wikilinks]].
857 * @author Erik Moeller <moeller@scireview.de>
859 * Note: there's not always a title to pass to this function.
860 * Since you can't set a default parameter for a reference, I've turned it
861 * temporarily to a value pass. Should be adjusted further. --brion
863 * $param string $comment
864 * @param mixed $title Title object (to generate link to the section in autocomment) or null
865 * @param bool $local Whether section links should refer to local page
867 function formatComment($comment, $title = NULL, $local = false) {
868 wfProfileIn( __METHOD__
);
871 $comment = str_replace( "\n", " ", $comment );
872 $comment = htmlspecialchars( $comment );
874 # The pattern for autogen comments is / * foo * /, which makes for
876 # We look for all comments, match any text before and after the comment,
877 # add a separator where needed and format the comment itself with CSS
879 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
887 # Generate a valid anchor name from the section title.
888 # Hackish, but should generally work - we strip wiki
889 # syntax, including the magic [[: that is used to
890 # "link rather than show" in case of images and
891 # interlanguage links.
892 $section = str_replace( '[[:', '', $section );
893 $section = str_replace( '[[', '', $section );
894 $section = str_replace( ']]', '', $section );
896 $sectionTitle = Title
::newFromText( '#' . $section);
898 $sectionTitle = wfClone( $title );
899 $sectionTitle->mFragment
= $section;
901 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
905 if($pre) { $auto = $sep.' '.$auto; }
906 if($post) { $auto .= ' '.$sep; }
907 $auto='<span class="autocomment">'.$auto.'</span>';
908 $comment=$pre.$auto.$post;
911 # format regular and media links - all other wiki formatting
913 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA
), '/' ) . '|';
914 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA
), '/' ) . '):';
915 while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
916 # Handle link renaming [[foo|text]] will show link as "text"
917 if( "" != $match[3] ) {
923 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
924 # Media link; trail not supported.
925 $linkRegexp = '/\[\[(.*?)\]\]/';
926 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
929 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
930 $trail = $submatch[1];
934 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
935 if (isset($match[1][0]) && $match[1][0] == ':')
936 $match[1] = substr($match[1], 1);
937 $thelink = $this->makeLink( $match[1], $text, "", $trail );
939 $comment = preg_replace( $linkRegexp, StringUtils
::escapeRegexReplacement( $thelink ), $comment, 1 );
941 wfProfileOut( __METHOD__
);
946 * Wrap a comment in standard punctuation and formatting if
947 * it's non-empty, otherwise return empty string.
949 * @param string $comment
950 * @param mixed $title Title object (to generate link to section in autocomment) or null
951 * @param bool $local Whether section links should refer to local page
955 function commentBlock( $comment, $title = NULL, $local = false ) {
956 // '*' used to be the comment inserted by the software way back
957 // in antiquity in case none was provided, here for backwards
958 // compatability, acc. to brion -ævar
959 if( $comment == '' ||
$comment == '*' ) {
962 $formatted = $this->formatComment( $comment, $title, $local );
963 return " <span class=\"comment\">($formatted)</span>";
968 * Wrap and format the given revision's comment block, if the current
969 * user is allowed to view it.
971 * @param Revision $rev
972 * @param bool $local Whether section links should refer to local page
973 * @return string HTML
975 function revComment( Revision
$rev, $local = false ) {
976 if( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
977 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
979 $block = " <span class=\"comment\">" .
980 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
982 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
983 return " <span class=\"history-deleted\">$block</span>";
988 /** @todo document */
989 function tocIndent() {
993 /** @todo document */
994 function tocUnindent($level) {
995 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
999 * parameter level defines if we are on an indentation level
1001 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1002 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1003 $anchor . '"><span class="tocnumber">' .
1004 $tocnumber . '</span> <span class="toctext">' .
1005 $tocline . '</span></a>';
1008 /** @todo document */
1009 function tocLineEnd() {
1013 /** @todo document */
1014 function tocList($toc) {
1015 global $wgJsMimeType;
1016 $title = wfMsgForContent('toc') ;
1018 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1019 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1021 # no trailing newline, script should not be wrapped in a
1023 . "</ul>\n</td></tr></table>"
1024 . '<script type="' . $wgJsMimeType . '">'
1025 . ' if (window.showTocToggle) {'
1026 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
1027 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
1028 . ' showTocToggle();'
1033 /** @todo document */
1034 public function editSectionLinkForOther( $title, $section ) {
1037 $title = Title
::newFromText( $title );
1038 $editurl = '§ion='.$section;
1039 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
1041 return "<span class=\"editsection\">[".$url."]</span>";
1046 * @param $title Title object.
1047 * @param $section Integer: section number.
1048 * @param $hint Link String: title, or default if omitted or empty
1050 public function editSectionLink( $nt, $section, $hint='' ) {
1053 $editurl = '§ion='.$section;
1054 $hint = ( $hint=='' ) ?
'' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
1055 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '', $hint );
1057 return "<span class=\"editsection\">[".$url."]</span>";
1061 * Create a headline for content
1063 * @param int $level The level of the headline (1-6)
1064 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1065 * This *must* be at least '>' for no attribs
1066 * @param string $anchor The anchor to give the headline (the bit after the #)
1067 * @param string $text The text of the header
1068 * @param string $link HTML to add for the section edit link
1070 * @return string HTML headline
1072 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1073 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1077 * Split a link trail, return the "inside" portion and the remainder of the trail
1078 * as a two-element array
1082 static function splitTrail( $trail ) {
1083 static $regex = false;
1084 if ( $regex === false ) {
1086 $regex = $wgContLang->linkTrail();
1089 if ( '' != $trail ) {
1091 if ( preg_match( $regex, $trail, $m ) ) {
1096 return array( $inside, $trail );
1100 * Generate a rollback link for a given revision. Currently it's the
1101 * caller's responsibility to ensure that the revision is the top one. If
1102 * it's not, of course, the user will get an error message.
1104 * If the calling page is called with the parameter &bot=1, all rollback
1105 * links also get that parameter. It causes the edit itself and the rollback
1106 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1107 * changes, so this allows sysops to combat a busy vandal without bothering
1110 * @param Revision $rev
1112 function generateRollback( $rev ) {
1113 global $wgUser, $wgRequest;
1114 $title = $rev->getTitle();
1116 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
1117 $extraRollback .= '&token=' . urlencode(
1118 $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ) );
1119 return '<span class="mw-rollback-link">['. $this->makeKnownLinkObj( $title,
1120 wfMsg('rollbacklink'),
1121 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extraRollback ) .']</span>';
1125 * Returns HTML for the "templates used on this page" list.
1127 * @param array $templates Array of templates from Article::getUsedTemplate
1129 * @param bool $preview Whether this is for a preview
1130 * @param bool $section Whether this is for a section edit
1131 * @return string HTML output
1133 public function formatTemplates( $templates, $preview = false, $section = false) {
1135 wfProfileIn( __METHOD__
);
1137 $sk =& $wgUser->getSkin();
1140 if ( count( $templates ) > 0 ) {
1141 # Do a batch existence check
1142 $batch = new LinkBatch
;
1143 foreach( $templates as $title ) {
1144 $batch->addObj( $title );
1148 # Construct the HTML
1149 $outText = '<div class="mw-templatesUsedExplanation">';
1151 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1152 } elseif ( $section ) {
1153 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1155 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1157 $outText .= '</div><ul>';
1159 foreach ( $templates as $titleObj ) {
1160 $r = $titleObj->getRestrictions( 'edit' );
1161 if ( in_array( 'sysop', $r ) ) {
1162 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1163 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1164 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1168 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1170 $outText .= '</ul>';
1172 wfProfileOut( __METHOD__
);
1177 * Format a size in bytes for output, using an appropriate
1178 * unit (B, KB, MB or GB) according to the magnitude in question
1180 * @param $size Size to format
1183 public function formatSize( $size ) {
1185 if( $size > 1024 ) {
1186 $size = $size / 1024;
1187 if( $size > 1024 ) {
1188 $size = $size / 1024;
1189 if( $size > 1024 ) {
1190 $size = $size / 1024;
1191 $msg = 'size-gigabytes';
1193 $msg = 'size-megabytes';
1196 $msg = 'size-kilobytes';
1199 $msg = 'size-bytes';
1201 $size = round( $size, 0 );
1202 return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
1206 * Given the id of an interface element, constructs the appropriate title
1207 * and accesskey attributes from the system messages. (Note, this is usu-
1208 * ally the id but isn't always, because sometimes the accesskey needs to
1209 * go on a different element than the id, for reverse-compatibility, etc.)
1211 * @param string $name Id of the element, minus prefixes.
1212 * @return string title and accesskey attributes, ready to drop in an
1213 * element (e.g., ' title="This does something [x]" accesskey="x"').
1215 public function tooltipAndAccesskey($name) {
1218 $tooltip = wfMsg('tooltip-'.$name);
1219 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1220 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1221 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1222 $out .= ' title="'.htmlspecialchars($tooltip);
1224 $accesskey = wfMsg('accesskey-'.$name);
1225 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1226 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1227 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1235 * Given the id of an interface element, constructs the appropriate title
1236 * attribute from the system messages. (Note, this is usually the id but
1237 * isn't always, because sometimes the accesskey needs to go on a different
1238 * element than the id, for reverse-compatibility, etc.)
1240 * @param string $name Id of the element, minus prefixes.
1241 * @return string title attribute, ready to drop in an element
1242 * (e.g., ' title="This does something"').
1244 public function tooltip($name) {
1247 $tooltip = wfMsg('tooltip-'.$name);
1248 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1249 $out = ' title="'.htmlspecialchars($tooltip).'"';