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
142 * @param int $threshold User defined threshold
143 * @return string CSS class
145 public static function getLinkColour( $t, $threshold ) {
147 if ( $t->isRedirect() ) {
149 $colour = 'mw-redirect';
150 } elseif ( $threshold > 0 && $t->isContentPage() &&
151 $t->exists() && $t->getLength() < $threshold
160 * This function returns an HTML link to the given target. It serves a few
162 * 1) If $target is a Title, the correct URL to link to will be figured
164 * 2) It automatically adds the usual classes for various types of link
165 * targets: "new" for red links, "stub" for short articles, etc.
166 * 3) It escapes all attribute values safely so there's no risk of XSS.
167 * 4) It provides a default tooltip if the target is a Title (the page
168 * name of the target).
169 * link() replaces the old functions in the makeLink() family.
171 * @since 1.18 Method exists since 1.16 as non-static, made static in 1.18.
173 * @param Title $target Can currently only be a Title, but this may
174 * change to support Images, literal URLs, etc.
175 * @param string $html The HTML contents of the <a> element, i.e.,
176 * the link text. This is raw HTML and will not be escaped. If null,
177 * defaults to the prefixed text of the Title; or if the Title is just a
178 * fragment, the contents of the fragment.
179 * @param array $customAttribs A key => value array of extra HTML attributes,
180 * such as title and class. (href is ignored.) Classes will be
181 * merged with the default classes, while other attributes will replace
182 * default attributes. All passed attribute values will be HTML-escaped.
183 * A false attribute value means to suppress that attribute.
184 * @param array $query The query string to append to the URL
185 * you're linking to, in key => value array form. Query keys and values
186 * will be URL-encoded.
187 * @param string|array $options String or array of strings:
188 * 'known': Page is known to exist, so don't check if it does.
189 * 'broken': Page is known not to exist, so don't check if it does.
190 * 'noclasses': Don't add any classes automatically (includes "new",
191 * "stub", "mw-redirect", "extiw"). Only use the class attribute
192 * provided, if any, so you get a simple blue link with no funny i-
194 * 'forcearticlepath': Use the article path always, even with a querystring.
195 * Has compatibility issues on some setups, so avoid wherever possible.
196 * 'http': Force a full URL with http:// as the scheme.
197 * 'https': Force a full URL with https:// as the scheme.
198 * 'stubThreshold' => (int): Stub threshold to use when determining link classes.
199 * @return string HTML <a> attribute
201 public static function link(
202 $target, $html = null, $customAttribs = [], $query = [], $options = []
204 if ( !$target instanceof Title
) {
205 wfWarn( __METHOD__
. ': Requires $target to be a Title object.', 2 );
206 return "<!-- ERROR -->$html";
209 if ( is_string( $query ) ) {
210 // some functions withing core using this still hand over query strings
211 wfDeprecated( __METHOD__
. ' with parameter $query as string (should be array)', '1.20' );
212 $query = wfCgiToArray( $query );
215 $services = MediaWikiServices
::getInstance();
216 $options = (array)$options;
218 // Custom options, create new LinkRenderer
219 if ( !isset( $options['stubThreshold'] ) ) {
220 $defaultLinkRenderer = $services->getLinkRenderer();
221 $options['stubThreshold'] = $defaultLinkRenderer->getStubThreshold();
223 $linkRenderer = $services->getLinkRendererFactory()
224 ->createFromLegacyOptions( $options );
226 $linkRenderer = $services->getLinkRenderer();
229 if ( $html !== null ) {
230 $text = new HtmlArmor( $html );
232 $text = $html; // null
234 if ( in_array( 'known', $options, true ) ) {
235 return $linkRenderer->makeKnownLink( $target, $text, $customAttribs, $query );
236 } elseif ( in_array( 'broken', $options, true ) ) {
237 return $linkRenderer->makeBrokenLink( $target, $text, $customAttribs, $query );
239 return $linkRenderer->makeLink( $target, $text, $customAttribs, $query );
244 * Identical to link(), except $options defaults to 'known'.
249 public static function linkKnown(
250 $target, $html = null, $customAttribs = [],
251 $query = [], $options = [ 'known' ]
253 return self
::link( $target, $html, $customAttribs, $query, $options );
257 * Make appropriate markup for a link to the current article. This is
258 * currently rendered as the bold link text. The calling sequence is the
259 * same as the other make*LinkObj static functions, despite $query not
264 * @param string $html [optional]
265 * @param string $query [optional]
266 * @param string $trail [optional]
267 * @param string $prefix [optional]
271 public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) {
272 $ret = "<strong class=\"selflink\">{$prefix}{$html}</strong>{$trail}";
273 if ( !Hooks
::run( 'SelfLinkBegin', [ $nt, &$html, &$trail, &$prefix, &$ret ] ) ) {
278 $html = htmlspecialchars( $nt->getPrefixedText() );
280 list( $inside, $trail ) = self
::splitTrail( $trail );
281 return "<strong class=\"selflink\">{$prefix}{$html}{$inside}</strong>{$trail}";
285 * Get a message saying that an invalid title was encountered.
286 * This should be called after a method like Title::makeTitleSafe() returned
287 * a value indicating that the title object is invalid.
289 * @param IContextSource $context Context to use to get the messages
290 * @param int $namespace Namespace number
291 * @param string $title Text of the title, without the namespace part
294 public static function getInvalidTitleDescription( IContextSource
$context, $namespace, $title ) {
297 // First we check whether the namespace exists or not.
298 if ( MWNamespace
::exists( $namespace ) ) {
299 if ( $namespace == NS_MAIN
) {
300 $name = $context->msg( 'blanknamespace' )->text();
302 $name = $wgContLang->getFormattedNsText( $namespace );
304 return $context->msg( 'invalidtitle-knownnamespace', $namespace, $name, $title )->text();
306 return $context->msg( 'invalidtitle-unknownnamespace', $namespace, $title )->text();
312 * @param LinkTarget $target
313 * @return LinkTarget|Title You will get back the same type you passed in, or a Title object
315 public static function normaliseSpecialPage( LinkTarget
$target ) {
316 if ( $target->getNamespace() == NS_SPECIAL
) {
317 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $target->getDBkey() );
321 $ret = SpecialPage
::getTitleFor( $name, $subpage, $target->getFragment() );
329 * Returns the filename part of an url.
330 * Used as alternative text for external images.
336 private static function fnamePart( $url ) {
337 $basename = strrchr( $url, '/' );
338 if ( false === $basename ) {
341 $basename = substr( $basename, 1 );
347 * Return the code for images which were added via external links,
348 * via Parser::maybeMakeExternalImage().
356 public static function makeExternalImage( $url, $alt = '' ) {
358 $alt = self
::fnamePart( $url );
361 $success = Hooks
::run( 'LinkerMakeExternalImage', [ &$url, &$alt, &$img ] );
363 wfDebug( "Hook LinkerMakeExternalImage changed the output of external image "
364 . "with url {$url} and alt text {$alt} to {$img}\n", true );
367 return Html
::element( 'img',
374 * Given parameters derived from [[Image:Foo|options...]], generate the
375 * HTML that that syntax inserts in the page.
377 * @param Parser $parser
378 * @param Title $title Title object of the file (not the currently viewed page)
379 * @param File $file File object, or false if it doesn't exist
380 * @param array $frameParams Associative array of parameters external to the media handler.
381 * Boolean parameters are indicated by presence or absence, the value is arbitrary and
382 * will often be false.
383 * thumbnail If present, downscale and frame
384 * manualthumb Image name to use as a thumbnail, instead of automatic scaling
385 * framed Shows image in original size in a frame
386 * frameless Downscale but don't frame
387 * upright If present, tweak default sizes for portrait orientation
388 * upright_factor Fudge factor for "upright" tweak (default 0.75)
389 * border If present, show a border around the image
390 * align Horizontal alignment (left, right, center, none)
391 * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
392 * bottom, text-bottom)
393 * alt Alternate text for image (i.e. alt attribute). Plain text.
394 * class HTML for image classes. Plain text.
395 * caption HTML for image caption.
396 * link-url URL to link to
397 * link-title Title object to link to
398 * link-target Value for the target attribute, only with link-url
399 * no-link Boolean, suppress description link
401 * @param array $handlerParams Associative array of media handler parameters, to be passed
402 * to transform(). Typical keys are "width" and "page".
403 * @param string|bool $time Timestamp of the file, set as false for current
404 * @param string $query Query params for desc url
405 * @param int|null $widthOption Used by the parser to remember the user preference thumbnailsize
407 * @return string HTML for an image, with links, wrappers, etc.
409 public static function makeImageLink( Parser
$parser, Title
$title,
410 $file, $frameParams = [], $handlerParams = [], $time = false,
411 $query = "", $widthOption = null
414 $dummy = new DummyLinker
;
415 if ( !Hooks
::run( 'ImageBeforeProduceHTML', [ &$dummy, &$title,
416 &$file, &$frameParams, &$handlerParams, &$time, &$res ] ) ) {
420 if ( $file && !$file->allowInlineDisplay() ) {
421 wfDebug( __METHOD__
. ': ' . $title->getPrefixedDBkey() . " does not allow inline display\n" );
422 return self
::link( $title );
427 $hp =& $handlerParams;
429 // Clean up parameters
430 $page = isset( $hp['page'] ) ?
$hp['page'] : false;
431 if ( !isset( $fp['align'] ) ) {
434 if ( !isset( $fp['alt'] ) ) {
437 if ( !isset( $fp['title'] ) ) {
440 if ( !isset( $fp['class'] ) ) {
444 $prefix = $postfix = '';
446 if ( 'center' == $fp['align'] ) {
447 $prefix = '<div class="center">';
449 $fp['align'] = 'none';
451 if ( $file && !isset( $hp['width'] ) ) {
452 if ( isset( $hp['height'] ) && $file->isVectorized() ) {
453 // If its a vector image, and user only specifies height
454 // we don't want it to be limited by its "normal" width.
455 global $wgSVGMaxSize;
456 $hp['width'] = $wgSVGMaxSize;
458 $hp['width'] = $file->getWidth( $page );
461 if ( isset( $fp['thumbnail'] )
462 ||
isset( $fp['manualthumb'] )
463 ||
isset( $fp['framed'] )
464 ||
isset( $fp['frameless'] )
467 global $wgThumbLimits, $wgThumbUpright;
469 if ( $widthOption === null ||
!isset( $wgThumbLimits[$widthOption] ) ) {
470 $widthOption = User
::getDefaultOption( 'thumbsize' );
473 // Reduce width for upright images when parameter 'upright' is used
474 if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
475 $fp['upright'] = $wgThumbUpright;
478 // For caching health: If width scaled down due to upright
479 // parameter, round to full __0 pixel to avoid the creation of a
480 // lot of odd thumbs.
481 $prefWidth = isset( $fp['upright'] ) ?
482 round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
483 $wgThumbLimits[$widthOption];
485 // Use width which is smaller: real image width or user preference width
486 // Unless image is scalable vector.
487 if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
488 $prefWidth < $hp['width'] ||
$file->isVectorized() ) ) {
489 $hp['width'] = $prefWidth;
494 if ( isset( $fp['thumbnail'] ) ||
isset( $fp['manualthumb'] ) ||
isset( $fp['framed'] ) ) {
495 # Create a thumbnail. Alignment depends on the writing direction of
496 # the page content language (right-aligned for LTR languages,
497 # left-aligned for RTL languages)
498 # If a thumbnail width has not been provided, it is set
499 # to the default user option as specified in Language*.php
500 if ( $fp['align'] == '' ) {
501 $fp['align'] = $parser->getTargetLanguage()->alignEnd();
503 return $prefix . self
::makeThumbLink2( $title, $file, $fp, $hp, $time, $query ) . $postfix;
506 if ( $file && isset( $fp['frameless'] ) ) {
507 $srcWidth = $file->getWidth( $page );
508 # For "frameless" option: do not present an image bigger than the
509 # source (for bitmap-style images). This is the same behavior as the
510 # "thumb" option does it already.
511 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
512 $hp['width'] = $srcWidth;
516 if ( $file && isset( $hp['width'] ) ) {
517 # Create a resized image, without the additional thumbnail features
518 $thumb = $file->transform( $hp );
524 $s = self
::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
526 self
::processResponsiveImages( $file, $thumb, $hp );
529 'title' => $fp['title'],
530 'valign' => isset( $fp['valign'] ) ?
$fp['valign'] : false,
531 'img-class' => $fp['class'] ];
532 if ( isset( $fp['border'] ) ) {
533 $params['img-class'] .= ( $params['img-class'] !== '' ?
' ' : '' ) . 'thumbborder';
535 $params = self
::getImageLinkMTOParams( $fp, $query, $parser ) +
$params;
537 $s = $thumb->toHtml( $params );
539 if ( $fp['align'] != '' ) {
540 $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
542 return str_replace( "\n", ' ', $prefix . $s . $postfix );
546 * Get the link parameters for MediaTransformOutput::toHtml() from given
547 * frame parameters supplied by the Parser.
548 * @param array $frameParams The frame parameters
549 * @param string $query An optional query string to add to description page links
550 * @param Parser|null $parser
553 private static function getImageLinkMTOParams( $frameParams, $query = '', $parser = null ) {
555 if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
556 $mtoParams['custom-url-link'] = $frameParams['link-url'];
557 if ( isset( $frameParams['link-target'] ) ) {
558 $mtoParams['custom-target-link'] = $frameParams['link-target'];
561 $extLinkAttrs = $parser->getExternalLinkAttribs( $frameParams['link-url'] );
562 foreach ( $extLinkAttrs as $name => $val ) {
563 // Currently could include 'rel' and 'target'
564 $mtoParams['parser-extlink-' . $name] = $val;
567 } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
568 $mtoParams['custom-title-link'] = self
::normaliseSpecialPage( $frameParams['link-title'] );
569 } elseif ( !empty( $frameParams['no-link'] ) ) {
572 $mtoParams['desc-link'] = true;
573 $mtoParams['desc-query'] = $query;
579 * Make HTML for a thumbnail including image, border and caption
580 * @param Title $title
581 * @param File|bool $file File object or false if it doesn't exist
582 * @param string $label
584 * @param string $align
585 * @param array $params
586 * @param bool $framed
587 * @param string $manualthumb
590 public static function makeThumbLinkObj( Title
$title, $file, $label = '', $alt,
591 $align = 'right', $params = [], $framed = false, $manualthumb = ""
599 $frameParams['framed'] = true;
601 if ( $manualthumb ) {
602 $frameParams['manualthumb'] = $manualthumb;
604 return self
::makeThumbLink2( $title, $file, $frameParams, $params );
608 * @param Title $title
610 * @param array $frameParams
611 * @param array $handlerParams
613 * @param string $query
616 public static function makeThumbLink2( Title
$title, $file, $frameParams = [],
617 $handlerParams = [], $time = false, $query = ""
619 $exists = $file && $file->exists();
623 $hp =& $handlerParams;
625 $page = isset( $hp['page'] ) ?
$hp['page'] : false;
626 if ( !isset( $fp['align'] ) ) {
627 $fp['align'] = 'right';
629 if ( !isset( $fp['alt'] ) ) {
632 if ( !isset( $fp['title'] ) ) {
635 if ( !isset( $fp['caption'] ) ) {
639 if ( empty( $hp['width'] ) ) {
640 // Reduce width for upright images when parameter 'upright' is used
641 $hp['width'] = isset( $fp['upright'] ) ?
130 : 180;
645 $manualthumb = false;
648 $outerWidth = $hp['width'] +
2;
650 if ( isset( $fp['manualthumb'] ) ) {
651 # Use manually specified thumbnail
652 $manual_title = Title
::makeTitleSafe( NS_FILE
, $fp['manualthumb'] );
653 if ( $manual_title ) {
654 $manual_img = wfFindFile( $manual_title );
656 $thumb = $manual_img->getUnscaledThumb( $hp );
662 } elseif ( isset( $fp['framed'] ) ) {
663 // Use image dimensions, don't scale
664 $thumb = $file->getUnscaledThumb( $hp );
667 # Do not present an image bigger than the source, for bitmap-style images
668 # This is a hack to maintain compatibility with arbitrary pre-1.10 behavior
669 $srcWidth = $file->getWidth( $page );
670 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
671 $hp['width'] = $srcWidth;
673 $thumb = $file->transform( $hp );
677 $outerWidth = $thumb->getWidth() +
2;
679 $outerWidth = $hp['width'] +
2;
683 # ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
684 # So we don't need to pass it here in $query. However, the URL for the
685 # zoom icon still needs it, so we make a unique query for it. See bug 14771
686 $url = $title->getLocalURL( $query );
688 $url = wfAppendQuery( $url, [ 'page' => $page ] );
691 && !isset( $fp['link-title'] )
692 && !isset( $fp['link-url'] )
693 && !isset( $fp['no-link'] ) ) {
694 $fp['link-url'] = $url;
697 $s = "<div class=\"thumb t{$fp['align']}\">"
698 . "<div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
701 $s .= self
::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
703 } elseif ( !$thumb ) {
704 $s .= wfMessage( 'thumbnail_error', '' )->escaped();
707 if ( !$noscale && !$manualthumb ) {
708 self
::processResponsiveImages( $file, $thumb, $hp );
712 'title' => $fp['title'],
713 'img-class' => ( isset( $fp['class'] ) && $fp['class'] !== ''
715 : '' ) . 'thumbimage'
717 $params = self
::getImageLinkMTOParams( $fp, $query ) +
$params;
718 $s .= $thumb->toHtml( $params );
719 if ( isset( $fp['framed'] ) ) {
722 $zoomIcon = Html
::rawElement( 'div', [ 'class' => 'magnify' ],
723 Html
::rawElement( 'a', [
725 'class' => 'internal',
726 'title' => wfMessage( 'thumbnail-more' )->text() ],
730 $s .= ' <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
731 return str_replace( "\n", ' ', $s );
735 * Process responsive images: add 1.5x and 2x subimages to the thumbnail, where
739 * @param MediaTransformOutput $thumb
740 * @param array $hp Image parameters
742 public static function processResponsiveImages( $file, $thumb, $hp ) {
743 global $wgResponsiveImages;
744 if ( $wgResponsiveImages && $thumb && !$thumb->isError() ) {
746 $hp15['width'] = round( $hp['width'] * 1.5 );
748 $hp20['width'] = $hp['width'] * 2;
749 if ( isset( $hp['height'] ) ) {
750 $hp15['height'] = round( $hp['height'] * 1.5 );
751 $hp20['height'] = $hp['height'] * 2;
754 $thumb15 = $file->transform( $hp15 );
755 $thumb20 = $file->transform( $hp20 );
756 if ( $thumb15 && !$thumb15->isError() && $thumb15->getUrl() !== $thumb->getUrl() ) {
757 $thumb->responsiveUrls
['1.5'] = $thumb15->getUrl();
759 if ( $thumb20 && !$thumb20->isError() && $thumb20->getUrl() !== $thumb->getUrl() ) {
760 $thumb->responsiveUrls
['2'] = $thumb20->getUrl();
766 * Make a "broken" link to an image
769 * @param Title $title
770 * @param string $label Link label (plain text)
771 * @param string $query Query string
772 * @param string $unused1 Unused parameter kept for b/c
773 * @param string $unused2 Unused parameter kept for b/c
774 * @param bool $time A file of a certain timestamp was requested
777 public static function makeBrokenImageLinkObj( $title, $label = '',
778 $query = '', $unused1 = '', $unused2 = '', $time = false
780 if ( !$title instanceof Title
) {
781 wfWarn( __METHOD__
. ': Requires $title to be a Title object.' );
782 return "<!-- ERROR -->" . htmlspecialchars( $label );
785 global $wgEnableUploads, $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
786 if ( $label == '' ) {
787 $label = $title->getPrefixedText();
789 $encLabel = htmlspecialchars( $label );
790 $currentExists = $time ?
( wfFindFile( $title ) != false ) : false;
792 if ( ( $wgUploadMissingFileUrl ||
$wgUploadNavigationUrl ||
$wgEnableUploads )
795 $redir = RepoGroup
::singleton()->getLocalRepo()->checkRedirect( $title );
798 // We already know it's a redirect, so mark it
803 [ 'class' => 'mw-redirect' ],
804 wfCgiToArray( $query ),
805 [ 'known', 'noclasses' ]
809 $href = self
::getUploadUrl( $title, $query );
811 return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
812 htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES
) . '">' .
816 return self
::link( $title, $encLabel, [], wfCgiToArray( $query ), [ 'known', 'noclasses' ] );
820 * Get the URL to upload a certain file
823 * @param Title $destFile Title object of the file to upload
824 * @param string $query Urlencoded query string to prepend
825 * @return string Urlencoded URL
827 protected static function getUploadUrl( $destFile, $query = '' ) {
828 global $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
829 $q = 'wpDestFile=' . $destFile->getPartialURL();
830 if ( $query != '' ) {
834 if ( $wgUploadMissingFileUrl ) {
835 return wfAppendQuery( $wgUploadMissingFileUrl, $q );
836 } elseif ( $wgUploadNavigationUrl ) {
837 return wfAppendQuery( $wgUploadNavigationUrl, $q );
839 $upload = SpecialPage
::getTitleFor( 'Upload' );
840 return $upload->getLocalURL( $q );
845 * Create a direct link to a given uploaded file.
848 * @param Title $title
849 * @param string $html Pre-sanitized HTML
850 * @param string $time MW timestamp of file creation time
851 * @return string HTML
853 public static function makeMediaLinkObj( $title, $html = '', $time = false ) {
854 $img = wfFindFile( $title, [ 'time' => $time ] );
855 return self
::makeMediaLinkFile( $title, $img, $html );
859 * Create a direct link to a given uploaded file.
860 * This will make a broken link if $file is false.
863 * @param Title $title
864 * @param File|bool $file File object or false
865 * @param string $html Pre-sanitized HTML
866 * @return string HTML
868 * @todo Handle invalid or missing images better.
870 public static function makeMediaLinkFile( Title
$title, $file, $html = '' ) {
871 if ( $file && $file->exists() ) {
872 $url = $file->getUrl();
875 $url = self
::getUploadUrl( $title );
879 $alt = $title->getText();
891 if ( !Hooks
::run( 'LinkerMakeMediaLinkFile',
892 [ $title, $file, &$html, &$attribs, &$ret ] ) ) {
893 wfDebug( "Hook LinkerMakeMediaLinkFile changed the output of link "
894 . "with url {$url} and text {$html} to {$ret}\n", true );
898 return Html
::rawElement( 'a', $attribs, $html );
902 * Make a link to a special page given its name and, optionally,
903 * a message key from the link text.
904 * Usage example: Linker::specialLink( 'Recentchanges' )
907 * @param string $name
911 public static function specialLink( $name, $key = '' ) {
913 $key = strtolower( $name );
916 return self
::linkKnown( SpecialPage
::getTitleFor( $name ), wfMessage( $key )->text() );
920 * Make an external link
921 * @since 1.16.3. $title added in 1.21
922 * @param string $url URL to link to
923 * @param string $text Text of link
924 * @param bool $escape Do we escape the link text?
925 * @param string $linktype Type of external link. Gets added to the classes
926 * @param array $attribs Array of extra attributes to <a>
927 * @param Title|null $title Title object used for title specific link attributes
930 public static function makeExternalLink( $url, $text, $escape = true,
931 $linktype = '', $attribs = [], $title = null
936 $class .= " $linktype";
938 if ( isset( $attribs['class'] ) && $attribs['class'] ) {
939 $class .= " {$attribs['class']}";
941 $attribs['class'] = $class;
944 $text = htmlspecialchars( $text );
950 $newRel = Parser
::getExternalLinkRel( $url, $title );
951 if ( !isset( $attribs['rel'] ) ||
$attribs['rel'] === '' ) {
952 $attribs['rel'] = $newRel;
953 } elseif ( $newRel !== '' ) {
954 // Merge the rel attributes.
955 $newRels = explode( ' ', $newRel );
956 $oldRels = explode( ' ', $attribs['rel'] );
957 $combined = array_unique( array_merge( $newRels, $oldRels ) );
958 $attribs['rel'] = implode( ' ', $combined );
961 $success = Hooks
::run( 'LinkerMakeExternalLink',
962 [ &$url, &$text, &$link, &$attribs, $linktype ] );
964 wfDebug( "Hook LinkerMakeExternalLink changed the output of link "
965 . "with url {$url} and text {$text} to {$link}\n", true );
968 $attribs['href'] = $url;
969 return Html
::rawElement( 'a', $attribs, $text );
973 * Make user link (or user contributions for unregistered users)
974 * @param int $userId User id in database.
975 * @param string $userName User name in database.
976 * @param string $altUserName Text to display instead of the user name (optional)
977 * @return string HTML fragment
978 * @since 1.16.3. $altUserName was added in 1.19.
980 public static function userLink( $userId, $userName, $altUserName = false ) {
981 $classes = 'mw-userlink';
982 if ( $userId == 0 ) {
983 $page = SpecialPage
::getTitleFor( 'Contributions', $userName );
984 if ( $altUserName === false ) {
985 $altUserName = IP
::prettifyIP( $userName );
987 $classes .= ' mw-anonuserlink'; // Separate link class for anons (bug 43179)
989 $page = Title
::makeTitle( NS_USER
, $userName );
994 htmlspecialchars( $altUserName !== false ?
$altUserName : $userName ),
995 [ 'class' => $classes ]
1000 * Generate standard user tool links (talk, contributions, block link, etc.)
1003 * @param int $userId User identifier
1004 * @param string $userText User name or IP address
1005 * @param bool $redContribsWhenNoEdits Should the contributions link be
1006 * red if the user has no edits?
1007 * @param int $flags Customisation flags (e.g. Linker::TOOL_LINKS_NOBLOCK
1008 * and Linker::TOOL_LINKS_EMAIL).
1009 * @param int $edits User edit count (optional, for performance)
1010 * @return string HTML fragment
1012 public static function userToolLinks(
1013 $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits = null
1015 global $wgUser, $wgDisableAnonTalk, $wgLang;
1016 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
1017 $blockable = !( $flags & self
::TOOL_LINKS_NOBLOCK
);
1018 $addEmailLink = $flags & self
::TOOL_LINKS_EMAIL
&& $userId;
1022 $items[] = self
::userTalkLink( $userId, $userText );
1025 // check if the user has an edit
1027 if ( $redContribsWhenNoEdits ) {
1028 if ( intval( $edits ) === 0 && $edits !== 0 ) {
1029 $user = User
::newFromId( $userId );
1030 $edits = $user->getEditCount();
1032 if ( $edits === 0 ) {
1033 $attribs['class'] = 'new';
1036 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $userText );
1038 $items[] = self
::link( $contribsPage, wfMessage( 'contribslink' )->escaped(), $attribs );
1040 if ( $blockable && $wgUser->isAllowed( 'block' ) ) {
1041 $items[] = self
::blockLink( $userId, $userText );
1044 if ( $addEmailLink && $wgUser->canSendEmail() ) {
1045 $items[] = self
::emailLink( $userId, $userText );
1048 Hooks
::run( 'UserToolLinksEdit', [ $userId, $userText, &$items ] );
1051 return wfMessage( 'word-separator' )->escaped()
1052 . '<span class="mw-usertoollinks">'
1053 . wfMessage( 'parentheses' )->rawParams( $wgLang->pipeList( $items ) )->escaped()
1061 * Alias for userToolLinks( $userId, $userText, true );
1063 * @param int $userId User identifier
1064 * @param string $userText User name or IP address
1065 * @param int $edits User edit count (optional, for performance)
1068 public static function userToolLinksRedContribs( $userId, $userText, $edits = null ) {
1069 return self
::userToolLinks( $userId, $userText, true, 0, $edits );
1074 * @param int $userId User id in database.
1075 * @param string $userText User name in database.
1076 * @return string HTML fragment with user talk link
1078 public static function userTalkLink( $userId, $userText ) {
1079 $userTalkPage = Title
::makeTitle( NS_USER_TALK
, $userText );
1080 $userTalkLink = self
::link( $userTalkPage, wfMessage( 'talkpagelinktext' )->escaped() );
1081 return $userTalkLink;
1086 * @param int $userId Userid
1087 * @param string $userText User name in database.
1088 * @return string HTML fragment with block link
1090 public static function blockLink( $userId, $userText ) {
1091 $blockPage = SpecialPage
::getTitleFor( 'Block', $userText );
1092 $blockLink = self
::link( $blockPage, wfMessage( 'blocklink' )->escaped() );
1097 * @param int $userId Userid
1098 * @param string $userText User name in database.
1099 * @return string HTML fragment with e-mail user link
1101 public static function emailLink( $userId, $userText ) {
1102 $emailPage = SpecialPage
::getTitleFor( 'Emailuser', $userText );
1103 $emailLink = self
::link( $emailPage, wfMessage( 'emaillink' )->escaped() );
1108 * Generate a user link if the current user is allowed to view it
1110 * @param Revision $rev
1111 * @param bool $isPublic Show only if all users can see it
1112 * @return string HTML fragment
1114 public static function revUserLink( $rev, $isPublic = false ) {
1115 if ( $rev->isDeleted( Revision
::DELETED_USER
) && $isPublic ) {
1116 $link = wfMessage( 'rev-deleted-user' )->escaped();
1117 } elseif ( $rev->userCan( Revision
::DELETED_USER
) ) {
1118 $link = self
::userLink( $rev->getUser( Revision
::FOR_THIS_USER
),
1119 $rev->getUserText( Revision
::FOR_THIS_USER
) );
1121 $link = wfMessage( 'rev-deleted-user' )->escaped();
1123 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
1124 return '<span class="history-deleted">' . $link . '</span>';
1130 * Generate a user tool link cluster if the current user is allowed to view it
1132 * @param Revision $rev
1133 * @param bool $isPublic Show only if all users can see it
1134 * @return string HTML
1136 public static function revUserTools( $rev, $isPublic = false ) {
1137 if ( $rev->isDeleted( Revision
::DELETED_USER
) && $isPublic ) {
1138 $link = wfMessage( 'rev-deleted-user' )->escaped();
1139 } elseif ( $rev->userCan( Revision
::DELETED_USER
) ) {
1140 $userId = $rev->getUser( Revision
::FOR_THIS_USER
);
1141 $userText = $rev->getUserText( Revision
::FOR_THIS_USER
);
1142 $link = self
::userLink( $userId, $userText )
1143 . self
::userToolLinks( $userId, $userText );
1145 $link = wfMessage( 'rev-deleted-user' )->escaped();
1147 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
1148 return ' <span class="history-deleted">' . $link . '</span>';
1154 * This function is called by all recent changes variants, by the page history,
1155 * and by the user contributions list. It is responsible for formatting edit
1156 * summaries. It escapes any HTML in the summary, but adds some CSS to format
1157 * auto-generated comments (from section editing) and formats [[wikilinks]].
1159 * @author Erik Moeller <moeller@scireview.de>
1160 * @since 1.16.3. $wikiId added in 1.26
1162 * Note: there's not always a title to pass to this function.
1163 * Since you can't set a default parameter for a reference, I've turned it
1164 * temporarily to a value pass. Should be adjusted further. --brion
1166 * @param string $comment
1167 * @param Title|null $title Title object (to generate link to the section in autocomment)
1169 * @param bool $local Whether section links should refer to local page
1170 * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to.
1171 * For use with external changes.
1173 * @return mixed|string
1175 public static function formatComment(
1176 $comment, $title = null, $local = false, $wikiId = null
1178 # Sanitize text a bit:
1179 $comment = str_replace( "\n", " ", $comment );
1180 # Allow HTML entities (for bug 13815)
1181 $comment = Sanitizer
::escapeHtmlAllowEntities( $comment );
1183 # Render autocomments and make links:
1184 $comment = self
::formatAutocomments( $comment, $title, $local, $wikiId );
1185 $comment = self
::formatLinksInComment( $comment, $title, $local, $wikiId );
1191 * Converts autogenerated comments in edit summaries into section links.
1193 * The pattern for autogen comments is / * foo * /, which makes for
1195 * We look for all comments, match any text before and after the comment,
1196 * add a separator where needed and format the comment itself with CSS
1197 * Called by Linker::formatComment.
1199 * @param string $comment Comment text
1200 * @param Title|null $title An optional title object used to links to sections
1201 * @param bool $local Whether section links should refer to local page
1202 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1203 * as used by WikiMap.
1205 * @return string Formatted comment (wikitext)
1207 private static function formatAutocomments(
1208 $comment, $title = null, $local = false, $wikiId = null
1210 // @todo $append here is something of a hack to preserve the status
1211 // quo. Someone who knows more about bidi and such should decide
1212 // (1) what sane rendering even *is* for an LTR edit summary on an RTL
1213 // wiki, both when autocomments exist and when they don't, and
1214 // (2) what markup will make that actually happen.
1216 $comment = preg_replace_callback(
1217 // To detect the presence of content before or after the
1218 // auto-comment, we use capturing groups inside optional zero-width
1219 // assertions. But older versions of PCRE can't directly make
1220 // zero-width assertions optional, so wrap them in a non-capturing
1222 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
1223 function ( $match ) use ( $title, $local, $wikiId, &$append ) {
1226 // Ensure all match positions are defined
1227 $match +
= [ '', '', '', '' ];
1229 $pre = $match[1] !== '';
1231 $post = $match[3] !== '';
1235 'FormatAutocomments',
1236 [ &$comment, $pre, $auto, $post, $title, $local, $wikiId ]
1239 if ( $comment === null ) {
1243 # Remove links that a user may have manually put in the autosummary
1244 # This could be improved by copying as much of Parser::stripSectionName as desired.
1245 $section = str_replace( '[[:', '', $section );
1246 $section = str_replace( '[[', '', $section );
1247 $section = str_replace( ']]', '', $section );
1249 $section = Sanitizer
::normalizeSectionNameWhitespace( $section ); # bug 22784
1251 $sectionTitle = Title
::newFromText( '#' . $section );
1253 $sectionTitle = Title
::makeTitleSafe( $title->getNamespace(),
1254 $title->getDBkey(), $section );
1256 if ( $sectionTitle ) {
1257 $link = Linker
::makeCommentLink( $sectionTitle, $wgLang->getArrow(), $wikiId, 'noclasses' );
1263 # written summary $presep autocomment (summary /* section */)
1264 $pre = wfMessage( 'autocomment-prefix' )->inContentLanguage()->escaped();
1267 # autocomment $postsep written summary (/* section */ summary)
1268 $auto .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped();
1270 $auto = '<span class="autocomment">' . $auto . '</span>';
1271 $comment = $pre . $link . $wgLang->getDirMark()
1272 . '<span dir="auto">' . $auto;
1273 $append .= '</span>';
1279 return $comment . $append;
1283 * Formats wiki links and media links in text; all other wiki formatting
1286 * @since 1.16.3. $wikiId added in 1.26
1287 * @todo FIXME: Doesn't handle sub-links as in image thumb texts like the main parser
1289 * @param string $comment Text to format links in. WARNING! Since the output of this
1290 * function is html, $comment must be sanitized for use as html. You probably want
1291 * to pass $comment through Sanitizer::escapeHtmlAllowEntities() before calling
1293 * @param Title|null $title An optional title object used to links to sections
1294 * @param bool $local Whether section links should refer to local page
1295 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1296 * as used by WikiMap.
1300 public static function formatLinksInComment(
1301 $comment, $title = null, $local = false, $wikiId = null
1303 return preg_replace_callback(
1306 :? # ignore optional leading colon
1307 ([^\]|]+) # 1. link target; page names cannot include ] or |
1309 # 2. a pipe-separated substring; only the last is captured
1310 # Stop matching at | and ]] without relying on backtracking.
1314 ([^[]*) # 3. link trail (the text up until the next link)
1316 function ( $match ) use ( $title, $local, $wikiId ) {
1319 $medians = '(?:' . preg_quote( MWNamespace
::getCanonicalName( NS_MEDIA
), '/' ) . '|';
1320 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA
), '/' ) . '):';
1322 $comment = $match[0];
1324 # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
1325 if ( strpos( $match[1], '%' ) !== false ) {
1327 rawurldecode( $match[1] ),
1328 [ '<' => '<', '>' => '>' ]
1332 # Handle link renaming [[foo|text]] will show link as "text"
1333 if ( $match[2] != "" ) {
1340 if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1341 # Media link; trail not supported.
1342 $linkRegexp = '/\[\[(.*?)\]\]/';
1343 $title = Title
::makeTitleSafe( NS_FILE
, $submatch[1] );
1345 $thelink = Linker
::makeMediaLinkObj( $title, $text );
1348 # Other kind of link
1349 # Make sure its target is non-empty
1350 if ( isset( $match[1][0] ) && $match[1][0] == ':' ) {
1351 $match[1] = substr( $match[1], 1 );
1353 if ( $match[1] !== false && $match[1] !== '' ) {
1354 if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
1355 $trail = $submatch[1];
1359 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1360 list( $inside, $trail ) = Linker
::splitTrail( $trail );
1363 $linkTarget = Linker
::normalizeSubpageLink( $title, $match[1], $linkText );
1365 $target = Title
::newFromText( $linkTarget );
1367 if ( $target->getText() == '' && !$target->isExternal()
1368 && !$local && $title
1370 $newTarget = clone $title;
1371 $newTarget->setFragment( '#' . $target->getFragment() );
1372 $target = $newTarget;
1375 $thelink = Linker
::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
1380 // If the link is still valid, go ahead and replace it in!
1381 $comment = preg_replace(
1383 StringUtils
::escapeRegexReplacement( $thelink ),
1396 * Generates a link to the given Title
1398 * @note This is only public for technical reasons. It's not intended for use outside Linker.
1400 * @param Title $title
1401 * @param string $text
1402 * @param string|null $wikiId Id of the wiki to link to (if not the local wiki),
1403 * as used by WikiMap.
1404 * @param string|string[] $options See the $options parameter in Linker::link.
1406 * @return string HTML link
1408 public static function makeCommentLink(
1409 Title
$title, $text, $wikiId = null, $options = []
1411 if ( $wikiId !== null && !$title->isExternal() ) {
1412 $link = Linker
::makeExternalLink(
1413 WikiMap
::getForeignURL(
1415 $title->getPrefixedText(),
1416 $title->getFragment()
1419 /* escape = */ false // Already escaped
1422 $link = Linker
::link( $title, $text, [], [], $options );
1429 * @param Title $contextTitle
1430 * @param string $target
1431 * @param string $text
1434 public static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
1437 # :Foobar -- override special treatment of prefix (images, language links)
1438 # /Foobar -- convert to CurrentPage/Foobar
1439 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial and final / from text
1440 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1441 # ../Foobar -- convert to CurrentPage/Foobar,
1442 # (from CurrentPage/CurrentSubPage)
1443 # ../Foobar/ -- convert to CurrentPage/Foobar, use 'Foobar' as text
1444 # (from CurrentPage/CurrentSubPage)
1446 $ret = $target; # default return value is no change
1448 # Some namespaces don't allow subpages,
1449 # so only perform processing if subpages are allowed
1450 if ( $contextTitle && MWNamespace
::hasSubpages( $contextTitle->getNamespace() ) ) {
1451 $hash = strpos( $target, '#' );
1452 if ( $hash !== false ) {
1453 $suffix = substr( $target, $hash );
1454 $target = substr( $target, 0, $hash );
1459 $target = trim( $target );
1460 # Look at the first character
1461 if ( $target != '' && $target[0] === '/' ) {
1462 # / at end means we don't want the slash to be shown
1464 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1465 if ( $trailingSlashes ) {
1466 $noslash = $target = substr( $target, 1, -strlen( $m[0][0] ) );
1468 $noslash = substr( $target, 1 );
1471 $ret = $contextTitle->getPrefixedText() . '/' . trim( $noslash ) . $suffix;
1472 if ( $text === '' ) {
1473 $text = $target . $suffix;
1474 } # this might be changed for ugliness reasons
1476 # check for .. subpage backlinks
1478 $nodotdot = $target;
1479 while ( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1481 $nodotdot = substr( $nodotdot, 3 );
1483 if ( $dotdotcount > 0 ) {
1484 $exploded = explode( '/', $contextTitle->getPrefixedText() );
1485 if ( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1486 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1487 # / at the end means don't show full path
1488 if ( substr( $nodotdot, -1, 1 ) === '/' ) {
1489 $nodotdot = rtrim( $nodotdot, '/' );
1490 if ( $text === '' ) {
1491 $text = $nodotdot . $suffix;
1494 $nodotdot = trim( $nodotdot );
1495 if ( $nodotdot != '' ) {
1496 $ret .= '/' . $nodotdot;
1508 * Wrap a comment in standard punctuation and formatting if
1509 * it's non-empty, otherwise return empty string.
1511 * @since 1.16.3. $wikiId added in 1.26
1512 * @param string $comment
1513 * @param Title|null $title Title object (to generate link to section in autocomment) or null
1514 * @param bool $local Whether section links should refer to local page
1515 * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to.
1516 * For use with external changes.
1520 public static function commentBlock(
1521 $comment, $title = null, $local = false, $wikiId = null
1523 // '*' used to be the comment inserted by the software way back
1524 // in antiquity in case none was provided, here for backwards
1525 // compatibility, acc. to brion -ævar
1526 if ( $comment == '' ||
$comment == '*' ) {
1529 $formatted = self
::formatComment( $comment, $title, $local, $wikiId );
1530 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
1531 return " <span class=\"comment\">$formatted</span>";
1536 * Wrap and format the given revision's comment block, if the current
1537 * user is allowed to view it.
1540 * @param Revision $rev
1541 * @param bool $local Whether section links should refer to local page
1542 * @param bool $isPublic Show only if all users can see it
1543 * @return string HTML fragment
1545 public static function revComment( Revision
$rev, $local = false, $isPublic = false ) {
1546 if ( $rev->getComment( Revision
::RAW
) == "" ) {
1549 if ( $rev->isDeleted( Revision
::DELETED_COMMENT
) && $isPublic ) {
1550 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
1551 } elseif ( $rev->userCan( Revision
::DELETED_COMMENT
) ) {
1552 $block = self
::commentBlock( $rev->getComment( Revision
::FOR_THIS_USER
),
1553 $rev->getTitle(), $local );
1555 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
1557 if ( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
1558 return " <span class=\"history-deleted\">$block</span>";
1568 public static function formatRevisionSize( $size ) {
1570 $stxt = wfMessage( 'historyempty' )->escaped();
1572 $stxt = wfMessage( 'nbytes' )->numParams( $size )->escaped();
1573 $stxt = wfMessage( 'parentheses' )->rawParams( $stxt )->escaped();
1575 return "<span class=\"history-size\">$stxt</span>";
1579 * Add another level to the Table of Contents
1584 public static function tocIndent() {
1589 * Finish one or more sublevels on the Table of Contents
1595 public static function tocUnindent( $level ) {
1596 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level > 0 ?
$level : 0 );
1600 * parameter level defines if we are on an indentation level
1603 * @param string $anchor
1604 * @param string $tocline
1605 * @param string $tocnumber
1606 * @param string $level
1607 * @param string|bool $sectionIndex
1610 public static function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
1611 $classes = "toclevel-$level";
1612 if ( $sectionIndex !== false ) {
1613 $classes .= " tocsection-$sectionIndex";
1615 return "\n<li class=\"$classes\"><a href=\"#" .
1616 $anchor . '"><span class="tocnumber">' .
1617 $tocnumber . '</span> <span class="toctext">' .
1618 $tocline . '</span></a>';
1622 * End a Table Of Contents line.
1623 * tocUnindent() will be used instead if we're ending a line below
1628 public static function tocLineEnd() {
1633 * Wraps the TOC in a table and provides the hide/collapse javascript.
1636 * @param string $toc Html of the Table Of Contents
1637 * @param string|Language|bool $lang Language for the toc title, defaults to user language
1638 * @return string Full html of the TOC
1640 public static function tocList( $toc, $lang = false ) {
1641 $lang = wfGetLangObj( $lang );
1642 $title = wfMessage( 'toc' )->inLanguage( $lang )->escaped();
1644 return '<div id="toc" class="toc">'
1645 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1647 . "</ul>\n</div>\n";
1651 * Generate a table of contents from a section tree.
1653 * @since 1.16.3. $lang added in 1.17
1654 * @param array $tree Return value of ParserOutput::getSections()
1655 * @param string|Language|bool $lang Language for the toc title, defaults to user language
1656 * @return string HTML fragment
1658 public static function generateTOC( $tree, $lang = false ) {
1661 foreach ( $tree as $section ) {
1662 if ( $section['toclevel'] > $lastLevel ) {
1663 $toc .= self
::tocIndent();
1664 } elseif ( $section['toclevel'] < $lastLevel ) {
1665 $toc .= self
::tocUnindent(
1666 $lastLevel - $section['toclevel'] );
1668 $toc .= self
::tocLineEnd();
1671 $toc .= self
::tocLine( $section['anchor'],
1672 $section['line'], $section['number'],
1673 $section['toclevel'], $section['index'] );
1674 $lastLevel = $section['toclevel'];
1676 $toc .= self
::tocLineEnd();
1677 return self
::tocList( $toc, $lang );
1681 * Create a headline for content
1684 * @param int $level The level of the headline (1-6)
1685 * @param string $attribs Any attributes for the headline, starting with
1686 * a space and ending with '>'
1687 * This *must* be at least '>' for no attribs
1688 * @param string $anchor The anchor to give the headline (the bit after the #)
1689 * @param string $html Html for the text of the header
1690 * @param string $link HTML to add for the section edit link
1691 * @param bool|string $legacyAnchor A second, optional anchor to give for
1692 * backward compatibility (false to omit)
1694 * @return string HTML headline
1696 public static function makeHeadline( $level, $attribs, $anchor, $html,
1697 $link, $legacyAnchor = false
1699 $ret = "<h$level$attribs"
1700 . "<span class=\"mw-headline\" id=\"$anchor\">$html</span>"
1703 if ( $legacyAnchor !== false ) {
1704 $ret = "<div id=\"$legacyAnchor\"></div>$ret";
1710 * Split a link trail, return the "inside" portion and the remainder of the trail
1711 * as a two-element array
1712 * @param string $trail
1715 static function splitTrail( $trail ) {
1717 $regex = $wgContLang->linkTrail();
1719 if ( $trail !== '' ) {
1721 if ( preg_match( $regex, $trail, $m ) ) {
1726 return [ $inside, $trail ];
1730 * Generate a rollback link for a given revision. Currently it's the
1731 * caller's responsibility to ensure that the revision is the top one. If
1732 * it's not, of course, the user will get an error message.
1734 * If the calling page is called with the parameter &bot=1, all rollback
1735 * links also get that parameter. It causes the edit itself and the rollback
1736 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1737 * changes, so this allows sysops to combat a busy vandal without bothering
1740 * If the option verify is set this function will return the link only in case the
1741 * revision can be reverted. Please note that due to performance limitations
1742 * it might be assumed that a user isn't the only contributor of a page while
1743 * (s)he is, which will lead to useless rollback links. Furthermore this wont
1744 * work if $wgShowRollbackEditCount is disabled, so this can only function
1745 * as an additional check.
1747 * If the option noBrackets is set the rollback link wont be enclosed in "[]".
1749 * See the "mediawiki.page.rollback" module for the client-side handling of this link.
1751 * @since 1.16.3. $context added in 1.20. $options added in 1.21
1753 * @param Revision $rev
1754 * @param IContextSource $context Context to use or null for the main context.
1755 * @param array $options
1758 public static function generateRollback( $rev, IContextSource
$context = null,
1759 $options = [ 'verify' ]
1761 if ( $context === null ) {
1762 $context = RequestContext
::getMain();
1766 if ( in_array( 'verify', $options, true ) ) {
1767 $editCount = self
::getRollbackEditCount( $rev, true );
1768 if ( $editCount === false ) {
1773 $inner = self
::buildRollbackLink( $rev, $context, $editCount );
1775 if ( !in_array( 'noBrackets', $options, true ) ) {
1776 $inner = $context->msg( 'brackets' )->rawParams( $inner )->escaped();
1779 $context->getOutput()->addModules( 'mediawiki.page.rollback' );
1781 return '<span class="mw-rollback-link">' . $inner . '</span>';
1785 * This function will return the number of revisions which a rollback
1786 * would revert and, if $verify is set it will verify that a revision
1787 * can be reverted (that the user isn't the only contributor and the
1788 * revision we might rollback to isn't deleted). These checks can only
1789 * function as an additional check as this function only checks against
1790 * the last $wgShowRollbackEditCount edits.
1792 * Returns null if $wgShowRollbackEditCount is disabled or false if $verify
1793 * is set and the user is the only contributor of the page.
1795 * @param Revision $rev
1796 * @param bool $verify Try to verify that this revision can really be rolled back
1797 * @return int|bool|null
1799 public static function getRollbackEditCount( $rev, $verify ) {
1800 global $wgShowRollbackEditCount;
1801 if ( !is_int( $wgShowRollbackEditCount ) ||
!$wgShowRollbackEditCount > 0 ) {
1802 // Nothing has happened, indicate this by returning 'null'
1806 $dbr = wfGetDB( DB_SLAVE
);
1808 // Up to the value of $wgShowRollbackEditCount revisions are counted
1809 $res = $dbr->select(
1811 [ 'rev_user_text', 'rev_deleted' ],
1812 // $rev->getPage() returns null sometimes
1813 [ 'rev_page' => $rev->getTitle()->getArticleID() ],
1816 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
1817 'ORDER BY' => 'rev_timestamp DESC',
1818 'LIMIT' => $wgShowRollbackEditCount +
1
1824 foreach ( $res as $row ) {
1825 if ( $rev->getUserText( Revision
::RAW
) != $row->rev_user_text
) {
1827 ( $row->rev_deleted
& Revision
::DELETED_TEXT
1828 ||
$row->rev_deleted
& Revision
::DELETED_USER
1830 // If the user or the text of the revision we might rollback
1831 // to is deleted in some way we can't rollback. Similar to
1832 // the sanity checks in WikiPage::commitRollback.
1841 if ( $verify && $editCount <= $wgShowRollbackEditCount && !$moreRevs ) {
1842 // We didn't find at least $wgShowRollbackEditCount revisions made by the current user
1843 // and there weren't any other revisions. That means that the current user is the only
1844 // editor, so we can't rollback
1851 * Build a raw rollback link, useful for collections of "tool" links
1853 * @since 1.16.3. $context added in 1.20. $editCount added in 1.21
1854 * @param Revision $rev
1855 * @param IContextSource|null $context Context to use or null for the main context.
1856 * @param int $editCount Number of edits that would be reverted
1857 * @return string HTML fragment
1859 public static function buildRollbackLink( $rev, IContextSource
$context = null,
1862 global $wgShowRollbackEditCount, $wgMiserMode;
1864 // To config which pages are affected by miser mode
1865 $disableRollbackEditCountSpecialPage = [ 'Recentchanges', 'Watchlist' ];
1867 if ( $context === null ) {
1868 $context = RequestContext
::getMain();
1871 $title = $rev->getTitle();
1873 'action' => 'rollback',
1874 'from' => $rev->getUserText(),
1877 'data-mw' => 'interface',
1878 'title' => $context->msg( 'tooltip-rollback' )->text(),
1880 $options = [ 'known', 'noclasses' ];
1882 if ( $context->getRequest()->getBool( 'bot' ) ) {
1883 $query['bot'] = '1';
1884 $query['hidediff'] = '1'; // bug 15999
1887 $disableRollbackEditCount = false;
1888 if ( $wgMiserMode ) {
1889 foreach ( $disableRollbackEditCountSpecialPage as $specialPage ) {
1890 if ( $context->getTitle()->isSpecial( $specialPage ) ) {
1891 $disableRollbackEditCount = true;
1897 if ( !$disableRollbackEditCount
1898 && is_int( $wgShowRollbackEditCount )
1899 && $wgShowRollbackEditCount > 0
1901 if ( !is_numeric( $editCount ) ) {
1902 $editCount = self
::getRollbackEditCount( $rev, false );
1905 if ( $editCount > $wgShowRollbackEditCount ) {
1906 $html = $context->msg( 'rollbacklinkcount-morethan' )
1907 ->numParams( $wgShowRollbackEditCount )->parse();
1909 $html = $context->msg( 'rollbacklinkcount' )->numParams( $editCount )->parse();
1912 return self
::link( $title, $html, $attrs, $query, $options );
1914 $html = $context->msg( 'rollbacklink' )->escaped();
1915 return self
::link( $title, $html, $attrs, $query, $options );
1920 * Returns HTML for the "templates used on this page" list.
1922 * Make an HTML list of templates, and then add a "More..." link at
1923 * the bottom. If $more is null, do not add a "More..." link. If $more
1924 * is a Title, make a link to that title and use it. If $more is a string,
1925 * directly paste it in as the link (escaping needs to be done manually).
1926 * Finally, if $more is a Message, call toString().
1928 * @since 1.16.3. $more added in 1.21
1929 * @param Title[] $templates Array of templates
1930 * @param bool $preview Whether this is for a preview
1931 * @param bool $section Whether this is for a section edit
1932 * @param Title|Message|string|null $more An escaped link for "More..." of the templates
1933 * @return string HTML output
1935 public static function formatTemplates( $templates, $preview = false,
1936 $section = false, $more = null
1941 if ( count( $templates ) > 0 ) {
1942 # Do a batch existence check
1943 $batch = new LinkBatch
;
1944 foreach ( $templates as $title ) {
1945 $batch->addObj( $title );
1949 # Construct the HTML
1950 $outText = '<div class="mw-templatesUsedExplanation">';
1952 $outText .= wfMessage( 'templatesusedpreview' )->numParams( count( $templates ) )
1954 } elseif ( $section ) {
1955 $outText .= wfMessage( 'templatesusedsection' )->numParams( count( $templates ) )
1958 $outText .= wfMessage( 'templatesused' )->numParams( count( $templates ) )
1961 $outText .= "</div><ul>\n";
1963 usort( $templates, 'Title::compare' );
1964 foreach ( $templates as $titleObj ) {
1966 $restrictions = $titleObj->getRestrictions( 'edit' );
1967 if ( $restrictions ) {
1968 // Check backwards-compatible messages
1970 if ( $restrictions === [ 'sysop' ] ) {
1971 $msg = wfMessage( 'template-protected' );
1972 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
1973 $msg = wfMessage( 'template-semiprotected' );
1975 if ( $msg && !$msg->isDisabled() ) {
1976 $protected = $msg->parse();
1978 // Construct the message from restriction-level-*
1979 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
1981 foreach ( $restrictions as $r ) {
1982 $msgs[] = wfMessage( "restriction-level-$r" )->parse();
1984 $protected = wfMessage( 'parentheses' )
1985 ->rawParams( $wgLang->commaList( $msgs ) )->escaped();
1988 if ( $titleObj->quickUserCan( 'edit' ) ) {
1989 $editLink = self
::link(
1991 wfMessage( 'editlink' )->escaped(),
1993 [ 'action' => 'edit' ]
1996 $editLink = self
::link(
1998 wfMessage( 'viewsourcelink' )->escaped(),
2000 [ 'action' => 'edit' ]
2003 $outText .= '<li>' . self
::link( $titleObj )
2004 . wfMessage( 'word-separator' )->escaped()
2005 . wfMessage( 'parentheses' )->rawParams( $editLink )->escaped()
2006 . wfMessage( 'word-separator' )->escaped()
2007 . $protected . '</li>';
2010 if ( $more instanceof Title
) {
2011 $outText .= '<li>' . self
::link( $more, wfMessage( 'moredotdotdot' ) ) . '</li>';
2012 } elseif ( $more ) {
2013 $outText .= "<li>$more</li>";
2016 $outText .= '</ul>';
2022 * Returns HTML for the "hidden categories on this page" list.
2025 * @param array $hiddencats Array of hidden categories from Article::getHiddenCategories
2027 * @return string HTML output
2029 public static function formatHiddenCategories( $hiddencats ) {
2032 if ( count( $hiddencats ) > 0 ) {
2033 # Construct the HTML
2034 $outText = '<div class="mw-hiddenCategoriesExplanation">';
2035 $outText .= wfMessage( 'hiddencategories' )->numParams( count( $hiddencats ) )->parseAsBlock();
2036 $outText .= "</div><ul>\n";
2038 foreach ( $hiddencats as $titleObj ) {
2039 # If it's hidden, it must exist - no need to check with a LinkBatch
2041 . self
::link( $titleObj, null, [], [], 'known' )
2044 $outText .= '</ul>';
2050 * Format a size in bytes for output, using an appropriate
2051 * unit (B, KB, MB or GB) according to the magnitude in question
2054 * @param int $size Size to format
2057 public static function formatSize( $size ) {
2059 return htmlspecialchars( $wgLang->formatSize( $size ) );
2063 * Given the id of an interface element, constructs the appropriate title
2064 * attribute from the system messages. (Note, this is usually the id but
2065 * isn't always, because sometimes the accesskey needs to go on a different
2066 * element than the id, for reverse-compatibility, etc.)
2068 * @since 1.16.3 $msgParams added in 1.27
2069 * @param string $name Id of the element, minus prefixes.
2070 * @param string|null $options Null or the string 'withaccess' to add an access-
2072 * @param array $msgParams Parameters to pass to the message
2074 * @return string Contents of the title attribute (which you must HTML-
2075 * escape), or false for no title attribute
2077 public static function titleAttrib( $name, $options = null, array $msgParams = [] ) {
2078 $message = wfMessage( "tooltip-$name", $msgParams );
2079 if ( !$message->exists() ) {
2082 $tooltip = $message->text();
2083 # Compatibility: formerly some tooltips had [alt-.] hardcoded
2084 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
2085 # Message equal to '-' means suppress it.
2086 if ( $tooltip == '-' ) {
2091 if ( $options == 'withaccess' ) {
2092 $accesskey = self
::accesskey( $name );
2093 if ( $accesskey !== false ) {
2094 // Should be build the same as in jquery.accessKeyLabel.js
2095 if ( $tooltip === false ||
$tooltip === '' ) {
2096 $tooltip = wfMessage( 'brackets', $accesskey )->text();
2098 $tooltip .= wfMessage( 'word-separator' )->text();
2099 $tooltip .= wfMessage( 'brackets', $accesskey )->text();
2107 public static $accesskeycache;
2110 * Given the id of an interface element, constructs the appropriate
2111 * accesskey attribute from the system messages. (Note, this is usually
2112 * the id but isn't always, because sometimes the accesskey needs to go on
2113 * a different element than the id, for reverse-compatibility, etc.)
2116 * @param string $name Id of the element, minus prefixes.
2117 * @return string Contents of the accesskey attribute (which you must HTML-
2118 * escape), or false for no accesskey attribute
2120 public static function accesskey( $name ) {
2121 if ( isset( self
::$accesskeycache[$name] ) ) {
2122 return self
::$accesskeycache[$name];
2125 $message = wfMessage( "accesskey-$name" );
2127 if ( !$message->exists() ) {
2130 $accesskey = $message->plain();
2131 if ( $accesskey === '' ||
$accesskey === '-' ) {
2132 # @todo FIXME: Per standard MW behavior, a value of '-' means to suppress the
2133 # attribute, but this is broken for accesskey: that might be a useful
2139 self
::$accesskeycache[$name] = $accesskey;
2140 return self
::$accesskeycache[$name];
2144 * Get a revision-deletion link, or disabled link, or nothing, depending
2145 * on user permissions & the settings on the revision.
2147 * Will use forward-compatible revision ID in the Special:RevDelete link
2148 * if possible, otherwise the timestamp-based ID which may break after
2152 * @param Revision $rev
2153 * @param Title $title
2154 * @return string HTML fragment
2156 public static function getRevDeleteLink( User
$user, Revision
$rev, Title
$title ) {
2157 $canHide = $user->isAllowed( 'deleterevision' );
2158 if ( !$canHide && !( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
2162 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
2163 return Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
2165 if ( $rev->getId() ) {
2166 // RevDelete links using revision ID are stable across
2167 // page deletion and undeletion; use when possible.
2169 'type' => 'revision',
2170 'target' => $title->getPrefixedDBkey(),
2171 'ids' => $rev->getId()
2174 // Older deleted entries didn't save a revision ID.
2175 // We have to refer to these by timestamp, ick!
2177 'type' => 'archive',
2178 'target' => $title->getPrefixedDBkey(),
2179 'ids' => $rev->getTimestamp()
2182 return Linker
::revDeleteLink( $query,
2183 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
2188 * Creates a (show/hide) link for deleting revisions/log entries
2190 * @param array $query Query parameters to be passed to link()
2191 * @param bool $restricted Set to true to use a "<strong>" instead of a "<span>"
2192 * @param bool $delete Set to true to use (show/hide) rather than (show)
2194 * @return string HTML "<a>" link to Special:Revisiondelete, wrapped in a
2195 * span to allow for customization of appearance with CSS
2197 public static function revDeleteLink( $query = [], $restricted = false, $delete = true ) {
2198 $sp = SpecialPage
::getTitleFor( 'Revisiondelete' );
2199 $msgKey = $delete ?
'rev-delundel' : 'rev-showdeleted';
2200 $html = wfMessage( $msgKey )->escaped();
2201 $tag = $restricted ?
'strong' : 'span';
2202 $link = self
::link( $sp, $html, [], $query, [ 'known', 'noclasses' ] );
2205 [ 'class' => 'mw-revdelundel-link' ],
2206 wfMessage( 'parentheses' )->rawParams( $link )->escaped()
2211 * Creates a dead (show/hide) link for deleting revisions/log entries
2214 * @param bool $delete Set to true to use (show/hide) rather than (show)
2216 * @return string HTML text wrapped in a span to allow for customization
2217 * of appearance with CSS
2219 public static function revDeleteLinkDisabled( $delete = true ) {
2220 $msgKey = $delete ?
'rev-delundel' : 'rev-showdeleted';
2221 $html = wfMessage( $msgKey )->escaped();
2222 $htmlParentheses = wfMessage( 'parentheses' )->rawParams( $html )->escaped();
2223 return Xml
::tags( 'span', [ 'class' => 'mw-revdelundel-link' ], $htmlParentheses );
2226 /* Deprecated methods */
2229 * Returns the attributes for the tooltip and access key.
2231 * @since 1.16.3. $msgParams introduced in 1.27
2232 * @param string $name
2233 * @param array $msgParams Params for constructing the message
2237 public static function tooltipAndAccesskeyAttribs( $name, array $msgParams = [] ) {
2238 # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2239 # no attribute" instead of "output '' as value for attribute", this
2240 # would be three lines.
2242 'title' => self
::titleAttrib( $name, 'withaccess', $msgParams ),
2243 'accesskey' => self
::accesskey( $name )
2245 if ( $attribs['title'] === false ) {
2246 unset( $attribs['title'] );
2248 if ( $attribs['accesskey'] === false ) {
2249 unset( $attribs['accesskey'] );
2255 * Returns raw bits of HTML, use titleAttrib()
2257 * @param string $name
2258 * @param array|null $options
2259 * @return null|string
2261 public static function tooltip( $name, $options = null ) {
2262 # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2263 # no attribute" instead of "output '' as value for attribute", this
2264 # would be two lines.
2265 $tooltip = self
::titleAttrib( $name, $options );
2266 if ( $tooltip === false ) {
2269 return Xml
::expandAttributes( [