Pastakhov has uploaded a new change for review. https://gerrit.wikimedia.org/r/67651
Change subject: Add String Functions (version 0.4.2) ...................................................................... Add String Functions (version 0.4.2) * fix for passByReference Change-Id: Id3a7f6ed3303660cff3bd3c3a41ffd45fa887993 --- M Foxway.body.php M Foxway.php M includes/Debug.php M includes/Interpreter.php M includes/Runtime.php M includes/RuntimeDebug.php A includes/functions/FString.php M tests/phpunit/includes/InterpreterTest.php 8 files changed, 302 insertions(+), 6 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway refs/changes/51/67651/1 diff --git a/Foxway.body.php b/Foxway.body.php index 0013902..10aade1 100644 --- a/Foxway.body.php +++ b/Foxway.body.php @@ -66,7 +66,7 @@ $return .= self::insertNoWiki( $parser, array_shift($result) ) . "\n"; } - return $return . self::insertGeneral( $parser, $parser->recursiveTagParse(implode('', $result),$frame) ); + return $return . self::insertGeneral( $parser, $parser->recursiveTagParse(implode($result),$frame) ); } /** diff --git a/Foxway.php b/Foxway.php index 2ddd4b0..6e5df7b 100644 --- a/Foxway.php +++ b/Foxway.php @@ -15,7 +15,7 @@ die( 'This file is an extension to MediaWiki and thus not a valid entry point.' ); } -define( 'Foxway_VERSION' , '0.4.1' ); +define( 'Foxway_VERSION' , '0.4.2' ); // Register this extension on Special:Version $wgExtensionCredits['parserhook'][] = array( @@ -64,6 +64,7 @@ $wgAutoloadClasses['Foxway\\BaseFunction'] = $dir . '/includes/functions/BaseFunction.php'; $wgAutoloadClasses['Foxway\\FArray'] = $dir . '/includes/functions/FArray.php'; +$wgAutoloadClasses['Foxway\\FString'] = $dir . '/includes/functions/FString.php'; $wgAutoloadClasses['Foxway\\FVariable'] = $dir . '/includes/functions/FVariable.php'; // Resources @@ -111,6 +112,7 @@ 'rsort' => 1, 'shuffle' => 1, 'sort' => 1, + 'similar_text' => 4, // 0b100 ); $wgFoxwayFunctions = array( @@ -208,4 +210,39 @@ 'sizeof', 'sort', ), + 'FString' => array( // String Functions @see http://www.php.net/manual/en/ref.strings.php + 'addcslashes', + 'addslashes', + 'chop', + 'chr', + 'chunk_split', + 'convert_cyr_string', + 'convert_uudecode', + 'convert_uuencode', + 'count_chars', + 'crc32', + 'crypt', + 'explode', + 'html_entity_decode', + 'htmlentities', + 'htmlspecialchars_decode', + 'htmlspecialchars', + 'implode', + 'join', + 'lcfirst', + 'levenshtein', + 'ltrim', + 'md5', + 'metaphone', + 'money_format', + 'nl_langinfo', + 'nl2br', + 'number_format', + 'ord', + 'printf', + 'quotemeta', + 'rtrim', + 'sha1', + 'similar_text', + ), ); diff --git a/includes/Debug.php b/includes/Debug.php index 25f7d23..437bfca 100644 --- a/includes/Debug.php +++ b/includes/Debug.php @@ -77,6 +77,7 @@ case T_BOOL_CAST: // (bool) case T_UNSET_CAST: // (unset) case T_ECHO: + case T_PRINT: case T_IF: case T_ELSE: case T_ELSEIF: diff --git a/includes/Interpreter.php b/includes/Interpreter.php index 57f0955..6426da0 100644 --- a/includes/Interpreter.php +++ b/includes/Interpreter.php @@ -292,6 +292,10 @@ $runtime->addCommand($id); $parenthesFlags = FOXWAY_ALLOW_LIST_PARAMS | FOXWAY_EXPECT_SEMICOLON; break; + case T_PRINT: + $runtime->addCommand($id); + $parenthesFlags = FOXWAY_EXPECT_SEMICOLON; + break; case T_CONSTANT_ENCAPSED_STRING: $is_apostrophe = substr($text, 0, 1) == '\'' ? true : false; $string = substr($text, 1, -1); @@ -443,6 +447,7 @@ } switch ($command) { case T_ECHO: + case T_PRINT: $return = array_merge($return, $result); break; case T_IF: @@ -554,6 +559,7 @@ } // break is not necessary here case T_ECHO: + case T_PRINT: case ',': case T_CONCAT_EQUAL: // .= case T_PLUS_EQUAL: // += @@ -973,6 +979,97 @@ //'SORT_NATURAL' => SORT_NATURAL, // @todo PHP >= 5.4.0 //'SORT_FLAG_CASE' => SORT_FLAG_CASE, // @todo PHP >= 5.4.0 'COUNT_RECURSIVE' => COUNT_RECURSIVE, + 'CRYPT_STD_DES' => CRYPT_STD_DES, + 'CRYPT_EXT_DES' => CRYPT_EXT_DES, + 'CRYPT_MD5' => CRYPT_MD5, + 'CRYPT_BLOWFISH' => CRYPT_BLOWFISH, + 'CRYPT_SHA256' => CRYPT_SHA256, + 'CRYPT_SHA512' => CRYPT_SHA512, + 'ENT_COMPAT' => ENT_COMPAT, + 'ENT_QUOTES' => ENT_QUOTES, + 'ENT_NOQUOTES' => ENT_NOQUOTES, + //'ENT_HTML401' => ENT_HTML401, PHP 5.4.0 + //'ENT_XML1' => ENT_XML1, PHP 5.4.0 + //'ENT_XHTML' => ENT_XHTML, PHP 5.4.0 + //'ENT_HTML5' => ENT_HTML5, PHP 5.4.0 + 'ENT_IGNORE' => ENT_IGNORE, + // 'ENT_SUBSTITUTE' => ENT_SUBSTITUTE, PHP 5.4.0 + // 'ENT_DISALLOWED' => ENT_DISALLOWED, PHP 5.4.0 + 'ABDAY_1' => ABDAY_1, + 'ABDAY_2' => ABDAY_2, + 'ABDAY_3' => ABDAY_3, + 'ABDAY_4' => ABDAY_4, + 'ABDAY_5' => ABDAY_5, + 'ABDAY_6' => ABDAY_6, + 'ABDAY_7' => ABDAY_7, + 'DAY_1' => DAY_1, + 'DAY_2' => DAY_2, + 'DAY_3' => DAY_3, + 'DAY_4' => DAY_4, + 'DAY_5' => DAY_5, + 'DAY_6' => DAY_6, + 'DAY_7' => DAY_7, + 'ABMON_1' => ABMON_1, + 'ABMON_2' => ABMON_2, + 'ABMON_3' => ABMON_3, + 'ABMON_4' => ABMON_4, + 'ABMON_5' => ABMON_5, + 'ABMON_6' => ABMON_6, + 'ABMON_7' => ABMON_7, + 'ABMON_8' => ABMON_8, + 'ABMON_9' => ABMON_9, + 'ABMON_10' => ABMON_10, + 'ABMON_11' => ABMON_11, + 'ABMON_12' => ABMON_12, + 'MON_1' => MON_1, + 'MON_2' => MON_2, + 'MON_3' => MON_3, + 'MON_4' => MON_4, + 'MON_5' => MON_5, + 'MON_6' => MON_6, + 'MON_7' => MON_7, + 'MON_8' => MON_8, + 'MON_9' => MON_9, + 'MON_10' => MON_10, + 'MON_11' => MON_11, + 'MON_12' => MON_12, + 'AM_STR' => AM_STR, + 'PM_STR' => PM_STR, + 'D_T_FMT' => D_T_FMT, + 'D_FMT' => D_FMT, + 'T_FMT' => T_FMT, + 'T_FMT_AMPM' => T_FMT_AMPM, + 'ERA' => ERA, + 'ERA_YEAR' => ERA_YEAR, + 'ERA_D_T_FMT' => ERA_D_T_FMT, + 'ERA_D_FMT' => ERA_D_FMT, + 'ERA_T_FMT' => ERA_T_FMT, + 'INT_CURR_SYMBOL' => INT_CURR_SYMBOL, + 'CURRENCY_SYMBOL' => CURRENCY_SYMBOL, + 'CRNCYSTR' => CRNCYSTR, + 'MON_DECIMAL_POINT' => MON_DECIMAL_POINT, + 'MON_THOUSANDS_SEP' => MON_THOUSANDS_SEP, + 'MON_GROUPING' => MON_GROUPING, + 'POSITIVE_SIGN' => POSITIVE_SIGN, + 'NEGATIVE_SIGN' => NEGATIVE_SIGN, + 'INT_FRAC_DIGITS' => INT_FRAC_DIGITS, + 'FRAC_DIGITS' => FRAC_DIGITS, + 'P_CS_PRECEDES' => P_CS_PRECEDES, + 'P_SEP_BY_SPACE' => P_SEP_BY_SPACE, + 'N_CS_PRECEDES' => N_CS_PRECEDES, + 'N_SEP_BY_SPACE' => N_SEP_BY_SPACE, + 'P_SIGN_POSN' => P_SIGN_POSN, + 'N_SIGN_POSN' => N_SIGN_POSN, + 'DECIMAL_POINT' => DECIMAL_POINT, + 'RADIXCHAR' => RADIXCHAR, + 'THOUSANDS_SEP' => THOUSANDS_SEP, + 'THOUSEP' => THOUSEP, + 'GROUPING' => GROUPING, + 'YESEXPR' => YESEXPR, + 'NOEXPR' => NOEXPR, + 'YESSTR' => YESSTR, + 'NOSTR' => NOSTR, + 'CODESET' => CODESET, ); } diff --git a/includes/Runtime.php b/includes/Runtime.php index 564bb39..a85826a 100644 --- a/includes/Runtime.php +++ b/includes/Runtime.php @@ -209,9 +209,6 @@ }else{ $this->listParams[] = $this->lastParam->getValue(); } - if( $this->passByReference > 0 ) { - $this->passByReference <<= 1; - } } $this->lastParam = $this->listParams; } @@ -231,7 +228,7 @@ $this->listParams[] = $this->lastParam->getValue(); } if( $this->passByReference > 0 ) { - $this->passByReference <<= 1; + $this->passByReference >>= 1; } } $this->lastParam = null; @@ -259,6 +256,7 @@ switch ($this->lastCommand) { case false: case T_ECHO: + case T_PRINT: break 2; case T_IF: $this->lastCommand = false; @@ -484,6 +482,7 @@ // Remember the child class RuntimeDebug switch ($this->lastCommand) { case T_ECHO: + case T_PRINT: $return = array( T_ECHO, $this->listParams ); break; case false: diff --git a/includes/RuntimeDebug.php b/includes/RuntimeDebug.php index a6e66f5..75cb095 100644 --- a/includes/RuntimeDebug.php +++ b/includes/RuntimeDebug.php @@ -102,6 +102,7 @@ case false: case T_ARRAY: case T_ECHO: + case T_PRINT: break; case T_IF: $this->debug[] = self::getHTMLForCommand(T_IF) . @@ -121,6 +122,7 @@ switch ($lastCommand) { case T_ECHO: + case T_PRINT: $this->debug[] = self::getHTMLForCommand($lastCommand) . ' ' . implode(', ', $this->savedListParams) . ';'; $this->debug[] = implode('', $return[1]); break; @@ -199,6 +201,9 @@ case T_ECHO: $return = \Html::element('span', array('class'=>'foxway_construct'), 'echo'); break; + case T_PRINT: + $return = \Html::element('span', array('class'=>'foxway_construct'), 'print'); + break; case T_IF: $return = \Html::element('span', array('class'=>'foxway_construct'), 'if'); break; diff --git a/includes/functions/FString.php b/includes/functions/FString.php new file mode 100644 index 0000000..6f7c7a2 --- /dev/null +++ b/includes/functions/FString.php @@ -0,0 +1,150 @@ +<?php +namespace Foxway; +/** + * FString class implements String Functions for Foxway extension. + * + * @file FString.php + * @ingroup Foxway + * @author Pavel Astakhov <pastak...@yandex.ru> + * @licence GNU General Public Licence 2.0 or later + */ +class FString extends BaseFunction { + protected static $listFunction = array( + 'f_addcslashes' => array('addcslashes', 2, 2), + 'f_addslashes' => array('addslashes', 1, 1), + //'f_bin2hex' => array('bin2hex', 1, 1), + 'f_chop' => array('rtrim', 1, 2), //chop — Alias of rtrim() + 'f_chr' => array('chr', 1, 1), + 'f_chunk_split' => array('chunk_split', 1, 3), + 'f_convert_cyr_string' => array('convert_cyr_string', 3, 3), + 'f_convert_uudecode' => array('convert_uudecode', 1, 1), + 'f_convert_uuencode' => array('convert_uuencode', 1, 1), + 'f_count_chars' => array('count_chars', 1, 2), + 'f_crc32' => array('crc32', 1, 1), + 'f_crypt' => array('crypt', 1, 2), + 'f_explode' => array('explode', 2, 3), + // ### fprintf — Write a formatted string to a stream ### + //'f_get_html_translation_table' => array('get_html_translation_table', 0, 3), + //'f_hebrev' => array('hebrev', 1, 2), + //'f_hebrevc' => array('hebrevc', 1, 2), + //'f_hex2bin' => array('hex2bin', 1, 1), PHP >= 5.4.0 + 'f_html_entity_decode' => array('html_entity_decode', 1, 3), + 'f_htmlentities' => array('htmlentities', 1, 4), + 'f_htmlspecialchars_decode' => array('htmlspecialchars_decode', 1, 2), + 'f_htmlspecialchars' => array('htmlspecialchars', 1, 4), + 'f_implode' => array('implode', 1, 2), + 'f_join' => array('implode', 1, 2), //join — Alias of implode() + 'f_lcfirst' => array('lcfirst', 1, 1), + //'levenshtein', @see sels::f_levenshtein + //'localeconv' @todo get info from MW + 'f_ltrim' => array('ltrim', 1, 2), //@todo check exsample + // ### md5_ file + 'f_md5' => array('md5', 1, 2), + 'f_metaphone' => array('metaphone', 1, 2), + 'f_money_format' => array('money_format', 2, 2), // @todo need setlocale + 'f_nl_langinfo' => array('nl_langinfo', 1, 1), // @todo need setlocale + 'f_nl2br' => array('nl2br', 1, 2), + // 'number_format' @see sels::number_format + 'f_ord' => array('ord', 1, 1), + // @todo parse_str + // 'print' implemented in Runtime.php + //'f_printf', @see sels::f_printf + //'f_quoted_printable_decode' => array('quoted_printable_decode', 1, 1), + //'f_quoted_printable_encode' => array('quoted_printable_encode', 1, 1), + 'f_quotemeta' => array('quotemeta', 1, 1), + 'f_rtrim' => array('rtrim', 1, 2), + // setlocale @todo need check for security + // ### sha1_file + 'f_sha1' => array('sha1', 1, 2), + 'f_addcslashes' => array('addcslashes', 2, 2), + + + ); + + public static function __callStatic($name, $arguments) { + if( isset(self::$listFunction[$name]) ) { + $funcData = &self::$listFunction[$name]; + $refarg = &$arguments[0]; + /*foreach ($refarg as $key => &$value) { + if( $value instanceof RValue ) { + $refarg[$key] = &$value->getReference(); + } + }*/ + $c = count($refarg); + if( $c >= $funcData[1] && $c <= $funcData[2] ) { + return new RValue( call_user_func_array($funcData[0], $refarg) ); + }else{ + return self::wrongParameterCount($name, __LINE__); + } + } else { + return self::callUnknownMethod($name, __LINE__); + } + } + + /** + * Output a formatted string + * @param array $arguments + * @return \Foxway\RValue + */ + public static function f_printf($arguments) { + if( count($arguments) == 0 ) { + return self::wrongParameterCount( __FUNCTION__, __LINE__ ); + } + ob_start(); + call_user_func_array('printf', $arguments); + return new ROutput( null, ob_get_clean(), 'pre' ); + } + + /** + * Calculate Levenshtein distance between two strings + * @param array $arguments + * @return \Foxway\RValue + */ + public static function f_levenshtein($arguments) { + switch ( count($arguments) ) { + case 2: + case 5: + return new RValue( call_user_func_array('levenshtein', $arguments) ); + break; + } + return self::wrongParameterCount( __FUNCTION__, __LINE__ ); + } + + /** + * Calculate Levenshtein distance between two strings + * @param array $arguments + * @return \Foxway\RValue + */ + public static function f_number_format($arguments) { + switch ( count($arguments) ) { + case 1: + case 2: + case 4: + return new RValue( call_user_func_array('number_format', $arguments) ); + break; + } + return self::wrongParameterCount( __FUNCTION__, __LINE__ ); + } + + /** + * Calculate the similarity between two strings + * @param array $arguments + * @return \Foxway\RValue + */ + public static function f_similar_text($arguments) { + switch ( count($arguments) ) { + case 2: + return new RValue( similar_text($arguments[0], $arguments[1]) ); + case 3: + \MWDebug::log( var_export($arguments,true)); + if( $arguments[2] instanceof RVariable ) { + return new RValue( similar_text($arguments[0], $arguments[1], $arguments[2]->getReference()) ); + } + return self::onlyVariablesCanBePassedByReference( __FUNCTION__, __LINE__ ); + break; + } + return self::wrongParameterCount( __FUNCTION__, __LINE__ ); + } + + +} diff --git a/tests/phpunit/includes/InterpreterTest.php b/tests/phpunit/includes/InterpreterTest.php index cddfa50..35e549f 100644 --- a/tests/phpunit/includes/InterpreterTest.php +++ b/tests/phpunit/includes/InterpreterTest.php @@ -1995,6 +1995,13 @@ ); } + public function testRun_RString_similar_text() { + $this->assertEquals( + Interpreter::run('$var_1 = "PHP IS GREAT"; $var_2 = "WITH MYSQL"; similar_text($var_1, $var_2, $percent); echo $percent;'), + array('27.272727272727') + ); + } + } // @todo echo is_scalar(array("foo","bar") ? "true" : "false"; // most return error -- To view, visit https://gerrit.wikimedia.org/r/67651 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id3a7f6ed3303660cff3bd3c3a41ffd45fa887993 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Foxway Gerrit-Branch: master Gerrit-Owner: Pastakhov <pastak...@yandex.ru> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits