c34fa211ec85662556ae76817047c60420c92b5b
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.
10 * For the moment, Skin is a descendent class of Linker.
11 * In the future, it should probably be further split
12 * so that ever other bit of the wiki doesn't have to
13 * go loading up Skin to get at it.
17 function __construct() {}
22 function postParseLinkColour( $s = NULL ) {
27 function getExternalLinkAttributes( $link, $text, $class='' ) {
28 $link = htmlspecialchars( $link );
30 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
32 $r .= " title=\"{$link}\"";
36 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
39 $link = urldecode( $link );
40 $link = $wgContLang->checkTitleEncoding( $link );
41 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
42 $link = htmlspecialchars( $link );
44 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
46 $r .= " title=\"{$link}\"";
51 function getInternalLinkAttributes( $link, $text, $broken = false ) {
52 $link = urldecode( $link );
53 $link = str_replace( '_', ' ', $link );
54 $link = htmlspecialchars( $link );
56 if( $broken == 'stub' ) {
58 } else if ( $broken == 'yes' ) {
64 $r .= " title=\"{$link}\"";
69 * @param $nt Title object.
70 * @param $text String: FIXME
71 * @param $broken Boolean: FIXME, default 'false'.
73 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
74 if( $broken == 'stub' ) {
76 } else if ( $broken == 'yes' ) {
82 $r .= ' title="' . $nt->getEscapedText() . '"';
87 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
88 * it if you already have a title object handy. See makeLinkObj for further documentation.
90 * @param $title String: the text of the title
91 * @param $text String: link text
92 * @param $query String: optional query part
93 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
94 * be included in the link text. Other characters will be appended after
95 * the end of the link.
97 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
98 wfProfileIn( 'Linker::makeLink' );
99 $nt = Title
::newFromText( $title );
101 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
103 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
104 $result = $text == "" ?
$title : $text;
107 wfProfileOut( 'Linker::makeLink' );
112 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
113 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
115 * @param $title String: the text of the title
116 * @param $text String: link text
117 * @param $query String: optional query part
118 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
119 * be included in the link text. Other characters will be appended after
120 * the end of the link.
122 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
123 $nt = Title
::newFromText( $title );
125 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
127 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
128 return $text == '' ?
$title : $text;
133 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
134 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
136 * @param string $title The text of the title
137 * @param string $text Link text
138 * @param string $query Optional query part
139 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
140 * be included in the link text. Other characters will be appended after
141 * the end of the link.
143 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
144 $nt = Title
::newFromText( $title );
146 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
148 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
149 return $text == '' ?
$title : $text;
154 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
155 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
157 * @param $title String: the text of the title
158 * @param $text String: link text
159 * @param $query String: optional query part
160 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
161 * be included in the link text. Other characters will be appended after
162 * the end of the link.
164 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
165 $nt = Title
::newFromText( $title );
167 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
169 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
170 return $text == '' ?
$title : $text;
175 * Make a link for a title which may or may not be in the database. If you need to
176 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
177 * call to this will result in a DB query.
179 * @param $nt Title: the title object to make the link from, e.g. from
180 * Title::newFromText.
181 * @param $text String: link text
182 * @param $query String: optional query part
183 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
184 * be included in the link text. Other characters will be appended after
185 * the end of the link.
186 * @param $prefix String: optional prefix. As trail, only before instead of after.
188 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
190 $fname = 'Linker::makeLinkObj';
191 wfProfileIn( $fname );
194 if ( ! is_object($nt) ) {
195 # throw new MWException();
196 wfProfileOut( $fname );
197 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
200 if ( $nt->isExternal() ) {
201 $u = $nt->getFullURL();
202 $link = $nt->getPrefixedURL();
203 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
204 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
207 if ( '' != $trail ) {
209 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
214 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
216 wfProfileOut( $fname );
218 } elseif ( $nt->isAlwaysKnown() ) {
219 # Image links, special page links and self-links with fragements are always known.
220 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
222 wfProfileIn( $fname.'-immediate' );
223 # Work out link colour immediately
224 $aid = $nt->getArticleID() ;
226 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
228 $threshold = $wgUser->getOption('stubthreshold') ;
229 if ( $threshold > 0 ) {
230 $dbr = wfGetDB( DB_SLAVE
);
231 $s = $dbr->selectRow(
235 'page_is_redirect' ),
236 array( 'page_id' => $aid ), $fname ) ;
237 if ( $s !== false ) {
238 $size = $s->page_len
;
239 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
240 $size = $threshold*2 ; # Really big
243 $size = $threshold*2 ; # Really big
248 if ( $size < $threshold ) {
249 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
251 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
254 wfProfileOut( $fname.'-immediate' );
256 wfProfileOut( $fname );
261 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
262 * it doesn't have to do a database query. It's also valid for interwiki titles and special
265 * @param $nt Title object of target page
266 * @param $text String: text to replace the title
267 * @param $query String: link target
268 * @param $trail String: text after link
269 * @param $prefix String: text before link text
270 * @param $aprops String: extra attributes to the a-element
271 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
272 * @return the a-element
274 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
276 $fname = 'Linker::makeKnownLinkObj';
277 wfProfileIn( $fname );
279 if ( !is_object( $nt ) ) {
280 wfProfileOut( $fname );
284 $u = $nt->escapeLocalURL( $query );
285 if ( $nt->getFragment() != '' ) {
286 if( $nt->getPrefixedDbkey() == '' ) {
289 $text = htmlspecialchars( $nt->getFragment() );
292 $u .= $nt->getFragmentForURL();
295 $text = htmlspecialchars( $nt->getPrefixedText() );
297 if ( $style == '' ) {
298 $style = $this->getInternalLinkAttributesObj( $nt, $text );
301 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
303 list( $inside, $trail ) = Linker
::splitTrail( $trail );
304 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
305 wfProfileOut( $fname );
310 * Make a red link to the edit page of a given title.
312 * @param $title String: The text of the title
313 * @param $text String: Link text
314 * @param $query String: Optional query part
315 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
316 * be included in the link text. Other characters will be appended after
317 * the end of the link.
319 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
321 if ( ! isset($nt) ) {
322 # throw new MWException();
323 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
326 $fname = 'Linker::makeBrokenLinkObj';
327 wfProfileIn( $fname );
329 if ( '' == $query ) {
332 $q = 'action=edit&'.$query;
334 $u = $nt->escapeLocalURL( $q );
337 $text = htmlspecialchars( $nt->getPrefixedText() );
339 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
341 list( $inside, $trail ) = Linker
::splitTrail( $trail );
342 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
344 wfProfileOut( $fname );
349 * Make a brown link to a short article.
351 * @param $title String: the text of the title
352 * @param $text String: link text
353 * @param $query String: optional query part
354 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
355 * be included in the link text. Other characters will be appended after
356 * the end of the link.
358 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
359 $u = $nt->escapeLocalURL( $query );
362 $text = htmlspecialchars( $nt->getPrefixedText() );
364 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
366 list( $inside, $trail ) = Linker
::splitTrail( $trail );
367 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
372 * Generate either a normal exists-style link or a stub link, depending
373 * on the given page size.
375 * @param $size Integer
376 * @param $nt Title object.
377 * @param $text String
378 * @param $query String
379 * @param $trail String
380 * @param $prefix String
381 * @return string HTML of link
383 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
385 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
386 if( $size < $threshold ) {
387 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
389 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
394 * Make appropriate markup for a link to the current article. This is currently rendered
395 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
396 * despite $query not being used.
398 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
400 $text = htmlspecialchars( $nt->getPrefixedText() );
402 list( $inside, $trail ) = Linker
::splitTrail( $trail );
403 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
406 /** @todo document */
407 function fnamePart( $url ) {
408 $basename = strrchr( $url, '/' );
409 if ( false === $basename ) {
412 $basename = substr( $basename, 1 );
414 return htmlspecialchars( $basename );
417 /** Obsolete alias */
418 function makeImage( $url, $alt = '' ) {
419 return $this->makeExternalImage( $url, $alt );
422 /** @todo document */
423 function makeExternalImage( $url, $alt = '' ) {
425 $alt = $this->fnamePart( $url );
427 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
431 /** @todo document */
432 function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
433 $thumb = false, $manual_thumb = '', $valign = '' )
435 global $wgContLang, $wgUser, $wgThumbLimits;
437 $img = new Image( $nt );
439 if ( !$img->allowInlineDisplay() && $img->exists() ) {
440 return $this->makeKnownLinkObj( $nt );
443 $error = $prefix = $postfix = '';
444 $page = isset( $params['page'] ) ?
$params['page'] : false;
446 if ( 'center' == $align )
448 $prefix = '<div class="center">';
453 if ( !isset( $params['width'] ) ) {
454 $wopt = $wgUser->getOption( 'thumbsize' );
456 if( !isset( $wgThumbLimits[$wopt] ) ) {
457 $wopt = User
::getDefaultOption( 'thumbsize' );
460 $params['width'] = min( $img->getWidth( $page ), $wgThumbLimits[$wopt] );
463 if ( $thumb ||
$framed ) {
465 # Create a thumbnail. Alignment depends on language
466 # writing direction, # right aligned for left-to-right-
467 # languages ("Western languages"), left-aligned
468 # for right-to-left-languages ("Semitic languages")
470 # If thumbnail width has not been provided, it is set
471 # to the default user option as specified in Language*.php
472 if ( $align == '' ) {
473 $align = $wgContLang->isRTL() ?
'left' : 'right';
475 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $params, $framed, $manual_thumb ).$postfix;
478 if ( $params['width'] && $img->exists() ) {
479 # Create a resized image, without the additional thumbnail features
480 $thumb = $img->transform( $params );
486 $query = 'page=' . urlencode( $page );
490 $u = $nt->getLocalURL( $query );
496 $imgAttribs['style'] = "vertical-align: $valign";
498 $linkAttribs = array(
505 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
507 $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
509 if ( '' != $align ) {
510 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
512 return str_replace("\n", ' ',$prefix.$s.$postfix);
516 * Make HTML for a thumbnail including image, border and caption
517 * $img is an Image object
519 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "" ) {
520 global $wgStylePath, $wgContLang;
524 $page = isset( $params['page'] ) ?
$params['page'] : false;
526 if ( empty( $params['width'] ) ) {
527 $params['width'] = 180;
530 if ( $manual_thumb != '' ) {
531 # Use manually specified thumbnail
532 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb );
533 if( $manual_title ) {
534 $manual_img = new Image( $manual_title );
535 $thumb = $manual_img->getUnscaledThumb();
537 } elseif ( $framed ) {
538 // Use image dimensions, don't scale
539 $thumb = $img->getUnscaledThumb( $page );
541 $thumb = $img->transform( $params );
545 $outerWidth = $thumb->getWidth() +
2;
547 $outerWidth = $params['width'] +
2;
550 $query = $page ?
'page=' . urlencode( $page ) : '';
551 $u = $img->getTitle()->getLocalURL( $query );
553 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
554 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
555 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
557 $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
559 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
561 } elseif( !$img->exists() ) {
562 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
568 'class' => 'thumbimage'
570 $linkAttribs = array(
573 'class' => 'internal'
576 $s .= $thumb->toHtml( $imgAttribs, $linkAttribs );
580 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
581 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
582 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
583 'width="15" height="11" alt="" /></a></div>';
586 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
587 return str_replace("\n", ' ', $s);
591 * Pass a title object, not a title string
593 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
595 if ( ! isset($nt) ) {
596 # throw new MWException();
597 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
600 $fname = 'Linker::makeBrokenImageLinkObj';
601 wfProfileIn( $fname );
603 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
604 if ( '' != $query ) {
607 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
608 $url = $uploadTitle->escapeLocalURL( $q );
611 $text = htmlspecialchars( $nt->getPrefixedText() );
613 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
614 list( $inside, $trail ) = Linker
::splitTrail( $trail );
615 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
617 wfProfileOut( $fname );
621 /** @todo document */
622 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
623 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
624 return $this->makeMediaLinkObj( $nt, $alt );
628 * Create a direct link to a given uploaded file.
630 * @param $title Title object.
631 * @param $text String: pre-sanitized HTML
632 * @return string HTML
635 * @todo Handle invalid or missing images better.
637 function makeMediaLinkObj( $title, $text = '' ) {
638 if( is_null( $title ) ) {
639 ### HOTFIX. Instead of breaking, return empty string.
642 $img = new Image( $title );
643 if( $img->exists() ) {
644 $url = $img->getURL();
647 $upload = SpecialPage
::getTitleFor( 'Upload' );
648 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
651 $alt = htmlspecialchars( $title->getText() );
655 $u = htmlspecialchars( $url );
656 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
660 /** @todo document */
661 function specialLink( $name, $key = '' ) {
664 if ( '' == $key ) { $key = strtolower( $name ); }
665 $pn = $wgContLang->ucfirst( $name );
666 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
670 /** @todo document */
671 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
672 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
673 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
674 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
675 $style .= ' rel="nofollow"';
677 $url = htmlspecialchars( $url );
679 $text = htmlspecialchars( $text );
681 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
685 * Make user link (or user contributions for unregistered users)
686 * @param $userId Integer: user id in database.
687 * @param $userText String: user name in database
688 * @return string HTML fragment
691 function userLink( $userId, $userText ) {
692 $encName = htmlspecialchars( $userText );
694 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
695 return $this->makeKnownLinkObj( $contribsPage,
698 $userPage = Title
::makeTitle( NS_USER
, $userText );
699 return $this->makeLinkObj( $userPage, $encName );
704 * @param $userId Integer: user id in database.
705 * @param $userText String: user name in database.
706 * @param $redContribsWhenNoEdits Bool: return a red contribs link when the user had no edits and this is true.
707 * @return string HTML fragment with talk and/or block links
709 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false ) {
710 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
711 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
712 $blockable = ( $wgSysopUserBans ||
0 == $userId );
716 $items[] = $this->userTalkLink( $userId, $userText );
719 // check if the user has an edit
720 if( $redContribsWhenNoEdits && User
::edits( $userId ) == 0 ) {
721 $style = "class='new'";
725 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
727 $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
729 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
730 $items[] = $this->blockLink( $userId, $userText );
734 return ' (' . implode( ' | ', $items ) . ')';
741 * Alias for userToolLinks( $userId, $userText, true );
743 public function userToolLinksRedContribs( $userId, $userText ) {
744 return $this->userToolLinks( $userId, $userText, true );
749 * @param $userId Integer: user id in database.
750 * @param $userText String: user name in database.
751 * @return string HTML fragment with user talk link
754 function userTalkLink( $userId, $userText ) {
755 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
756 $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
757 return $userTalkLink;
761 * @param $userId Integer: userid
762 * @param $userText String: user name in database.
763 * @return string HTML fragment with block link
766 function blockLink( $userId, $userText ) {
767 $blockPage = SpecialPage
::getTitleFor( 'Blockip', $userText );
768 $blockLink = $this->makeKnownLinkObj( $blockPage,
769 wfMsgHtml( 'blocklink' ) );
774 * Generate a user link if the current user is allowed to view it
775 * @param $rev Revision object.
776 * @return string HTML
778 function revUserLink( $rev ) {
779 if( $rev->userCan( Revision
::DELETED_USER
) ) {
780 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
782 $link = wfMsgHtml( 'rev-deleted-user' );
784 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
785 return '<span class="history-deleted">' . $link . '</span>';
791 * Generate a user tool link cluster if the current user is allowed to view it
792 * @param $rev Revision object.
793 * @return string HTML
795 function revUserTools( $rev ) {
796 if( $rev->userCan( Revision
::DELETED_USER
) ) {
797 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
799 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
801 $link = wfMsgHtml( 'rev-deleted-user' );
803 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
804 return '<span class="history-deleted">' . $link . '</span>';
810 * This function is called by all recent changes variants, by the page history,
811 * and by the user contributions list. It is responsible for formatting edit
812 * comments. It escapes any HTML in the comment, but adds some CSS to format
813 * auto-generated comments (from section editing) and formats [[wikilinks]].
815 * @author Erik Moeller <moeller@scireview.de>
817 * Note: there's not always a title to pass to this function.
818 * Since you can't set a default parameter for a reference, I've turned it
819 * temporarily to a value pass. Should be adjusted further. --brion
821 * @param string $comment
822 * @param mixed $title Title object (to generate link to the section in autocomment) or null
823 * @param bool $local Whether section links should refer to local page
825 function formatComment($comment, $title = NULL, $local = false) {
826 wfProfileIn( __METHOD__
);
829 $comment = str_replace( "\n", " ", $comment );
830 $comment = htmlspecialchars( $comment );
832 # The pattern for autogen comments is / * foo * /, which makes for
834 # We look for all comments, match any text before and after the comment,
835 # add a separator where needed and format the comment itself with CSS
837 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
845 # Generate a valid anchor name from the section title.
846 # Hackish, but should generally work - we strip wiki
847 # syntax, including the magic [[: that is used to
848 # "link rather than show" in case of images and
849 # interlanguage links.
850 $section = str_replace( '[[:', '', $section );
851 $section = str_replace( '[[', '', $section );
852 $section = str_replace( ']]', '', $section );
854 $sectionTitle = Title
::newFromText( '#' . $section);
856 $sectionTitle = wfClone( $title );
857 $sectionTitle->mFragment
= $section;
859 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
863 if($pre) { $auto = $sep.' '.$auto; }
864 if($post) { $auto .= ' '.$sep; }
865 $auto='<span class="autocomment">'.$auto.'</span>';
866 $comment=$pre.$auto.$post;
869 # format regular and media links - all other wiki formatting
871 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA
), '/' ) . '|';
872 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA
), '/' ) . '):';
873 while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
874 # Handle link renaming [[foo|text]] will show link as "text"
875 if( "" != $match[3] ) {
881 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
882 # Media link; trail not supported.
883 $linkRegexp = '/\[\[(.*?)\]\]/';
884 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
887 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
888 $trail = $submatch[1];
892 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
893 if (isset($match[1][0]) && $match[1][0] == ':')
894 $match[1] = substr($match[1], 1);
895 $thelink = $this->makeLink( $match[1], $text, "", $trail );
897 $comment = preg_replace( $linkRegexp, StringUtils
::escapeRegexReplacement( $thelink ), $comment, 1 );
899 wfProfileOut( __METHOD__
);
904 * Wrap a comment in standard punctuation and formatting if
905 * it's non-empty, otherwise return empty string.
907 * @param string $comment
908 * @param mixed $title Title object (to generate link to section in autocomment) or null
909 * @param bool $local Whether section links should refer to local page
913 function commentBlock( $comment, $title = NULL, $local = false ) {
914 // '*' used to be the comment inserted by the software way back
915 // in antiquity in case none was provided, here for backwards
916 // compatability, acc. to brion -ævar
917 if( $comment == '' ||
$comment == '*' ) {
920 $formatted = $this->formatComment( $comment, $title, $local );
921 return " <span class=\"comment\">($formatted)</span>";
926 * Wrap and format the given revision's comment block, if the current
927 * user is allowed to view it.
929 * @param Revision $rev
930 * @param bool $local Whether section links should refer to local page
931 * @return string HTML
933 function revComment( Revision
$rev, $local = false ) {
934 if( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
935 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
937 $block = " <span class=\"comment\">" .
938 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
940 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
941 return " <span class=\"history-deleted\">$block</span>";
946 /** @todo document */
947 function tocIndent() {
951 /** @todo document */
952 function tocUnindent($level) {
953 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
957 * parameter level defines if we are on an indentation level
959 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
960 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
961 $anchor . '"><span class="tocnumber">' .
962 $tocnumber . '</span> <span class="toctext">' .
963 $tocline . '</span></a>';
966 /** @todo document */
967 function tocLineEnd() {
971 /** @todo document */
972 function tocList($toc) {
973 global $wgJsMimeType;
974 $title = wfMsgHtml('toc') ;
976 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
977 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
979 # no trailing newline, script should not be wrapped in a
981 . "</ul>\n</td></tr></table>"
982 . '<script type="' . $wgJsMimeType . '">'
983 . ' if (window.showTocToggle) {'
984 . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
985 . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
986 . ' showTocToggle();'
991 /** @todo document */
992 public function editSectionLinkForOther( $title, $section ) {
995 $title = Title
::newFromText( $title );
996 $editurl = '§ion='.$section;
997 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
999 return "<span class=\"editsection\">[".$url."]</span>";
1004 * @param $title Title object.
1005 * @param $section Integer: section number.
1006 * @param $hint Link String: title, or default if omitted or empty
1008 public function editSectionLink( $nt, $section, $hint='' ) {
1011 $editurl = '§ion='.$section;
1012 $hint = ( $hint=='' ) ?
'' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
1013 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '', $hint );
1015 return "<span class=\"editsection\">[".$url."]</span>";
1019 * Create a headline for content
1021 * @param int $level The level of the headline (1-6)
1022 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1023 * This *must* be at least '>' for no attribs
1024 * @param string $anchor The anchor to give the headline (the bit after the #)
1025 * @param string $text The text of the header
1026 * @param string $link HTML to add for the section edit link
1028 * @return string HTML headline
1030 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1031 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1035 * Split a link trail, return the "inside" portion and the remainder of the trail
1036 * as a two-element array
1040 static function splitTrail( $trail ) {
1041 static $regex = false;
1042 if ( $regex === false ) {
1044 $regex = $wgContLang->linkTrail();
1047 if ( '' != $trail ) {
1049 if ( preg_match( $regex, $trail, $m ) ) {
1054 return array( $inside, $trail );
1058 * Generate a rollback link for a given revision. Currently it's the
1059 * caller's responsibility to ensure that the revision is the top one. If
1060 * it's not, of course, the user will get an error message.
1062 * If the calling page is called with the parameter &bot=1, all rollback
1063 * links also get that parameter. It causes the edit itself and the rollback
1064 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1065 * changes, so this allows sysops to combat a busy vandal without bothering
1068 * @param Revision $rev
1070 function generateRollback( $rev ) {
1071 global $wgUser, $wgRequest;
1072 $title = $rev->getTitle();
1074 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
1075 $extraRollback .= '&token=' . urlencode(
1076 $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ) );
1077 return '<span class="mw-rollback-link">['. $this->makeKnownLinkObj( $title,
1078 wfMsg('rollbacklink'),
1079 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extraRollback ) .']</span>';
1083 * Returns HTML for the "templates used on this page" list.
1085 * @param array $templates Array of templates from Article::getUsedTemplate
1087 * @param bool $preview Whether this is for a preview
1088 * @param bool $section Whether this is for a section edit
1089 * @return string HTML output
1091 public function formatTemplates( $templates, $preview = false, $section = false) {
1093 wfProfileIn( __METHOD__
);
1095 $sk = $wgUser->getSkin();
1098 if ( count( $templates ) > 0 ) {
1099 # Do a batch existence check
1100 $batch = new LinkBatch
;
1101 foreach( $templates as $title ) {
1102 $batch->addObj( $title );
1106 # Construct the HTML
1107 $outText = '<div class="mw-templatesUsedExplanation">';
1109 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1110 } elseif ( $section ) {
1111 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1113 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1115 $outText .= '</div><ul>';
1117 foreach ( $templates as $titleObj ) {
1118 $r = $titleObj->getRestrictions( 'edit' );
1119 if ( in_array( 'sysop', $r ) ) {
1120 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1121 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1122 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1126 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1128 $outText .= '</ul>';
1130 wfProfileOut( __METHOD__
);
1135 * Format a size in bytes for output, using an appropriate
1136 * unit (B, KB, MB or GB) according to the magnitude in question
1138 * @param $size Size to format
1141 public function formatSize( $size ) {
1143 // For small sizes no decimal places necessary
1145 if( $size > 1024 ) {
1146 $size = $size / 1024;
1147 if( $size > 1024 ) {
1148 $size = $size / 1024;
1149 // For MB and bigger two decimal places are smarter
1151 if( $size > 1024 ) {
1152 $size = $size / 1024;
1153 $msg = 'size-gigabytes';
1155 $msg = 'size-megabytes';
1158 $msg = 'size-kilobytes';
1161 $msg = 'size-bytes';
1163 $size = round( $size, $round );
1164 return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
1168 * Given the id of an interface element, constructs the appropriate title
1169 * and accesskey attributes from the system messages. (Note, this is usu-
1170 * ally the id but isn't always, because sometimes the accesskey needs to
1171 * go on a different element than the id, for reverse-compatibility, etc.)
1173 * @param string $name Id of the element, minus prefixes.
1174 * @return string title and accesskey attributes, ready to drop in an
1175 * element (e.g., ' title="This does something [x]" accesskey="x"').
1177 public function tooltipAndAccesskey($name) {
1180 $tooltip = wfMsg('tooltip-'.$name);
1181 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1182 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1183 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1184 $out .= ' title="'.htmlspecialchars($tooltip);
1186 $accesskey = wfMsg('accesskey-'.$name);
1187 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1188 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1189 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1197 * Given the id of an interface element, constructs the appropriate title
1198 * attribute from the system messages. (Note, this is usually the id but
1199 * isn't always, because sometimes the accesskey needs to go on a different
1200 * element than the id, for reverse-compatibility, etc.)
1202 * @param string $name Id of the element, minus prefixes.
1203 * @return string title attribute, ready to drop in an element
1204 * (e.g., ' title="This does something"').
1206 public function tooltip($name) {
1209 $tooltip = wfMsg('tooltip-'.$name);
1210 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1211 $out = ' title="'.htmlspecialchars($tooltip).'"';