2019-03-28 03:52:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extensions\ParserFunctions;
|
|
|
|
|
|
|
|
use Parser;
|
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
public static function onParserTestGlobals( array &$globals ) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public static function onParserFirstCallInit( Parser $parser ) {
|
|
|
|
global $wgPFEnableStringFunctions;
|
|
|
|
|
|
|
|
// These functions accept DOM-style arguments
|
|
|
|
$class = ParserFunctions::class;
|
2019-04-13 03:39:45 +00:00
|
|
|
$parser->setFunctionHook( 'if', "$class::if", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifeq', "$class::ifeq", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'switch', "$class::switch", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexist', "$class::ifexist", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexpr', "$class::ifexpr", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'iferror', "$class::iferror", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'time', "$class::time", Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'timel', "$class::localTime", Parser::SFH_OBJECT_ARGS );
|
2019-03-28 03:52:55 +00:00
|
|
|
|
|
|
|
$parser->setFunctionHook( 'expr', "$class::expr" );
|
|
|
|
$parser->setFunctionHook( 'rel2abs', "$class::rel2abs" );
|
|
|
|
$parser->setFunctionHook( 'titleparts', "$class::titleparts" );
|
|
|
|
|
|
|
|
// String Functions: enable if configured
|
|
|
|
if ( $wgPFEnableStringFunctions ) {
|
|
|
|
$parser->setFunctionHook( 'len', "$class::runLen" );
|
|
|
|
$parser->setFunctionHook( 'pos', "$class::runPos" );
|
|
|
|
$parser->setFunctionHook( 'rpos', "$class::runRPos" );
|
|
|
|
$parser->setFunctionHook( 'sub', "$class::runSub" );
|
|
|
|
$parser->setFunctionHook( 'count', "$class::runCount" );
|
|
|
|
$parser->setFunctionHook( 'replace', "$class::runReplace" );
|
|
|
|
$parser->setFunctionHook( 'explode', "$class::runExplode" );
|
|
|
|
$parser->setFunctionHook( 'urldecode', "$class::runUrlDecode" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|