Switch to hook handlers and inject ConfigFactory
Moving hooks into a separate class, and using dependency injection for configuration. Due to hook interfaces being added in MW 1.35, this change also raises the MediaWiki requirement to >1.35.0. This patch is a part of my RemexHtml deriver chain (split into multiple patches to avoid a single commit altering almost the entirety of the codebase), which raises the floor to 1.38 later. There's not really a point in merging this if the rest of the patch chain is declined. Depends-On: I484feeb51beab0c2e06c9f958a1c15c40853b967 Change-Id: I73c61ce045dcf31ac1ca5888f1548de8fd8b56ff
This commit is contained in:
parent
15f2edbaee
commit
43eb47183e
|
@ -9,7 +9,7 @@
|
|||
"license-name": "GPL-2.0-or-later",
|
||||
"type": "other",
|
||||
"requires": {
|
||||
"MediaWiki": ">= 1.31.0"
|
||||
"MediaWiki": ">= 1.35.0"
|
||||
},
|
||||
"config": {
|
||||
"EnableMetaDescriptionFunctions": false
|
||||
|
@ -28,10 +28,18 @@
|
|||
"i18n"
|
||||
]
|
||||
},
|
||||
"HookHandlers": {
|
||||
"Description2": {
|
||||
"class": "MediaWiki\\Extension\\Description2\\Hooks",
|
||||
"services": [
|
||||
"ConfigFactory"
|
||||
]
|
||||
}
|
||||
},
|
||||
"Hooks": {
|
||||
"OutputPageParserOutput": "MediaWiki\\Extension\\Description2\\Description2::onOutputPageParserOutput",
|
||||
"ParserAfterTidy": "MediaWiki\\Extension\\Description2\\Description2::onParserAfterTidy",
|
||||
"ParserFirstCallInit": "MediaWiki\\Extension\\Description2\\Description2::onParserFirstCallInit"
|
||||
"OutputPageParserOutput": "Description2",
|
||||
"ParserAfterTidy": "Description2",
|
||||
"ParserFirstCallInit": "Description2"
|
||||
},
|
||||
"manifest_version": 1
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@
|
|||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use OutputPage;
|
||||
use Parser;
|
||||
use ParserOutput;
|
||||
use PPFrame;
|
||||
|
||||
/**
|
||||
|
@ -41,57 +38,6 @@ class Description2 {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
|
||||
* @param Parser &$parser The parser.
|
||||
* @param string &$text The page text.
|
||||
* @return bool
|
||||
*/
|
||||
public static function onParserAfterTidy( Parser &$parser, &$text ) {
|
||||
$desc = '';
|
||||
|
||||
$pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
|
||||
$myText = preg_replace( $pattern, '', $text );
|
||||
|
||||
$paragraphs = [];
|
||||
if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
|
||||
foreach ( $paragraphs[0] as $paragraph ) {
|
||||
$paragraph = trim( strip_tags( $paragraph ) );
|
||||
if ( !$paragraph ) {
|
||||
continue;
|
||||
}
|
||||
$desc = $paragraph;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $desc ) {
|
||||
self::setDescription( $parser, $desc );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser &$parser The parser.
|
||||
* @return bool
|
||||
*/
|
||||
public static function onParserFirstCallInit( Parser &$parser ) {
|
||||
$config = MediaWikiServices::getInstance()
|
||||
->getConfigFactory()
|
||||
->makeConfig( 'Description2' );
|
||||
if ( !$config->get( 'EnableMetaDescriptionFunctions' ) ) {
|
||||
// Functions and tags are disabled
|
||||
return true;
|
||||
}
|
||||
$parser->setFunctionHook(
|
||||
'description2',
|
||||
[ static::class, 'parserFunctionCallback' ],
|
||||
Parser::SFH_OBJECT_ARGS
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser $parser The parser.
|
||||
* @param PPFrame $frame The frame.
|
||||
|
@ -103,24 +49,4 @@ class Description2 {
|
|||
self::setDescription( $parser, $desc );
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OutputPage &$out The output page to add the meta element to.
|
||||
* @param ParserOutput $parserOutput The parser output to get the description from.
|
||||
*/
|
||||
public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) {
|
||||
// Export the description from the main parser output into the OutputPage
|
||||
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
||||
// MW 1.38+
|
||||
$description = $parserOutput->getPageProperty( 'description' );
|
||||
} else {
|
||||
$description = $parserOutput->getProperty( 'description' );
|
||||
if ( $description === false ) {
|
||||
$description = null;
|
||||
}
|
||||
}
|
||||
if ( $description !== null ) {
|
||||
$out->addMeta( 'description', $description );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
106
includes/Hooks.php
Normal file
106
includes/Hooks.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
use Config;
|
||||
use ConfigFactory;
|
||||
use OutputPage;
|
||||
use Parser;
|
||||
use ParserOutput;
|
||||
|
||||
/**
|
||||
* Description2 – Adds meaningful description <meta> tag to MW pages and into the parser output
|
||||
*
|
||||
* @file
|
||||
* @ingroup Extensions
|
||||
* @author Daniel Friesen (http://danf.ca/mw/)
|
||||
* @copyright Copyright 2010 – Daniel Friesen
|
||||
* @license GPL-2.0-or-later
|
||||
* @link https://www.mediawiki.org/wiki/Extension:Description2 Documentation
|
||||
*/
|
||||
|
||||
class Hooks implements
|
||||
\MediaWiki\Hook\ParserAfterTidyHook,
|
||||
\MediaWiki\Hook\ParserFirstCallInitHook,
|
||||
\MediaWiki\Hook\OutputPageParserOutputHook
|
||||
{
|
||||
|
||||
private Config $config;
|
||||
|
||||
/**
|
||||
* @param ConfigFactory $configFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigFactory $configFactory
|
||||
) {
|
||||
$this->config = $configFactory->makeConfig( 'Description2' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
|
||||
* @param Parser $parser The parser.
|
||||
* @param string &$text The page text.
|
||||
* @return bool
|
||||
*/
|
||||
public function onParserAfterTidy( $parser, &$text ) {
|
||||
$desc = '';
|
||||
|
||||
$pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
|
||||
$myText = preg_replace( $pattern, '', $text );
|
||||
|
||||
$paragraphs = [];
|
||||
if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
|
||||
foreach ( $paragraphs[0] as $paragraph ) {
|
||||
$paragraph = trim( strip_tags( $paragraph ) );
|
||||
if ( !$paragraph ) {
|
||||
continue;
|
||||
}
|
||||
$desc = $paragraph;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $desc ) {
|
||||
Description2::setDescription( $parser, $desc );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser $parser The parser.
|
||||
* @return bool
|
||||
*/
|
||||
public function onParserFirstCallInit( $parser ) {
|
||||
if ( !$this->config->get( 'EnableMetaDescriptionFunctions' ) ) {
|
||||
// Functions and tags are disabled
|
||||
return true;
|
||||
}
|
||||
$parser->setFunctionHook(
|
||||
'description2',
|
||||
[ Description2::class, 'parserFunctionCallback' ],
|
||||
Parser::SFH_OBJECT_ARGS
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OutputPage $out The output page to add the meta element to.
|
||||
* @param ParserOutput $parserOutput The parser output to get the description from.
|
||||
*/
|
||||
public function onOutputPageParserOutput( $out, $parserOutput ): void {
|
||||
// Export the description from the main parser output into the OutputPage
|
||||
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
||||
// MW 1.38+
|
||||
$description = $parserOutput->getPageProperty( 'description' );
|
||||
} else {
|
||||
$description = $parserOutput->getProperty( 'description' );
|
||||
if ( $description === false ) {
|
||||
$description = null;
|
||||
}
|
||||
}
|
||||
if ( $description !== null ) {
|
||||
$out->addMeta( 'description', $description );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue