Use appendJsConfigVar to track related page titles

This simplifies the codebase by avoiding some unnecesary custom hooks,
and also improves Parsoid compatibility.

We look for old extensiondata coming from cached pages for
compatibility, but that code can be removed once the ParserCache expires
(T371421) for further simplification.

Bug: T263772
Change-Id: I843a920a0476290a8b0efd5eba4d8d44777d27d0
This commit is contained in:
C. Scott Ananian 2024-07-30 15:02:44 -04:00
parent 1d6d7a0273
commit 28e4254be5
3 changed files with 11 additions and 34 deletions

View file

@ -24,7 +24,6 @@
"Hooks": {
"ParserFirstCallInit": "main",
"OutputPageParserOutput": "main",
"MakeGlobalVariablesScript": "main",
"BeforePageDisplay": "main",
"ResourceLoaderGetConfigVars": "main",
"SkinAfterContent": "main"

View file

@ -10,7 +10,6 @@ use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\SkinAfterContentHook;
use MediaWiki\Html\Html;
use MediaWiki\Output\Hook\BeforePageDisplayHook;
use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
use MediaWiki\Output\Hook\OutputPageParserOutputHook;
use MediaWiki\Output\OutputPage;
use MediaWiki\Parser\Parser;
@ -22,7 +21,6 @@ use Skin;
class Hooks implements
ParserFirstCallInitHook,
OutputPageParserOutputHook,
MakeGlobalVariablesScriptHook,
BeforePageDisplayHook,
ResourceLoaderGetConfigVarsHook,
SkinAfterContentHook
@ -38,22 +36,6 @@ class Hooks implements
$this->disambiguatorLookup = $disambiguatorLookup;
}
/**
* Handler for the <code>MakeGlobalVariablesScript</code> hook.
*
* Sets the value of the <code>wgRelatedArticles</code> global variable
* to the list of related articles in the cached parser output.
*
* @param array &$vars variables to be added into the output of OutputPage::headElement.
* @param OutputPage $out OutputPage instance calling the hook
*/
public function onMakeGlobalVariablesScript( &$vars, $out ): void {
$editorCuratedPages = $out->getProperty( 'RelatedArticles' );
if ( $editorCuratedPages ) {
$vars['wgRelatedArticles'] = $editorCuratedPages;
}
}
/**
* Uses the Disambiguator extension to test whether the page is a disambiguation page.
*
@ -191,17 +173,11 @@ class Hooks implements
*/
public static function onFuncRelated( Parser $parser, ...$args ) {
$parserOutput = $parser->getOutput();
$relatedPages = $parserOutput->getExtensionData( 'RelatedArticles' );
if ( !$relatedPages ) {
$relatedPages = [];
}
// Add all the related pages passed by the parser function
// {{#related:Test with read more|Foo|Bar}}
foreach ( $args as $relatedPage ) {
$relatedPages[] = $relatedPage;
$parserOutput->appendJsConfigVar( 'wgRelatedArticles', $relatedPage );
}
$parserOutput->setExtensionData( 'RelatedArticles', $relatedPages );
return '';
}
@ -213,15 +189,14 @@ class Hooks implements
* The list of related pages will be retrieved using
* <code>ParserOutput#getExtensionData</code>.
*
* @param OutputPage $out the OutputPage object
* @param OutputPage $outputPage the OutputPage object
* @param ParserOutput $parserOutput ParserOutput object
*/
public function onOutputPageParserOutput( $out, $parserOutput ): void {
$related = $parserOutput->getExtensionData( 'RelatedArticles' );
if ( $related ) {
$out->setProperty( 'RelatedArticles', $related );
}
public function onOutputPageParserOutput( $outputPage, $parserOutput ): void {
// Backwards-compatibility with old cached content (T371421)
// This hook can be removed once this is no longer needed.
$related = $parserOutput->getExtensionData( 'RelatedArticles' ) ?? [];
$outputPage->addJsConfigVars( 'wgRelatedArticlesCompat', $related );
}
/**

View file

@ -10,7 +10,10 @@ const relatedPages = new RelatedPagesGateway(
}
} ),
mw.config.get( 'wgPageName' ),
mw.config.get( 'wgRelatedArticles' ),
Object.keys( mw.config.get( 'wgRelatedArticles' ) || Object.create( null ) )
// Backward compatibility with old parser cache contents
// This .concat can be removed once no longer needed (T371421)
.concat( mw.config.get( 'wgRelatedArticlesCompat' ) ),
data.useCirrusSearch,
data.onlyUseCirrusSearch,
data.descriptionSource