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.
7 * For the moment, Skin is a descendent class of Linker.
8 * In the future, it should probably be further split
9 * so that ever other bit of the wiki doesn't have to
10 * go loading up Skin to get at it.
15 function __construct() {}
20 function postParseLinkColour( $s = NULL ) {
25 function getExternalLinkAttributes( $link, $text, $class='' ) {
26 $link = htmlspecialchars( $link );
28 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
30 $r .= " title=\"{$link}\"";
34 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
37 $link = urldecode( $link );
38 $link = $wgContLang->checkTitleEncoding( $link );
39 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
40 $link = htmlspecialchars( $link );
42 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
44 $r .= " title=\"{$link}\"";
49 function getInternalLinkAttributes( $link, $text, $broken = false ) {
50 $link = urldecode( $link );
51 $link = str_replace( '_', ' ', $link );
52 $link = htmlspecialchars( $link );
54 if( $broken == 'stub' ) {
56 } else if ( $broken == 'yes' ) {
62 $r .= " title=\"{$link}\"";
67 * @param $nt Title object.
68 * @param $text String: FIXME
69 * @param $broken Boolean: FIXME, default 'false'.
71 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
72 if( $broken == 'stub' ) {
74 } else if ( $broken == 'yes' ) {
80 $r .= ' title="' . $nt->getEscapedText() . '"';
85 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
86 * it if you already have a title object handy. See makeLinkObj for further documentation.
88 * @param $title String: the text of the title
89 * @param $text String: link text
90 * @param $query String: optional query part
91 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
92 * be included in the link text. Other characters will be appended after
93 * the end of the link.
95 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
96 wfProfileIn( 'Linker::makeLink' );
97 $nt = Title
::newFromText( $title );
99 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
101 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
102 $result = $text == "" ?
$title : $text;
105 wfProfileOut( 'Linker::makeLink' );
110 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
111 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
113 * @param $title String: the text of the title
114 * @param $text String: link text
115 * @param $query String: optional query part
116 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
117 * be included in the link text. Other characters will be appended after
118 * the end of the link.
120 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
121 $nt = Title
::newFromText( $title );
123 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
125 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
126 return $text == '' ?
$title : $text;
131 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
132 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
134 * @param string $title The text of the title
135 * @param string $text Link text
136 * @param string $query Optional query part
137 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
138 * be included in the link text. Other characters will be appended after
139 * the end of the link.
141 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
142 $nt = Title
::newFromText( $title );
144 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
146 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
147 return $text == '' ?
$title : $text;
152 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
153 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
155 * @param $title String: the text of the title
156 * @param $text String: link text
157 * @param $query String: optional query part
158 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
159 * be included in the link text. Other characters will be appended after
160 * the end of the link.
162 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
163 $nt = Title
::newFromText( $title );
165 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
167 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
168 return $text == '' ?
$title : $text;
173 * Make a link for a title which may or may not be in the database. If you need to
174 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
175 * call to this will result in a DB query.
177 * @param $nt Title: the title object to make the link from, e.g. from
178 * Title::newFromText.
179 * @param $text String: link text
180 * @param $query String: optional query part
181 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
182 * be included in the link text. Other characters will be appended after
183 * the end of the link.
184 * @param $prefix String: optional prefix. As trail, only before instead of after.
186 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
188 $fname = 'Linker::makeLinkObj';
189 wfProfileIn( $fname );
192 if ( ! is_object($nt) ) {
193 # throw new MWException();
194 wfProfileOut( $fname );
195 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
198 if ( $nt->isExternal() ) {
199 $u = $nt->getFullURL();
200 $link = $nt->getPrefixedURL();
201 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
202 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
205 if ( '' != $trail ) {
207 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
212 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
214 wfProfileOut( $fname );
216 } elseif ( $nt->isAlwaysKnown() ) {
217 # Image links, special page links and self-links with fragements are always known.
218 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
220 wfProfileIn( $fname.'-immediate' );
221 # Work out link colour immediately
222 $aid = $nt->getArticleID() ;
224 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
226 $threshold = $wgUser->getOption('stubthreshold') ;
227 if ( $threshold > 0 ) {
228 $dbr = wfGetDB( DB_SLAVE
);
229 $s = $dbr->selectRow(
233 'page_is_redirect' ),
234 array( 'page_id' => $aid ), $fname ) ;
235 if ( $s !== false ) {
236 $size = $s->page_len
;
237 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
238 $size = $threshold*2 ; # Really big
241 $size = $threshold*2 ; # Really big
246 if ( $size < $threshold ) {
247 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
249 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
252 wfProfileOut( $fname.'-immediate' );
254 wfProfileOut( $fname );
259 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
260 * it doesn't have to do a database query. It's also valid for interwiki titles and special
263 * @param $nt Title object of target page
264 * @param $text String: text to replace the title
265 * @param $query String: link target
266 * @param $trail String: text after link
267 * @param $prefix String: text before link text
268 * @param $aprops String: extra attributes to the a-element
269 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
270 * @return the a-element
272 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
274 $fname = 'Linker::makeKnownLinkObj';
275 wfProfileIn( $fname );
277 if ( !is_object( $nt ) ) {
278 wfProfileOut( $fname );
282 $u = $nt->escapeLocalURL( $query );
283 if ( $nt->getFragment() != '' ) {
284 if( $nt->getPrefixedDbkey() == '' ) {
287 $text = htmlspecialchars( $nt->getFragment() );
290 $u .= $nt->getFragmentForURL();
293 $text = htmlspecialchars( $nt->getPrefixedText() );
295 if ( $style == '' ) {
296 $style = $this->getInternalLinkAttributesObj( $nt, $text );
299 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
301 list( $inside, $trail ) = Linker
::splitTrail( $trail );
302 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
303 wfProfileOut( $fname );
308 * Make a red link to the edit page of a given title.
310 * @param $title String: The text of the title
311 * @param $text String: Link text
312 * @param $query String: Optional query part
313 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
314 * be included in the link text. Other characters will be appended after
315 * the end of the link.
317 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
319 if ( ! isset($nt) ) {
320 # throw new MWException();
321 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
324 $fname = 'Linker::makeBrokenLinkObj';
325 wfProfileIn( $fname );
327 if ( '' == $query ) {
330 $q = 'action=edit&'.$query;
332 $u = $nt->escapeLocalURL( $q );
335 $text = htmlspecialchars( $nt->getPrefixedText() );
337 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
339 list( $inside, $trail ) = Linker
::splitTrail( $trail );
340 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
342 wfProfileOut( $fname );
347 * Make a brown link to a short article.
349 * @param $title String: the text of the title
350 * @param $text String: link text
351 * @param $query String: optional query part
352 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
353 * be included in the link text. Other characters will be appended after
354 * the end of the link.
356 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
357 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
358 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
362 * Generate either a normal exists-style link or a stub link, depending
363 * on the given page size.
365 * @param $size Integer
366 * @param $nt Title object.
367 * @param $text String
368 * @param $query String
369 * @param $trail String
370 * @param $prefix String
371 * @return string HTML of link
373 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
375 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
376 if( $size < $threshold ) {
377 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
379 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
384 * Make appropriate markup for a link to the current article. This is currently rendered
385 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
386 * despite $query not being used.
388 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
390 $text = htmlspecialchars( $nt->getPrefixedText() );
392 list( $inside, $trail ) = Linker
::splitTrail( $trail );
393 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
396 /** @todo document */
397 function fnamePart( $url ) {
398 $basename = strrchr( $url, '/' );
399 if ( false === $basename ) {
402 $basename = substr( $basename, 1 );
404 return htmlspecialchars( $basename );
407 /** Obsolete alias */
408 function makeImage( $url, $alt = '' ) {
409 return $this->makeExternalImage( $url, $alt );
412 /** @todo document */
413 function makeExternalImage( $url, $alt = '' ) {
415 $alt = $this->fnamePart( $url );
417 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
421 /** @todo document */
422 function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
423 $thumb = false, $manual_thumb = '', $valign = '' )
425 global $wgContLang, $wgUser, $wgThumbLimits;
427 $img = new Image( $nt );
429 if ( !$img->allowInlineDisplay() && $img->exists() ) {
430 return $this->makeKnownLinkObj( $nt );
433 $error = $prefix = $postfix = '';
434 $page = isset( $params['page'] ) ?
$params['page'] : false;
436 if ( 'center' == $align )
438 $prefix = '<div class="center">';
443 if ( !isset( $params['width'] ) ) {
444 $params['width'] = $img->getWidth( $page );
445 if( $thumb ||
$framed ) {
446 $wopt = $wgUser->getOption( 'thumbsize' );
448 if( !isset( $wgThumbLimits[$wopt] ) ) {
449 $wopt = User
::getDefaultOption( 'thumbsize' );
452 $params['width'] = min( $params['width'], $wgThumbLimits[$wopt] );
456 if ( $thumb ||
$framed ) {
458 # Create a thumbnail. Alignment depends on language
459 # writing direction, # right aligned for left-to-right-
460 # languages ("Western languages"), left-aligned
461 # for right-to-left-languages ("Semitic languages")
463 # If thumbnail width has not been provided, it is set
464 # to the default user option as specified in Language*.php
465 if ( $align == '' ) {
466 $align = $wgContLang->isRTL() ?
'left' : 'right';
468 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $params, $framed, $manual_thumb ).$postfix;
471 if ( $params['width'] && $img->exists() ) {
472 # Create a resized image, without the additional thumbnail features
473 $thumb = $img->transform( $params );
479 $query = 'page=' . urlencode( $page );
483 $u = $nt->getLocalURL( $query );
489 $imgAttribs['style'] = "vertical-align: $valign";
491 $linkAttribs = array(
498 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
500 $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
502 if ( '' != $align ) {
503 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
505 return str_replace("\n", ' ',$prefix.$s.$postfix);
509 * Make HTML for a thumbnail including image, border and caption
510 * $img is an Image object
512 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "" ) {
513 global $wgStylePath, $wgContLang;
517 $page = isset( $params['page'] ) ?
$params['page'] : false;
519 if ( empty( $params['width'] ) ) {
520 $params['width'] = 180;
523 if ( $manual_thumb != '' ) {
524 # Use manually specified thumbnail
525 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb );
526 if( $manual_title ) {
527 $manual_img = new Image( $manual_title );
528 $thumb = $manual_img->getUnscaledThumb();
530 } elseif ( $framed ) {
531 // Use image dimensions, don't scale
532 $thumb = $img->getUnscaledThumb( $page );
534 # Do not present an image bigger than the source, for bitmap-style images
535 # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
536 $srcWidth = $img->getWidth( $page );
537 if ( $srcWidth && !$img->mustRender() && $params['width'] > $srcWidth ) {
538 $params['width'] = $srcWidth;
540 $thumb = $img->transform( $params );
544 $outerWidth = $thumb->getWidth() +
2;
546 $outerWidth = $params['width'] +
2;
549 $query = $page ?
'page=' . urlencode( $page ) : '';
550 $u = $img->getTitle()->getLocalURL( $query );
552 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
553 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
554 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
556 $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
558 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
560 } elseif( !$img->exists() ) {
561 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
567 'class' => 'thumbimage'
569 $linkAttribs = array(
571 'class' => 'internal',
575 $s .= $thumb->toHtml( $imgAttribs, $linkAttribs );
579 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
580 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
581 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
582 'width="15" height="11" alt="" /></a></div>';
585 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
586 return str_replace("\n", ' ', $s);
590 * Pass a title object, not a title string
592 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
594 if ( ! isset($nt) ) {
595 # throw new MWException();
596 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
599 $fname = 'Linker::makeBrokenImageLinkObj';
600 wfProfileIn( $fname );
602 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
603 if ( '' != $query ) {
606 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
607 $url = $uploadTitle->escapeLocalURL( $q );
610 $text = htmlspecialchars( $nt->getPrefixedText() );
612 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
613 list( $inside, $trail ) = Linker
::splitTrail( $trail );
614 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
616 wfProfileOut( $fname );
620 /** @todo document */
621 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
622 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
623 return $this->makeMediaLinkObj( $nt, $alt );
627 * Create a direct link to a given uploaded file.
629 * @param $title Title object.
630 * @param $text String: pre-sanitized HTML
631 * @return string HTML
634 * @todo Handle invalid or missing images better.
636 function makeMediaLinkObj( $title, $text = '' ) {
637 if( is_null( $title ) ) {
638 ### HOTFIX. Instead of breaking, return empty string.
641 $img = new Image( $title );
642 if( $img->exists() ) {
643 $url = $img->getURL();
646 $upload = SpecialPage
::getTitleFor( 'Upload' );
647 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
650 $alt = htmlspecialchars( $title->getText() );
654 $u = htmlspecialchars( $url );
655 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
659 /** @todo document */
660 function specialLink( $name, $key = '' ) {
663 if ( '' == $key ) { $key = strtolower( $name ); }
664 $pn = $wgContLang->ucfirst( $name );
665 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
669 /** @todo document */
670 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
671 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
672 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
673 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
674 $style .= ' rel="nofollow"';
676 $url = htmlspecialchars( $url );
678 $text = htmlspecialchars( $text );
680 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
684 * Make user link (or user contributions for unregistered users)
685 * @param $userId Integer: user id in database.
686 * @param $userText String: user name in database
687 * @return string HTML fragment
690 function userLink( $userId, $userText ) {
691 $encName = htmlspecialchars( $userText );
693 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
694 return $this->makeKnownLinkObj( $contribsPage,
697 $userPage = Title
::makeTitle( NS_USER
, $userText );
698 return $this->makeLinkObj( $userPage, $encName );
703 * @param $userId Integer: user id in database.
704 * @param $userText String: user name in database.
705 * @param $redContribsWhenNoEdits Bool: return a red contribs link when the user had no edits and this is true.
706 * @return string HTML fragment with talk and/or block links
708 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false ) {
709 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
710 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
711 $blockable = ( $wgSysopUserBans ||
0 == $userId );
715 $items[] = $this->userTalkLink( $userId, $userText );
718 // check if the user has an edit
719 if( $redContribsWhenNoEdits && User
::edits( $userId ) == 0 ) {
720 $style = "class='new'";
724 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
726 $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
728 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
729 $items[] = $this->blockLink( $userId, $userText );
733 return ' (' . implode( ' | ', $items ) . ')';
740 * Alias for userToolLinks( $userId, $userText, true );
742 public function userToolLinksRedContribs( $userId, $userText ) {
743 return $this->userToolLinks( $userId, $userText, true );
748 * @param $userId Integer: user id in database.
749 * @param $userText String: user name in database.
750 * @return string HTML fragment with user talk link
753 function userTalkLink( $userId, $userText ) {
754 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
755 $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
756 return $userTalkLink;
760 * @param $userId Integer: userid
761 * @param $userText String: user name in database.
762 * @return string HTML fragment with block link
765 function blockLink( $userId, $userText ) {
766 $blockPage = SpecialPage
::getTitleFor( 'Blockip', $userText );
767 $blockLink = $this->makeKnownLinkObj( $blockPage,
768 wfMsgHtml( 'blocklink' ) );
773 * Generate a user link if the current user is allowed to view it
774 * @param $rev Revision object.
775 * @return string HTML
777 function revUserLink( $rev ) {
778 if( $rev->userCan( Revision
::DELETED_USER
) ) {
779 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
781 $link = wfMsgHtml( 'rev-deleted-user' );
783 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
784 return '<span class="history-deleted">' . $link . '</span>';
790 * Generate a user tool link cluster if the current user is allowed to view it
791 * @param $rev Revision object.
792 * @return string HTML
794 function revUserTools( $rev ) {
795 if( $rev->userCan( Revision
::DELETED_USER
) ) {
796 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
798 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
800 $link = wfMsgHtml( 'rev-deleted-user' );
802 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
803 return '<span class="history-deleted">' . $link . '</span>';
809 * This function is called by all recent changes variants, by the page history,
810 * and by the user contributions list. It is responsible for formatting edit
811 * comments. It escapes any HTML in the comment, but adds some CSS to format
812 * auto-generated comments (from section editing) and formats [[wikilinks]].
814 * @author Erik Moeller <moeller@scireview.de>
816 * Note: there's not always a title to pass to this function.
817 * Since you can't set a default parameter for a reference, I've turned it
818 * temporarily to a value pass. Should be adjusted further. --brion
820 * @param string $comment
821 * @param mixed $title Title object (to generate link to the section in autocomment) or null
822 * @param bool $local Whether section links should refer to local page
824 function formatComment($comment, $title = NULL, $local = false) {
825 wfProfileIn( __METHOD__
);
828 $comment = str_replace( "\n", " ", $comment );
829 $comment = htmlspecialchars( $comment );
831 # The pattern for autogen comments is / * foo * /, which makes for
833 # We look for all comments, match any text before and after the comment,
834 # add a separator where needed and format the comment itself with CSS
836 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
844 # Generate a valid anchor name from the section title.
845 # Hackish, but should generally work - we strip wiki
846 # syntax, including the magic [[: that is used to
847 # "link rather than show" in case of images and
848 # interlanguage links.
849 $section = str_replace( '[[:', '', $section );
850 $section = str_replace( '[[', '', $section );
851 $section = str_replace( ']]', '', $section );
853 $sectionTitle = Title
::newFromText( '#' . $section);
855 $sectionTitle = wfClone( $title );
856 $sectionTitle->mFragment
= $section;
858 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
862 if($pre) { $auto = $sep.' '.$auto; }
863 if($post) { $auto .= ' '.$sep; }
864 $auto='<span class="autocomment">'.$auto.'</span>';
865 $comment=$pre.$auto.$post;
868 # format regular and media links - all other wiki formatting
870 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA
), '/' ) . '|';
871 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA
), '/' ) . '):';
872 while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
873 # Handle link renaming [[foo|text]] will show link as "text"
874 if( "" != $match[3] ) {
880 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
881 # Media link; trail not supported.
882 $linkRegexp = '/\[\[(.*?)\]\]/';
883 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
886 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
887 $trail = $submatch[1];
891 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
892 if (isset($match[1][0]) && $match[1][0] == ':')
893 $match[1] = substr($match[1], 1);
894 $thelink = $this->makeLink( $match[1], $text, "", $trail );
896 $comment = preg_replace( $linkRegexp, StringUtils
::escapeRegexReplacement( $thelink ), $comment, 1 );
898 wfProfileOut( __METHOD__
);
903 * Wrap a comment in standard punctuation and formatting if
904 * it's non-empty, otherwise return empty string.
906 * @param string $comment
907 * @param mixed $title Title object (to generate link to section in autocomment) or null
908 * @param bool $local Whether section links should refer to local page
912 function commentBlock( $comment, $title = NULL, $local = false ) {
913 // '*' used to be the comment inserted by the software way back
914 // in antiquity in case none was provided, here for backwards
915 // compatability, acc. to brion -ævar
916 if( $comment == '' ||
$comment == '*' ) {
919 $formatted = $this->formatComment( $comment, $title, $local );
920 return " <span class=\"comment\">($formatted)</span>";
925 * Wrap and format the given revision's comment block, if the current
926 * user is allowed to view it.
928 * @param Revision $rev
929 * @param bool $local Whether section links should refer to local page
930 * @return string HTML
932 function revComment( Revision
$rev, $local = false ) {
933 if( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
934 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
936 $block = " <span class=\"comment\">" .
937 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
939 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
940 return " <span class=\"history-deleted\">$block</span>";
945 /** @todo document */
946 function tocIndent() {
950 /** @todo document */
951 function tocUnindent($level) {
952 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
956 * parameter level defines if we are on an indentation level
958 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
959 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
960 $anchor . '"><span class="tocnumber">' .
961 $tocnumber . '</span> <span class="toctext">' .
962 $tocline . '</span></a>';
965 /** @todo document */
966 function tocLineEnd() {
970 /** @todo document */
971 function tocList($toc) {
972 global $wgJsMimeType;
973 $title = wfMsgHtml('toc') ;
975 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
976 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
978 # no trailing newline, script should not be wrapped in a
980 . "</ul>\n</td></tr></table>"
981 . '<script type="' . $wgJsMimeType . '">'
982 . ' if (window.showTocToggle) {'
983 . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
984 . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
985 . ' showTocToggle();'
990 /** @todo document */
991 public function editSectionLinkForOther( $title, $section ) {
994 $title = Title
::newFromText( $title );
995 $editurl = '§ion='.$section;
996 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
998 return "<span class=\"editsection\">[".$url."]</span>";
1003 * @param $title Title object.
1004 * @param $section Integer: section number.
1005 * @param $hint Link String: title, or default if omitted or empty
1007 public function editSectionLink( $nt, $section, $hint='' ) {
1010 $editurl = '§ion='.$section;
1011 $hint = ( $hint=='' ) ?
'' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
1012 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '', $hint );
1014 return "<span class=\"editsection\">[".$url."]</span>";
1018 * Create a headline for content
1020 * @param int $level The level of the headline (1-6)
1021 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1022 * This *must* be at least '>' for no attribs
1023 * @param string $anchor The anchor to give the headline (the bit after the #)
1024 * @param string $text The text of the header
1025 * @param string $link HTML to add for the section edit link
1027 * @return string HTML headline
1029 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1030 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1034 * Split a link trail, return the "inside" portion and the remainder of the trail
1035 * as a two-element array
1039 static function splitTrail( $trail ) {
1040 static $regex = false;
1041 if ( $regex === false ) {
1043 $regex = $wgContLang->linkTrail();
1046 if ( '' != $trail ) {
1048 if ( preg_match( $regex, $trail, $m ) ) {
1053 return array( $inside, $trail );
1057 * Generate a rollback link for a given revision. Currently it's the
1058 * caller's responsibility to ensure that the revision is the top one. If
1059 * it's not, of course, the user will get an error message.
1061 * If the calling page is called with the parameter &bot=1, all rollback
1062 * links also get that parameter. It causes the edit itself and the rollback
1063 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1064 * changes, so this allows sysops to combat a busy vandal without bothering
1067 * @param Revision $rev
1069 function generateRollback( $rev ) {
1070 global $wgUser, $wgRequest;
1071 $title = $rev->getTitle();
1073 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
1074 $extraRollback .= '&token=' . urlencode(
1075 $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ) );
1076 return '<span class="mw-rollback-link">['. $this->makeKnownLinkObj( $title,
1077 wfMsg('rollbacklink'),
1078 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extraRollback ) .']</span>';
1082 * Returns HTML for the "templates used on this page" list.
1084 * @param array $templates Array of templates from Article::getUsedTemplate
1086 * @param bool $preview Whether this is for a preview
1087 * @param bool $section Whether this is for a section edit
1088 * @return string HTML output
1090 public function formatTemplates( $templates, $preview = false, $section = false) {
1092 wfProfileIn( __METHOD__
);
1094 $sk = $wgUser->getSkin();
1097 if ( count( $templates ) > 0 ) {
1098 # Do a batch existence check
1099 $batch = new LinkBatch
;
1100 foreach( $templates as $title ) {
1101 $batch->addObj( $title );
1105 # Construct the HTML
1106 $outText = '<div class="mw-templatesUsedExplanation">';
1108 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1109 } elseif ( $section ) {
1110 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1112 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1114 $outText .= '</div><ul>';
1116 foreach ( $templates as $titleObj ) {
1117 $r = $titleObj->getRestrictions( 'edit' );
1118 if ( in_array( 'sysop', $r ) ) {
1119 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1120 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1121 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1125 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1127 $outText .= '</ul>';
1129 wfProfileOut( __METHOD__
);
1134 * Format a size in bytes for output, using an appropriate
1135 * unit (B, KB, MB or GB) according to the magnitude in question
1137 * @param $size Size to format
1140 public function formatSize( $size ) {
1142 // For small sizes no decimal places necessary
1144 if( $size > 1024 ) {
1145 $size = $size / 1024;
1146 if( $size > 1024 ) {
1147 $size = $size / 1024;
1148 // For MB and bigger two decimal places are smarter
1150 if( $size > 1024 ) {
1151 $size = $size / 1024;
1152 $msg = 'size-gigabytes';
1154 $msg = 'size-megabytes';
1157 $msg = 'size-kilobytes';
1160 $msg = 'size-bytes';
1162 $size = round( $size, $round );
1163 return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
1167 * Given the id of an interface element, constructs the appropriate title
1168 * and accesskey attributes from the system messages. (Note, this is usu-
1169 * ally the id but isn't always, because sometimes the accesskey needs to
1170 * go on a different element than the id, for reverse-compatibility, etc.)
1172 * @param string $name Id of the element, minus prefixes.
1173 * @return string title and accesskey attributes, ready to drop in an
1174 * element (e.g., ' title="This does something [x]" accesskey="x"').
1176 public function tooltipAndAccesskey($name) {
1179 $tooltip = wfMsg('tooltip-'.$name);
1180 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1181 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1182 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1183 $out .= ' title="'.htmlspecialchars($tooltip);
1185 $accesskey = wfMsg('accesskey-'.$name);
1186 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1187 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1188 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1196 * Given the id of an interface element, constructs the appropriate title
1197 * attribute from the system messages. (Note, this is usually the id but
1198 * isn't always, because sometimes the accesskey needs to go on a different
1199 * element than the id, for reverse-compatibility, etc.)
1201 * @param string $name Id of the element, minus prefixes.
1202 * @return string title attribute, ready to drop in an element
1203 * (e.g., ' title="This does something"').
1205 public function tooltip($name) {
1208 $tooltip = wfMsg('tooltip-'.$name);
1209 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1210 $out = ' title="'.htmlspecialchars($tooltip).'"';