summaryrefslogtreecommitdiff
path: root/extensions/ParserFunctions/ParserFunctions_body.php
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/ParserFunctions/ParserFunctions_body.php')
-rw-r--r--extensions/ParserFunctions/ParserFunctions_body.php343
1 files changed, 194 insertions, 149 deletions
diff --git a/extensions/ParserFunctions/ParserFunctions_body.php b/extensions/ParserFunctions/ParserFunctions_body.php
index 13611510..d68ed8b7 100644
--- a/extensions/ParserFunctions/ParserFunctions_body.php
+++ b/extensions/ParserFunctions/ParserFunctions_body.php
@@ -7,6 +7,10 @@ class ExtParserFunctions {
static $mTimeChars = 0;
static $mMaxTimeChars = 6000; # ~10 seconds
+ /**
+ * @param $parser Parser
+ * @return bool
+ */
public static function clearState( $parser ) {
self::$mTimeChars = 0;
$parser->pf_ifexist_breakdown = array();
@@ -30,6 +34,8 @@ class ExtParserFunctions {
/**
* Get the marker regex. Cached.
+ * @param $parser Parser
+ * @return
*/
public static function getMarkerRegex( $parser ) {
self::registerClearHook();
@@ -41,16 +47,7 @@ class ExtParserFunctions {
$prefix = preg_quote( $parser->uniqPrefix(), '/' );
- // The first line represents Parser from release 1.12 forward.
- // subsequent lines are hacks to accomodate old Mediawiki versions.
- if ( defined( 'Parser::MARKER_SUFFIX' ) )
- $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' );
- elseif ( isset( $parser->mMarkerSuffix ) )
- $suffix = preg_quote( $parser->mMarkerSuffix, '/' );
- elseif ( defined( 'MW_PARSER_VERSION' ) &&
- strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 )
- $suffix = "QINU\x07";
- else $suffix = 'QINU';
+ $suffix = preg_quote( Parser::MARKER_SUFFIX, '/' );
$parser->pf_markerRegex = '/' . $prefix . '(?:(?!' . $suffix . ').)*' . $suffix . '/us';
@@ -58,7 +55,11 @@ class ExtParserFunctions {
return $parser->pf_markerRegex;
}
- // Removes unique markers from passed parameters, used by string functions.
+ /**
+ * @param $parser Parser
+ * @param $text string
+ * @return string
+ */
private static function killMarkers ( $parser, $text ) {
return preg_replace( self::getMarkerRegex( $parser ), '' , $text );
}
@@ -73,6 +74,11 @@ class ExtParserFunctions {
return self::$mExprParser;
}
+ /**
+ * @param $parser Parser
+ * @param $expr string
+ * @return string
+ */
public static function expr( $parser, $expr = '' ) {
try {
return self::getExprParser()->doExpression( $expr );
@@ -81,6 +87,13 @@ class ExtParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $expr string
+ * @param $then string
+ * @param $else string
+ * @return string
+ */
public static function ifexpr( $parser, $expr = '', $then = '', $else = '' ) {
try {
$ret = self::getExprParser()->doExpression( $expr );
@@ -97,6 +110,12 @@ class ExtParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $args array
+ * @return string
+ */
public static function ifexprObj( $parser, $frame, $args ) {
$expr = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$then = isset( $args[1] ) ? $args[1] : '';
@@ -108,14 +127,12 @@ class ExtParserFunctions {
return $result;
}
- public static function ifHook( $parser, $test = '', $then = '', $else = '' ) {
- if ( $test !== '' ) {
- return $then;
- } else {
- return $else;
- }
- }
-
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $args array
+ * @return string
+ */
public static function ifObj( $parser, $frame, $args ) {
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
if ( $test !== '' ) {
@@ -125,14 +142,12 @@ class ExtParserFunctions {
}
}
- public static function ifeq( $parser, $left = '', $right = '', $then = '', $else = '' ) {
- if ( $left == $right ) {
- return $then;
- } else {
- return $else;
- }
- }
-
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $args array
+ * @return string
+ */
public static function ifeqObj( $parser, $frame, $args ) {
$left = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$right = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
@@ -143,6 +158,13 @@ class ExtParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $test string
+ * @param $then string
+ * @param $else bool
+ * @return bool|string
+ */
public static function iferror( $parser, $test = '', $then = '', $else = false ) {
if ( preg_match( '/<(?:strong|span|p|div)\s(?:[^\s>]*\s+)*?class="(?:[^"\s>]*\s+)*?error(?:\s[^">]*)?"/', $test ) ) {
return $then;
@@ -153,6 +175,12 @@ class ExtParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $args array
+ * @return string
+ */
public static function iferrorObj( $parser, $frame, $args ) {
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$then = isset( $args[1] ) ? $args[1] : false;
@@ -165,47 +193,7 @@ class ExtParserFunctions {
}
}
- public static function switchHook( $parser /*,...*/ ) {
- $args = func_get_args();
- array_shift( $args );
- $primary = trim( array_shift( $args ) );
- $found = $defaultFound = false;
- $parts = null;
- $default = null;
- $mwDefault =& MagicWord::get( 'default' );
- foreach ( $args as $arg ) {
- $parts = array_map( 'trim', explode( '=', $arg, 2 ) );
- if ( count( $parts ) == 2 ) {
- # Found "="
- if ( $found || $parts[0] == $primary ) {
- # Found a match, return now
- return $parts[1];
- } elseif ( $defaultFound || $mwDefault->matchStartAndRemove( $parts[0] ) ) {
- $default = $parts[1];
- } # else wrong case, continue
- } elseif ( count( $parts ) == 1 ) {
- # Multiple input, single output
- # If the value matches, set a flag and continue
- if ( $parts[0] == $primary ) {
- $found = true;
- } elseif ( $mwDefault->matchStartAndRemove( $parts[0] ) ) {
- $defaultFound = true;
- }
- } # else RAM corruption due to cosmic ray?
- }
- # Default case
- # Check if the last item had no = sign, thus specifying the default case
- if ( count( $parts ) == 1 ) {
- return $parts[0];
- } elseif ( !is_null( $default ) ) {
- return $default;
- } else {
- return '';
- }
- }
-
/**
- * @static
* @param $parser Parser
* @param $frame PPFrame
* @param $args
@@ -272,6 +260,10 @@ class ExtParserFunctions {
* initial slash is treated as a relative path, and vice versa.
*
* @param $parser Parser
+ * @param $to string
+ * @param $from string
+ *
+ * @return string
*/
public static function rel2abs( $parser , $to = '' , $from = '' ) {
@@ -328,7 +320,6 @@ class ExtParserFunctions {
}
/**
- * @static
* @param $parser Parser
* @param $frame PPFrame
* @return bool
@@ -348,10 +339,15 @@ class ExtParserFunctions {
return $parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit;
}
- public static function ifexist( $parser, $title = '', $then = '', $else = '' ) {
- return self::ifexistCommon( $parser, false, $title, $then, $else );
- }
-
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $titletext string
+ * @param $then string
+ * @param $else string
+ *
+ * @return string
+ */
public static function ifexistCommon( $parser, $frame, $titletext = '', $then = '', $else = '' ) {
global $wgContLang;
$title = Title::newFromText( $titletext );
@@ -385,11 +381,12 @@ class ExtParserFunctions {
return $else;
} else {
$pdbk = $title->getPrefixedDBkey();
- $lc = LinkCache::singleton();
if ( !self::incrementIfexistCount( $parser, $frame ) ) {
return $else;
}
- if ( 0 != ( $id = $lc->getGoodLinkID( $pdbk ) ) ) {
+ $lc = LinkCache::singleton();
+ $id = $lc->getGoodLinkID( $pdbk );
+ if ( $id != 0 ) {
$parser->mOutput->addLink( $title, $id );
return $then;
} elseif ( $lc->isBadLink( $pdbk ) ) {
@@ -406,6 +403,12 @@ class ExtParserFunctions {
return $else;
}
+ /**
+ * @param $parser Parser
+ * @param $frame PPFrame
+ * @param $args array
+ * @return string
+ */
public static function ifexistObj( $parser, $frame, $args ) {
$title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$then = isset( $args[1] ) ? $args[1] : null;
@@ -419,81 +422,65 @@ class ExtParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $format string
+ * @param $date string
+ * @param $language string
+ * @param $local string|bool
+ * @return string
+ */
public static function time( $parser, $format = '', $date = '', $language = '', $local = false ) {
- global $wgLang, $wgContLang, $wgLocaltimezone;
+ global $wgLocaltimezone;
self::registerClearHook();
- if ( isset( self::$mTimeCache[$format][$date][$language][$local] ) ) {
- return self::$mTimeCache[$format][$date][$language][$local];
+ if ( $date === '' ) {
+ $cacheKey = $parser->getOptions()->getTimestamp();
+ $date = wfTimestamp( TS_ISO_8601, $cacheKey );
+ } else {
+ $cacheKey = $date;
+ }
+ if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
+ return self::$mTimeCache[$format][$cacheKey][$language][$local];
}
# compute the timestamp string $ts
# PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object
- # PHP < 5.2 is limited to dates between 1970 and 2038
$invalidTime = false;
- if ( class_exists( 'DateTime' ) ) { # PHP >= 5.2
- # 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 {
- # Determine timezone
- if ( $local ) {
- # convert to MediaWiki local timezone if set
- if ( isset( $wgLocaltimezone ) ) {
- $tz = new DateTimeZone( $wgLocaltimezone );
- } else {
- $tz = new DateTimeZone( date_default_timezone_get() );
- }
- } else {
- # if local time was not requested, convert to UTC
- $tz = new DateTimeZone( 'UTC' );
- }
-
- # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
- if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
- $date = '00:00 '.$date;
- }
+ # 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 {
- # Parse date
- if ( $date !== '' ) {
- $dateObject = new DateTime( $date, $tz );
- } else {
- # use current date and time
- $dateObject = new DateTime( 'now', $tz );
- }
+ # Default input timezone is UTC.
+ $utc = new DateTimeZone( 'UTC' );
- # Generate timestamp
- $ts = $dateObject->format( 'YmdHis' );
- } catch ( Exception $ex ) {
- $invalidTime = true;
- }
- } else { # PHP < 5.2
- if ( $date !== '' ) {
- $unix = @strtotime( $date );
- } else {
- $unix = time();
+ # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
+ if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
+ $date = '00:00 '.$date;
}
- if ( $unix == -1 || $unix == false ) {
- $invalidTime = true;
- } else {
- if ( $local ) {
- # Use the time zone
- if ( isset( $wgLocaltimezone ) ) {
- $oldtz = getenv( 'TZ' );
- putenv( 'TZ=' . $wgLocaltimezone );
- }
- wfSuppressWarnings(); // E_STRICT system time bitching
- $ts = date( 'YmdHis', $unix );
- wfRestoreWarnings();
- if ( isset( $wgLocaltimezone ) ) {
- putenv( 'TZ=' . $oldtz );
- }
+ # Parse date
+ # UTC is a default input timezone.
+ $dateObject = new DateTime( $date, $utc );
+
+ # Set output timezone.
+ if ( $local ) {
+ if ( isset( $wgLocaltimezone ) ) {
+ $tz = new DateTimeZone( $wgLocaltimezone );
} else {
- $ts = wfTimestamp( TS_MW, $unix );
+ $tz = new DateTimeZone( date_default_timezone_get() );
}
+ $dateObject->setTimezone( $tz );
+ } else {
+ $dateObject->setTimezone( $utc );
}
+ # Generate timestamp
+ $ts = $dateObject->format( 'YmdHis' );
+
+ } catch ( Exception $ex ) {
+ $invalidTime = true;
}
# format the timestamp and return the result
@@ -518,10 +505,17 @@ class ExtParserFunctions {
}
}
}
- self::$mTimeCache[$format][$date][$language][$local] = $result;
+ self::$mTimeCache[$format][$cacheKey][$language][$local] = $result;
return $result;
}
+ /**
+ * @param $parser Parser
+ * @param $format string
+ * @param $date string
+ * @param $language string
+ * @return string
+ */
public static function localTime( $parser, $format = '', $date = '', $language = '' ) {
return self::time( $parser, $format, $date, $language, true );
}
@@ -530,10 +524,10 @@ class ExtParserFunctions {
* Obtain a specified number of slash-separated parts of a title,
* e.g. {{#titleparts:Hello/World|1}} => "Hello"
*
- * @param Parser $parser Parent parser
- * @param string $title Title to split
- * @param int $parts Number of parts to keep
- * @param int $offset Offset starting at 1
+ * @param $parser Parent parser
+ * @param $title string Title to split
+ * @param $parts int Number of parts to keep
+ * @param $offset int Offset starting at 1
* @return string
*/
public static function titleparts( $parser, $title = '', $parts = 0, $offset = 0 ) {
@@ -570,6 +564,9 @@ class ExtParserFunctions {
return self::$mConvertParser;
}
+ /**
+ * @return string
+ */
public static function convert( /*...*/ ) {
try {
$args = func_get_args();
@@ -579,13 +576,20 @@ class ExtParserFunctions {
}
}
- // Verifies parameter is less than max string length.
+ /**
+ * Verifies parameter is less than max string length.
+ * @param $text
+ * @return bool
+ */
private static function checkLength( $text ) {
global $wgPFStringLengthLimit;
return ( mb_strlen( $text ) < $wgPFStringLengthLimit );
}
- // Generates error message. Called when string is too long.
+ /**
+ * Generates error message. Called when string is too long.
+ * @return string
+ */
private static function tooLongError() {
global $wgPFStringLengthLimit, $wgContLang;
return '<strong class="error">' .
@@ -599,6 +603,9 @@ class ExtParserFunctions {
* {{#len:string}}
*
* Reports number of characters in string.
+ * @param $parser Parser
+ * @param $inStr string
+ * @return int
*/
public static function runLen ( $parser, $inStr = '' ) {
wfProfileIn( __METHOD__ );
@@ -617,6 +624,11 @@ class ExtParserFunctions {
*
* Note: If the needle is an empty string, single space is used instead.
* Note: If the needle is not found, empty string is returned.
+ * @param $parser Parser
+ * @param $inStr string
+ * @param $inNeedle int
+ * @param $inOffset int
+ * @return int|string
*/
public static function runPos ( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
wfProfileIn( __METHOD__ );
@@ -625,7 +637,7 @@ class ExtParserFunctions {
$inNeedle = self::killMarkers( $parser, (string)$inNeedle );
if ( !self::checkLength( $inStr ) ||
- !self::checkLength( $inNeedle ) ) {
+ !self::checkLength( $inNeedle ) ) {
wfProfileOut( __METHOD__ );
return self::tooLongError();
}
@@ -646,6 +658,10 @@ class ExtParserFunctions {
*
* Note: If the needle is an empty string, single space is used instead.
* Note: If the needle is not found, -1 is returned.
+ * @param $parser Parser
+ * @param $inStr string
+ * @param $inNeedle int
+ * @return int|string
*/
public static function runRPos ( $parser, $inStr = '', $inNeedle = '' ) {
wfProfileIn( __METHOD__ );
@@ -654,7 +670,7 @@ class ExtParserFunctions {
$inNeedle = self::killMarkers( $parser, (string)$inNeedle );
if ( !self::checkLength( $inStr ) ||
- !self::checkLength( $inNeedle ) ) {
+ !self::checkLength( $inNeedle ) ) {
wfProfileOut( __METHOD__ );
return self::tooLongError();
}
@@ -679,6 +695,12 @@ class ExtParserFunctions {
* "string".
* Note: A negative value for "length" returns a string reduced in
* length by that amount.
+ *
+ * @param $parser Parser
+ * @param $inStr string
+ * @param $inStart int
+ * @param $inLength int
+ * @return string
*/
public static function runSub ( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) {
wfProfileIn( __METHOD__ );
@@ -693,7 +715,7 @@ class ExtParserFunctions {
if ( intval( $inLength ) == 0 ) {
$result = mb_substr( $inStr, $inStart );
} else {
- $result = mb_substr( $inStr, $inStart, $inLength );
+ $result = mb_substr( $inStr, intval( $inStart ), intval( $inLength ) );
}
wfProfileOut( __METHOD__ );
@@ -706,6 +728,10 @@ class ExtParserFunctions {
* Returns number of occurrences of "substr" in "string".
*
* Note: If "substr" is empty, a single space is used.
+ * @param $parser
+ * @param $inStr string
+ * @param $inSubStr string
+ * @return int|string
*/
public static function runCount ( $parser, $inStr = '', $inSubStr = '' ) {
wfProfileIn( __METHOD__ );
@@ -714,12 +740,14 @@ class ExtParserFunctions {
$inSubStr = self::killMarkers( $parser, (string)$inSubStr );
if ( !self::checkLength( $inStr ) ||
- !self::checkLength( $inSubStr ) ) {
+ !self::checkLength( $inSubStr ) ) {
wfProfileOut( __METHOD__ );
return self::tooLongError();
}
- if ( $inSubStr == '' ) { $inSubStr = ' '; }
+ if ( $inSubStr == '' ) {
+ $inSubStr = ' ';
+ }
$result = mb_substr_count( $inStr, $inSubStr );
@@ -735,6 +763,12 @@ class ExtParserFunctions {
*
* Note: Armored against replacements that would generate huge strings.
* Note: If "from" is an empty string, single space is used instead.
+ * @param $parser Parser
+ * @param $inStr string
+ * @param $inReplaceFrom string
+ * @param $inReplaceTo string
+ * @param $inLimit int
+ * @return mixed|string
*/
public static function runReplace( $parser, $inStr = '',
$inReplaceFrom = '', $inReplaceTo = '', $inLimit = -1 ) {
@@ -746,8 +780,8 @@ class ExtParserFunctions {
$inReplaceTo = self::killMarkers( $parser, (string)$inReplaceTo );
if ( !self::checkLength( $inStr ) ||
- !self::checkLength( $inReplaceFrom ) ||
- !self::checkLength( $inReplaceTo ) ) {
+ !self::checkLength( $inReplaceFrom ) ||
+ !self::checkLength( $inReplaceTo ) ) {
wfProfileOut( __METHOD__ );
return self::tooLongError();
}
@@ -793,6 +827,12 @@ class ExtParserFunctions {
* 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.
+ * @param $parser Parser
+ * @param $inStr string
+ * @param $inDiv string
+ * @param $inPos int
+ * @param $inLim int|null
+ * @return string
*/
public static function runExplode ( $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null ) {
wfProfileIn( __METHOD__ );
@@ -800,10 +840,12 @@ class ExtParserFunctions {
$inStr = self::killMarkers( $parser, (string)$inStr );
$inDiv = self::killMarkers( $parser, (string)$inDiv );
- if ( $inDiv == '' ) { $inDiv = ' '; }
+ if ( $inDiv == '' ) {
+ $inDiv = ' ';
+ }
if ( !self::checkLength( $inStr ) ||
- !self::checkLength( $inDiv ) ) {
+ !self::checkLength( $inDiv ) ) {
wfProfileOut( __METHOD__ );
return self::tooLongError();
}
@@ -828,6 +870,9 @@ class ExtParserFunctions {
* {{#urldecode:string}}
*
* Decodes URL-encoded (like%20that) strings.
+ * @param $parser Parser
+ * @param $inStr string
+ * @return string
*/
public static function runUrlDecode( $parser, $inStr = '' ) {
wfProfileIn( __METHOD__ );