2019-03-28 03:52:55 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-05 15:06:59 +00:00
|
|
|
namespace MediaWiki\Extension\ParserFunctions;
|
2019-03-28 03:52:55 +00:00
|
|
|
|
2021-03-06 17:24:04 +00:00
|
|
|
use Config;
|
2023-11-01 20:08:47 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2019-03-28 03:52:55 +00:00
|
|
|
use Parser;
|
2023-11-01 20:01:59 +00:00
|
|
|
use RepoGroup;
|
2019-03-28 03:52:55 +00:00
|
|
|
|
2021-03-06 17:24:04 +00:00
|
|
|
class Hooks implements
|
|
|
|
\MediaWiki\Hook\ParserFirstCallInitHook,
|
|
|
|
\MediaWiki\Hook\ParserTestGlobalsHook
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
/** @var ParserFunctions */
|
|
|
|
private $parserFunctions;
|
|
|
|
|
2021-03-06 17:24:04 +00:00
|
|
|
/**
|
|
|
|
* @param Config $config
|
2023-11-01 20:01:59 +00:00
|
|
|
* @param RepoGroup $repoGroup
|
2023-11-01 20:08:47 +00:00
|
|
|
* @param SpecialPageFactory $specialPageFactory
|
2021-03-06 17:24:04 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2023-11-01 20:01:59 +00:00
|
|
|
Config $config,
|
2023-11-01 20:08:47 +00:00
|
|
|
RepoGroup $repoGroup,
|
|
|
|
SpecialPageFactory $specialPageFactory
|
2021-03-06 17:24:04 +00:00
|
|
|
) {
|
|
|
|
$this->config = $config;
|
2023-11-01 20:01:59 +00:00
|
|
|
$this->parserFunctions = new ParserFunctions(
|
|
|
|
$config,
|
2023-11-01 20:08:47 +00:00
|
|
|
$repoGroup,
|
|
|
|
$specialPageFactory
|
2023-11-01 20:01:59 +00:00
|
|
|
);
|
2021-03-06 17:24:04 +00:00
|
|
|
}
|
2019-03-28 03:52:55 +00:00
|
|
|
|
|
|
|
/**
|
2019-03-28 04:29:58 +00:00
|
|
|
* Enables string functions during parser tests.
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserTestGlobals
|
|
|
|
*
|
2019-03-28 03:52:55 +00:00
|
|
|
* @param array &$globals
|
|
|
|
*/
|
2021-03-06 17:24:04 +00:00
|
|
|
public function onParserTestGlobals( &$globals ) {
|
2019-03-28 03:52:55 +00:00
|
|
|
$globals['wgPFEnableStringFunctions'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-28 04:29:58 +00:00
|
|
|
* Registers our parser functions with a fresh parser.
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
|
|
|
|
*
|
2019-03-28 03:52:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
*/
|
2021-03-06 17:24:04 +00:00
|
|
|
public function onParserFirstCallInit( $parser ) {
|
2019-03-28 03:52:55 +00:00
|
|
|
// These functions accept DOM-style arguments
|
2023-11-01 19:38:34 +00:00
|
|
|
$parser->setFunctionHook( 'if', [ $this->parserFunctions, 'if' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifeq', [ $this->parserFunctions, 'ifeq' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'switch', [ $this->parserFunctions, 'switch' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexist', [ $this->parserFunctions, 'ifexist' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexpr', [ $this->parserFunctions, 'ifexpr' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'iferror', [ $this->parserFunctions, 'iferror' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'time', [ $this->parserFunctions, 'time' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'timel', [ $this->parserFunctions, 'localTime' ], Parser::SFH_OBJECT_ARGS );
|
2019-03-28 03:52:55 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
$parser->setFunctionHook( 'expr', [ $this->parserFunctions, 'expr' ] );
|
|
|
|
$parser->setFunctionHook( 'rel2abs', [ $this->parserFunctions, 'rel2abs' ] );
|
|
|
|
$parser->setFunctionHook( 'titleparts', [ $this->parserFunctions, 'titleparts' ] );
|
2019-03-28 03:52:55 +00:00
|
|
|
|
|
|
|
// String Functions: enable if configured
|
2021-03-06 17:24:04 +00:00
|
|
|
if ( $this->config->get( 'PFEnableStringFunctions' ) ) {
|
2023-11-01 19:38:34 +00:00
|
|
|
$parser->setFunctionHook( 'len', [ $this->parserFunctions, 'runLen' ] );
|
|
|
|
$parser->setFunctionHook( 'pos', [ $this->parserFunctions, 'runPos' ] );
|
|
|
|
$parser->setFunctionHook( 'rpos', [ $this->parserFunctions, 'runRPos' ] );
|
|
|
|
$parser->setFunctionHook( 'sub', [ $this->parserFunctions, 'runSub' ] );
|
|
|
|
$parser->setFunctionHook( 'count', [ $this->parserFunctions, 'runCount' ] );
|
|
|
|
$parser->setFunctionHook( 'replace', [ $this->parserFunctions, 'runReplace' ] );
|
|
|
|
$parser->setFunctionHook( 'explode', [ $this->parserFunctions, 'runExplode' ] );
|
|
|
|
$parser->setFunctionHook( 'urldecode', [ $this->parserFunctions, 'runUrlDecode' ] );
|
2019-03-28 03:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 04:29:58 +00:00
|
|
|
/**
|
|
|
|
* Registers ParserFunctions' lua function with Scribunto
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Extension:Scribunto/ScribuntoExternalLibraries
|
|
|
|
*
|
|
|
|
* @param string $engine
|
|
|
|
* @param string[] &$extraLibraries
|
|
|
|
*/
|
2019-03-28 03:52:55 +00:00
|
|
|
public static function onScribuntoExternalLibraries( $engine, array &$extraLibraries ) {
|
|
|
|
if ( $engine === 'lua' ) {
|
|
|
|
$extraLibraries['mw.ext.ParserFunctions'] = LuaLibrary::class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|