mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ParserFunctions
synced 2024-11-27 09:41:37 +00:00
Merge "Use PHP type hints"
This commit is contained in:
commit
8a3a8de07a
|
@ -22,8 +22,7 @@ use Exception;
|
|||
use MediaWiki\Message\Message;
|
||||
|
||||
class ExprError extends Exception {
|
||||
/** @var Message */
|
||||
private $mwMessage;
|
||||
private Message $mwMessage;
|
||||
|
||||
/**
|
||||
* @param string $msg
|
||||
|
|
|
@ -16,23 +16,9 @@ class Hooks implements
|
|||
\MediaWiki\Hook\ParserFirstCallInitHook,
|
||||
\MediaWiki\Hook\ParserTestGlobalsHook
|
||||
{
|
||||
private Config $config;
|
||||
private ParserFunctions $parserFunctions;
|
||||
|
||||
/** @var Config */
|
||||
private $config;
|
||||
|
||||
/** @var ParserFunctions */
|
||||
private $parserFunctions;
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param HookContainer $hookContainer
|
||||
* @param LanguageConverterFactory $languageConverterFactory
|
||||
* @param LanguageFactory $languageFactory
|
||||
* @param LanguageNameUtils $languageNameUtils
|
||||
* @param LinkCache $linkCache
|
||||
* @param RepoGroup $repoGroup
|
||||
* @param SpecialPageFactory $specialPageFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Config $config,
|
||||
HookContainer $hookContainer,
|
||||
|
|
|
@ -28,50 +28,23 @@ use Wikimedia\RequestTimeout\TimeoutException;
|
|||
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions
|
||||
*/
|
||||
class ParserFunctions {
|
||||
/** @var ExprParser|null */
|
||||
private static $mExprParser = null;
|
||||
private static ?ExprParser $mExprParser = null;
|
||||
/** @var array[][][][] */
|
||||
private static $mTimeCache = [];
|
||||
/** @var int */
|
||||
private static $mTimeChars = 0;
|
||||
private static array $mTimeCache = [];
|
||||
private static int $mTimeChars = 0;
|
||||
|
||||
/** ~10 seconds */
|
||||
private const MAX_TIME_CHARS = 6000;
|
||||
|
||||
/** @var Config */
|
||||
private $config;
|
||||
private Config $config;
|
||||
private HookContainer $hookContainer;
|
||||
private LanguageConverterFactory $languageConverterFactory;
|
||||
private LanguageFactory $languageFactory;
|
||||
private LanguageNameUtils $languageNameUtils;
|
||||
private LinkCache $linkCache;
|
||||
private RepoGroup $repoGroup;
|
||||
private SpecialPageFactory $specialPageFactory;
|
||||
|
||||
/** @var HookContainer */
|
||||
private $hookContainer;
|
||||
|
||||
/** @var LanguageConverterFactory */
|
||||
private $languageConverterFactory;
|
||||
|
||||
/** @var LanguageFactory */
|
||||
private $languageFactory;
|
||||
|
||||
/** @var LanguageNameUtils */
|
||||
private $languageNameUtils;
|
||||
|
||||
/** @var LinkCache */
|
||||
private $linkCache;
|
||||
|
||||
/** @var RepoGroup */
|
||||
private $repoGroup;
|
||||
|
||||
/** @var SpecialPageFactory */
|
||||
private $specialPageFactory;
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param HookContainer $hookContainer
|
||||
* @param LanguageConverterFactory $languageConverterFactory
|
||||
* @param LanguageFactory $languageFactory
|
||||
* @param LanguageNameUtils $languageNameUtils
|
||||
* @param LinkCache $linkCache
|
||||
* @param RepoGroup $repoGroup
|
||||
* @param SpecialPageFactory $specialPageFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Config $config,
|
||||
HookContainer $hookContainer,
|
||||
|
@ -92,10 +65,7 @@ class ParserFunctions {
|
|||
$this->specialPageFactory = $specialPageFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ExprParser
|
||||
*/
|
||||
private static function &getExprParser() {
|
||||
private static function &getExprParser(): ExprParser {
|
||||
if ( self::$mExprParser === null ) {
|
||||
self::$mExprParser = new ExprParser;
|
||||
}
|
||||
|
@ -373,13 +343,7 @@ class ParserFunctions {
|
|||
return implode( '/', $newExploded );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser $parser
|
||||
* @param string $titletext
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function ifexistInternal( Parser $parser, $titletext ): bool {
|
||||
private function ifexistInternal( Parser $parser, string $titletext ): bool {
|
||||
$title = Title::newFromText( $titletext );
|
||||
$this->languageConverterFactory->getLanguageConverter( $parser->getContentLanguage() )
|
||||
->findVariantLink( $titletext, $title, true );
|
||||
|
@ -462,18 +426,10 @@ class ParserFunctions {
|
|||
|
||||
/**
|
||||
* Used by time() and localTime()
|
||||
*
|
||||
* @param Parser $parser
|
||||
* @param PPFrame $frame
|
||||
* @param string $format
|
||||
* @param string $date
|
||||
* @param string $language
|
||||
* @param string|bool $local
|
||||
* @return string
|
||||
*/
|
||||
private function timeCommon(
|
||||
Parser $parser, PPFrame $frame, $format, $date, $language, $local
|
||||
) {
|
||||
Parser $parser, PPFrame $frame, string $format, string $date, string $language, bool $local
|
||||
): string {
|
||||
$this->hookContainer->register(
|
||||
'ParserClearState',
|
||||
static function () {
|
||||
|
@ -490,8 +446,8 @@ class ParserFunctions {
|
|||
$cacheKey = $date;
|
||||
$useTTL = false;
|
||||
}
|
||||
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
|
||||
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local];
|
||||
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][(int)$local] ) ) {
|
||||
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][(int)$local];
|
||||
if ( $useTTL && $cachedVal[1] !== null ) {
|
||||
$frame->setTTL( $cachedVal[1] );
|
||||
}
|
||||
|
@ -570,7 +526,7 @@ class ParserFunctions {
|
|||
$this->normalizeLangCode( $parser, $language ) );
|
||||
$result = $langObject->sprintfDate( $format, $ts, $tz, $ttl );
|
||||
}
|
||||
self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ];
|
||||
self::$mTimeCache[$format][$cacheKey][$language][(int)$local] = [ $result, $ttl ];
|
||||
if ( $useTTL && $ttl !== null ) {
|
||||
$frame->setTTL( $ttl );
|
||||
}
|
||||
|
@ -579,12 +535,8 @@ class ParserFunctions {
|
|||
|
||||
/**
|
||||
* Convert an input string to a known language code for time formatting
|
||||
*
|
||||
* @param Parser $parser
|
||||
* @param string $langCode
|
||||
* @return string
|
||||
*/
|
||||
private function normalizeLangCode( Parser $parser, string $langCode ) {
|
||||
private function normalizeLangCode( Parser $parser, string $langCode ): string {
|
||||
if ( $langCode !== '' && $this->languageNameUtils->isKnownLanguageTag( $langCode ) ) {
|
||||
return $langCode;
|
||||
} else {
|
||||
|
@ -659,14 +611,8 @@ class ParserFunctions {
|
|||
|
||||
/**
|
||||
* Helper for timef and timefl
|
||||
*
|
||||
* @param Parser $parser
|
||||
* @param PPFrame $frame
|
||||
* @param array $args
|
||||
* @param bool $local
|
||||
* @return string
|
||||
*/
|
||||
private function timefCommon( Parser $parser, PPFrame $frame, array $args, $local ) {
|
||||
private function timefCommon( Parser $parser, PPFrame $frame, array $args, bool $local ): string {
|
||||
$date = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
||||
$inputType = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
||||
|
||||
|
@ -724,19 +670,15 @@ class ParserFunctions {
|
|||
|
||||
/**
|
||||
* Verifies parameter is less than max string length.
|
||||
*
|
||||
* @param string $text
|
||||
* @return bool
|
||||
*/
|
||||
private function checkLength( $text ) {
|
||||
return ( mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' ) );
|
||||
private function checkLength( string $text ): bool {
|
||||
return mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates error message. Called when string is too long.
|
||||
* @return string
|
||||
*/
|
||||
private function tooLongError() {
|
||||
private function tooLongError(): string {
|
||||
$msg = wfMessage( 'pfunc_string_too_long' )
|
||||
->numParams( $this->config->get( 'PFStringLengthLimit' ) );
|
||||
return '<strong class="error">' . $msg->inContentLanguage()->escaped() . '</strong>';
|
||||
|
@ -1025,7 +967,7 @@ class ParserFunctions {
|
|||
* but with char refs intact
|
||||
* @return string The trimmed, expanded and entity reference decoded version of the PPNode
|
||||
*/
|
||||
private static function decodeTrimExpand( $obj, PPFrame $frame, &$trimExpanded = '' ) {
|
||||
private static function decodeTrimExpand( $obj, PPFrame $frame, string &$trimExpanded = '' ): string {
|
||||
$expanded = $frame->expand( $obj );
|
||||
$trimExpanded = trim( $expanded );
|
||||
return trim( Sanitizer::decodeCharReferences( $expanded ) );
|
||||
|
|
Loading…
Reference in a new issue