Umherirrender has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/100198


Change subject: Raw option of parser functions should match complete word
......................................................................

Raw option of parser functions should match complete word

The MagicWord raw was not matched against he whole given string, which
result in a raw output, when this was not indented.
Fixing this by adding a new regex, which matches the string from start
to end.

Bug: 56199
Change-Id: I7781c415bd61447dd91872575877dd21f36fae9f
---
M RELEASE-NOTES-1.23
M includes/MagicWord.php
M includes/parser/CoreParserFunctions.php
M tests/parser/parserTests.txt
4 files changed, 45 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/98/100198/1

diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23
index bf7c8cc..b3ac0a8 100644
--- a/RELEASE-NOTES-1.23
+++ b/RELEASE-NOTES-1.23
@@ -67,6 +67,8 @@
 * (bug 57098) SpecialPasswordReset now obeys returnto parameter
 * (bug 37812) ResourceLoader will notice when a module's definition changes and
   recompile it accordingly.
+* (bug 56199) Raw option of parser functions must now match complete word,
+  to take effect.
 
 === API changes in 1.23 ===
 * (bug 54884) action=parse&prop=categories now indicates hidden and missing
diff --git a/includes/MagicWord.php b/includes/MagicWord.php
index 427a1ad..5f87ab1 100644
--- a/includes/MagicWord.php
+++ b/includes/MagicWord.php
@@ -65,6 +65,7 @@
        var $mId, $mSynonyms, $mCaseSensitive;
        var $mRegex = '';
        var $mRegexStart = '';
+       private $regexStartToEnd = '';
        var $mBaseRegex = '';
        var $mVariableRegex = '';
        var $mVariableStartToEndRegex = '';
@@ -338,6 +339,7 @@
                $case = $this->mCaseSensitive ? '' : 'iu';
                $this->mRegex = "/{$this->mBaseRegex}/{$case}";
                $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
+               $this->regexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
                $this->mVariableRegex = str_replace( "\\$1", "(.*?)", 
$this->mRegex );
                $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
                        "/^(?:{$this->mBaseRegex})$/{$case}" );
@@ -405,6 +407,19 @@
        }
 
        /**
+        * Gets a regex matching the word from start to end of a string
+        *
+        * @return string
+        * @since 1.23
+        */
+       function getRegexStartToEnd() {
+               if ( $this->regexStartToEnd == '' ) {
+                       $this->initRegex();
+               }
+               return $this->regexStartToEnd;
+       }
+
+       /**
         * regex without the slashes and what not
         *
         * @return string
@@ -439,6 +454,18 @@
        }
 
        /**
+        * 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
         * part in the regex and the matched variable part ($1) if there
diff --git a/includes/parser/CoreParserFunctions.php 
b/includes/parser/CoreParserFunctions.php
index f57d412..ba52828 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -428,11 +428,12 @@
         * @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 ) {
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index cc935da..6adf615 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -16281,6 +16281,19 @@
 !! 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
 language=fi

-- 
To view, visit https://gerrit.wikimedia.org/r/100198
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7781c415bd61447dd91872575877dd21f36fae9f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <umherirrender_de...@web.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to