2009-06-05 09:53:05 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-05 15:06:59 +00:00
|
|
|
namespace MediaWiki\Extension\ParserFunctions;
|
2019-03-28 03:52:55 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
use Config;
|
2019-03-28 03:52:55 +00:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
|
|
|
use Exception;
|
2023-11-01 20:20:45 +00:00
|
|
|
use LinkCache;
|
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;
|
2023-11-01 20:08:47 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2019-03-28 03:52:55 +00:00
|
|
|
use MWTimestamp;
|
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
|
|
|
use PPNode;
|
2023-11-01 20:01:59 +00:00
|
|
|
use RepoGroup;
|
2019-03-28 03:52:55 +00:00
|
|
|
use Sanitizer;
|
|
|
|
use StringUtils;
|
|
|
|
use Title;
|
2023-10-02 18:57:38 +00:00
|
|
|
use Wikimedia\RequestTimeout\TimeoutException;
|
2018-08-07 11:44:41 +00:00
|
|
|
|
2019-04-13 05:14:30 +00:00
|
|
|
/**
|
|
|
|
* Parser function handlers
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions
|
|
|
|
*/
|
2019-03-28 03:52:55 +00:00
|
|
|
class ParserFunctions {
|
2024-03-26 19:14:48 +00:00
|
|
|
private static $mExprParser = null;
|
2019-04-13 03:45:24 +00:00
|
|
|
private static $mTimeCache = [];
|
|
|
|
private static $mTimeChars = 0;
|
2019-03-28 04:33:58 +00:00
|
|
|
|
|
|
|
/** ~10 seconds */
|
2020-05-30 00:27:32 +00:00
|
|
|
private const MAX_TIME_CHARS = 6000;
|
2011-04-11 17:27:51 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
2023-11-01 21:07:51 +00:00
|
|
|
/** @var HookContainer */
|
|
|
|
private $hookContainer;
|
|
|
|
|
2023-11-01 20:52:30 +00:00
|
|
|
/** @var LanguageConverterFactory */
|
|
|
|
private $languageConverterFactory;
|
|
|
|
|
|
|
|
/** @var LanguageFactory */
|
|
|
|
private $languageFactory;
|
|
|
|
|
|
|
|
/** @var LanguageNameUtils */
|
|
|
|
private $languageNameUtils;
|
|
|
|
|
2023-11-01 20:20:45 +00:00
|
|
|
/** @var LinkCache */
|
|
|
|
private $linkCache;
|
|
|
|
|
2023-11-01 20:01:59 +00:00
|
|
|
/** @var RepoGroup */
|
|
|
|
private $repoGroup;
|
|
|
|
|
2023-11-01 20:08:47 +00:00
|
|
|
/** @var SpecialPageFactory */
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
2023-11-01 19:38:34 +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
|
2023-11-01 19:38:34 +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
|
2023-11-01 19:38:34 +00:00
|
|
|
) {
|
|
|
|
$this->config = $config;
|
2023-11-01 21:07:51 +00:00
|
|
|
$this->hookContainer = $hookContainer;
|
2023-11-01 20:52:30 +00:00
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
|
|
|
$this->languageFactory = $languageFactory;
|
|
|
|
$this->languageNameUtils = $languageNameUtils;
|
2023-11-01 20:20:45 +00:00
|
|
|
$this->linkCache = $linkCache;
|
2023-11-01 20:01:59 +00:00
|
|
|
$this->repoGroup = $repoGroup;
|
2023-11-01 20:08:47 +00:00
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
2023-11-01 19:38:34 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 00:40:30 +00:00
|
|
|
/**
|
|
|
|
* @return ExprParser
|
|
|
|
*/
|
2019-04-13 03:45:24 +00:00
|
|
|
private static function &getExprParser() {
|
2024-03-26 19:14:48 +00:00
|
|
|
if ( self::$mExprParser === null ) {
|
2011-04-11 17:27:51 +00:00
|
|
|
self::$mExprParser = new ExprParser;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2011-04-11 17:27:51 +00:00
|
|
|
return self::$mExprParser;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#expr: expression }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##expr
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $expr
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function expr( Parser $parser, $expr = '' ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
try {
|
2011-04-11 17:27:51 +00:00
|
|
|
return self::getExprParser()->doExpression( $expr );
|
2010-10-02 22:36:34 +00:00
|
|
|
} catch ( ExprError $e ) {
|
2019-09-05 22:07:54 +00:00
|
|
|
return '<strong class="error">' . htmlspecialchars( $e->getUserFriendlyMessage() ) . '</strong>';
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#ifexpr: expression | value if true | value if false }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##ifexpr
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
2019-04-13 03:39:45 +00:00
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function ifexpr( Parser $parser, PPFrame $frame, array $args ) {
|
2019-04-13 03:39:45 +00:00
|
|
|
$expr = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$then = $args[1] ?? '';
|
|
|
|
$else = $args[2] ?? '';
|
|
|
|
|
2010-10-02 22:36:34 +00:00
|
|
|
try {
|
2019-04-13 03:39:45 +00:00
|
|
|
$result = self::getExprParser()->doExpression( $expr );
|
|
|
|
if ( is_numeric( $result ) ) {
|
|
|
|
$result = (float)$result;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2019-04-13 03:39:45 +00:00
|
|
|
$result = $result ? $then : $else;
|
2010-10-02 22:36:34 +00:00
|
|
|
} catch ( ExprError $e ) {
|
2019-09-05 22:07:54 +00:00
|
|
|
return '<strong class="error">' . htmlspecialchars( $e->getUserFriendlyMessage() ) . '</strong>';
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_object( $result ) ) {
|
|
|
|
$result = trim( $frame->expand( $result ) );
|
|
|
|
}
|
2019-04-13 03:39:45 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#if: test string | value if test string is not empty | value if test string is empty }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##if
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function if( Parser $parser, PPFrame $frame, array $args ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
if ( $test !== '' ) {
|
|
|
|
return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
} else {
|
|
|
|
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#ifeq: string 1 | string 2 | value if identical | value if different }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##ifeq
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function ifeq( Parser $parser, PPFrame $frame, array $args ) {
|
2014-02-15 03:26:30 +00:00
|
|
|
$left = isset( $args[0] ) ? self::decodeTrimExpand( $args[0], $frame ) : '';
|
|
|
|
$right = isset( $args[1] ) ? self::decodeTrimExpand( $args[1], $frame ) : '';
|
2016-03-08 11:54:05 +00:00
|
|
|
|
|
|
|
// Strict compare is not possible here. 01 should equal 1 for example.
|
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */
|
2009-06-05 09:53:05 +00:00
|
|
|
if ( $left == $right ) {
|
|
|
|
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
} else {
|
|
|
|
return isset( $args[3] ) ? trim( $frame->expand( $args[3] ) ) : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#iferror: test string | value if error | value if no error }}
|
|
|
|
*
|
|
|
|
* Error is when the input string contains an HTML object with class="error", as
|
|
|
|
* generated by other parser functions such as #expr, #time and #rel2abs.
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##iferror
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
2019-04-13 03:39:45 +00:00
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2019-04-13 03:39:45 +00:00
|
|
|
* @return string
|
2011-12-19 00:54:31 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function iferror( Parser $parser, PPFrame $frame, array $args ) {
|
2019-04-13 03:39:45 +00:00
|
|
|
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$then = $args[1] ?? false;
|
|
|
|
$else = $args[2] ?? false;
|
|
|
|
|
2017-06-02 18:55:29 +00:00
|
|
|
if ( preg_match(
|
|
|
|
'/<(?:strong|span|p|div)\s(?:[^\s>]*\s+)*?class="(?:[^"\s>]*\s+)*?error(?:\s[^">]*)?"/',
|
|
|
|
$test )
|
|
|
|
) {
|
2019-04-13 03:39:45 +00:00
|
|
|
$result = $then;
|
2009-06-05 09:53:05 +00:00
|
|
|
} elseif ( $else === false ) {
|
2019-04-13 03:39:45 +00:00
|
|
|
$result = $test;
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
2019-04-13 03:39:45 +00:00
|
|
|
$result = $else;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
if ( $result === false ) {
|
|
|
|
return '';
|
|
|
|
}
|
2019-04-13 03:39:45 +00:00
|
|
|
|
|
|
|
return trim( $frame->expand( $result ) );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 17:44:19 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#switch: comparison string
|
|
|
|
* | case = result
|
|
|
|
* | case = result
|
|
|
|
* | ...
|
|
|
|
* | default result
|
|
|
|
* }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##switch
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2011-04-15 17:44:19 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function switch( Parser $parser, PPFrame $frame, array $args ) {
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( count( $args ) === 0 ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
return '';
|
|
|
|
}
|
2014-02-15 03:26:30 +00:00
|
|
|
$primary = self::decodeTrimExpand( array_shift( $args ), $frame );
|
2009-06-05 16:53:59 +00:00
|
|
|
$found = $defaultFound = false;
|
2009-06-05 09:53:05 +00:00
|
|
|
$default = null;
|
|
|
|
$lastItemHadNoEquals = false;
|
2014-02-15 03:26:30 +00:00
|
|
|
$lastItem = '';
|
2019-03-28 04:21:01 +00:00
|
|
|
$mwDefault = $parser->getMagicWordFactory()->get( 'default' );
|
2009-06-05 09:53:05 +00:00
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
$bits = $arg->splitArg();
|
|
|
|
$nameNode = $bits['name'];
|
|
|
|
$index = $bits['index'];
|
|
|
|
$valueNode = $bits['value'];
|
|
|
|
|
|
|
|
if ( $index === '' ) {
|
|
|
|
# Found "="
|
|
|
|
$lastItemHadNoEquals = false;
|
|
|
|
if ( $found ) {
|
|
|
|
# Multiple input match
|
|
|
|
return trim( $frame->expand( $valueNode ) );
|
|
|
|
}
|
2023-01-14 21:43:00 +00:00
|
|
|
$test = self::decodeTrimExpand( $nameNode, $frame );
|
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */
|
|
|
|
if ( $test == $primary ) {
|
|
|
|
# Found a match, return now
|
|
|
|
return trim( $frame->expand( $valueNode ) );
|
|
|
|
}
|
|
|
|
if ( $defaultFound || $mwDefault->matchStartToEnd( $test ) ) {
|
|
|
|
$default = $valueNode;
|
|
|
|
$defaultFound = false;
|
|
|
|
} # else wrong case, continue
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
|
|
|
# Multiple input, single output
|
|
|
|
# If the value matches, set a flag and continue
|
|
|
|
$lastItemHadNoEquals = true;
|
2014-02-15 03:26:30 +00:00
|
|
|
// $lastItem is an "out" variable
|
|
|
|
$decodedTest = self::decodeTrimExpand( $valueNode, $frame, $lastItem );
|
2016-03-08 11:54:05 +00:00
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */
|
2014-02-15 03:26:30 +00:00
|
|
|
if ( $decodedTest == $primary ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$found = true;
|
2014-07-11 18:14:36 +00:00
|
|
|
} elseif ( $mwDefault->matchStartToEnd( $decodedTest ) ) {
|
2009-06-05 16:53:59 +00:00
|
|
|
$defaultFound = true;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Default case
|
|
|
|
# Check if the last item had no = sign, thus specifying the default case
|
|
|
|
if ( $lastItemHadNoEquals ) {
|
2014-02-15 03:26:30 +00:00
|
|
|
return $lastItem;
|
2023-01-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
if ( $default === null ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
return '';
|
|
|
|
}
|
2023-01-14 21:43:00 +00:00
|
|
|
return trim( $frame->expand( $default ) );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#rel2abs: path }} or {{#rel2abs: path | base path }}
|
|
|
|
*
|
2009-06-05 09:53:05 +00:00
|
|
|
* Returns the absolute path to a subpage, relative to the current article
|
|
|
|
* title. Treats titles as slash-separated paths.
|
|
|
|
*
|
|
|
|
* Following subpage link syntax instead of standard path syntax, an
|
|
|
|
* initial slash is treated as a relative path, and vice versa.
|
2011-04-15 17:44:19 +00:00
|
|
|
*
|
2019-04-13 05:14:30 +00:00
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##rel2abs
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
2011-12-19 00:54:31 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function rel2abs( Parser $parser, $to = '', $from = '' ) {
|
2010-10-02 22:36:34 +00:00
|
|
|
$from = trim( $from );
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $from === '' ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$from = $parser->getTitle()->getPrefixedText();
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:20:02 +00:00
|
|
|
$to = rtrim( $to, ' /' );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
// if we have an empty path, or just one containing a dot
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $to === '' || $to === '.' ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
return $from;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the path isn't relative
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( substr( $to, 0, 1 ) !== '/' &&
|
2017-06-20 07:18:25 +00:00
|
|
|
substr( $to, 0, 2 ) !== './' &&
|
|
|
|
substr( $to, 0, 3 ) !== '../' &&
|
|
|
|
$to !== '..'
|
|
|
|
) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$from = '';
|
|
|
|
}
|
|
|
|
// Make a long path, containing both, enclose it in /.../
|
2018-09-03 06:58:49 +00:00
|
|
|
$fullPath = '/' . $from . '/' . $to . '/';
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
// remove redundant current path dots
|
|
|
|
$fullPath = preg_replace( '!/(\./)+!', '/', $fullPath );
|
|
|
|
|
|
|
|
// remove double slashes
|
|
|
|
$fullPath = preg_replace( '!/{2,}!', '/', $fullPath );
|
|
|
|
|
|
|
|
// remove the enclosing slashes now
|
2017-06-06 15:20:02 +00:00
|
|
|
$fullPath = trim( $fullPath, '/' );
|
|
|
|
$exploded = explode( '/', $fullPath );
|
|
|
|
$newExploded = [];
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
foreach ( $exploded as $current ) {
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $current === '..' ) { // removing one level
|
2010-10-02 22:36:34 +00:00
|
|
|
if ( !count( $newExploded ) ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
// attempted to access a node above root node
|
2017-06-02 18:55:29 +00:00
|
|
|
$msg = wfMessage( 'pfunc_rel2abs_invalid_depth', $fullPath )
|
|
|
|
->inContentLanguage()->escaped();
|
2012-09-07 23:12:24 +00:00
|
|
|
return '<strong class="error">' . $msg . '</strong>';
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
// remove last level from the stack
|
|
|
|
array_pop( $newExploded );
|
|
|
|
} else {
|
|
|
|
// add the current level to the stack
|
|
|
|
$newExploded[] = $current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can now join it again
|
2017-06-06 15:20:02 +00:00
|
|
|
return implode( '/', $newExploded );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $titletext
|
2011-12-19 00:54:31 +00:00
|
|
|
*
|
2022-11-02 20:21:41 +00:00
|
|
|
* @return bool
|
2011-12-19 00:54:31 +00:00
|
|
|
*/
|
2023-11-01 20:01:59 +00:00
|
|
|
private function ifexistInternal( Parser $parser, $titletext ): bool {
|
2009-06-05 09:53:05 +00:00
|
|
|
$title = Title::newFromText( $titletext );
|
2023-11-01 20:52:30 +00:00
|
|
|
$this->languageConverterFactory->getLanguageConverter( $parser->getContentLanguage() )
|
2020-05-06 00:42:53 +00:00
|
|
|
->findVariantLink( $titletext, $title, true );
|
2022-11-03 17:07:00 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $title->getNamespace() === NS_MEDIA ) {
|
|
|
|
/* If namespace is specified as NS_MEDIA, then we want to
|
|
|
|
* check the physical file, not the "description" page.
|
|
|
|
*/
|
|
|
|
if ( !$parser->incrementExpensiveFunctionCount() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-01 20:01:59 +00:00
|
|
|
$file = $this->repoGroup->findFile( $title );
|
2022-11-03 17:07:00 +00:00
|
|
|
if ( !$file ) {
|
2021-02-18 23:51:56 +00:00
|
|
|
$parser->getOutput()->addImage(
|
2022-11-03 17:07:00 +00:00
|
|
|
$title->getDBKey(), false, false );
|
2022-11-02 20:21:41 +00:00
|
|
|
return false;
|
2022-11-03 17:07:00 +00:00
|
|
|
}
|
|
|
|
$parser->getOutput()->addImage(
|
|
|
|
$file->getName(), $file->getTimestamp(), $file->getSha1() );
|
|
|
|
return $file->exists();
|
2023-01-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
if ( $title->isSpecialPage() ) {
|
2022-11-03 17:07:00 +00:00
|
|
|
/* Don't bother with the count for special pages,
|
|
|
|
* since their existence can be checked without
|
|
|
|
* accessing the database.
|
|
|
|
*/
|
2023-11-01 20:08:47 +00:00
|
|
|
return $this->specialPageFactory->exists( $title->getDBkey() );
|
2023-01-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
if ( $title->isExternal() ) {
|
2022-11-03 17:07:00 +00:00
|
|
|
/* Can't check the existence of pages on other sites,
|
|
|
|
* so just return false. Makes a sort of sense, since
|
|
|
|
* they don't exist _locally_.
|
|
|
|
*/
|
|
|
|
return false;
|
2023-01-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
$pdbk = $title->getPrefixedDBkey();
|
|
|
|
$id = $this->linkCache->getGoodLinkID( $pdbk );
|
|
|
|
if ( $id !== 0 ) {
|
2022-11-03 17:07:00 +00:00
|
|
|
$parser->getOutput()->addLink( $title, $id );
|
2023-01-14 21:43:00 +00:00
|
|
|
return true;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2023-01-14 21:43:00 +00:00
|
|
|
if ( $this->linkCache->isBadLink( $pdbk ) ) {
|
|
|
|
$parser->getOutput()->addLink( $title, 0 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( !$parser->incrementExpensiveFunctionCount() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$id = $title->getArticleID();
|
|
|
|
$parser->getOutput()->addLink( $title, $id );
|
|
|
|
|
|
|
|
// bug 70495: don't just check whether the ID != 0
|
|
|
|
return $title->exists();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#ifexist: page title | value if exists | value if doesn't exist }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##ifexist
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function ifexist( Parser $parser, PPFrame $frame, array $args ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
2019-03-17 05:39:09 +00:00
|
|
|
$then = $args[1] ?? null;
|
|
|
|
$else = $args[2] ?? null;
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 20:01:59 +00:00
|
|
|
$result = $this->ifexistInternal( $parser, $title ) ? $then : $else;
|
2009-06-05 09:53:05 +00:00
|
|
|
if ( $result === null ) {
|
|
|
|
return '';
|
|
|
|
}
|
2023-01-14 21:43:00 +00:00
|
|
|
return trim( $frame->expand( $result ) );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* Used by time() and localTime()
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
2022-11-02 21:07:19 +00:00
|
|
|
* @param PPFrame $frame
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param string $format
|
|
|
|
* @param string $date
|
|
|
|
* @param string $language
|
|
|
|
* @param string|bool $local
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
private function timeCommon(
|
2022-11-02 21:07:19 +00:00
|
|
|
Parser $parser, PPFrame $frame, $format, $date, $language, $local
|
2017-06-02 18:55:29 +00:00
|
|
|
) {
|
2023-11-01 21:07:51 +00:00
|
|
|
$this->hookContainer->register(
|
2022-11-04 11:22:09 +00:00
|
|
|
'ParserClearState',
|
|
|
|
static function () {
|
|
|
|
self::$mTimeChars = 0;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2011-12-19 11:18:13 +00:00
|
|
|
if ( $date === '' ) {
|
|
|
|
$cacheKey = $parser->getOptions()->getTimestamp();
|
2012-09-07 23:12:24 +00:00
|
|
|
$timestamp = new MWTimestamp( $cacheKey );
|
|
|
|
$date = $timestamp->getTimestamp( TS_ISO_8601 );
|
2014-06-01 02:43:36 +00:00
|
|
|
$useTTL = true;
|
2011-12-19 11:18:13 +00:00
|
|
|
} else {
|
|
|
|
$cacheKey = $date;
|
2014-06-01 02:43:36 +00:00
|
|
|
$useTTL = false;
|
2011-12-19 11:18:13 +00:00
|
|
|
}
|
|
|
|
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
|
2014-06-01 02:43:36 +00:00
|
|
|
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local];
|
2022-11-02 21:07:19 +00:00
|
|
|
if ( $useTTL && $cachedVal[1] !== null ) {
|
2014-06-01 02:43:36 +00:00
|
|
|
$frame->setTTL( $cachedVal[1] );
|
|
|
|
}
|
|
|
|
return $cachedVal[0];
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2010-10-02 22:36:34 +00:00
|
|
|
|
|
|
|
# compute the timestamp string $ts
|
|
|
|
# PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object
|
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
$invalidTime = false;
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2011-12-19 10:51:39 +00:00
|
|
|
# the DateTime constructor must be used because it throws exceptions
|
|
|
|
# when errors occur, whereas date_create appears to just output a warning
|
|
|
|
# that can't really be detected from within the code
|
|
|
|
try {
|
2011-12-07 20:52:41 +00:00
|
|
|
|
2011-12-19 10:51:39 +00:00
|
|
|
# Default input timezone is UTC.
|
|
|
|
$utc = new DateTimeZone( 'UTC' );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2011-12-19 10:51:39 +00:00
|
|
|
# Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
|
|
|
|
if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
|
2018-09-03 06:58:49 +00:00
|
|
|
$date = '00:00 ' . $date;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2011-12-19 10:51:39 +00:00
|
|
|
|
|
|
|
# Parse date
|
2011-12-19 11:18:13 +00:00
|
|
|
# UTC is a default input timezone.
|
|
|
|
$dateObject = new DateTime( $date, $utc );
|
|
|
|
|
2011-12-19 10:51:39 +00:00
|
|
|
# Set output timezone.
|
|
|
|
if ( $local ) {
|
2023-11-01 19:38:34 +00:00
|
|
|
$tz = new DateTimeZone(
|
|
|
|
$this->config->get( 'Localtimezone' ) ??
|
|
|
|
date_default_timezone_get()
|
|
|
|
);
|
2011-12-19 10:51:39 +00:00
|
|
|
} else {
|
2013-03-22 16:52:31 +00:00
|
|
|
$tz = $utc;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2013-03-22 16:52:31 +00:00
|
|
|
$dateObject->setTimezone( $tz );
|
2011-12-19 10:51:39 +00:00
|
|
|
# Generate timestamp
|
|
|
|
$ts = $dateObject->format( 'YmdHis' );
|
|
|
|
|
2023-10-02 18:57:38 +00:00
|
|
|
} catch ( TimeoutException $ex ) {
|
|
|
|
// Unfortunately DateTime throws a generic Exception, but we can't
|
|
|
|
// ignore an exception generated by the RequestTimeout library.
|
|
|
|
throw $ex;
|
2011-12-19 10:51:39 +00:00
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
$invalidTime = true;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2014-06-01 02:43:36 +00:00
|
|
|
$ttl = null;
|
2010-10-02 22:36:34 +00:00
|
|
|
# format the timestamp and return the result
|
2009-06-05 09:53:05 +00:00
|
|
|
if ( $invalidTime ) {
|
2017-06-02 18:55:29 +00:00
|
|
|
$result = '<strong class="error">' .
|
|
|
|
wfMessage( 'pfunc_time_error' )->inContentLanguage()->escaped() .
|
|
|
|
'</strong>';
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
2011-04-11 17:27:51 +00:00
|
|
|
self::$mTimeChars += strlen( $format );
|
2019-03-28 04:33:58 +00:00
|
|
|
if ( self::$mTimeChars > self::MAX_TIME_CHARS ) {
|
2017-06-02 18:55:29 +00:00
|
|
|
return '<strong class="error">' .
|
|
|
|
wfMessage( 'pfunc_time_too_long' )->inContentLanguage()->escaped() .
|
|
|
|
'</strong>';
|
2022-11-03 17:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $ts < 0 ) { // Language can't deal with BC years
|
|
|
|
return '<strong class="error">' .
|
|
|
|
wfMessage( 'pfunc_time_too_small' )->inContentLanguage()->escaped() .
|
|
|
|
'</strong>';
|
2023-01-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
if ( $ts >= 100000000000000 ) { // Language can't deal with years after 9999
|
2022-11-03 17:07:00 +00:00
|
|
|
return '<strong class="error">' .
|
|
|
|
wfMessage( 'pfunc_time_too_big' )->inContentLanguage()->escaped() .
|
|
|
|
'</strong>';
|
|
|
|
}
|
|
|
|
|
2024-07-11 06:28:32 +00:00
|
|
|
$langObject = $this->languageFactory->getLanguage(
|
|
|
|
$this->normalizeLangCode( $parser, $language ) );
|
2022-11-03 17:07:00 +00:00
|
|
|
$result = $langObject->sprintfDate( $format, $ts, $tz, $ttl );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2017-06-06 15:20:02 +00:00
|
|
|
self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ];
|
2022-11-02 21:07:19 +00:00
|
|
|
if ( $useTTL && $ttl !== null ) {
|
2014-06-01 02:43:36 +00:00
|
|
|
$frame->setTTL( $ttl );
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2024-07-11 06:28:32 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
|
|
|
if ( $langCode !== '' && $this->languageNameUtils->isKnownLanguageTag( $langCode ) ) {
|
|
|
|
return $langCode;
|
|
|
|
} else {
|
|
|
|
return $parser->getTargetLanguage()->getCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 02:43:36 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#time: format string }}
|
|
|
|
* {{#time: format string | date/time object }}
|
|
|
|
* {{#time: format string | date/time object | language code }}
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##time
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2014-06-01 02:43:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function time( Parser $parser, PPFrame $frame, array $args ) {
|
2014-06-01 02:43:36 +00:00
|
|
|
$format = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$date = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
$language = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
$local = isset( $args[3] ) && trim( $frame->expand( $args[3] ) );
|
2023-11-01 19:38:34 +00:00
|
|
|
return $this->timeCommon( $parser, $frame, $format, $date, $language, $local );
|
2014-06-01 02:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* {{#timel: ... }}
|
|
|
|
*
|
|
|
|
* Identical to {{#time: ... }}, except that it uses the local time of the wiki
|
|
|
|
* (as set in $wgLocaltimezone) when no date is given.
|
|
|
|
*
|
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##timel
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param PPNode[] $args
|
2014-06-01 02:43:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function localTime( Parser $parser, PPFrame $frame, array $args ) {
|
2014-06-01 02:43:36 +00:00
|
|
|
$format = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$date = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
$language = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
2023-11-01 19:38:34 +00:00
|
|
|
return $this->timeCommon( $parser, $frame, $format, $date, $language, true );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 06:28:32 +00:00
|
|
|
/**
|
|
|
|
* Formatted time -- time with a symbolic rather than explicit format
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
|
|
|
* @param array $args
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function timef( Parser $parser, PPFrame $frame, array $args ) {
|
|
|
|
return $this->timefCommon( $parser, $frame, $args, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatted time -- time with a symbolic rather than explicit format
|
|
|
|
* Using the local timezone of the wiki.
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
|
|
|
* @param array $args
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function timefl( Parser $parser, PPFrame $frame, array $args ) {
|
|
|
|
return $this->timefCommon( $parser, $frame, $args, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
|
|
|
$date = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$inputType = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
|
|
|
|
if ( $inputType !== '' ) {
|
|
|
|
$types = $parser->getMagicWordFactory()->newArray( [
|
|
|
|
'timef-time',
|
|
|
|
'timef-date',
|
|
|
|
'timef-both',
|
|
|
|
'timef-pretty'
|
|
|
|
] );
|
|
|
|
$id = $types->matchStartToEnd( $inputType );
|
|
|
|
if ( $id === false ) {
|
|
|
|
return '<strong class="error">' .
|
|
|
|
wfMessage( 'pfunc_timef_bad_format' ) .
|
|
|
|
'</strong>';
|
|
|
|
}
|
|
|
|
$type = str_replace( 'timef-', '', $id );
|
|
|
|
} else {
|
|
|
|
$type = 'both';
|
|
|
|
}
|
|
|
|
|
|
|
|
$langCode = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
$langCode = $this->normalizeLangCode( $parser, $langCode );
|
|
|
|
$lang = $this->languageFactory->getLanguage( $langCode );
|
|
|
|
$format = $lang->getDateFormatString( $type, 'default' );
|
|
|
|
return $this->timeCommon( $parser, $frame, $format, $date, $langCode, $local );
|
|
|
|
}
|
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
/**
|
|
|
|
* Obtain a specified number of slash-separated parts of a title,
|
|
|
|
* e.g. {{#titleparts:Hello/World|1}} => "Hello"
|
|
|
|
*
|
2019-04-13 05:14:30 +00:00
|
|
|
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##titleparts
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser Parent parser
|
|
|
|
* @param string $title Title to split
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string|int $parts Number of parts to keep
|
|
|
|
* @param string|int $offset Offset starting at 1
|
2009-06-05 09:53:05 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function titleparts( Parser $parser, $title = '', $parts = 0, $offset = 0 ) {
|
2016-03-25 11:26:57 +00:00
|
|
|
$parts = (int)$parts;
|
|
|
|
$offset = (int)$offset;
|
2009-06-05 09:53:05 +00:00
|
|
|
$ntitle = Title::newFromText( $title );
|
2022-11-03 17:07:00 +00:00
|
|
|
if ( !$ntitle ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
return $title;
|
|
|
|
}
|
2022-11-03 17:07:00 +00:00
|
|
|
|
|
|
|
$bits = explode( '/', $ntitle->getPrefixedText(), 25 );
|
|
|
|
if ( $offset > 0 ) {
|
|
|
|
--$offset;
|
|
|
|
}
|
|
|
|
return implode( '/', array_slice( $bits, $offset, $parts ?: null ) );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* Verifies parameter is less than max string length.
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param string $text
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
private function checkLength( $text ) {
|
|
|
|
return ( mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' ) );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:54:31 +00:00
|
|
|
/**
|
2019-04-13 05:14:30 +00:00
|
|
|
* Generates error message. Called when string is too long.
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
private function tooLongError() {
|
|
|
|
$msg = wfMessage( 'pfunc_string_too_long' )
|
|
|
|
->numParams( $this->config->get( 'PFStringLengthLimit' ) );
|
2012-09-07 23:12:24 +00:00
|
|
|
return '<strong class="error">' . $msg->inContentLanguage()->escaped() . '</strong>';
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#len:string}}
|
2010-10-02 22:36:34 +00:00
|
|
|
*
|
2009-06-05 09:53:05 +00:00
|
|
|
* Reports number of characters in string.
|
2019-04-13 05:14:30 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return int
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runLen( Parser $parser, $inStr = '' ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
2015-02-12 20:06:09 +00:00
|
|
|
return mb_strlen( $inStr );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#pos: string | needle | offset}}
|
|
|
|
*
|
|
|
|
* Finds first occurrence of "needle" in "string" starting at "offset".
|
|
|
|
*
|
|
|
|
* Note: If the needle is an empty string, single space is used instead.
|
|
|
|
* Note: If the needle is not found, empty string is returned.
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string $inNeedle
|
|
|
|
* @param string|int $inOffset
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return int|string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runPos( Parser $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
|
|
|
$inNeedle = $parser->killMarkers( (string)$inNeedle );
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ||
|
|
|
|
!$this->checkLength( $inNeedle ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( $inNeedle === '' ) {
|
|
|
|
$inNeedle = ' ';
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2018-11-15 16:09:15 +00:00
|
|
|
$pos = mb_strpos( $inStr, $inNeedle, min( (int)$inOffset, mb_strlen( $inStr ) ) );
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( $pos === false ) {
|
|
|
|
$pos = '';
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
return $pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#rpos: string | needle}}
|
|
|
|
*
|
|
|
|
* Finds last occurrence of "needle" in "string".
|
|
|
|
*
|
|
|
|
* Note: If the needle is an empty string, single space is used instead.
|
|
|
|
* Note: If the needle is not found, -1 is returned.
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string $inNeedle
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return int|string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runRPos( Parser $parser, $inStr = '', $inNeedle = '' ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
|
|
|
$inNeedle = $parser->killMarkers( (string)$inNeedle );
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ||
|
|
|
|
!$this->checkLength( $inNeedle ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( $inNeedle === '' ) {
|
|
|
|
$inNeedle = ' ';
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
$pos = mb_strrpos( $inStr, $inNeedle );
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( $pos === false ) {
|
|
|
|
$pos = -1;
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
return $pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#sub: string | start | length }}
|
|
|
|
*
|
|
|
|
* Returns substring of "string" starting at "start" and having
|
2010-10-02 22:36:34 +00:00
|
|
|
* "length" characters.
|
2009-06-05 09:53:05 +00:00
|
|
|
*
|
|
|
|
* Note: If length is zero, the rest of the input is returned.
|
2010-10-02 22:36:34 +00:00
|
|
|
* Note: A negative value for "start" operates from the end of the
|
2009-06-05 09:53:05 +00:00
|
|
|
* "string".
|
|
|
|
* Note: A negative value for "length" returns a string reduced in
|
|
|
|
* length by that amount.
|
2011-12-19 00:54:31 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string|int $inStart
|
|
|
|
* @param string|int $inLength
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runSub( Parser $parser, $inStr = '', $inStart = 0, $inLength = 0 ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2016-03-25 11:26:57 +00:00
|
|
|
if ( (int)$inLength === 0 ) {
|
|
|
|
$result = mb_substr( $inStr, (int)$inStart );
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
2016-03-25 11:26:57 +00:00
|
|
|
$result = mb_substr( $inStr, (int)$inStart, (int)$inLength );
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 22:36:34 +00:00
|
|
|
return $result;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#count: string | substr }}
|
|
|
|
*
|
|
|
|
* Returns number of occurrences of "substr" in "string".
|
|
|
|
*
|
|
|
|
* Note: If "substr" is empty, a single space is used.
|
2019-04-13 05:14:30 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
|
|
|
* @param string $inSubStr
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return int|string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runCount( Parser $parser, $inStr = '', $inSubStr = '' ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
|
|
|
$inSubStr = $parser->killMarkers( (string)$inSubStr );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ||
|
|
|
|
!$this->checkLength( $inSubStr ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $inSubStr === '' ) {
|
2011-12-19 00:54:31 +00:00
|
|
|
$inSubStr = ' ';
|
|
|
|
}
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
$result = mb_substr_count( $inStr, $inSubStr );
|
|
|
|
|
2010-10-02 22:36:34 +00:00
|
|
|
return $result;
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#replace:string | from | to | limit }}
|
|
|
|
*
|
|
|
|
* Replaces each occurrence of "from" in "string" with "to".
|
|
|
|
* At most "limit" replacements are performed.
|
2010-10-02 22:36:34 +00:00
|
|
|
*
|
2009-06-05 09:53:05 +00:00
|
|
|
* Note: Armored against replacements that would generate huge strings.
|
|
|
|
* Note: If "from" is an empty string, single space is used instead.
|
2019-04-13 05:14:30 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
|
|
|
* @param string $inReplaceFrom
|
|
|
|
* @param string $inReplaceTo
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string|int $inLimit
|
|
|
|
* @return string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runReplace( Parser $parser, $inStr = '',
|
2009-06-05 09:53:05 +00:00
|
|
|
$inReplaceFrom = '', $inReplaceTo = '', $inLimit = -1 ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
|
|
|
$inReplaceFrom = $parser->killMarkers( (string)$inReplaceFrom );
|
|
|
|
$inReplaceTo = $parser->killMarkers( (string)$inReplaceTo );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ||
|
|
|
|
!$this->checkLength( $inReplaceFrom ) ||
|
|
|
|
!$this->checkLength( $inReplaceTo ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 15:20:02 +00:00
|
|
|
if ( $inReplaceFrom === '' ) {
|
|
|
|
$inReplaceFrom = ' ';
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
// Precompute limit to avoid generating enormous string:
|
|
|
|
$diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom );
|
2010-10-02 22:36:34 +00:00
|
|
|
if ( $diff > 0 ) {
|
2023-11-15 14:33:03 +00:00
|
|
|
$limit = (int)( ( $this->config->get( 'PFStringLengthLimit' ) - mb_strlen( $inStr ) ) / $diff ) + 1;
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
|
|
|
$limit = -1;
|
|
|
|
}
|
|
|
|
|
2016-03-25 11:26:57 +00:00
|
|
|
$inLimit = (int)$inLimit;
|
2010-10-02 22:36:34 +00:00
|
|
|
if ( $inLimit >= 0 ) {
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $limit > $inLimit || $limit == -1 ) {
|
|
|
|
$limit = $inLimit;
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use regex to allow limit and handle UTF-8 correctly.
|
|
|
|
$inReplaceFrom = preg_quote( $inReplaceFrom, '/' );
|
2010-01-27 11:25:23 +00:00
|
|
|
$inReplaceTo = StringUtils::escapeRegexReplacement( $inReplaceTo );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2010-10-02 22:36:34 +00:00
|
|
|
$result = preg_replace( '/' . $inReplaceFrom . '/u',
|
|
|
|
$inReplaceTo, $inStr, $limit );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $result ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-08 16:18:33 +00:00
|
|
|
* {{#explode:string | delimiter | position | limit}}
|
2009-06-05 09:53:05 +00:00
|
|
|
*
|
|
|
|
* Breaks "string" into chunks separated by "delimiter" and returns the
|
|
|
|
* chunk identified by "position".
|
|
|
|
*
|
|
|
|
* Note: Negative position can be used to specify tokens from the end.
|
|
|
|
* Note: If the divider is an empty string, single space is used instead.
|
|
|
|
* Note: Empty string is returned if there are not enough exploded chunks.
|
2019-04-13 05:14:30 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
|
|
|
* @param string $inDiv
|
2021-11-19 17:14:51 +00:00
|
|
|
* @param string|int $inPos
|
|
|
|
* @param string|null $inLim
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
2009-06-05 09:53:05 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runExplode(
|
2019-04-13 05:14:30 +00:00
|
|
|
Parser $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null
|
2017-06-02 18:55:29 +00:00
|
|
|
) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
|
|
|
$inDiv = $parser->killMarkers( (string)$inDiv );
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2016-03-08 11:54:05 +00:00
|
|
|
if ( $inDiv === '' ) {
|
2011-12-19 00:54:31 +00:00
|
|
|
$inDiv = ' ';
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ||
|
|
|
|
!$this->checkLength( $inDiv ) ) {
|
|
|
|
return $this->tooLongError();
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$inDiv = preg_quote( $inDiv, '/' );
|
2010-10-02 22:36:34 +00:00
|
|
|
|
2021-11-19 17:14:51 +00:00
|
|
|
$matches = preg_split( '/' . $inDiv . '/u', $inStr, (int)$inLim );
|
2010-10-02 22:36:34 +00:00
|
|
|
|
|
|
|
if ( $inPos >= 0 && isset( $matches[$inPos] ) ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
$result = $matches[$inPos];
|
2010-10-02 22:36:34 +00:00
|
|
|
} elseif ( $inPos < 0 && isset( $matches[count( $matches ) + $inPos] ) ) {
|
|
|
|
$result = $matches[count( $matches ) + $inPos];
|
2009-06-05 09:53:05 +00:00
|
|
|
} else {
|
|
|
|
$result = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2010-05-29 16:12:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {{#urldecode:string}}
|
|
|
|
*
|
|
|
|
* Decodes URL-encoded (like%20that) strings.
|
2019-04-13 05:14:30 +00:00
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $inStr
|
2011-12-19 00:54:31 +00:00
|
|
|
* @return string
|
2010-05-29 16:12:41 +00:00
|
|
|
*/
|
2023-11-01 19:38:34 +00:00
|
|
|
public function runUrlDecode( Parser $parser, $inStr = '' ) {
|
2014-10-20 07:21:15 +00:00
|
|
|
$inStr = $parser->killMarkers( (string)$inStr );
|
2023-11-01 19:38:34 +00:00
|
|
|
if ( !$this->checkLength( $inStr ) ) {
|
|
|
|
return $this->tooLongError();
|
2010-05-29 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 20:06:09 +00:00
|
|
|
return urldecode( $inStr );
|
2010-05-29 16:12:41 +00:00
|
|
|
}
|
2014-02-15 03:26:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a PPNode (-ish thing), expand it, remove entities, and trim.
|
|
|
|
*
|
|
|
|
* For use when doing string comparisions, where user expects entities
|
|
|
|
* to be equal for what they stand for (e.g. comparisions with {{PAGENAME}})
|
|
|
|
*
|
2017-10-14 13:46:55 +00:00
|
|
|
* @param PPNode|string $obj Thing to expand
|
|
|
|
* @param PPFrame $frame
|
2022-10-09 08:17:19 +00:00
|
|
|
* @param string &$trimExpanded @phan-output-reference Expanded and trimmed version of PPNode,
|
2017-06-02 18:55:29 +00:00
|
|
|
* but with char refs intact
|
2017-10-14 13:46:55 +00:00
|
|
|
* @return string The trimmed, expanded and entity reference decoded version of the PPNode
|
2014-02-15 03:26:30 +00:00
|
|
|
*/
|
2022-10-09 08:17:19 +00:00
|
|
|
private static function decodeTrimExpand( $obj, PPFrame $frame, &$trimExpanded = '' ) {
|
2014-02-15 03:26:30 +00:00
|
|
|
$expanded = $frame->expand( $obj );
|
|
|
|
$trimExpanded = trim( $expanded );
|
|
|
|
return trim( Sanitizer::decodeCharReferences( $expanded ) );
|
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
}
|