Raw option of parser functions should match complete word
authorumherirrender <umherirrender_de.wp@web.de>
Tue, 17 Dec 2013 18:14:15 +0000 (19:14 +0100)
committerTim Starling <tstarling@wikimedia.org>
Sun, 12 Jan 2014 22:25:33 +0000 (22:25 +0000)
The MagicWord raw was not matched against the whole given string, which
result in a raw output, when this was not intented.
Fixing this by adding a new regex, which matches the string from start
to end.

Bug: 56199
Change-Id: I7781c415bd61447dd91872575877dd21f36fae9f

RELEASE-NOTES-1.23
includes/MagicWord.php
includes/parser/CoreParserFunctions.php
tests/parser/parserTests.txt

index 1bdce42..b0d97aa 100644 (file)
@@ -104,6 +104,8 @@ production.
   templatelinks does).
 * (bug 58167) The web installer no longer throws an exception when PHP is
   compiled without support for MySQL yet with support for another DBMS.
+* (bug 56199) Raw option of parser functions must now match complete word,
+  to take effect.
 
 === Web API changes in 1.23 ===
 * (bug 54884) action=parse&prop=categories now indicates hidden and missing
index bf15bf3..377d406 100644 (file)
@@ -65,6 +65,7 @@ class MagicWord {
        var $mId, $mSynonyms, $mCaseSensitive;
        var $mRegex = '';
        var $mRegexStart = '';
+       var $mRegexStartToEnd = '';
        var $mBaseRegex = '';
        var $mVariableRegex = '';
        var $mVariableStartToEndRegex = '';
@@ -339,6 +340,7 @@ class MagicWord {
                $case = $this->mCaseSensitive ? '' : 'iu';
                $this->mRegex = "/{$this->mBaseRegex}/{$case}";
                $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
+               $this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
                $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
                $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
                        "/^(?:{$this->mBaseRegex})$/{$case}" );
@@ -405,6 +407,19 @@ class MagicWord {
                return $this->mRegexStart;
        }
 
+       /**
+        * Gets a regex matching the word from start to end of a string
+        *
+        * @return string
+        * @since 1.23
+        */
+       function getRegexStartToEnd() {
+               if ( $this->mRegexStartToEnd == '' ) {
+                       $this->initRegex();
+               }
+               return $this->mRegexStartToEnd;
+       }
+
        /**
         * regex without the slashes and what not
         *
@@ -439,6 +454,18 @@ class MagicWord {
                return (bool)preg_match( $this->getRegexStart(), $text );
        }
 
+       /**
+        * Returns true if the text matched the word
+        *
+        * @param $text string
+        *
+        * @return bool
+        * @since 1.23
+        */
+       function matchStartToEnd( $text ) {
+               return (bool)preg_match( $this->getRegexStartToEnd(), $text );
+       }
+
        /**
         * Returns NULL if there's no match, the value of $1 otherwise
         * The return code is the matched string, if there's no variable
index 250442c..df0f118 100644 (file)
@@ -402,11 +402,12 @@ class CoreParserFunctions {
         * @return boolean true on successful match
         */
        private static function matchAgainstMagicword( $magicword, $value ) {
-               if ( strval( $value ) === '' ) {
+               $value = trim( strval( $value ) );
+               if ( $value === '' ) {
                        return false;
                }
                $mwObject = MagicWord::get( $magicword );
-               return $mwObject->match( $value );
+               return $mwObject->matchStartToEnd( $value );
        }
 
        static function formatRaw( $num, $raw ) {
index ce2c734..6dff54e 100644 (file)
@@ -16687,6 +16687,19 @@ language=be-tarask
 </p>
 !! end
 
+!! test
+Wrong option for formatNum (bug 56199)
+!! input
+{{formatnum:1,234.56|Random}}
+{{formatnum:1,234.56|EVERYTHING}}
+{{formatnum:1234.56|any argument that has the string 'NOSEP'}}
+!! result
+<p>1,234.56
+1,234.56
+1,234.56
+</p>
+!! end
+
 !! test
 Strip marker in grammar
 !! options