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;
|
2019-03-28 03:52:55 +00:00
|
|
|
use Parser;
|
|
|
|
|
2021-03-06 17:24:04 +00:00
|
|
|
class Hooks implements
|
|
|
|
\MediaWiki\Hook\ParserFirstCallInitHook,
|
|
|
|
\MediaWiki\Hook\ParserTestGlobalsHook
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Config $config
|
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
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
|
2021-11-19 17:25:10 +00:00
|
|
|
$parser->setFunctionHook( 'if', [ ParserFunctions::class, 'if' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifeq', [ ParserFunctions::class, 'ifeq' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'switch', [ ParserFunctions::class, 'switch' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexist', [ ParserFunctions::class, 'ifexist' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexpr', [ ParserFunctions::class, 'ifexpr' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'iferror', [ ParserFunctions::class, 'iferror' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'time', [ ParserFunctions::class, 'time' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'timel', [ ParserFunctions::class, 'localTime' ], Parser::SFH_OBJECT_ARGS );
|
2019-03-28 03:52:55 +00:00
|
|
|
|
2021-11-19 17:25:10 +00:00
|
|
|
$parser->setFunctionHook( 'expr', [ ParserFunctions::class, 'expr' ] );
|
|
|
|
$parser->setFunctionHook( 'rel2abs', [ ParserFunctions::class, 'rel2abs' ] );
|
|
|
|
$parser->setFunctionHook( 'titleparts', [ ParserFunctions::class, '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' ) ) {
|
2021-11-19 17:25:10 +00:00
|
|
|
$parser->setFunctionHook( 'len', [ ParserFunctions::class, 'runLen' ] );
|
|
|
|
$parser->setFunctionHook( 'pos', [ ParserFunctions::class, 'runPos' ] );
|
|
|
|
$parser->setFunctionHook( 'rpos', [ ParserFunctions::class, 'runRPos' ] );
|
|
|
|
$parser->setFunctionHook( 'sub', [ ParserFunctions::class, 'runSub' ] );
|
|
|
|
$parser->setFunctionHook( 'count', [ ParserFunctions::class, 'runCount' ] );
|
|
|
|
$parser->setFunctionHook( 'replace', [ ParserFunctions::class, 'runReplace' ] );
|
|
|
|
$parser->setFunctionHook( 'explode', [ ParserFunctions::class, 'runExplode' ] );
|
|
|
|
$parser->setFunctionHook( 'urldecode', [ ParserFunctions::class, '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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|