mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ParserFunctions
synced 2024-11-15 03:35:52 +00:00
Add phpcs and make pass
Change-Id: I8f985ca83223f1a2fdb4365294701f3b1d9b019b
This commit is contained in:
parent
42ef20c152
commit
04e4c1b300
30
Expr.php
30
Expr.php
|
@ -48,7 +48,7 @@ define( 'EXPR_CEIL', 34 );
|
|||
define( 'EXPR_POW', 35 );
|
||||
define( 'EXPR_PI', 36 );
|
||||
define( 'EXPR_FMOD', 37 );
|
||||
define( 'EXPR_SQRT' , 38 );
|
||||
define( 'EXPR_SQRT', 38 );
|
||||
|
||||
class ExprError extends Exception {
|
||||
/**
|
||||
|
@ -69,7 +69,7 @@ class ExprError extends Exception {
|
|||
class ExprParser {
|
||||
public $maxStackSize = 100;
|
||||
|
||||
public $precedence = array(
|
||||
public $precedence = [
|
||||
EXPR_NEGATIVE => 10,
|
||||
EXPR_POSITIVE => 10,
|
||||
EXPR_EXPONENT => 10,
|
||||
|
@ -106,9 +106,9 @@ class ExprParser {
|
|||
EXPR_PI => 0,
|
||||
EXPR_OPEN => -1,
|
||||
EXPR_CLOSE => -1,
|
||||
);
|
||||
];
|
||||
|
||||
public $names = array(
|
||||
public $names = [
|
||||
EXPR_NEGATIVE => '-',
|
||||
EXPR_POSITIVE => '+',
|
||||
EXPR_NOT => 'not',
|
||||
|
@ -143,9 +143,9 @@ class ExprParser {
|
|||
EXPR_POW => '^',
|
||||
EXPR_PI => 'pi',
|
||||
EXPR_SQRT => 'sqrt',
|
||||
);
|
||||
];
|
||||
|
||||
public $words = array(
|
||||
public $words = [
|
||||
'mod' => EXPR_MOD,
|
||||
'fmod' => EXPR_FMOD,
|
||||
'and' => EXPR_AND,
|
||||
|
@ -168,7 +168,7 @@ class ExprParser {
|
|||
'ceil' => EXPR_CEIL,
|
||||
'pi' => EXPR_PI,
|
||||
'sqrt' => EXPR_SQRT,
|
||||
);
|
||||
];
|
||||
|
||||
/**
|
||||
* Evaluate a mathematical expression
|
||||
|
@ -181,12 +181,12 @@ class ExprParser {
|
|||
* @return string
|
||||
*/
|
||||
public function doExpression( $expr ) {
|
||||
$operands = array();
|
||||
$operators = array();
|
||||
$operands = [];
|
||||
$operators = [];
|
||||
|
||||
# Unescape inequality operators
|
||||
$expr = strtr( $expr, array( '<' => '<', '>' => '>',
|
||||
'−' => '-', '−' => '-' ) );
|
||||
$expr = strtr( $expr, [ '<' => '<', '>' => '>',
|
||||
'−' => '-', '−' => '-' ] );
|
||||
|
||||
$p = 0;
|
||||
$end = strlen( $expr );
|
||||
|
@ -239,7 +239,7 @@ class ExprParser {
|
|||
throw new ExprError( 'unrecognised_word', $word );
|
||||
}
|
||||
$op = $this->words[$word];
|
||||
switch( $op ) {
|
||||
switch ( $op ) {
|
||||
// constant
|
||||
case EXPR_EXPONENT:
|
||||
if ( $expecting !== 'expression' ) {
|
||||
|
@ -385,7 +385,9 @@ class ExprParser {
|
|||
}
|
||||
|
||||
// Finish off the operator array
|
||||
// @codingStandardsIgnoreStart
|
||||
while ( $op = array_pop( $operators ) ) {
|
||||
// @codingStandardsIgnoreEnd
|
||||
if ( $op == EXPR_OPEN ) {
|
||||
throw new ExprError( 'unclosed_bracket' );
|
||||
}
|
||||
|
@ -657,9 +659,11 @@ class ExprParser {
|
|||
}
|
||||
$right = array_pop( $stack );
|
||||
$left = array_pop( $stack );
|
||||
if ( false === ( $stack[] = pow( $left, $right ) ) ) {
|
||||
$result = pow( $left, $right );
|
||||
if ( $result === false ) {
|
||||
throw new ExprError( 'division_by_zero', $this->names[$op] );
|
||||
}
|
||||
$stack[] = $result;
|
||||
break;
|
||||
case EXPR_SQRT:
|
||||
if ( count( $stack ) < 1 ) {
|
||||
|
|
|
@ -37,13 +37,13 @@ class ParserFunctionsHooks {
|
|||
|
||||
// String Functions
|
||||
if ( $wgPFEnableStringFunctions ) {
|
||||
$parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' );
|
||||
$parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' );
|
||||
$parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' );
|
||||
$parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' );
|
||||
$parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' );
|
||||
$parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' );
|
||||
$parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' );
|
||||
$parser->setFunctionHook( 'len', 'ExtParserFunctions::runLen' );
|
||||
$parser->setFunctionHook( 'pos', 'ExtParserFunctions::runPos' );
|
||||
$parser->setFunctionHook( 'rpos', 'ExtParserFunctions::runRPos' );
|
||||
$parser->setFunctionHook( 'sub', 'ExtParserFunctions::runSub' );
|
||||
$parser->setFunctionHook( 'count', 'ExtParserFunctions::runCount' );
|
||||
$parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' );
|
||||
$parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' );
|
||||
$parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' );
|
||||
}
|
||||
|
||||
|
|
|
@ -2,19 +2,19 @@
|
|||
|
||||
class Scribunto_LuaParserFunctionsLibrary extends Scribunto_LuaLibraryBase {
|
||||
public function register() {
|
||||
$lib = array(
|
||||
'expr' => array( $this, 'expr' ),
|
||||
);
|
||||
$lib = [
|
||||
'expr' => [ $this, 'expr' ],
|
||||
];
|
||||
|
||||
return $this->getEngine()->registerInterface(
|
||||
__DIR__ . '/mw.ext.ParserFunctions.lua', $lib, array()
|
||||
__DIR__ . '/mw.ext.ParserFunctions.lua', $lib, []
|
||||
);
|
||||
}
|
||||
|
||||
public function expr( $expression = null ) {
|
||||
$this->checkType( 'mw.ext.ParserFunctions.expr', 1, $expression, 'string' );
|
||||
try {
|
||||
return array( ExtParserFunctions::getExprParser()->doExpression( $expression ) );
|
||||
return [ ExtParserFunctions::getExprParser()->doExpression( $expression ) ];
|
||||
} catch ( ExprError $e ) {
|
||||
throw new Scribunto_LuaError( $e->getMessage() );
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
class ExtParserFunctions {
|
||||
public static $mExprParser;
|
||||
public static $mTimeCache = array();
|
||||
public static $mTimeCache = [];
|
||||
public static $mTimeChars = 0;
|
||||
public static $mMaxTimeChars = 6000; # ~10 seconds
|
||||
|
||||
|
@ -22,7 +22,7 @@ class ExtParserFunctions {
|
|||
*/
|
||||
public static function registerClearHook() {
|
||||
static $done = false;
|
||||
if( !$done ) {
|
||||
if ( !$done ) {
|
||||
global $wgHooks;
|
||||
$wgHooks['ParserClearState'][] = __CLASS__ . '::clearState';
|
||||
$done = true;
|
||||
|
@ -248,7 +248,7 @@ class ExtParserFunctions {
|
|||
$from = $parser->getTitle()->getPrefixedText();
|
||||
}
|
||||
|
||||
$to = rtrim( $to , ' /' );
|
||||
$to = rtrim( $to, ' /' );
|
||||
|
||||
// if we have an empty path, or just one containing a dot
|
||||
if ( $to === '' || $to === '.' ) {
|
||||
|
@ -256,9 +256,9 @@ class ExtParserFunctions {
|
|||
}
|
||||
|
||||
// if the path isn't relative
|
||||
if ( substr( $to , 0 , 1 ) !== '/' &&
|
||||
substr( $to , 0 , 2 ) !== './' &&
|
||||
substr( $to , 0 , 3 ) !== '../' &&
|
||||
if ( substr( $to, 0, 1 ) !== '/' &&
|
||||
substr( $to, 0, 2 ) !== './' &&
|
||||
substr( $to, 0, 3 ) !== '../' &&
|
||||
$to !== '..' )
|
||||
{
|
||||
$from = '';
|
||||
|
@ -273,9 +273,9 @@ class ExtParserFunctions {
|
|||
$fullPath = preg_replace( '!/{2,}!', '/', $fullPath );
|
||||
|
||||
// remove the enclosing slashes now
|
||||
$fullPath = trim( $fullPath , '/' );
|
||||
$exploded = explode ( '/' , $fullPath );
|
||||
$newExploded = array();
|
||||
$fullPath = trim( $fullPath, '/' );
|
||||
$exploded = explode( '/', $fullPath );
|
||||
$newExploded = [];
|
||||
|
||||
foreach ( $exploded as $current ) {
|
||||
if ( $current === '..' ) { // removing one level
|
||||
|
@ -294,7 +294,7 @@ class ExtParserFunctions {
|
|||
}
|
||||
|
||||
// we can now join it again
|
||||
return implode( '/' , $newExploded );
|
||||
return implode( '/', $newExploded );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -350,7 +350,7 @@ class ExtParserFunctions {
|
|||
$parser->mOutput->addLink( $title, 0 );
|
||||
return $else;
|
||||
}
|
||||
if ( !$parser->incrementExpensiveFunctionCount() ) {
|
||||
if ( !$parser->incrementExpensiveFunctionCount() ) {
|
||||
return $else;
|
||||
}
|
||||
$id = $title->getArticleID();
|
||||
|
@ -410,7 +410,7 @@ class ExtParserFunctions {
|
|||
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
|
||||
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local];
|
||||
if ( $useTTL
|
||||
&& $cachedVal[1] !== null && $frame && is_callable( array( $frame, 'setTTL' ) )
|
||||
&& $cachedVal[1] !== null && $frame && is_callable( [ $frame, 'setTTL' ] )
|
||||
) {
|
||||
$frame->setTTL( $cachedVal[1] );
|
||||
}
|
||||
|
@ -492,8 +492,8 @@ class ExtParserFunctions {
|
|||
}
|
||||
}
|
||||
}
|
||||
self::$mTimeCache[$format][$cacheKey][$language][$local] = array( $result, $ttl );
|
||||
if ( $useTTL && $ttl !== null && $frame && is_callable( array( $frame, 'setTTL' ) ) ) {
|
||||
self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ];
|
||||
if ( $useTTL && $ttl !== null && $frame && is_callable( [ $frame, 'setTTL' ] ) ) {
|
||||
$frame->setTTL( $ttl );
|
||||
}
|
||||
return $result;
|
||||
|
@ -513,7 +513,6 @@ class ExtParserFunctions {
|
|||
return self::timeCommon( $parser, null, $format, $date, $language, $local );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $parser Parser
|
||||
* @param $frame PPFrame
|
||||
|
@ -613,7 +612,7 @@ class ExtParserFunctions {
|
|||
* @param $inStr string
|
||||
* @return int
|
||||
*/
|
||||
public static function runLen ( $parser, $inStr = '' ) {
|
||||
public static function runLen( $parser, $inStr = '' ) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
return mb_strlen( $inStr );
|
||||
}
|
||||
|
@ -631,7 +630,7 @@ class ExtParserFunctions {
|
|||
* @param $inOffset int
|
||||
* @return int|string
|
||||
*/
|
||||
public static function runPos ( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
|
||||
public static function runPos( $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
$inNeedle = $parser->killMarkers( (string)$inNeedle );
|
||||
|
||||
|
@ -640,10 +639,14 @@ class ExtParserFunctions {
|
|||
return self::tooLongError();
|
||||
}
|
||||
|
||||
if ( $inNeedle === '' ) { $inNeedle = ' '; }
|
||||
if ( $inNeedle === '' ) {
|
||||
$inNeedle = ' ';
|
||||
}
|
||||
|
||||
$pos = mb_strpos( $inStr, $inNeedle, (int)$inOffset );
|
||||
if ( $pos === false ) { $pos = ''; }
|
||||
if ( $pos === false ) {
|
||||
$pos = '';
|
||||
}
|
||||
|
||||
return $pos;
|
||||
}
|
||||
|
@ -660,7 +663,7 @@ class ExtParserFunctions {
|
|||
* @param $inNeedle int|string
|
||||
* @return int|string
|
||||
*/
|
||||
public static function runRPos ( $parser, $inStr = '', $inNeedle = '' ) {
|
||||
public static function runRPos( $parser, $inStr = '', $inNeedle = '' ) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
$inNeedle = $parser->killMarkers( (string)$inNeedle );
|
||||
|
||||
|
@ -669,10 +672,14 @@ class ExtParserFunctions {
|
|||
return self::tooLongError();
|
||||
}
|
||||
|
||||
if ( $inNeedle === '' ) { $inNeedle = ' '; }
|
||||
if ( $inNeedle === '' ) {
|
||||
$inNeedle = ' ';
|
||||
}
|
||||
|
||||
$pos = mb_strrpos( $inStr, $inNeedle );
|
||||
if ( $pos === false ) { $pos = -1; }
|
||||
if ( $pos === false ) {
|
||||
$pos = -1;
|
||||
}
|
||||
|
||||
return $pos;
|
||||
}
|
||||
|
@ -695,7 +702,7 @@ class ExtParserFunctions {
|
|||
* @param $inLength int
|
||||
* @return string
|
||||
*/
|
||||
public static function runSub ( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) {
|
||||
public static function runSub( $parser, $inStr = '', $inStart = 0, $inLength = 0 ) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
|
||||
if ( !self::checkLength( $inStr ) ) {
|
||||
|
@ -722,7 +729,7 @@ class ExtParserFunctions {
|
|||
* @param $inSubStr string
|
||||
* @return int|string
|
||||
*/
|
||||
public static function runCount ( $parser, $inStr = '', $inSubStr = '' ) {
|
||||
public static function runCount( $parser, $inStr = '', $inSubStr = '' ) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
$inSubStr = $parser->killMarkers( (string)$inSubStr );
|
||||
|
||||
|
@ -769,7 +776,9 @@ class ExtParserFunctions {
|
|||
return self::tooLongError();
|
||||
}
|
||||
|
||||
if ( $inReplaceFrom === '' ) { $inReplaceFrom = ' '; }
|
||||
if ( $inReplaceFrom === '' ) {
|
||||
$inReplaceFrom = ' ';
|
||||
}
|
||||
|
||||
// Precompute limit to avoid generating enormous string:
|
||||
$diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom );
|
||||
|
@ -800,7 +809,6 @@ class ExtParserFunctions {
|
|||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {{#explode:string | delimiter | position | limit}}
|
||||
*
|
||||
|
@ -817,7 +825,7 @@ class ExtParserFunctions {
|
|||
* @param $inLim int|null
|
||||
* @return string
|
||||
*/
|
||||
public static function runExplode (
|
||||
public static function runExplode(
|
||||
$parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null
|
||||
) {
|
||||
$inStr = $parser->killMarkers( (string)$inStr );
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
{
|
||||
"require-dev": {
|
||||
"jakub-onderka/php-parallel-lint": "0.9.2",
|
||||
"jakub-onderka/php-console-highlighter": "0.3.2"
|
||||
"jakub-onderka/php-console-highlighter": "0.3.2",
|
||||
"mediawiki/mediawiki-codesniffer": "0.7.2"
|
||||
},
|
||||
"scripts": {
|
||||
"fix": "phpcbf",
|
||||
"test": [
|
||||
"parallel-lint . --exclude node_modules --exclude vendor"
|
||||
"parallel-lint . --exclude node_modules --exclude vendor",
|
||||
"phpcs -p -s"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
12
phpcs.xml
Normal file
12
phpcs.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ruleset>
|
||||
<rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
|
||||
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
|
||||
<exclude name="MediaWiki.ControlStructures.IfElseStructure"/>
|
||||
</rule>
|
||||
<file>.</file>
|
||||
<arg name="extensions" value="php,php5,inc"/>
|
||||
<arg name="encoding" value="UTF-8"/>
|
||||
<exclude-pattern>vendor</exclude-pattern>
|
||||
<exclude-pattern>node_modules</exclude-pattern>
|
||||
</ruleset>
|
|
@ -22,55 +22,55 @@ class ExpressionTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
function provideExpressions() {
|
||||
return array(
|
||||
array( '1 or 0', '1' ),
|
||||
array( 'not (1 and 0)', '1' ),
|
||||
array( 'not 0', '1' ),
|
||||
array( '4 < 5', '1' ),
|
||||
array( '-5 < 2', '1' ),
|
||||
array( '-2 <= -2', '1' ),
|
||||
array( '4 > 3', '1' ),
|
||||
array( '4 > -3', '1' ),
|
||||
array( '5 >= 2', '1' ),
|
||||
array( '2 >= 2', '1' ),
|
||||
array( '1 != 2', '1' ),
|
||||
array( '-4 * -4 = 4 * 4', '1' ),
|
||||
array( 'not (1 != 1)', '1' ),
|
||||
array( '1 + 1', '2' ),
|
||||
array( '-1 + 1', '0' ),
|
||||
array( '+1 + 1', '2' ),
|
||||
array( '4 * 4', '16' ),
|
||||
array( '(1/3) * 3', '1' ),
|
||||
array( '3 / 1.5', '2' ),
|
||||
array( '3 / 0.2', '15' ),
|
||||
array( '3 / ( 2.0 * 0.1 )', '15' ),
|
||||
array( '3 / ( 2.0 / 10 )', '15' ),
|
||||
array( '3 / (- 0.2 )', '-15' ),
|
||||
array( '3 / abs( 0.2 )', '15' ),
|
||||
array( '3 mod 2', '1' ),
|
||||
array( '1e4', '10000' ),
|
||||
array( '1e-2', '0.01' ),
|
||||
array( '4.0 round 0', '4' ),
|
||||
array( 'ceil 4', '4' ),
|
||||
array( 'floor 4', '4' ),
|
||||
array( '4.5 round 0', '5' ),
|
||||
array( '4.2 round 0', '4' ),
|
||||
array( '-4.2 round 0', '-4' ),
|
||||
array( '-4.5 round 0', '-5' ),
|
||||
array( '-2.0 round 0', '-2' ),
|
||||
array( 'ceil -3', '-3' ),
|
||||
array( 'floor -6.0', '-6' ),
|
||||
array( 'ceil 4.2', '5' ),
|
||||
array( 'ceil -4.5', '-4' ),
|
||||
array( 'floor -4.5', '-5' ),
|
||||
array( 'abs(-2)', '2' ),
|
||||
array( 'ln(exp(1))', '1' ),
|
||||
array( 'trunc(4.5)', '4' ),
|
||||
array( 'trunc(-4.5)', '-4' ),
|
||||
array( '123 fmod (2^64-1)', '123' ),
|
||||
array( '5.7 mod 1.3', '0' ),
|
||||
array( '5.7 fmod 1.3', '0.5' ),
|
||||
);
|
||||
return [
|
||||
[ '1 or 0', '1' ],
|
||||
[ 'not (1 and 0)', '1' ],
|
||||
[ 'not 0', '1' ],
|
||||
[ '4 < 5', '1' ],
|
||||
[ '-5 < 2', '1' ],
|
||||
[ '-2 <= -2', '1' ],
|
||||
[ '4 > 3', '1' ],
|
||||
[ '4 > -3', '1' ],
|
||||
[ '5 >= 2', '1' ],
|
||||
[ '2 >= 2', '1' ],
|
||||
[ '1 != 2', '1' ],
|
||||
[ '-4 * -4 = 4 * 4', '1' ],
|
||||
[ 'not (1 != 1)', '1' ],
|
||||
[ '1 + 1', '2' ],
|
||||
[ '-1 + 1', '0' ],
|
||||
[ '+1 + 1', '2' ],
|
||||
[ '4 * 4', '16' ],
|
||||
[ '(1/3) * 3', '1' ],
|
||||
[ '3 / 1.5', '2' ],
|
||||
[ '3 / 0.2', '15' ],
|
||||
[ '3 / ( 2.0 * 0.1 )', '15' ],
|
||||
[ '3 / ( 2.0 / 10 )', '15' ],
|
||||
[ '3 / (- 0.2 )', '-15' ],
|
||||
[ '3 / abs( 0.2 )', '15' ],
|
||||
[ '3 mod 2', '1' ],
|
||||
[ '1e4', '10000' ],
|
||||
[ '1e-2', '0.01' ],
|
||||
[ '4.0 round 0', '4' ],
|
||||
[ 'ceil 4', '4' ],
|
||||
[ 'floor 4', '4' ],
|
||||
[ '4.5 round 0', '5' ],
|
||||
[ '4.2 round 0', '4' ],
|
||||
[ '-4.2 round 0', '-4' ],
|
||||
[ '-4.5 round 0', '-5' ],
|
||||
[ '-2.0 round 0', '-2' ],
|
||||
[ 'ceil -3', '-3' ],
|
||||
[ 'floor -6.0', '-6' ],
|
||||
[ 'ceil 4.2', '5' ],
|
||||
[ 'ceil -4.5', '-4' ],
|
||||
[ 'floor -4.5', '-5' ],
|
||||
[ 'abs(-2)', '2' ],
|
||||
[ 'ln(exp(1))', '1' ],
|
||||
[ 'trunc(4.5)', '4' ],
|
||||
[ 'trunc(-4.5)', '-4' ],
|
||||
[ '123 fmod (2^64-1)', '123' ],
|
||||
[ '5.7 mod 1.3', '0' ],
|
||||
[ '5.7 fmod 1.3', '0.5' ],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue