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
|
|
|
|
2024-10-20 09:15:08 +00:00
|
|
|
use MediaWiki\Cache\LinkCache;
|
|
|
|
use MediaWiki\Config\Config;
|
2023-11-01 21:07:51 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2023-11-01 20:52:30 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2024-10-20 09:15:08 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2023-11-01 20:08:47 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
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 21:07:51 +00:00
|
|
|
* @param HookContainer $hookContainer
|
2023-11-01 20:52:30 +00:00
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
|
|
|
* @param LanguageFactory $languageFactory
|
|
|
|
* @param LanguageNameUtils $languageNameUtils
|
2023-11-01 20:20:45 +00:00
|
|
|
* @param LinkCache $linkCache
|
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 21:07:51 +00:00
|
|
|
HookContainer $hookContainer,
|
2023-11-01 20:52:30 +00:00
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
|
|
|
LanguageFactory $languageFactory,
|
|
|
|
LanguageNameUtils $languageNameUtils,
|
2023-11-01 20:20:45 +00:00
|
|
|
LinkCache $linkCache,
|
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 21:07:51 +00:00
|
|
|
$hookContainer,
|
2023-11-01 20:52:30 +00:00
|
|
|
$languageConverterFactory,
|
|
|
|
$languageFactory,
|
|
|
|
$languageNameUtils,
|
2023-11-01 20:20:45 +00:00
|
|
|
$linkCache,
|
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 );
|
2024-07-11 06:28:32 +00:00
|
|
|
$parser->setFunctionHook( 'timef', [ $this->parserFunctions, 'timef' ], Parser::SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'timefl', [ $this->parserFunctions, 'timefl' ], 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|