手册:钩子/LinkBegin

From mediawiki.org
This page is a translated version of the page Manual:Hooks/LinkBegin and the translation is 26% complete.
LinkBegin
version 1.14.0 版可用
version 1.36.0 (Gerrit change 593874)版被移除
Used when generating internal and interwiki links in Linker::link(), before processing starts.
定义函数:
public static function onLinkBegin( $dummy, $target, &$html, &$customAttribs, &$query, &$options, &$ret ) { ... }
附加钩子: extension.json中:
{
	"Hooks": {
		"LinkBegin": "MediaWiki\\Extension\\MyExtension\\Hooks::onLinkBegin"
	}
}
调用自: 文件: linker/LinkRenderer.php
功能: runLegacyBeginHook
介面: LinkBeginHook.php

更多有关附加钩子的信息,请参见Manual:Hooks
有关使用此钩子的扩展示例,请参见Category:LinkBegin extensions/zh

警告

在MediaWiki 1.28中它被HtmlPageLinkRendererBegin 取代。 LinkBegin可认为已过时。

详情

Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters.

  • $dummy: used to be a skin, but that eliminated.
  • $target: the Title that the link is pointing to.
  • &$html
  • &$customAttribs: the HTML attributes (such as title and class; href is ignored) that the ‎<a> tag should have, in associative array form, with keys and values unescaped. Should be merged with default values, with a value of false meaning to suppress the attribute. Other attributes will replace default attributes.
  • &$query: the query string to add to the generated URL (the bit after the "?"), in associative array form, with keys and values unescaped. Query keys and values will be URL-encoded.
  • &$options: 选项。 Can include 'known', 'broken', 'noclasses', 'forcearticlepath', 'http', or 'https'.
  • &$ret: the value to return if your hook returns false.

$options的值

  • '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 icons.
  • '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.

示例

The following code will show all wikilinks as broken (i.e. as red links) by changing the $customAttribs class to "new":

$wgHooks['LinkBegin'][] = 'ExampleExtension::exampleExtensionLinkBegin';

class ExampleExtension {
	public static function exampleExtensionLinkBegin( $dummy, $target, &$html, &$customAttribs, &$query,
		&$options, &$ret ) {
		$customAttribs['class'] = 'new';
		return true;
	}
}

Another way to achieve the same result would be to change $ret instead of $customAttribs and return false, like this:

$wgHooks['LinkBegin'][] = 'ExampleExtension::exampleExtensionLinkBegin';

class ExampleExtension {
	public static function exampleExtensionLinkBegin( $dummy, $target, &$html, &$customAttribs, &$query,
		&$options, &$ret ) {
		$ret = Html::rawElement ( 'a', array ( 'href' => $target->getFullURL(), 'class' => 'new' ),
			$target->getPrefixedDBKey() );
		return false;
	}
}

Note that neither of these examples will have any effect on interwiki links. For that, you'll need to use LinkEnd . See the example.

參見