element, i.e., * the link text. This is raw HTML and will not be escaped. If null, * defaults to the prefixed text of the LinkTarget; or if the LinkTarget is just a * fragment, the contents of the fragment. * @param array $customAttribs A key => value array of extra HTML attributes, * such as title and class. (href is ignored.) Classes will be * merged with the default classes, while other attributes will replace * default attributes. All passed attribute values will be HTML-escaped. * A false attribute value means to suppress that attribute. * @param array $query The query string to append to the URL * you're linking to, in key => value array form. Query keys and values * will be URL-encoded. * @param string|array $options String or array of strings: * 'known': Page is known to exist, so don't check if it does. * 'broken': Page is known not to exist, so don't check if it does. * 'noclasses': Don't add any classes automatically (includes "new", * "stub", "mw-redirect", "extiw"). Only use the class attribute * provided, if any, so you get a simple blue link with no funny i- * cons. * 'forcearticlepath': Use the article path always, even with a querystring. * Has compatibility issues on some setups, so avoid wherever possible. * 'http': Force a full URL with http:// as the scheme. * 'https': Force a full URL with https:// as the scheme. * @return string HTML attribute */ public static function link( $target, $html = null, $customAttribs = [], $query = [], $options = [] ) { if ( !$target instanceof LinkTarget ) { wfWarn( __METHOD__ . ': Requires $target to be a LinkTarget object.', 2 ); return "$html"; } $services = MediaWikiServices::getInstance(); $options = (array)$options; if ( $options ) { // Custom options, create new LinkRenderer $linkRenderer = $services->getLinkRendererFactory() ->createFromLegacyOptions( $options ); } else { $linkRenderer = $services->getLinkRenderer(); } if ( $html !== null ) { $text = new HtmlArmor( $html ); } else { $text = null; } if ( in_array( 'known', $options, true ) ) { return $linkRenderer->makeKnownLink( $target, $text, $customAttribs, $query ); } if ( in_array( 'broken', $options, true ) ) { return $linkRenderer->makeBrokenLink( $target, $text, $customAttribs, $query ); } if ( in_array( 'noclasses', $options, true ) ) { return $linkRenderer->makePreloadedLink( $target, $text, '', $customAttribs, $query ); } return $linkRenderer->makeLink( $target, $text, $customAttribs, $query ); } /** * Identical to link(), except $options defaults to 'known'. * * @since 1.16.3 * @deprecated since 1.28, use MediaWiki\Linker\LinkRenderer instead * @see Linker::link * @param LinkTarget $target * @param string|null $html * @param array $customAttribs * @param array $query * @param string|array $options * @return string */ public static function linkKnown( $target, $html = null, $customAttribs = [], $query = [], $options = [ 'known' ] ) { return self::link( $target, $html, $customAttribs, $query, $options ); } /** * Make appropriate markup for a link to the current article. This is since * MediaWiki 1.29.0 rendered as an tag without an href and with a class * showing the link text. The calling sequence is the same as for the other * make*LinkObj static functions, but $query is not used. * * @since 1.16.3 * @param LinkTarget $nt * @param string $html * @param string $query * @param string $trail * @param string $prefix * * @return string */ public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) { $nt = Title::newFromLinkTarget( $nt ); $ret = "{$prefix}{$html}{$trail}"; if ( !Hooks::runner()->onSelfLinkBegin( $nt, $html, $trail, $prefix, $ret ) ) { return $ret; } if ( $html == '' ) { $html = htmlspecialchars( $nt->getPrefixedText() ); } [ $inside, $trail ] = self::splitTrail( $trail ); return "{$prefix}{$html}{$inside}{$trail}"; } /** * Get a message saying that an invalid title was encountered. * This should be called after a method like Title::makeTitleSafe() returned * a value indicating that the title object is invalid. * * @param IContextSource $context Context to use to get the messages * @param int $namespace Namespace number * @param string $title Text of the title, without the namespace part * @return string */ public static function getInvalidTitleDescription( IContextSource $context, $namespace, $title ) { // First we check whether the namespace exists or not. if ( MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $namespace ) ) { if ( $namespace == NS_MAIN ) { $name = $context->msg( 'blanknamespace' )->text(); } else { $name = MediaWikiServices::getInstance()->getContentLanguage()-> getFormattedNsText( $namespace ); } return $context->msg( 'invalidtitle-knownnamespace', $namespace, $name, $title )->text(); } return $context->msg( 'invalidtitle-unknownnamespace', $namespace, $title )->text(); } /** * @since 1.16.3 * @deprecated since 1.35, use LinkRenderer::normalizeTarget() * @param LinkTarget $target * @return LinkTarget */ public static function normaliseSpecialPage( LinkTarget $target ) { wfDeprecated( __METHOD__, '1.35' ); $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); return $linkRenderer->normalizeTarget( $target ); } /** * Returns the filename part of an url. * Used as alternative text for external images. * * @param string $url * * @return string */ private static function fnamePart( $url ) { $basename = strrchr( $url, '/' ); if ( $basename === false ) { $basename = $url; } else { $basename = substr( $basename, 1 ); } return $basename; } /** * Return the code for images which were added via external links, * via Parser::maybeMakeExternalImage(). * * @since 1.16.3 * @param string $url * @param string $alt * * @return string */ public static function makeExternalImage( $url, $alt = '' ) { if ( $alt == '' ) { $alt = self::fnamePart( $url ); } $img = ''; $success = Hooks::runner()->onLinkerMakeExternalImage( $url, $alt, $img ); if ( !$success ) { wfDebug( "Hook LinkerMakeExternalImage changed the output of external image " . "with url {$url} and alt text {$alt} to {$img}" ); return $img; } return Html::element( 'img', [ 'src' => $url, 'alt' => $alt ] ); } /** * Given parameters derived from [[Image:Foo|options...]], generate the * HTML that that syntax inserts in the page. * * @param Parser $parser * @param LinkTarget $title LinkTarget object of the file (not the currently viewed page) * @param File|false $file File object, or false if it doesn't exist * @param array $frameParams Associative array of parameters external to the media handler. * Boolean parameters are indicated by presence or absence, the value is arbitrary and * will often be false. * thumbnail If present, downscale and frame * manualthumb Image name to use as a thumbnail, instead of automatic scaling * framed Shows image in original size in a frame * frameless Downscale but don't frame * upright If present, tweak default sizes for portrait orientation * upright_factor Fudge factor for "upright" tweak (default 0.75) * border If present, show a border around the image * align Horizontal alignment (left, right, center, none) * valign Vertical alignment (baseline, sub, super, top, text-top, middle, * bottom, text-bottom) * alt Alternate text for image (i.e. alt attribute). Plain text. * title Used for tooltips if caption isn't visible. * class HTML for image classes. Plain text. * caption HTML for image caption. * link-url URL to link to * link-title LinkTarget object to link to * link-target Value for the target attribute, only with link-url * no-link Boolean, suppress description link * * @param array $handlerParams Associative array of media handler parameters, to be passed * to transform(). Typical keys are "width" and "page". * targetlang (optional) Target language code, see Parser::getTargetLanguage() * @param string|false $time Timestamp of the file, set as false for current * @param string $query Query params for desc url * @param int|null $widthOption Used by the parser to remember the user preference thumbnailsize * @since 1.20 * @return string HTML for an image, with links, wrappers, etc. */ public static function makeImageLink( Parser $parser, LinkTarget $title, $file, $frameParams = [], $handlerParams = [], $time = false, $query = '', $widthOption = null ) { $title = Title::newFromLinkTarget( $title ); $res = null; $dummy = new DummyLinker; if ( !Hooks::runner()->onImageBeforeProduceHTML( $dummy, $title, // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args $file, $frameParams, $handlerParams, $time, $res, // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args $parser, $query, $widthOption ) ) { return $res; } if ( $file && !$file->allowInlineDisplay() ) { wfDebug( __METHOD__ . ': ' . $title->getPrefixedDBkey() . ' does not allow inline display' ); return self::link( $title ); } // Clean up parameters $page = $handlerParams['page'] ?? false; if ( !isset( $frameParams['align'] ) ) { $frameParams['align'] = ''; } if ( !isset( $frameParams['alt'] ) ) { $frameParams['alt'] = ''; } if ( !isset( $frameParams['title'] ) ) { $frameParams['title'] = ''; } if ( !isset( $frameParams['class'] ) ) { $frameParams['class'] = ''; } $services = MediaWikiServices::getInstance(); $config = $services->getMainConfig(); $enableLegacyMediaDOM = $config->get( MainConfigNames::ParserEnableLegacyMediaDOM ); $classes = []; if ( !isset( $handlerParams['width'] ) && !isset( $frameParams['manualthumb'] ) && !isset( $frameParams['framed'] ) ) { $classes[] = 'mw-default-size'; } $prefix = $postfix = ''; if ( $enableLegacyMediaDOM ) { if ( $frameParams['align'] == 'center' ) { $prefix = '