3 * Methods to make links and related items.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\Linker\LinkTarget
;
23 use MediaWiki\MediaWikiServices
;
26 * Some internal bits split of from Skin.php. These functions are used
27 * for primarily page content: links, embedded images, table of contents. Links
28 * are also used in the skin.
30 * @todo turn this into a legacy interface for HtmlPageLinkRenderer and similar services.
36 * Flags for userToolLinks()
38 const TOOL_LINKS_NOBLOCK
= 1;
39 const TOOL_LINKS_EMAIL
= 2;
42 * Get the appropriate HTML attributes to add to the "a" element of an interwiki link.
45 * @deprecated since 1.25
47 * @param string $title The title text for the link, URL-encoded (???) but
49 * @param string $unused Unused
50 * @param string $class The contents of the class attribute; if an empty
51 * string is passed, which is the default value, defaults to 'external'.
54 static function getInterwikiLinkAttributes( $title, $unused = null, $class = 'external' ) {
57 wfDeprecated( __METHOD__
, '1.25' );
59 # @todo FIXME: We have a whole bunch of handling here that doesn't happen in
60 # getExternalLinkAttributes, why?
61 $title = urldecode( $title );
62 $title = $wgContLang->checkTitleEncoding( $title );
63 $title = preg_replace( '/[\\x00-\\x1f]/', ' ', $title );
65 return self
::getLinkAttributesInternal( $title, $class );
69 * Get the appropriate HTML attributes to add to the "a" element of an internal link.
72 * @deprecated since 1.25
74 * @param string $title The title text for the link, URL-encoded (???) but
76 * @param string $unused Unused
77 * @param string $class The contents of the class attribute, default none
80 static function getInternalLinkAttributes( $title, $unused = null, $class = '' ) {
81 wfDeprecated( __METHOD__
, '1.25' );
83 $title = urldecode( $title );
84 $title = strtr( $title, '_', ' ' );
85 return self
::getLinkAttributesInternal( $title, $class );
89 * Get the appropriate HTML attributes to add to the "a" element of an internal
90 * link, given the Title object for the page we want to link to.
93 * @deprecated since 1.25
96 * @param string $unused Unused
97 * @param string $class The contents of the class attribute, default none
98 * @param string|bool $title Optional (unescaped) string to use in the title
99 * attribute; if false, default to the name of the page we're linking to
102 static function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
103 wfDeprecated( __METHOD__
, '1.25' );
105 if ( $title === false ) {
106 $title = $nt->getPrefixedText();
108 return self
::getLinkAttributesInternal( $title, $class );
112 * Common code for getLinkAttributesX functions
115 * @deprecated since 1.25
117 * @param string $title
118 * @param string $class
122 private static function getLinkAttributesInternal( $title, $class ) {
123 wfDeprecated( __METHOD__
, '1.25' );
125 $title = htmlspecialchars( $title );
126 $class = htmlspecialchars( $class );
128 if ( $class != '' ) {
129 $r .= " class=\"$class\"";
131 if ( $title != '' ) {
132 $r .= " title=\"$title\"";
138 * Return the CSS colour of a known link
141 * @param LinkTarget $t
142 * @param int $threshold User defined threshold
143 * @return string CSS class
145 public static function getLinkColour( LinkTarget
$t, $threshold ) {
146 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
147 // Make sure the target is in the cache
148 $id = $linkCache->addLinkObj( $t );
154 if ( $linkCache->getGoodLinkFieldObj( $t, 'redirect' ) ) {
156 return 'mw-redirect';
157 } elseif ( $threshold > 0 && MWNamespace
::isContent( $t->getNamespace() )
158 && $linkCache->getGoodLinkFieldObj( $t, 'length' ) < $threshold
168 * This function returns an HTML link to the given target. It serves a few
170 * 1) If $target is a Title, the correct URL to link to will be figured
172 * 2) It automatically adds the usual classes for various types of link
173 * targets: "new" for red links, "stub" for short articles, etc.
174 * 3) It escapes all attribute values safely so there's no risk of XSS.
175 * 4) It provides a default tooltip if the target is a Title (the page
176 * name of the target).
177 * link() replaces the old functions in the makeLink() family.
179 * @since 1.18 Method exists since 1.16 as non-static, made static in 1.18.
181 * @param Title $target Can currently only be a Title, but this may
182 * change to support Images, literal URLs, etc.
183 * @param string $html The HTML contents of the <a> element, i.e.,
184 * the link text. This is raw HTML and will not be escaped. If null,
185 * defaults to the prefixed text of the Title; or if the Title is just a
186 * fragment, the contents of the fragment.
187 * @param array $customAttribs A key => value array of extra HTML attributes,
188 * such as title and class. (href is ignored.) Classes will be
189 * merged with the default classes, while other attributes will replace
190 * default attributes. All passed attribute values will be HTML-escaped.
191 * A false attribute value means to suppress that attribute.
192 * @param array $query The query string to append to the URL
193 * you're linking to, in key => value array form. Query keys and values
194 * will be URL-encoded.
195 * @param string|array $options String or array of strings:
196 * 'known': Page is known to exist, so don't check if it does.
197 * 'broken': Page is known not to exist, so don't check if it does.
198 * 'noclasses': Don't add any classes automatically (includes "new",
199 * "stub", "mw-redirect", "extiw"). Only use the class attribute
200 * provided, if any, so you get a simple blue link with no funny i-
202 * 'forcearticlepath': Use the article path always, even with a querystring.
203 * Has compatibility issues on some setups, so avoid wherever possible.
204 * 'http': Force a full URL with http:// as the scheme.
205 * 'https': Force a full URL with https:// as the scheme.
206 * 'stubThreshold' => (int): Stub threshold to use when determining link classes.
207 * @return string HTML <a> attribute
209 public static function link(
210 $target, $html = null, $customAttribs = [], $query = [], $options = []
212 if ( !$target instanceof Title
) {
213 wfWarn( __METHOD__
. ': Requires $target to be a Title object.', 2 );
214 return "<!-- ERROR -->$html";
217 if ( is_string( $query ) ) {
218 // some functions withing core using this still hand over query strings
219 wfDeprecated( __METHOD__
. ' with parameter $query as string (should be array)', '1.20' );
220 $query = wfCgiToArray( $query );
223 $services = MediaWikiServices
::getInstance();
224 $options = (array)$options;
226 // Custom options, create new LinkRenderer
227 if ( !isset( $options['stubThreshold'] ) ) {
228 $defaultLinkRenderer = $services->getLinkRenderer();
229 $options['stubThreshold'] = $defaultLinkRenderer->getStubThreshold();
231 $linkRenderer = $services->getLinkRendererFactory()
232 ->createFromLegacyOptions( $options );
234 $linkRenderer = $services->getLinkRenderer();
237 if ( $html !== null ) {
238 $text = new HtmlArmor( $html );
240 $text = $html; // null
242 if ( in_array( 'known', $options, true ) ) {
243 return $linkRenderer->makeKnownLink( $target, $text, $customAttribs, $query );
244 } elseif ( in_array( 'broken', $options, true ) ) {
245 return $linkRenderer->makeBrokenLink( $target, $text, $customAttribs, $query );
246 } elseif ( in_array( 'noclasses', $options, true ) ) {
247 return $linkRenderer->makePreloadedLink( $target, $text, '', $customAttribs, $query );
249 return $linkRenderer->makeLink( $target, $text, $customAttribs, $query );
254 * Identical to link(), except $options defaults to 'known'.
259 public static function linkKnown(
260 $target, $html = null, $customAttribs = [],
261 $query = [], $options = [ 'known' ]
263 return self
::link( $target, $html, $customAttribs, $query, $options );
267 * Make appropriate markup for a link to the current article. This is
268 * currently rendered as the bold link text. The calling sequence is the
269 * same as the other make*LinkObj static functions, despite $query not
274 * @param string $html [optional]
275 * @param string $query [optional]
276 * @param string $trail [optional]
277 * @param string $prefix [optional]
281 public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) {
282 $ret = "<strong class=\"selflink\">{$prefix}{$html}</strong>{$trail}";
283 if ( !Hooks
::run( 'SelfLinkBegin', [ $nt, &$html, &$trail, &$prefix, &$ret ] ) ) {
288 $html = htmlspecialchars( $nt->getPrefixedText() );
290 list( $inside, $trail ) = self
::splitTrail( $trail );
291 return "<strong class=\"selflink\">{$prefix}{$html}{$inside}</strong>{$trail}";
295 * Get a message saying that an invalid title was encountered.
296 * This should be called after a method like Title::makeTitleSafe() returned
297 * a value indicating that the title object is invalid.
299 * @param IContextSource $context Context to use to get the messages
300 * @param int $namespace Namespace number
301 * @param string $title Text of the title, without the namespace part
304 public static function getInvalidTitleDescription( IContextSource
$context, $namespace, $title ) {
307 // First we check whether the namespace exists or not.
308 if ( MWNamespace
::exists( $namespace ) ) {
309 if ( $namespace == NS_MAIN
) {
310 $name = $context->msg( 'blanknamespace' )->text();
312 $name = $wgContLang->getFormattedNsText( $namespace );
314 return $context->msg( 'invalidtitle-knownnamespace', $namespace, $name, $title )->text();
316 return $context->msg( 'invalidtitle-unknownnamespace', $namespace, $title )->text();
322 * @param LinkTarget $target
323 * @return LinkTarget|Title You will get back the same type you passed in, or a Title object
325 public static function normaliseSpecialPage( LinkTarget
$target ) {
326 if ( $target->getNamespace() == NS_SPECIAL
) {
327 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $target->getDBkey() );
331 $ret = SpecialPage
::getTitleFor( $name, $subpage, $target->getFragment() );
339 * Returns the filename part of an url.
340 * Used as alternative text for external images.
346 private static function fnamePart( $url ) {
347 $basename = strrchr( $url, '/' );
348 if ( false === $basename ) {
351 $basename = substr( $basename, 1 );
357 * Return the code for images which were added via external links,
358 * via Parser::maybeMakeExternalImage().
366 public static function makeExternalImage( $url, $alt = '' ) {
368 $alt = self
::fnamePart( $url );
371 $success = Hooks
::run( 'LinkerMakeExternalImage', [ &$url, &$alt, &$img ] );
373 wfDebug( "Hook LinkerMakeExternalImage changed the output of external image "
374 . "with url {$url} and alt text {$alt} to {$img}\n", true );
377 return Html
::element( 'img',
384 * Given parameters derived from [[Image:Foo|options...]], generate the
385 * HTML that that syntax inserts in the page.
387 * @param Parser $parser
388 * @param Title $title Title object of the file (not the currently viewed page)
389 * @param File $file File object, or false if it doesn't exist
390 * @param array $frameParams Associative array of parameters external to the media handler.
391 * Boolean parameters are indicated by presence or absence, the value is arbitrary and
392 * will often be false.
393 * thumbnail If present, downscale and frame
394 * manualthumb Image name to use as a thumbnail, instead of automatic scaling
395 * framed Shows image in original size in a frame
396 * frameless Downscale but don't frame
397 * upright If present, tweak default sizes for portrait orientation
398 * upright_factor Fudge factor for "upright" tweak (default 0.75)
399 * border If present, show a border around the image
400 * align Horizontal alignment (left, right, center, none)
401 * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
402 * bottom, text-bottom)
403 * alt Alternate text for image (i.e. alt attribute). Plain text.
404 * class HTML for image classes. Plain text.
405 * caption HTML for image caption.
406 * link-url URL to link to
407 * link-title Title object to link to
408 * link-target Value for the target attribute, only with link-url
409 * no-link Boolean, suppress description link
411 * @param array $handlerParams Associative array of media handler parameters, to be passed
412 * to transform(). Typical keys are "width" and "page".
413 * @param string|bool $time Timestamp of the file, set as false for current
414 * @param string $query Query params for desc url
415 * @param int|null $widthOption Used by the parser to remember the user preference thumbnailsize
417 * @return string HTML for an image, with links, wrappers, etc.
419 public static function makeImageLink( Parser
$parser, Title
$title,
420 $file, $frameParams = [], $handlerParams = [], $time = false,
421 $query = "", $widthOption = null
424 $dummy = new DummyLinker
;
425 if ( !Hooks
::run( 'ImageBeforeProduceHTML', [ &$dummy, &$title,
426 &$file, &$frameParams, &$handlerParams, &$time, &$res ] ) ) {
430 if ( $file && !$file->allowInlineDisplay() ) {
431 wfDebug( __METHOD__
. ': ' . $title->getPrefixedDBkey() . " does not allow inline display\n" );
432 return self
::link( $title );
437 $hp =& $handlerParams;
439 // Clean up parameters
440 $page = isset( $hp['page'] ) ?
$hp['page'] : false;
441 if ( !isset( $fp['align'] ) ) {
444 if ( !isset( $fp['alt'] ) ) {
447 if ( !isset( $fp['title'] ) ) {
450 if ( !isset( $fp['class'] ) ) {
454 $prefix = $postfix = '';
456 if ( 'center' == $fp['align'] ) {
457 $prefix = '<div class="center">';
459 $fp['align'] = 'none';
461 if ( $file && !isset( $hp['width'] ) ) {
462 if ( isset( $hp['height'] ) && $file->isVectorized() ) {
463 // If its a vector image, and user only specifies height
464 // we don't want it to be limited by its "normal" width.
465 global $wgSVGMaxSize;
466 $hp['width'] = $wgSVGMaxSize;
468 $hp['width'] = $file->getWidth( $page );
471 if ( isset( $fp['thumbnail'] )
472 ||
isset( $fp['manualthumb'] )
473 ||
isset( $fp['framed'] )
474 ||
isset( $fp['frameless'] )
477 global $wgThumbLimits, $wgThumbUpright;
479 if ( $widthOption === null ||
!isset( $wgThumbLimits[$widthOption] ) ) {
480 $widthOption = User
::getDefaultOption( 'thumbsize' );
483 // Reduce width for upright images when parameter 'upright' is used
484 if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
485 $fp['upright'] = $wgThumbUpright;
488 // For caching health: If width scaled down due to upright
489 // parameter, round to full __0 pixel to avoid the creation of a
490 // lot of odd thumbs.
491 $prefWidth = isset( $fp['upright'] ) ?
492 round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
493 $wgThumbLimits[$widthOption];
495 // Use width which is smaller: real image width or user preference width
496 // Unless image is scalable vector.
497 if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
498 $prefWidth < $hp['width'] ||
$file->isVectorized() ) ) {
499 $hp['width'] = $prefWidth;
504 if ( isset( $fp['thumbnail'] ) ||
isset( $fp['manualthumb'] ) ||
isset( $fp['framed'] ) ) {
505 # Create a thumbnail. Alignment depends on the writing direction of
506 # the page content language (right-aligned for LTR languages,
507 # left-aligned for RTL languages)
508 # If a thumbnail width has not been provided, it is set
509 # to the default user option as specified in Language*.php
510 if ( $fp['align'] == '' ) {
511 $fp['align'] = $parser->getTargetLanguage()->alignEnd();
513 return $prefix . self
::makeThumbLink2( $title, $file, $fp, $hp, $time, $query ) . $postfix;
516 if ( $file && isset( $fp['frameless'] ) ) {
517 $srcWidth = $file->getWidth( $page );
518 # For "frameless" option: do not present an image bigger than the
519 # source (for bitmap-style images). This is the same behavior as the
520 # "thumb" option does it already.
521 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
522 $hp['width'] = $srcWidth;
526 if ( $file && isset( $hp['width'] ) ) {
527 # Create a resized image, without the additional thumbnail features
528 $thumb = $file->transform( $hp );
534 $s = self
::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
536 self
::processResponsiveImages( $file, $thumb, $hp );
539 'title' => $fp['title'],
540 'valign' => isset( $fp['valign'] ) ?
$fp['valign'] : false,
541 'img-class' => $fp['class'] ];
542 if ( isset( $fp['border'] ) ) {
543 $params['img-class'] .= ( $params['img-class'] !== '' ?
' ' : '' ) . 'thumbborder';
545 $params = self
::getImageLinkMTOParams( $fp, $query, $parser ) +
$params;
547 $s = $thumb->toHtml( $params );
549 if ( $fp['align'] != '' ) {
550 $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
552 return str_replace( "\n", ' ', $prefix . $s . $postfix );
556 * Get the link parameters for MediaTransformOutput::toHtml() from given
557 * frame parameters supplied by the Parser.
558 * @param array $frameParams The frame parameters
559 * @param string $query An optional query string to add to description page links
560 * @param Parser|null $parser
563 private static function getImageLinkMTOParams( $frameParams, $query = '', $parser = null ) {
565 if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
566 $mtoParams['custom-url-link'] = $frameParams['link-url'];
567 if ( isset( $frameParams['link-target'] ) ) {
568 $mtoParams['custom-target-link'] = $frameParams['link-target'];
571 $extLinkAttrs = $parser->getExternalLinkAttribs( $frameParams['link-url'] );
572 foreach ( $extLinkAttrs as $name => $val ) {
573 // Currently could include 'rel' and 'target'
574 $mtoParams['parser-extlink-' . $name] = $val;
577 } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
578 $mtoParams['custom-title-link'] = self
::normaliseSpecialPage( $frameParams['link-title'] );
579 } elseif ( !empty( $frameParams['no-link'] ) ) {
582 $mtoParams['desc-link'] = true;
583 $mtoParams['desc-query'] = $query;
589 * Make HTML for a thumbnail including image, border and caption
590 * @param Title $title
591 * @param File|bool $file File object or false if it doesn't exist
592 * @param string $label
594 * @param string $align
595 * @param array $params
596 * @param bool $framed
597 * @param string $manualthumb
600 public static function makeThumbLinkObj( Title
$title, $file, $label = '', $alt,
601 $align = 'right', $params = [], $framed = false, $manualthumb = ""
609 $frameParams['framed'] = true;
611 if ( $manualthumb ) {
612 $frameParams['manualthumb'] = $manualthumb;
614 return self
::makeThumbLink2( $title, $file, $frameParams, $params );
618 * @param Title $title
620 * @param array $frameParams
621 * @param array $handlerParams
623 * @param string $query
626 public static function makeThumbLink2( Title
$title, $file, $frameParams = [],
627 $handlerParams = [], $time = false, $query = ""
629 $exists = $file && $file->exists();
633 $hp =& $handlerParams;
635 $page = isset( $hp['page'] ) ?
$hp['page'] : false;
636 if ( !isset( $fp['align'] ) ) {
637 $fp['align'] = 'right';
639 if ( !isset( $fp['alt'] ) ) {
642 if ( !isset( $fp['title'] ) ) {
645 if ( !isset( $fp['caption'] ) ) {
649 if ( empty( $hp['width'] ) ) {
650 // Reduce width for upright images when parameter 'upright' is used
651 $hp['width'] = isset( $fp['upright'] ) ?
130 : 180;
655 $manualthumb = false;
658 $outerWidth = $hp['width'] +
2;
660 if ( isset( $fp['manualthumb'] ) ) {
661 # Use manually specified thumbnail
662 $manual_title = Title
::makeTitleSafe( NS_FILE
, $fp['manualthumb'] );
663 if ( $manual_title ) {
664 $manual_img = wfFindFile( $manual_title );
666 $thumb = $manual_img->getUnscaledThumb( $hp );
672 } elseif ( isset( $fp['framed'] ) ) {
673 // Use image dimensions, don't scale
674 $thumb = $file->getUnscaledThumb( $hp );
677 # Do not present an image bigger than the source, for bitmap-style images
678 # This is a hack to maintain compatibility with arbitrary pre-1.10 behavior
679 $srcWidth = $file->getWidth( $page );
680 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
681 $hp['width'] = $srcWidth;
683 $thumb = $file->transform( $hp );
687 $outerWidth = $thumb->getWidth() +
2;
689 $outerWidth = $hp['width'] +
2;
693 # ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
694 # So we don't need to pass it here in $query. However, the URL for the
695 # zoom icon still needs it, so we make a unique query for it. See bug 14771
696 $url = $title->getLocalURL( $query );
698 $url = wfAppendQuery( $url, [ 'page' => $page ] );
701 && !isset( $fp['link-title'] )
702 && !isset( $fp['link-url'] )
703 && !isset( $fp['no-link'] ) ) {
704 $fp['link-url'] = $url;
707 $s = "<div class=\"thumb t{$fp['align']}\">"
708 . "<div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
711 $s .= self
::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
713 } elseif ( !$thumb ) {
714 $s .= wfMessage( 'thumbnail_error', '' )->escaped();
717 if ( !$noscale && !$manualthumb ) {
718 self
::processResponsiveImages( $file, $thumb, $hp );
722 'title' => $fp['title'],
723 'img-class' => ( isset( $fp['class'] ) && $fp['class'] !== ''
725 : '' ) . 'thumbimage'
727 $params = self
::getImageLinkMTOParams( $fp, $query ) +
$params;
728 $s .= $thumb->toHtml( $params );
729 if ( isset( $fp['framed'] ) ) {
732 $zoomIcon = Html
::rawElement( 'div', [ 'class' => 'magnify' ],
733 Html
::rawElement( 'a', [
735 'class' => 'internal',
736 'title' => wfMessage( 'thumbnail-more' )->text() ],
740 $s .= ' <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
741 return str_replace( "\n", ' ', $s );
745 * Process responsive images: add 1.5x and 2x subimages to the thumbnail, where
749 * @param MediaTransformOutput $thumb
750 * @param array $hp Image parameters
752 public static function processResponsiveImages( $file, $thumb, $hp ) {
753 global $wgResponsiveImages;
754 if ( $wgResponsiveImages && $thumb && !$thumb->isError() ) {
756 $hp15['width'] = round( $hp['width'] * 1.5 );
758 $hp20['width'] = $hp['width'] * 2;
759 if ( isset( $hp['height'] ) ) {
760 $hp15['height'] = round( $hp['height'] * 1.5 );
761 $hp20['height'] = $hp['height'] * 2;
764 $thumb15 = $file->transform( $hp15 );
765 $thumb20 = $file->transform( $hp20 );
766 if ( $thumb15 && !$thumb15->isError() && $thumb15->getUrl() !== $thumb->getUrl() ) {
767 $thumb->responsiveUrls
['1.5'] = $thumb15->getUrl();
769 if ( $thumb20 && !$thumb20->isError() && $thumb20->getUrl() !== $thumb->getUrl() ) {
770 $thumb->responsiveUrls
['2'] = $thumb20->getUrl();
776 * Make a "broken" link to an image
779 * @param Title $title
780 * @param string $label Link label (plain text)
781 * @param string $query Query string
782 * @param string $unused1 Unused parameter kept for b/c
783 * @param string $unused2 Unused parameter kept for b/c
784 * @param bool $time A file of a certain timestamp was requested
787 public static function makeBrokenImageLinkObj( $title, $label = '',
788 $query = '', $unused1 = '', $unused2 = '', $time = false
790 if ( !$title instanceof Title
) {
791 wfWarn( __METHOD__
. ': Requires $title to be a Title object.' );
792 return "<!-- ERROR -->" . htmlspecialchars( $label );
795 global $wgEnableUploads, $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
796 if ( $label == '' ) {
797 $label = $title->getPrefixedText();
799 $encLabel = htmlspecialchars( $label );
800 $currentExists = $time ?
( wfFindFile( $title ) != false ) : false;
802 if ( ( $wgUploadMissingFileUrl ||
$wgUploadNavigationUrl ||
$wgEnableUploads )
805 $redir = RepoGroup
::singleton()->getLocalRepo()->checkRedirect( $title );
808 // We already know it's a redirect, so mark it
813 [ 'class' => 'mw-redirect' ],
814 wfCgiToArray( $query ),
815 [ 'known', 'noclasses' ]
819 $href = self
::getUploadUrl( $title, $query );
821 return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
822 htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES
) . '">' .
826 return self
::link( $title, $encLabel, [], wfCgiToArray( $query ), [ 'known', 'noclasses' ] );
830 * Get the URL to upload a certain file
833 * @param Title $destFile Title object of the file to upload
834 * @param string $query Urlencoded query string to prepend
835 * @return string Urlencoded URL
837 protected static function getUploadUrl( $destFile, $query = '' ) {
838 global $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
839 $q = 'wpDestFile=' . $destFile->getPartialURL();
840 if ( $query != '' ) {
844 if ( $wgUploadMissingFileUrl ) {
845 return wfAppendQuery( $wgUploadMissingFileUrl, $q );
846 } elseif ( $wgUploadNavigationUrl ) {
847 return wfAppendQuery( $wgUploadNavigationUrl, $q );
849 $upload = SpecialPage
::getTitleFor( 'Upload' );
850 return $upload->getLocalURL( $q );
855 * Create a direct link to a given uploaded file.
858 * @param Title $title
859 * @param string $html Pre-sanitized HTML
860 * @param string $time MW timestamp of file creation time
861 * @return string HTML
863 public static function makeMediaLinkObj( $title, $html = '', $time = false ) {
864 $img = wfFindFile( $title, [ 'time' => $time ] );
865 return self
::makeMediaLinkFile( $title, $img, $html );
869 * Create a direct link to a given uploaded file.
870 * This will make a broken link if $file is false.
873 * @param Title $title
874 * @param File|bool $file File object or false
875 * @param string $html Pre-sanitized HTML
876 * @return string HTML
878 * @todo Handle invalid or missing images better.
880 public static function makeMediaLinkFile( Title
$title, $file, $html = '' ) {
881 if ( $file && $file->exists() ) {
882 $url = $file->getUrl();
885 $url = self
::getUploadUrl( $title );
889 $alt = $title->getText();
901 if ( !Hooks
::run( 'LinkerMakeMediaLinkFile',
902 [ $title, $file, &$html, &$attribs, &$ret ] ) ) {
903 wfDebug( "Hook LinkerMakeMediaLinkFile changed the output of link "
904 . "with url {$url} and text {$html} to {$ret}\n", true );
908 return Html
::rawElement( 'a', $attribs, $html );
912 * Make a link to a special page given its name and, optionally,
913 * a message key from the link text.
914 * Usage example: Linker::specialLink( 'Recentchanges' )
917 * @param string $name
921 public static function specialLink( $name, $key = '' ) {
923 $key = strtolower( $name );
926 return self
::linkKnown( SpecialPage
::getTitleFor( $name ), wfMessage( $key )->text() );
930 * Make an external link
931 * @since 1.16.3. $title added in 1.21
932 * @param string $url URL to link to
933 * @param string $text Text of link
934 * @param bool $escape Do we escape the link text?
935 * @param string $linktype Type of external link. Gets added to the classes
936 * @param array $attribs Array of extra attributes to <a>
937 * @param Title|null $title Title object used for title specific link attributes
940 public static function makeExternalLink( $url, $text, $escape = true,
941 $linktype = '', $attribs = [], $title = null
946 $class .= " $linktype";
948 if ( isset( $attribs['class'] ) && $attribs['class'] ) {
949 $class .= " {$attribs['class']}";
951 $attribs['class'] = $class;
954 $text = htmlspecialchars( $text );
960 $newRel = Parser
::getExternalLinkRel( $url, $title );
961 if ( !isset( $attribs['rel'] ) ||
$attribs['rel'] === '' ) {
962 $attribs['rel'] = $newRel;
963 } elseif ( $newRel !== '' ) {
964 // Merge the rel attributes.
965 $newRels = explode( ' ', $newRel );
966 $oldRels = explode( ' ', $attribs['rel'] );
967 $combined = array_unique( array_merge( $newRels, $oldRels ) );
968 $attribs['rel'] = implode( ' ', $combined );
971 $success = Hooks
::run( 'LinkerMakeExternalLink',
972 [ &$url, &$text, &$link, &$attribs, $linktype ] );
974 wfDebug( "Hook LinkerMakeExternalLink changed the output of link "
975 . "with url {$url} and text {$text} to {$link}\n", true );
978 $attribs['href'] = $url;
979 return Html
::rawElement( 'a', $attribs, $text );
983 * Make user link (or user contributions for unregistered users)
984 * @param int $userId User id in database.
985 * @param string $userName User name in database.
986 * @param string $altUserName Text to display instead of the user name (optional)
987 * @return string HTML fragment
988 * @since 1.16.3. $altUserName was added in 1.19.
990 public static function userLink( $userId, $userName, $altUserName = false ) {
991 $classes = 'mw-userlink';
992 if ( $userId == 0 ) {
993 $page = SpecialPage
::getTitleFor( 'Contributions', $userName );
994 if ( $altUserName === false ) {
995 $altUserName = IP
::prettifyIP( $userName );
997 $classes .= ' mw-anonuserlink'; // Separate link class for anons (bug 43179)
999 $page = Title
::makeTitle( NS_USER
, $userName );
1004 htmlspecialchars( $altUserName !== false ?
$altUserName : $userName ),
1005 [ 'class' => $classes ]
1010 * Generate standard user tool links (talk, contributions, block link, etc.)
1013 * @param int $userId User identifier
1014 * @param string $userText User name or IP address
1015 * @param bool $redContribsWhenNoEdits Should the contributions link be
1016 * red if the user has no edits?
1017 * @param int $flags Customisation flags (e.g. Linker::TOOL_LINKS_NOBLOCK
1018 * and Linker::TOOL_LINKS_EMAIL).
1019 * @param int $edits User edit count (optional, for performance)
1020 * @return string HTML fragment
1022 public static function userToolLinks(
1023 $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits = null
1025 global $wgUser, $wgDisableAnonTalk, $wgLang;
1026 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
1027 $blockable = !( $flags & self
::TOOL_LINKS_NOBLOCK
);
1028 $addEmailLink = $flags & self
::TOOL_LINKS_EMAIL
&& $userId;
1032 $items[] = self
::userTalkLink( $userId, $userText );
1035 // check if the user has an edit
1037 if ( $redContribsWhenNoEdits ) {
1038 if ( intval( $edits ) === 0 && $edits !== 0 ) {
1039 $user = User
::newFromId( $userId );
1040 $edits = $user->getEditCount();
1042 if ( $edits === 0 ) {
1043 $attribs['class'] = 'new';
1046 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
1048 $items[] = self
::link( $contribsPage, wfMessage( 'contribslink' )->escaped(), $attribs );
1050 if ( $blockable && $wgUser->isAllowed( 'block' ) ) {
1051 $items[] = self
::blockLink( $userId, $userText );
1054 if ( $addEmailLink && $wgUser->canSendEmail() ) {
1055 $items[] = self
::emailLink( $userId, $userText );
1058 Hooks
::run( 'UserToolLinksEdit', [ $userId, $userText, &$items ] );
1061 return wfMessage( 'word-separator' )->escaped()
1062 . '<span class="mw-usertoollinks">'
1063 . wfMessage( 'parentheses' )->rawParams( $wgLang->pipeList( $items ) )->escaped()
1071 * Alias for userToolLinks( $userId, $userText, true );
1073 * @param int $userId User identifier
1074 * @param string $userText User name or IP address
1075 * @param int $edits User edit count (optional, for performance)
1078 public static function userToolLinksRedContribs( $userId, $userText, $edits = null ) {
1079 return self
::userToolLinks( $userId, $userText, true, 0, $edits );
1084 * @param int $userId User id in database.
1085 * @param string $userText User name in database.
1086 * @return string HTML fragment with user talk link
1088 public static function userTalkLink( $userId, $userText ) {
1089 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
1090 $userTalkLink = self
::link( $userTalkPage, wfMessage( 'talkpagelinktext' )->escaped() );
1091 return $userTalkLink;
1096 * @param int $userId Userid
1097 * @param string $userText User name in database.
1098 * @return string HTML fragment with block link
1100 public static function blockLink( $userId, $userText ) {
1101 $blockPage = SpecialPage
::getTitleFor( 'Block', $userText );
1102 $blockLink = self
::link( $blockPage, wfMessage( 'blocklink' )->escaped() );
1107 * @param int $userId Userid
1108 * @param string $userText User name in database.
1109 * @return string HTML fragment with e-mail user link
1111 public static function emailLink( $userId, $userText ) {
1112 $emailPage = SpecialPage
::getTitleFor( 'Emailuser', $userText );
1113 $emailLink = self
::link( $emailPage, wfMessage( 'emaillink' )->escaped() );
1118 * Generate a user link if the current user is allowed to view it
1120 * @param Revision $rev
1121 * @param bool $isPublic Show only if all users can see it
1122 * @return string HTML fragment
1124 public static function revUserLink( $rev, $isPublic = false ) {
1125 if ( $rev->isDeleted( Revision
::DELETED_USER
) && $isPublic ) {
1126 $link = wfMessage( 'rev-deleted-user' )->escaped();
1127 } elseif ( $rev->userCan( Revision
::DELETED_USER
) ) {
1128 $link = self
::userLink( $rev->getUser( Revision
::FOR_THIS_USER
),
1129 $rev->getUserText( Revision
::FOR_THIS_USER
) );
1131 $link = wfMessage( 'rev-deleted-user' )->escaped();
1133 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
1134 return '<span class="history-deleted">' . $link . '</span>';
1140 * Generate a user tool link cluster if the current user is allowed to view it
1142 * @param Revision $rev
1143 * @param bool $isPublic Show only if all users can see it
1144 * @return string HTML
1146 public static function revUserTools( $rev, $isPublic = false ) {
1147 if ( $rev->isDeleted( Revision
::DELETED_USER
) && $isPublic ) {
1148 $link = wfMessage( 'rev-deleted-user' )->escaped();
1149 } elseif ( $rev->userCan( Revision
::DELETED_USER
) ) {
1150 $userId = $rev->getUser( Revision
::FOR_THIS_USER
);
1151 $userText = $rev->getUserText( Revision
::FOR_THIS_USER
);
1152 $link = self
::userLink( $userId, $userText )
1153 . self
::userToolLinks( $userId, $userText );
1155 $link = wfMessage( 'rev-deleted-user' )->escaped();
1157 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
1158 return ' <span class="history-deleted">' . $link . '</span>';
1164 * This function is called by all recent changes variants, by the page history,
1165 * and by the user contributions list. It is responsible for formatting edit
1166 * summaries. It escapes any HTML in the summary, but adds some CSS to format
1167 * auto-generated comments (from section editing) and formats [[wikilinks]].
1169 * @author Erik Moeller <moeller@scireview.de>
1170 * @since 1.16.3. $wikiId added in 1.26
1172 * Note: there's not always a title to pass to this function.
1173 * Since you can't set a default parameter for a reference, I've turned it
1174 * temporarily to a value pass. Should be adjusted further. --brion
1176 * @param string $comment
1177 * @param Title|null $title Title object (to generate link to the section in autocomment)
1179 * @param bool $local Whether section links should refer to local page
1180 * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to.
1181 * For use with external changes.
1183 * @return mixed|string
1185 public static function formatComment(
1186 $comment, $title = null, $local = false, $wikiId = null
1188 # Sanitize text a bit:
1189 $comment = str_replace( "\n", " ", $comment );
1190 # Allow HTML entities (for bug 13815)
1191 $comment = Sanitizer
::escapeHtmlAllowEntities( $comment );
1193 # Render autocomments and make links:
1194 $comment = self
::formatAutocomments( $comment, $title, $local, $wikiId );
1195 $comment = self
::formatLinksInComment( $comment, $title, $local, $wikiId );
1201 * Converts autogenerated comments in edit summaries into section links.
1203 * The pattern for autogen comments is / * foo * /, which makes for
1205 * We look for all comments, match any text before and after the comment,
1206 * add a separator where needed and format the comment itself with CSS
1207 * Called by Linker::formatComment.
1209 * @param string $comment Comment text
1210 * @param Title|null $title An optional title object used to links to sections
1211 * @param bool $local Whether section links should refer to local page
1212 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1213 * as used by WikiMap.
1215 * @return string Formatted comment (wikitext)
1217 private static function formatAutocomments(
1218 $comment, $title = null, $local = false, $wikiId = null
1220 // @todo $append here is something of a hack to preserve the status
1221 // quo. Someone who knows more about bidi and such should decide
1222 // (1) what sane rendering even *is* for an LTR edit summary on an RTL
1223 // wiki, both when autocomments exist and when they don't, and
1224 // (2) what markup will make that actually happen.
1226 $comment = preg_replace_callback(
1227 // To detect the presence of content before or after the
1228 // auto-comment, we use capturing groups inside optional zero-width
1229 // assertions. But older versions of PCRE can't directly make
1230 // zero-width assertions optional, so wrap them in a non-capturing
1232 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
1233 function ( $match ) use ( $title, $local, $wikiId, &$append ) {
1236 // Ensure all match positions are defined
1237 $match +
= [ '', '', '', '' ];
1239 $pre = $match[1] !== '';
1241 $post = $match[3] !== '';
1245 'FormatAutocomments',
1246 [ &$comment, $pre, $auto, $post, $title, $local, $wikiId ]
1249 if ( $comment === null ) {
1253 # Remove links that a user may have manually put in the autosummary
1254 # This could be improved by copying as much of Parser::stripSectionName as desired.
1255 $section = str_replace( '[[:', '', $section );
1256 $section = str_replace( '[[', '', $section );
1257 $section = str_replace( ']]', '', $section );
1259 $section = Sanitizer
::normalizeSectionNameWhitespace( $section ); # bug 22784
1261 $sectionTitle = Title
::newFromText( '#' . $section );
1263 $sectionTitle = Title
::makeTitleSafe( $title->getNamespace(),
1264 $title->getDBkey(), $section );
1266 if ( $sectionTitle ) {
1267 $link = Linker
::makeCommentLink( $sectionTitle, $wgLang->getArrow(), $wikiId, 'noclasses' );
1273 # written summary $presep autocomment (summary /* section */)
1274 $pre = wfMessage( 'autocomment-prefix' )->inContentLanguage()->escaped();
1277 # autocomment $postsep written summary (/* section */ summary)
1278 $auto .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped();
1280 $auto = '<span class="autocomment">' . $auto . '</span>';
1281 $comment = $pre . $link . $wgLang->getDirMark()
1282 . '<span dir="auto">' . $auto;
1283 $append .= '</span>';
1289 return $comment . $append;
1293 * Formats wiki links and media links in text; all other wiki formatting
1296 * @since 1.16.3. $wikiId added in 1.26
1297 * @todo FIXME: Doesn't handle sub-links as in image thumb texts like the main parser
1299 * @param string $comment Text to format links in. WARNING! Since the output of this
1300 * function is html, $comment must be sanitized for use as html. You probably want
1301 * to pass $comment through Sanitizer::escapeHtmlAllowEntities() before calling
1303 * @param Title|null $title An optional title object used to links to sections
1304 * @param bool $local Whether section links should refer to local page
1305 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1306 * as used by WikiMap.
1310 public static function formatLinksInComment(
1311 $comment, $title = null, $local = false, $wikiId = null
1313 return preg_replace_callback(
1316 :? # ignore optional leading colon
1317 ([^\]|]+) # 1. link target; page names cannot include ] or |
1319 # 2. a pipe-separated substring; only the last is captured
1320 # Stop matching at | and ]] without relying on backtracking.
1324 ([^[]*) # 3. link trail (the text up until the next link)
1326 function ( $match ) use ( $title, $local, $wikiId ) {
1329 $medians = '(?:' . preg_quote( MWNamespace
::getCanonicalName( NS_MEDIA
), '/' ) . '|';
1330 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA
), '/' ) . '):';
1332 $comment = $match[0];
1334 # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
1335 if ( strpos( $match[1], '%' ) !== false ) {
1337 rawurldecode( $match[1] ),
1338 [ '<' => '<', '>' => '>' ]
1342 # Handle link renaming [[foo|text]] will show link as "text"
1343 if ( $match[2] != "" ) {
1350 if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1351 # Media link; trail not supported.
1352 $linkRegexp = '/\[\[(.*?)\]\]/';
1353 $title = Title
::makeTitleSafe( NS_FILE
, $submatch[1] );
1355 $thelink = Linker
::makeMediaLinkObj( $title, $text );
1358 # Other kind of link
1359 # Make sure its target is non-empty
1360 if ( isset( $match[1][0] ) && $match[1][0] == ':' ) {
1361 $match[1] = substr( $match[1], 1 );
1363 if ( $match[1] !== false && $match[1] !== '' ) {
1364 if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
1365 $trail = $submatch[1];
1369 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1370 list( $inside, $trail ) = Linker
::splitTrail( $trail );
1373 $linkTarget = Linker
::normalizeSubpageLink( $title, $match[1], $linkText );
1375 $target = Title
::newFromText( $linkTarget );
1377 if ( $target->getText() == '' && !$target->isExternal()
1378 && !$local && $title
1380 $newTarget = clone $title;
1381 $newTarget->setFragment( '#' . $target->getFragment() );
1382 $target = $newTarget;
1385 $thelink = Linker
::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
1390 // If the link is still valid, go ahead and replace it in!
1391 $comment = preg_replace(
1393 StringUtils
::escapeRegexReplacement( $thelink ),
1406 * Generates a link to the given Title
1408 * @note This is only public for technical reasons. It's not intended for use outside Linker.
1410 * @param Title $title
1411 * @param string $text
1412 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1413 * as used by WikiMap.
1414 * @param string|string[] $options See the $options parameter in Linker::link.
1416 * @return string HTML link
1418 public static function makeCommentLink(
1419 Title
$title, $text, $wikiId = null, $options = []
1421 if ( $wikiId !== null && !$title->isExternal() ) {
1422 $link = Linker
::makeExternalLink(
1423 WikiMap
::getForeignURL(
1425 $title->getPrefixedText(),
1426 $title->getFragment()
1429 /* escape = */ false // Already escaped
1432 $link = Linker
::link( $title, $text, [], [], $options );
1439 * @param Title $contextTitle
1440 * @param string $target
1441 * @param string $text
1444 public static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
1447 # :Foobar -- override special treatment of prefix (images, language links)
1448 # /Foobar -- convert to CurrentPage/Foobar
1449 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial and final / from text
1450 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1451 # ../Foobar -- convert to CurrentPage/Foobar,
1452 # (from CurrentPage/CurrentSubPage)
1453 # ../Foobar/ -- convert to CurrentPage/Foobar, use 'Foobar' as text
1454 # (from CurrentPage/CurrentSubPage)
1456 $ret = $target; # default return value is no change
1458 # Some namespaces don't allow subpages,
1459 # so only perform processing if subpages are allowed
1460 if ( $contextTitle && MWNamespace
::hasSubpages( $contextTitle->getNamespace() ) ) {
1461 $hash = strpos( $target, '#' );
1462 if ( $hash !== false ) {
1463 $suffix = substr( $target, $hash );
1464 $target = substr( $target, 0, $hash );
1469 $target = trim( $target );
1470 # Look at the first character
1471 if ( $target != '' && $target[0] === '/' ) {
1472 # / at end means we don't want the slash to be shown
1474 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1475 if ( $trailingSlashes ) {
1476 $noslash = $target = substr( $target, 1, -strlen( $m[0][0] ) );
1478 $noslash = substr( $target, 1 );
1481 $ret = $contextTitle->getPrefixedText() . '/' . trim( $noslash ) . $suffix;
1482 if ( $text === '' ) {
1483 $text = $target . $suffix;
1484 } # this might be changed for ugliness reasons
1486 # check for .. subpage backlinks
1488 $nodotdot = $target;
1489 while ( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1491 $nodotdot = substr( $nodotdot, 3 );
1493 if ( $dotdotcount > 0 ) {
1494 $exploded = explode( '/', $contextTitle->getPrefixedText() );
1495 if ( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1496 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1497 # / at the end means don't show full path
1498 if ( substr( $nodotdot, -1, 1 ) === '/' ) {
1499 $nodotdot = rtrim( $nodotdot, '/' );
1500 if ( $text === '' ) {
1501 $text = $nodotdot . $suffix;
1504 $nodotdot = trim( $nodotdot );
1505 if ( $nodotdot != '' ) {
1506 $ret .= '/' . $nodotdot;
1518 * Wrap a comment in standard punctuation and formatting if
1519 * it's non-empty, otherwise return empty string.
1521 * @since 1.16.3. $wikiId added in 1.26
1522 * @param string $comment
1523 * @param Title|null $title Title object (to generate link to section in autocomment) or null
1524 * @param bool $local Whether section links should refer to local page
1525 * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to.
1526 * For use with external changes.
1530 public static function commentBlock(
1531 $comment, $title = null, $local = false, $wikiId = null
1533 // '*' used to be the comment inserted by the software way back
1534 // in antiquity in case none was provided, here for backwards
1535 // compatibility, acc. to brion -ævar
1536 if ( $comment == '' ||
$comment == '*' ) {
1539 $formatted = self
::formatComment( $comment, $title, $local, $wikiId );
1540 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
1541 return " <span class=\"comment\">$formatted</span>";
1546 * Wrap and format the given revision's comment block, if the current
1547 * user is allowed to view it.
1550 * @param Revision $rev
1551 * @param bool $local Whether section links should refer to local page
1552 * @param bool $isPublic Show only if all users can see it
1553 * @return string HTML fragment
1555 public static function revComment( Revision
$rev, $local = false, $isPublic = false ) {
1556 if ( $rev->getComment( Revision
::RAW
) == "" ) {
1559 if ( $rev->isDeleted( Revision
::DELETED_COMMENT
) && $isPublic ) {
1560 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
1561 } elseif ( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
1562 $block = self
::commentBlock( $rev->getComment( Revision
::FOR_THIS_USER
),
1563 $rev->getTitle(), $local );
1565 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
1567 if ( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
1568 return " <span class=\"history-deleted\">$block</span>";
1578 public static function formatRevisionSize( $size ) {
1580 $stxt = wfMessage( 'historyempty' )->escaped();
1582 $stxt = wfMessage( 'nbytes' )->numParams( $size )->escaped();
1583 $stxt = wfMessage( 'parentheses' )->rawParams( $stxt )->escaped();
1585 return "<span class=\"history-size\">$stxt</span>";
1589 * Add another level to the Table of Contents
1594 public static function tocIndent() {
1599 * Finish one or more sublevels on the Table of Contents
1605 public static function tocUnindent( $level ) {
1606 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level > 0 ?
$level : 0 );
1610 * parameter level defines if we are on an indentation level
1613 * @param string $anchor
1614 * @param string $tocline
1615 * @param string $tocnumber
1616 * @param string $level
1617 * @param string|bool $sectionIndex
1620 public static function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
1621 $classes = "toclevel-$level";
1622 if ( $sectionIndex !== false ) {
1623 $classes .= " tocsection-$sectionIndex";
1625 return "\n<li class=\"$classes\"><a href=\"#" .
1626 $anchor . '"><span class="tocnumber">' .
1627 $tocnumber . '</span> <span class="toctext">' .
1628 $tocline . '</span></a>';
1632 * End a Table Of Contents line.
1633 * tocUnindent() will be used instead if we're ending a line below
1638 public static function tocLineEnd() {
1643 * Wraps the TOC in a table and provides the hide/collapse javascript.
1646 * @param string $toc Html of the Table Of Contents
1647 * @param string|Language|bool $lang Language for the toc title, defaults to user language
1648 * @return string Full html of the TOC
1650 public static function tocList( $toc, $lang = false ) {
1651 $lang = wfGetLangObj( $lang );
1652 $title = wfMessage( 'toc' )->inLanguage( $lang )->escaped();
1654 return '<div id="toc" class="toc">'
1655 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1657 . "</ul>\n</div>\n";
1661 * Generate a table of contents from a section tree.
1663 * @since 1.16.3. $lang added in 1.17
1664 * @param array $tree Return value of ParserOutput::getSections()
1665 * @param string|Language|bool $lang Language for the toc title, defaults to user language
1666 * @return string HTML fragment
1668 public static function generateTOC( $tree, $lang = false ) {
1671 foreach ( $tree as $section ) {
1672 if ( $section['toclevel'] > $lastLevel ) {
1673 $toc .= self
::tocIndent();
1674 } elseif ( $section['toclevel'] < $lastLevel ) {
1675 $toc .= self
::tocUnindent(
1676 $lastLevel - $section['toclevel'] );
1678 $toc .= self
::tocLineEnd();
1681 $toc .= self
::tocLine( $section['anchor'],
1682 $section['line'], $section['number'],
1683 $section['toclevel'], $section['index'] );
1684 $lastLevel = $section['toclevel'];
1686 $toc .= self
::tocLineEnd();
1687 return self
::tocList( $toc, $lang );
1691 * Create a headline for content
1694 * @param int $level The level of the headline (1-6)
1695 * @param string $attribs Any attributes for the headline, starting with
1696 * a space and ending with '>'
1697 * This *must* be at least '>' for no attribs
1698 * @param string $anchor The anchor to give the headline (the bit after the #)
1699 * @param string $html Html for the text of the header
1700 * @param string $link HTML to add for the section edit link
1701 * @param bool|string $legacyAnchor A second, optional anchor to give for
1702 * backward compatibility (false to omit)
1704 * @return string HTML headline
1706 public static function makeHeadline( $level, $attribs, $anchor, $html,
1707 $link, $legacyAnchor = false
1709 $ret = "<h$level$attribs"
1710 . "<span class=\"mw-headline\" id=\"$anchor\">$html</span>"
1713 if ( $legacyAnchor !== false ) {
1714 $ret = "<div id=\"$legacyAnchor\"></div>$ret";
1720 * Split a link trail, return the "inside" portion and the remainder of the trail
1721 * as a two-element array
1722 * @param string $trail
1725 static function splitTrail( $trail ) {
1727 $regex = $wgContLang->linkTrail();
1729 if ( $trail !== '' ) {
1731 if ( preg_match( $regex, $trail, $m ) ) {
1736 return [ $inside, $trail ];
1740 * Generate a rollback link for a given revision. Currently it's the
1741 * caller's responsibility to ensure that the revision is the top one. If
1742 * it's not, of course, the user will get an error message.
1744 * If the calling page is called with the parameter &bot=1, all rollback
1745 * links also get that parameter. It causes the edit itself and the rollback
1746 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1747 * changes, so this allows sysops to combat a busy vandal without bothering
1750 * If the option verify is set this function will return the link only in case the
1751 * revision can be reverted. Please note that due to performance limitations
1752 * it might be assumed that a user isn't the only contributor of a page while
1753 * (s)he is, which will lead to useless rollback links. Furthermore this wont
1754 * work if $wgShowRollbackEditCount is disabled, so this can only function
1755 * as an additional check.
1757 * If the option noBrackets is set the rollback link wont be enclosed in "[]".
1759 * @since 1.16.3. $context added in 1.20. $options added in 1.21
1761 * @param Revision $rev
1762 * @param IContextSource $context Context to use or null for the main context.
1763 * @param array $options
1766 public static function generateRollback( $rev, IContextSource
$context = null,
1767 $options = [ 'verify' ]
1769 if ( $context === null ) {
1770 $context = RequestContext
::getMain();
1774 if ( in_array( 'verify', $options, true ) ) {
1775 $editCount = self
::getRollbackEditCount( $rev, true );
1776 if ( $editCount === false ) {
1781 $inner = self
::buildRollbackLink( $rev, $context, $editCount );
1783 if ( !in_array( 'noBrackets', $options, true ) ) {
1784 $inner = $context->msg( 'brackets' )->rawParams( $inner )->escaped();
1787 return '<span class="mw-rollback-link">' . $inner . '</span>';
1791 * This function will return the number of revisions which a rollback
1792 * would revert and, if $verify is set it will verify that a revision
1793 * can be reverted (that the user isn't the only contributor and the
1794 * revision we might rollback to isn't deleted). These checks can only
1795 * function as an additional check as this function only checks against
1796 * the last $wgShowRollbackEditCount edits.
1798 * Returns null if $wgShowRollbackEditCount is disabled or false if $verify
1799 * is set and the user is the only contributor of the page.
1801 * @param Revision $rev
1802 * @param bool $verify Try to verify that this revision can really be rolled back
1803 * @return int|bool|null
1805 public static function getRollbackEditCount( $rev, $verify ) {
1806 global $wgShowRollbackEditCount;
1807 if ( !is_int( $wgShowRollbackEditCount ) ||
!$wgShowRollbackEditCount > 0 ) {
1808 // Nothing has happened, indicate this by returning 'null'
1812 $dbr = wfGetDB( DB_SLAVE
);
1814 // Up to the value of $wgShowRollbackEditCount revisions are counted
1815 $res = $dbr->select(
1817 [ 'rev_user_text', 'rev_deleted' ],
1818 // $rev->getPage() returns null sometimes
1819 [ 'rev_page' => $rev->getTitle()->getArticleID() ],
1822 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
1823 'ORDER BY' => 'rev_timestamp DESC',
1824 'LIMIT' => $wgShowRollbackEditCount +
1
1830 foreach ( $res as $row ) {
1831 if ( $rev->getUserText( Revision
::RAW
) != $row->rev_user_text
) {
1833 ( $row->rev_deleted
& Revision
::DELETED_TEXT
1834 ||
$row->rev_deleted
& Revision
::DELETED_USER
1836 // If the user or the text of the revision we might rollback
1837 // to is deleted in some way we can't rollback. Similar to
1838 // the sanity checks in WikiPage::commitRollback.
1847 if ( $verify && $editCount <= $wgShowRollbackEditCount && !$moreRevs ) {
1848 // We didn't find at least $wgShowRollbackEditCount revisions made by the current user
1849 // and there weren't any other revisions. That means that the current user is the only
1850 // editor, so we can't rollback
1857 * Build a raw rollback link, useful for collections of "tool" links
1859 * @since 1.16.3. $context added in 1.20. $editCount added in 1.21
1860 * @param Revision $rev
1861 * @param IContextSource|null $context Context to use or null for the main context.
1862 * @param int $editCount Number of edits that would be reverted
1863 * @return string HTML fragment
1865 public static function buildRollbackLink( $rev, IContextSource
$context = null,
1868 global $wgShowRollbackEditCount, $wgMiserMode;
1870 // To config which pages are affected by miser mode
1871 $disableRollbackEditCountSpecialPage = [ 'Recentchanges', 'Watchlist' ];
1873 if ( $context === null ) {
1874 $context = RequestContext
::getMain();
1877 $title = $rev->getTitle();
1879 'action' => 'rollback',
1880 'from' => $rev->getUserText(),
1881 'token' => $context->getUser()->getEditToken( 'rollback' ),
1884 'data-mw' => 'interface',
1885 'title' => $context->msg( 'tooltip-rollback' )->text(),
1887 $options = [ 'known', 'noclasses' ];
1889 if ( $context->getRequest()->getBool( 'bot' ) ) {
1890 $query['bot'] = '1';
1891 $query['hidediff'] = '1'; // bug 15999
1894 $disableRollbackEditCount = false;
1895 if ( $wgMiserMode ) {
1896 foreach ( $disableRollbackEditCountSpecialPage as $specialPage ) {
1897 if ( $context->getTitle()->isSpecial( $specialPage ) ) {
1898 $disableRollbackEditCount = true;
1904 if ( !$disableRollbackEditCount
1905 && is_int( $wgShowRollbackEditCount )
1906 && $wgShowRollbackEditCount > 0
1908 if ( !is_numeric( $editCount ) ) {
1909 $editCount = self
::getRollbackEditCount( $rev, false );
1912 if ( $editCount > $wgShowRollbackEditCount ) {
1913 $html = $context->msg( 'rollbacklinkcount-morethan' )
1914 ->numParams( $wgShowRollbackEditCount )->parse();
1916 $html = $context->msg( 'rollbacklinkcount' )->numParams( $editCount )->parse();
1919 return self
::link( $title, $html, $attrs, $query, $options );
1921 $html = $context->msg( 'rollbacklink' )->escaped();
1922 return self
::link( $title, $html, $attrs, $query, $options );
1927 * Returns HTML for the "templates used on this page" list.
1929 * Make an HTML list of templates, and then add a "More..." link at
1930 * the bottom. If $more is null, do not add a "More..." link. If $more
1931 * is a Title, make a link to that title and use it. If $more is a string,
1932 * directly paste it in as the link (escaping needs to be done manually).
1933 * Finally, if $more is a Message, call toString().
1935 * @since 1.16.3. $more added in 1.21
1936 * @param Title[] $templates Array of templates
1937 * @param bool $preview Whether this is for a preview
1938 * @param bool $section Whether this is for a section edit
1939 * @param Title|Message|string|null $more An escaped link for "More..." of the templates
1940 * @return string HTML output
1942 public static function formatTemplates( $templates, $preview = false,
1943 $section = false, $more = null
1948 if ( count( $templates ) > 0 ) {
1949 # Do a batch existence check
1950 $batch = new LinkBatch
;
1951 foreach ( $templates as $title ) {
1952 $batch->addObj( $title );
1956 # Construct the HTML
1957 $outText = '<div class="mw-templatesUsedExplanation">';
1959 $outText .= wfMessage( 'templatesusedpreview' )->numParams( count( $templates ) )
1961 } elseif ( $section ) {
1962 $outText .= wfMessage( 'templatesusedsection' )->numParams( count( $templates ) )
1965 $outText .= wfMessage( 'templatesused' )->numParams( count( $templates ) )
1968 $outText .= "</div><ul>\n";
1970 usort( $templates, 'Title::compare' );
1971 foreach ( $templates as $titleObj ) {
1973 $restrictions = $titleObj->getRestrictions( 'edit' );
1974 if ( $restrictions ) {
1975 // Check backwards-compatible messages
1977 if ( $restrictions === [ 'sysop' ] ) {
1978 $msg = wfMessage( 'template-protected' );
1979 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
1980 $msg = wfMessage( 'template-semiprotected' );
1982 if ( $msg && !$msg->isDisabled() ) {
1983 $protected = $msg->parse();
1985 // Construct the message from restriction-level-*
1986 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
1988 foreach ( $restrictions as $r ) {
1989 $msgs[] = wfMessage( "restriction-level-$r" )->parse();
1991 $protected = wfMessage( 'parentheses' )
1992 ->rawParams( $wgLang->commaList( $msgs ) )->escaped();
1995 if ( $titleObj->quickUserCan( 'edit' ) ) {
1996 $editLink = self
::link(
1998 wfMessage( 'editlink' )->escaped(),
2000 [ 'action' => 'edit' ]
2003 $editLink = self
::link(
2005 wfMessage( 'viewsourcelink' )->escaped(),
2007 [ 'action' => 'edit' ]
2010 $outText .= '<li>' . self
::link( $titleObj )
2011 . wfMessage( 'word-separator' )->escaped()
2012 . wfMessage( 'parentheses' )->rawParams( $editLink )->escaped()
2013 . wfMessage( 'word-separator' )->escaped()
2014 . $protected . '</li>';
2017 if ( $more instanceof Title
) {
2018 $outText .= '<li>' . self
::link( $more, wfMessage( 'moredotdotdot' ) ) . '</li>';
2019 } elseif ( $more ) {
2020 $outText .= "<li>$more</li>";
2023 $outText .= '</ul>';
2029 * Returns HTML for the "hidden categories on this page" list.
2032 * @param array $hiddencats Array of hidden categories from Article::getHiddenCategories
2034 * @return string HTML output
2036 public static function formatHiddenCategories( $hiddencats ) {
2039 if ( count( $hiddencats ) > 0 ) {
2040 # Construct the HTML
2041 $outText = '<div class="mw-hiddenCategoriesExplanation">';
2042 $outText .= wfMessage( 'hiddencategories' )->numParams( count( $hiddencats ) )->parseAsBlock();
2043 $outText .= "</div><ul>\n";
2045 foreach ( $hiddencats as $titleObj ) {
2046 # If it's hidden, it must exist - no need to check with a LinkBatch
2048 . self
::link( $titleObj, null, [], [], 'known' )
2051 $outText .= '</ul>';
2057 * Format a size in bytes for output, using an appropriate
2058 * unit (B, KB, MB or GB) according to the magnitude in question
2061 * @param int $size Size to format
2064 public static function formatSize( $size ) {
2066 return htmlspecialchars( $wgLang->formatSize( $size ) );
2070 * Given the id of an interface element, constructs the appropriate title
2071 * attribute from the system messages. (Note, this is usually the id but
2072 * isn't always, because sometimes the accesskey needs to go on a different
2073 * element than the id, for reverse-compatibility, etc.)
2075 * @since 1.16.3 $msgParams added in 1.27
2076 * @param string $name Id of the element, minus prefixes.
2077 * @param string|null $options Null or the string 'withaccess' to add an access-
2079 * @param array $msgParams Parameters to pass to the message
2081 * @return string Contents of the title attribute (which you must HTML-
2082 * escape), or false for no title attribute
2084 public static function titleAttrib( $name, $options = null, array $msgParams = [] ) {
2085 $message = wfMessage( "tooltip-$name", $msgParams );
2086 if ( !$message->exists() ) {
2089 $tooltip = $message->text();
2090 # Compatibility: formerly some tooltips had [alt-.] hardcoded
2091 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
2092 # Message equal to '-' means suppress it.
2093 if ( $tooltip == '-' ) {
2098 if ( $options == 'withaccess' ) {
2099 $accesskey = self
::accesskey( $name );
2100 if ( $accesskey !== false ) {
2101 // Should be build the same as in jquery.accessKeyLabel.js
2102 if ( $tooltip === false ||
$tooltip === '' ) {
2103 $tooltip = wfMessage( 'brackets', $accesskey )->text();
2105 $tooltip .= wfMessage( 'word-separator' )->text();
2106 $tooltip .= wfMessage( 'brackets', $accesskey )->text();
2114 public static $accesskeycache;
2117 * Given the id of an interface element, constructs the appropriate
2118 * accesskey attribute from the system messages. (Note, this is usually
2119 * the id but isn't always, because sometimes the accesskey needs to go on
2120 * a different element than the id, for reverse-compatibility, etc.)
2123 * @param string $name Id of the element, minus prefixes.
2124 * @return string Contents of the accesskey attribute (which you must HTML-
2125 * escape), or false for no accesskey attribute
2127 public static function accesskey( $name ) {
2128 if ( isset( self
::$accesskeycache[$name] ) ) {
2129 return self
::$accesskeycache[$name];
2132 $message = wfMessage( "accesskey-$name" );
2134 if ( !$message->exists() ) {
2137 $accesskey = $message->plain();
2138 if ( $accesskey === '' ||
$accesskey === '-' ) {
2139 # @todo FIXME: Per standard MW behavior, a value of '-' means to suppress the
2140 # attribute, but this is broken for accesskey: that might be a useful
2146 self
::$accesskeycache[$name] = $accesskey;
2147 return self
::$accesskeycache[$name];
2151 * Get a revision-deletion link, or disabled link, or nothing, depending
2152 * on user permissions & the settings on the revision.
2154 * Will use forward-compatible revision ID in the Special:RevDelete link
2155 * if possible, otherwise the timestamp-based ID which may break after
2159 * @param Revision $rev
2160 * @param Title $title
2161 * @return string HTML fragment
2163 public static function getRevDeleteLink( User
$user, Revision
$rev, Title
$title ) {
2164 $canHide = $user->isAllowed( 'deleterevision' );
2165 if ( !$canHide && !( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
2169 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
2170 return Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
2172 if ( $rev->getId() ) {
2173 // RevDelete links using revision ID are stable across
2174 // page deletion and undeletion; use when possible.
2176 'type' => 'revision',
2177 'target' => $title->getPrefixedDBkey(),
2178 'ids' => $rev->getId()
2181 // Older deleted entries didn't save a revision ID.
2182 // We have to refer to these by timestamp, ick!
2184 'type' => 'archive',
2185 'target' => $title->getPrefixedDBkey(),
2186 'ids' => $rev->getTimestamp()
2189 return Linker
::revDeleteLink( $query,
2190 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
2195 * Creates a (show/hide) link for deleting revisions/log entries
2197 * @param array $query Query parameters to be passed to link()
2198 * @param bool $restricted Set to true to use a "<strong>" instead of a "<span>"
2199 * @param bool $delete Set to true to use (show/hide) rather than (show)
2201 * @return string HTML "<a>" link to Special:Revisiondelete, wrapped in a
2202 * span to allow for customization of appearance with CSS
2204 public static function revDeleteLink( $query = [], $restricted = false, $delete = true ) {
2205 $sp = SpecialPage
::getTitleFor( 'Revisiondelete' );
2206 $msgKey = $delete ?
'rev-delundel' : 'rev-showdeleted';
2207 $html = wfMessage( $msgKey )->escaped();
2208 $tag = $restricted ?
'strong' : 'span';
2209 $link = self
::link( $sp, $html, [], $query, [ 'known', 'noclasses' ] );
2212 [ 'class' => 'mw-revdelundel-link' ],
2213 wfMessage( 'parentheses' )->rawParams( $link )->escaped()
2218 * Creates a dead (show/hide) link for deleting revisions/log entries
2221 * @param bool $delete Set to true to use (show/hide) rather than (show)
2223 * @return string HTML text wrapped in a span to allow for customization
2224 * of appearance with CSS
2226 public static function revDeleteLinkDisabled( $delete = true ) {
2227 $msgKey = $delete ?
'rev-delundel' : 'rev-showdeleted';
2228 $html = wfMessage( $msgKey )->escaped();
2229 $htmlParentheses = wfMessage( 'parentheses' )->rawParams( $html )->escaped();
2230 return Xml
::tags( 'span', [ 'class' => 'mw-revdelundel-link' ], $htmlParentheses );
2233 /* Deprecated methods */
2236 * Returns the attributes for the tooltip and access key.
2238 * @since 1.16.3. $msgParams introduced in 1.27
2239 * @param string $name
2240 * @param array $msgParams Params for constructing the message
2244 public static function tooltipAndAccesskeyAttribs( $name, array $msgParams = [] ) {
2245 # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2246 # no attribute" instead of "output '' as value for attribute", this
2247 # would be three lines.
2249 'title' => self
::titleAttrib( $name, 'withaccess', $msgParams ),
2250 'accesskey' => self
::accesskey( $name )
2252 if ( $attribs['title'] === false ) {
2253 unset( $attribs['title'] );
2255 if ( $attribs['accesskey'] === false ) {
2256 unset( $attribs['accesskey'] );
2262 * Returns raw bits of HTML, use titleAttrib()
2264 * @param string $name
2265 * @param array|null $options
2266 * @return null|string
2268 public static function tooltip( $name, $options = null ) {
2269 # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2270 # no attribute" instead of "output '' as value for attribute", this
2271 # would be two lines.
2272 $tooltip = self
::titleAttrib( $name, $options );
2273 if ( $tooltip === false ) {
2276 return Xml
::expandAttributes( [