2a50547d619464971febc43d78f0d4788a338309
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 $same = ($link == $text);
42 $link = urldecode( $link );
43 $link = $wgContLang->checkTitleEncoding( $link );
44 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
45 $link = htmlspecialchars( $link );
47 $r = ($class != '') ?
" class=\"$class\"" : " class=\"external\"";
49 $r .= " title=\"{$link}\"";
54 function getInternalLinkAttributes( $link, $text, $broken = false ) {
55 $link = urldecode( $link );
56 $link = str_replace( '_', ' ', $link );
57 $link = htmlspecialchars( $link );
59 if( $broken == 'stub' ) {
61 } else if ( $broken == 'yes' ) {
67 $r .= " title=\"{$link}\"";
72 * @param $nt Title object.
73 * @param $text String: FIXME
74 * @param $broken Boolean: FIXME, default 'false'.
76 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
77 if( $broken == 'stub' ) {
79 } else if ( $broken == 'yes' ) {
85 $r .= ' title="' . $nt->getEscapedText() . '"';
90 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
91 * it if you already have a title object handy. See makeLinkObj for further documentation.
93 * @param $title String: the text of the title
94 * @param $text String: link text
95 * @param $query String: optional query part
96 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
97 * be included in the link text. Other characters will be appended after
98 * the end of the link.
100 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
101 wfProfileIn( 'Linker::makeLink' );
102 $nt = Title
::newFromText( $title );
104 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
106 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
107 $result = $text == "" ?
$title : $text;
110 wfProfileOut( 'Linker::makeLink' );
115 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
116 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
118 * @param $title String: the text of the title
119 * @param $text String: link text
120 * @param $query String: optional query part
121 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
122 * be included in the link text. Other characters will be appended after
123 * the end of the link.
125 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
126 $nt = Title
::newFromText( $title );
128 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
130 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
131 return $text == '' ?
$title : $text;
136 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
137 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
139 * @param string $title The text of the title
140 * @param string $text Link text
141 * @param string $query Optional query part
142 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
143 * be included in the link text. Other characters will be appended after
144 * the end of the link.
146 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
147 $nt = Title
::newFromText( $title );
149 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
151 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
152 return $text == '' ?
$title : $text;
157 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
158 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
160 * @param $title String: the text of the title
161 * @param $text String: link text
162 * @param $query String: optional query part
163 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
164 * be included in the link text. Other characters will be appended after
165 * the end of the link.
167 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
168 $nt = Title
::newFromText( $title );
170 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
172 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
173 return $text == '' ?
$title : $text;
178 * Make a link for a title which may or may not be in the database. If you need to
179 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
180 * call to this will result in a DB query.
182 * @param $nt Title: the title object to make the link from, e.g. from
183 * Title::newFromText.
184 * @param $text String: link text
185 * @param $query String: optional query part
186 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
187 * be included in the link text. Other characters will be appended after
188 * the end of the link.
189 * @param $prefix String: optional prefix. As trail, only before instead of after.
191 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
193 $fname = 'Linker::makeLinkObj';
194 wfProfileIn( $fname );
197 if ( ! is_object($nt) ) {
198 # throw new MWException();
199 wfProfileOut( $fname );
200 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
203 $ns = $nt->getNamespace();
204 $dbkey = $nt->getDBkey();
205 if ( $nt->isExternal() ) {
206 $u = $nt->getFullURL();
207 $link = $nt->getPrefixedURL();
208 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
209 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
212 if ( '' != $trail ) {
213 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
219 # Check for anchors, normalize the anchor
221 $parts = explode( '#', $u, 2 );
222 if ( count( $parts ) == 2 ) {
223 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
224 $replacearray = array(
228 $u = $parts[0] . '#' .
229 str_replace( array_keys( $replacearray ),
230 array_values( $replacearray ),
234 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
236 wfProfileOut( $fname );
238 } elseif ( $nt->isAlwaysKnown() ) {
239 # Image links, special page links and self-links with fragements are always known.
240 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
242 wfProfileIn( $fname.'-immediate' );
243 # Work out link colour immediately
244 $aid = $nt->getArticleID() ;
246 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
248 $threshold = $wgUser->getOption('stubthreshold') ;
249 if ( $threshold > 0 ) {
250 $dbr =& wfGetDB( DB_SLAVE
);
251 $s = $dbr->selectRow(
255 'page_is_redirect' ),
256 array( 'page_id' => $aid ), $fname ) ;
257 if ( $s !== false ) {
258 $size = $s->page_len
;
259 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
260 $size = $threshold*2 ; # Really big
263 $size = $threshold*2 ; # Really big
268 if ( $size < $threshold ) {
269 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
271 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
274 wfProfileOut( $fname.'-immediate' );
276 wfProfileOut( $fname );
281 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
282 * it doesn't have to do a database query. It's also valid for interwiki titles and special
285 * @param $nt Title object of target page
286 * @param $text String: text to replace the title
287 * @param $query String: link target
288 * @param $trail String: text after link
289 * @param $prefix String: text before link text
290 * @param $aprops String: extra attributes to the a-element
291 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
292 * @return the a-element
294 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
296 $fname = 'Linker::makeKnownLinkObj';
297 wfProfileIn( $fname );
299 if ( !is_object( $nt ) ) {
300 wfProfileOut( $fname );
304 $u = $nt->escapeLocalURL( $query );
305 if ( $nt->getFragment() != '' ) {
306 if( $nt->getPrefixedDbkey() == '' ) {
309 $text = htmlspecialchars( $nt->getFragment() );
312 $anchor = urlencode( Sanitizer
::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
313 $replacearray = array(
317 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
320 $text = htmlspecialchars( $nt->getPrefixedText() );
322 if ( $style == '' ) {
323 $style = $this->getInternalLinkAttributesObj( $nt, $text );
326 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
328 list( $inside, $trail ) = Linker
::splitTrail( $trail );
329 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
330 wfProfileOut( $fname );
335 * Make a red link to the edit page of a given title.
337 * @param $title String: The text of the title
338 * @param $text String: Link text
339 * @param $query String: Optional query part
340 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
341 * be included in the link text. Other characters will be appended after
342 * the end of the link.
344 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
346 if ( ! isset($nt) ) {
347 # throw new MWException();
348 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
351 $fname = 'Linker::makeBrokenLinkObj';
352 wfProfileIn( $fname );
354 if ( '' == $query ) {
357 $q = 'action=edit&'.$query;
359 $u = $nt->escapeLocalURL( $q );
362 $text = htmlspecialchars( $nt->getPrefixedText() );
364 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
366 list( $inside, $trail ) = Linker
::splitTrail( $trail );
367 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
369 wfProfileOut( $fname );
374 * Make a brown link to a short article.
376 * @param $title String: the text of the title
377 * @param $text String: link text
378 * @param $query String: optional query part
379 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
380 * be included in the link text. Other characters will be appended after
381 * the end of the link.
383 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
384 $link = $nt->getPrefixedURL();
386 $u = $nt->escapeLocalURL( $query );
389 $text = htmlspecialchars( $nt->getPrefixedText() );
391 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
393 list( $inside, $trail ) = Linker
::splitTrail( $trail );
394 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
399 * Generate either a normal exists-style link or a stub link, depending
400 * on the given page size.
402 * @param $size Integer
403 * @param $nt Title object.
404 * @param $text String
405 * @param $query String
406 * @param $trail String
407 * @param $prefix String
408 * @return string HTML of link
410 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
412 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
413 if( $size < $threshold ) {
414 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
416 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
421 * Make appropriate markup for a link to the current article. This is currently rendered
422 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
423 * despite $query not being used.
425 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
427 $text = htmlspecialchars( $nt->getPrefixedText() );
429 list( $inside, $trail ) = Linker
::splitTrail( $trail );
430 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
433 /** @todo document */
434 function fnamePart( $url ) {
435 $basename = strrchr( $url, '/' );
436 if ( false === $basename ) {
439 $basename = substr( $basename, 1 );
441 return htmlspecialchars( $basename );
444 /** Obsolete alias */
445 function makeImage( $url, $alt = '' ) {
446 return $this->makeExternalImage( $url, $alt );
449 /** @todo document */
450 function makeExternalImage( $url, $alt = '' ) {
452 $alt = $this->fnamePart( $url );
454 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
458 /** @todo document */
459 function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
460 $thumb = false, $manual_thumb = '', $page = null )
462 global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
464 $img = new Image( $nt );
466 if ( ! is_null( $page ) ) {
467 $img->selectPage( $page );
470 if ( !$img->allowInlineDisplay() && $img->exists() ) {
471 return $this->makeKnownLinkObj( $nt );
474 $url = $img->getViewURL();
475 $error = $prefix = $postfix = '';
477 wfDebug( "makeImageLinkObj: '$width'x'$height', \"$label\"\n" );
479 if ( 'center' == $align )
481 $prefix = '<div class="center">';
486 if ( $thumb ||
$framed ) {
488 # Create a thumbnail. Alignment depends on language
489 # writing direction, # right aligned for left-to-right-
490 # languages ("Western languages"), left-aligned
491 # for right-to-left-languages ("Semitic languages")
493 # If thumbnail width has not been provided, it is set
494 # to the default user option as specified in Language*.php
495 if ( $align == '' ) {
496 $align = $wgContLang->isRTL() ?
'left' : 'right';
500 if ( $width === false ) {
501 $wopt = $wgUser->getOption( 'thumbsize' );
503 if( !isset( $wgThumbLimits[$wopt] ) ) {
504 $wopt = User
::getDefaultOption( 'thumbsize' );
507 $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
510 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
513 if ( $width && $img->exists() ) {
515 # Create a resized image, without the additional thumbnail
518 if ( $height == false )
520 if ( $manual_thumb == '') {
521 $thumb = $img->getThumbnail( $width, $height, $wgGenerateThumbnailOnParse );
523 // In most cases, $width = $thumb->width or $height = $thumb->height.
524 // If not, we're scaling the image larger than it can be scaled,
525 // so we send to the browser a smaller thumbnail, and let the client do the scaling.
527 if ($height != -1 && $width > $thumb->width
* $height / $thumb->height
) {
528 // $height is the limiting factor, not $width
529 // set $width to the largest it can be, such that the resulting
530 // scaled height is at most $height
531 $width = floor($thumb->width
* $height / $thumb->height
);
533 $height = round($thumb->height
* $width / $thumb->width
);
535 wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" );
536 $url = $thumb->getUrl();
538 $error = htmlspecialchars( $img->getLastError() );
539 // Do client-side scaling...
540 $height = intval( $img->getHeight() * $width / $img->getWidth() );
544 $width = $img->width
;
545 $height = $img->height
;
548 wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
549 $u = $nt->escapeLocalURL();
552 } elseif ( $url == '' ) {
553 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
554 //$s .= "<br />{$alt}<br />{$url}<br />\n";
556 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
557 '<img src="'.$url.'" alt="'.$alt.'" ' .
559 ?
( 'width="'.$width.'" height="'.$height.'" ' )
561 'longdesc="'.$u.'" /></a>';
563 if ( '' != $align ) {
564 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
566 return str_replace("\n", ' ',$prefix.$s.$postfix);
570 * Make HTML for a thumbnail including image, border and caption
571 * $img is an Image object
573 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
574 global $wgStylePath, $wgContLang, $wgGenerateThumbnailOnParse;
578 $width = $height = 0;
579 if ( $img->exists() ) {
580 $width = $img->getWidth();
581 $height = $img->getHeight();
583 if ( 0 == $width ||
0 == $height ) {
584 $width = $height = 180;
586 if ( $boxwidth == 0 ) {
590 // Use image dimensions, don't scale
592 $boxheight = $height;
593 $thumbUrl = $img->getViewURL();
595 if ( $boxheight === false )
597 if ( '' == $manual_thumb ) {
598 $thumb = $img->getThumbnail( $boxwidth, $boxheight, $wgGenerateThumbnailOnParse );
600 $thumbUrl = $thumb->getUrl();
601 $boxwidth = $thumb->width
;
602 $boxheight = $thumb->height
;
604 $error = $img->getLastError();
608 $oboxwidth = $boxwidth +
2;
610 if ( $manual_thumb != '' ) # Use manually specified thumbnail
612 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
613 if( $manual_title ) {
614 $manual_img = new Image( $manual_title );
615 $thumbUrl = $manual_img->getViewURL();
616 if ( $manual_img->exists() )
618 $width = $manual_img->getWidth();
619 $height = $manual_img->getHeight();
621 $boxheight = $height ;
622 $oboxwidth = $boxwidth +
2 ;
627 $u = $img->getEscapeLocalURL();
629 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
630 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
631 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
633 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
634 if( $thumbUrl == '' ) {
635 // Couldn't generate thumbnail? Scale the image client-side.
636 $thumbUrl = $img->getViewURL();
637 if( $boxheight == -1 ) {
639 $boxheight = intval( $height * $boxwidth / $width );
643 $s .= htmlspecialchars( $error );
645 } elseif( !$img->exists() ) {
646 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
649 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
650 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
651 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
652 'longdesc="'.$u.'" /></a>';
656 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
657 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
658 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
659 'width="15" height="11" alt="'.$more.'" /></a></div>';
662 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
663 return str_replace("\n", ' ', $s);
667 * Pass a title object, not a title string
669 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
671 if ( ! isset($nt) ) {
672 # throw new MWException();
673 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
676 $fname = 'Linker::makeBrokenImageLinkObj';
677 wfProfileIn( $fname );
679 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
680 if ( '' != $query ) {
683 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
684 $url = $uploadTitle->escapeLocalURL( $q );
687 $text = htmlspecialchars( $nt->getPrefixedText() );
689 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
690 list( $inside, $trail ) = Linker
::splitTrail( $trail );
691 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
693 wfProfileOut( $fname );
697 /** @todo document */
698 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
699 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
700 return $this->makeMediaLinkObj( $nt, $alt );
704 * Create a direct link to a given uploaded file.
706 * @param $title Title object.
707 * @param $text String: pre-sanitized HTML
708 * @param $nourl Boolean: Mask absolute URLs, so the parser doesn't
709 * linkify them (it is currently not context-aware)
710 * @return string HTML
713 * @todo Handle invalid or missing images better.
715 function makeMediaLinkObj( $title, $text = '' ) {
716 if( is_null( $title ) ) {
717 ### HOTFIX. Instead of breaking, return empty string.
720 $name = $title->getDBKey();
721 $img = new Image( $title );
722 if( $img->exists() ) {
723 $url = $img->getURL();
726 $upload = SpecialPage
::getTitleFor( 'Upload' );
727 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
730 $alt = htmlspecialchars( $title->getText() );
734 $u = htmlspecialchars( $url );
735 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
739 /** @todo document */
740 function specialLink( $name, $key = '' ) {
743 if ( '' == $key ) { $key = strtolower( $name ); }
744 $pn = $wgContLang->ucfirst( $name );
745 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
749 /** @todo document */
750 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
751 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
752 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
753 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
754 $style .= ' rel="nofollow"';
756 $url = htmlspecialchars( $url );
758 $text = htmlspecialchars( $text );
760 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
764 * Make user link (or user contributions for unregistered users)
765 * @param $userId Integer: user id in database.
766 * @param $userText String: user name in database
767 * @return string HTML fragment
770 function userLink( $userId, $userText ) {
771 $encName = htmlspecialchars( $userText );
773 $contribsPage = SpecialPage
::getTitleFor( 'Contributions' );
774 return $this->makeKnownLinkObj( $contribsPage,
775 $encName, 'target=' . urlencode( $userText ) );
777 $userPage = Title
::makeTitle( NS_USER
, $userText );
778 return $this->makeLinkObj( $userPage, $encName );
783 * @param $userId Integer: user id in database.
784 * @param $userText String: user name in database.
785 * @return string HTML fragment with talk and/or block links
788 function userToolLinks( $userId, $userText ) {
789 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
790 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
791 $blockable = ( $wgSysopUserBans ||
0 == $userId );
795 $items[] = $this->userTalkLink( $userId, $userText );
798 $contribsPage = SpecialPage
::getTitleFor( 'Contributions' );
799 $items[] = $this->makeKnownLinkObj( $contribsPage,
800 wfMsgHtml( 'contribslink' ), 'target=' . urlencode( $userText ) );
802 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
803 $items[] = $this->blockLink( $userId, $userText );
807 return ' (' . implode( ' | ', $items ) . ')';
814 * @param $userId Integer: user id in database.
815 * @param $userText String: user name in database.
816 * @return string HTML fragment with user talk link
819 function userTalkLink( $userId, $userText ) {
821 $talkname = $wgLang->getNsText( NS_TALK
); # use the shorter name
823 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
824 $userTalkLink = $this->makeLinkObj( $userTalkPage, $talkname );
825 return $userTalkLink;
829 * @param $userId Integer: userid
830 * @param $userText String: user name in database.
831 * @return string HTML fragment with block link
834 function blockLink( $userId, $userText ) {
835 $blockPage = SpecialPage
::getTitleFor( 'Blockip' );
836 $blockLink = $this->makeKnownLinkObj( $blockPage,
837 wfMsgHtml( 'blocklink' ), 'ip=' . urlencode( $userText ) );
842 * Generate a user link if the current user is allowed to view it
843 * @param $rev Revision object.
844 * @return string HTML
846 function revUserLink( $rev ) {
847 if( $rev->userCan( Revision
::DELETED_USER
) ) {
848 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
850 $link = wfMsgHtml( 'rev-deleted-user' );
852 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
853 return '<span class="history-deleted">' . $link . '</span>';
859 * Generate a user tool link cluster if the current user is allowed to view it
860 * @param $rev Revision object.
861 * @return string HTML
863 function revUserTools( $rev ) {
864 if( $rev->userCan( Revision
::DELETED_USER
) ) {
865 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
867 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
869 $link = wfMsgHtml( 'rev-deleted-user' );
871 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
872 return '<span class="history-deleted">' . $link . '</span>';
878 * This function is called by all recent changes variants, by the page history,
879 * and by the user contributions list. It is responsible for formatting edit
880 * comments. It escapes any HTML in the comment, but adds some CSS to format
881 * auto-generated comments (from section editing) and formats [[wikilinks]].
883 * @author Erik Moeller <moeller@scireview.de>
885 * Note: there's not always a title to pass to this function.
886 * Since you can't set a default parameter for a reference, I've turned it
887 * temporarily to a value pass. Should be adjusted further. --brion
889 * $param string $comment
890 * @param Title $title (to generate link to the section in autocomment)
891 * @param bool $local Whether section links should refer to local page
893 function formatComment($comment, Title
$title = NULL, $local = false) {
894 wfProfileIn( __METHOD__
);
897 $comment = str_replace( "\n", " ", $comment );
898 $comment = htmlspecialchars( $comment );
900 # The pattern for autogen comments is / * foo * /, which makes for
902 # We look for all comments, match any text before and after the comment,
903 # add a separator where needed and format the comment itself with CSS
904 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
912 # Generate a valid anchor name from the section title.
913 # Hackish, but should generally work - we strip wiki
914 # syntax, including the magic [[: that is used to
915 # "link rather than show" in case of images and
916 # interlanguage links.
917 $section = str_replace( '[[:', '', $section );
918 $section = str_replace( '[[', '', $section );
919 $section = str_replace( ']]', '', $section );
921 $sectionTitle = Title
::newFromText( '#' . $section);
923 $sectionTitle = wfClone( $title );
924 $sectionTitle->mFragment
= $section;
926 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
930 if($pre) { $auto = $sep.' '.$auto; }
931 if($post) { $auto .= ' '.$sep; }
932 $auto='<span class="autocomment">'.$auto.'</span>';
933 $comment=$pre.$auto.$post;
936 # format regular and media links - all other wiki formatting
938 $medians = $wgContLang->getNsText( NS_MEDIA
) . ':';
939 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
940 # Handle link renaming [[foo|text]] will show link as "text"
941 if( "" != $match[3] ) {
946 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
947 # Media link; trail not supported.
948 $linkRegexp = '/\[\[(.*?)\]\]/';
949 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
952 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
953 $trail = $submatch[1];
957 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
958 if (isset($match[1][0]) && $match[1][0] == ':')
959 $match[1] = substr($match[1], 1);
960 $thelink = $this->makeLink( $match[1], $text, "", $trail );
962 $comment = preg_replace( $linkRegexp, wfRegexReplacement( $thelink ), $comment, 1 );
964 wfProfileOut( __METHOD__
);
969 * Wrap a comment in standard punctuation and formatting if
970 * it's non-empty, otherwise return empty string.
972 * @param string $comment
973 * @param Title $title
974 * @param bool $local Whether section links should refer to local page
978 function commentBlock( $comment, Title
$title = NULL, $local = false ) {
979 // '*' used to be the comment inserted by the software way back
980 // in antiquity in case none was provided, here for backwards
981 // compatability, acc. to brion -ævar
982 if( $comment == '' ||
$comment == '*' ) {
985 $formatted = $this->formatComment( $comment, $title, $local );
986 return " <span class=\"comment\">($formatted)</span>";
991 * Wrap and format the given revision's comment block, if the current
992 * user is allowed to view it.
994 * @param Revision $rev
995 * @param bool $local Whether section links should refer to local page
996 * @return string HTML
998 function revComment( Revision
$rev, $local = false ) {
999 if( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
1000 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
1002 $block = " <span class=\"comment\">" .
1003 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1005 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
1006 return " <span class=\"history-deleted\">$block</span>";
1011 /** @todo document */
1012 function tocIndent() {
1016 /** @todo document */
1017 function tocUnindent($level) {
1018 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
1022 * parameter level defines if we are on an indentation level
1024 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1025 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1026 $anchor . '"><span class="tocnumber">' .
1027 $tocnumber . '</span> <span class="toctext">' .
1028 $tocline . '</span></a>';
1031 /** @todo document */
1032 function tocLineEnd() {
1036 /** @todo document */
1037 function tocList($toc) {
1038 global $wgJsMimeType;
1039 $title = wfMsgForContent('toc') ;
1041 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1042 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1044 # no trailing newline, script should not be wrapped in a
1046 . "</ul>\n</td></tr></table>"
1047 . '<script type="' . $wgJsMimeType . '">'
1048 . ' if (window.showTocToggle) {'
1049 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
1050 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
1051 . ' showTocToggle();'
1056 /** @todo document */
1057 function editSectionLinkForOther( $title, $section ) {
1060 $title = Title
::newFromText( $title );
1061 $editurl = '§ion='.$section;
1062 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
1064 return "<span class=\"editsection\">[".$url."]</span>";
1069 * @param $title Title object.
1070 * @param $section Integer: section number.
1071 * @param $hint Link String: title, or default if omitted or empty
1073 function editSectionLink( $nt, $section, $hint='' ) {
1076 $editurl = '§ion='.$section;
1077 $hint = ( $hint=='' ) ?
'' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
1078 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '', $hint );
1080 return "<span class=\"editsection\">[".$url."]</span>";
1084 * Split a link trail, return the "inside" portion and the remainder of the trail
1085 * as a two-element array
1089 static function splitTrail( $trail ) {
1090 static $regex = false;
1091 if ( $regex === false ) {
1093 $regex = $wgContLang->linkTrail();
1096 if ( '' != $trail ) {
1097 if ( preg_match( $regex, $trail, $m ) ) {
1102 return array( $inside, $trail );
1106 * Generate a rollback link for a given revision. Currently it's the
1107 * caller's responsibility to ensure that the revision is the top one. If
1108 * it's not, of course, the user will get an error message.
1110 * If the calling page is called with the parameter &bot=1, all rollback
1111 * links also get that parameter. It causes the edit itself and the rollback
1112 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1113 * changes, so this allows sysops to combat a busy vandal without bothering
1116 * @param Revision $rev
1118 function generateRollback( $rev ) {
1119 global $wgUser, $wgRequest;
1120 $title = $rev->getTitle();
1122 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
1123 $extraRollback .= '&token=' . urlencode(
1124 $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ) );
1125 return '<span class="mw-rollback-link">['. $this->makeKnownLinkObj( $title,
1126 wfMsg('rollbacklink'),
1127 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extraRollback ) .']</span>';
1131 * Returns HTML for the "templates used on this page" list.
1133 * @param array $templates Array of templates from Article::getUsedTemplate
1135 * @param bool $preview Whether this is for a preview
1136 * @param bool $section Whether this is for a section edit
1137 * @return string HTML output
1139 public function formatTemplates( $templates, $preview = false, $section = false) {
1141 wfProfileIn( __METHOD__
);
1143 $sk =& $wgUser->getSkin();
1146 if ( count( $templates ) > 0 ) {
1147 # Do a batch existence check
1148 $batch = new LinkBatch
;
1149 foreach( $templates as $title ) {
1150 $batch->addObj( $title );
1154 # Construct the HTML
1155 $outText = '<div class="mw-templatesUsedExplanation">';
1157 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1159 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1161 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1162 $outText .= '</div><ul>';
1163 foreach ( $templates as $titleObj ) {
1164 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
1166 $outText .= '</ul>';
1168 wfProfileOut( __METHOD__
);