wiki.techinc.nl/includes/parser/Hook/ParserFetchTemplateHook.php
C. Scott Ananian 0676256ccc parser: new BeforeParserFetchTemplateRevisionRecord hook
This new hook provides for the use case in T47096 (allowing the
Translate extension to transclude a page from another language) by
adding a new hook which would let us deprecate and replace two awkward
legacy hooks (one with an embarrassing capitalization issue).  The new
hook is a little more tightly scoped in terms of what it allows and
gives access to, and it uses the new RevisionRecord API.

In addition, the new hook uses LinkTarget instead of Title per
current best practices. (PageIdentity is not appropriate for
reasons documented at the hook invocation site.)

The original BeforeParserFetchTemplateAndtitle (sic) hook allowed
redirecting the revision id of a template inclusion, but not the
title. The only known current use is Extension:ApprovedRevs; the
FlaggedRevs extension replaces the entire function using
ParserOptions::setCurrentRevisionRecordCallback().

Extension:Translate would like to redirect the title as well, possibly
recursively (for a limited number of hops) to handle fallback
languages.  That is, when invoked on Foo/fr, including Template:Bar
would redirect to Template:Bar/fr -- and, if that doesn't exist, then
Template:Bar/fr would redirect to its fallback language, say
Template:Bar/en. It uses the top-level page title as context to set
the desired page language.  This would require 2 invocations of the
hook; we've set the recursion limit to 3 to provide a little bit
of future-proofing.

The hook added in this patch uses RevisionRecord instead of int
$rev_id, and thus can handle the case where the redirect is to a page
which doesn't exist (by setting the RevisionRecord to a
MutableRevisionRecord with the correct title and no main slot content)
in the fallback language case above.

The new hook deprecates BeforeParserFetchTemplateAndtitle and replaces
ParserFetchTemplate as well (deprecated in 1.35).  Code search:

https://codesearch.wmcloud.org/search/?q=BeforeParserFetchTemplateAndtitle&i=nope&files=&repos=

Bug: T47096
Change-Id: Ia5b5d339706ce4084c16948300e0e3418b11792e
2021-02-05 09:37:32 -05:00

32 lines
1,009 B
PHP

<?php
namespace MediaWiki\Hook;
use Parser;
use Revision;
use Title;
/**
* This is a hook handler interface, see docs/Hooks.md.
* Use the hook name "ParserFetchTemplate" to register handlers implementing this interface.
*
* @deprecated since 1.35
* @ingroup Hooks
*/
interface ParserFetchTemplateHook {
/**
* This hook is called when the parser fetches a template.
*
* @since 1.35
* @deprecated since 1.35, use BeforeParserFetchTemplateRevisionRecordHook
*
* @param Parser|bool $parser Parser object or false
* @param Title $title Title object of the template to be fetched
* @param Revision $rev Revision object of the template
* @param string|bool|null &$text Transclusion text of the template or false or null
* @param array &$deps Array of template dependencies with 'title', 'page_id', and
* 'rev_id' keys
* @return bool|void True or no return value to continue or false to abort
*/
public function onParserFetchTemplate( $parser, $title, $rev, &$text, &$deps );
}