* (bug 688) MagicWord.php uses incorrect regex and logic for "$1" variable capturing
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 4 May 2005 07:49:42 +0000 (07:49 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 4 May 2005 07:49:42 +0000 (07:49 +0000)
Fixes cyrillic 'pks' form for image widths (eg 150pks instead of 150px) in
Russian localization.

includes/MagicWord.php

index e5a389c..5c53c8b 100644 (file)
@@ -137,15 +137,18 @@ class MagicWord {
         * @private
         */
        function initRegex() {
-               $variableClass = Title::legalChars();
+               #$variableClass = Title::legalChars();
+               # This was used for matching "$1" variables, but different uses of the feature will have
+               # different restrictions, which should be checked *after* the MagicWord has been matched,
+               # not here. - IMSoP
                $escSyn = array_map( 'preg_quote', $this->mSynonyms );
                $this->mBaseRegex = implode( '|', $escSyn );
                $case = $this->mCaseSensitive ? '' : 'i';
                $this->mRegex = "/{$this->mBaseRegex}/{$case}";
-               $this->mRegexStart = "/^({$this->mBaseRegex})/{$case}";
-               $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
-               $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)", 
-                       "/^({$this->mBaseRegex})$/{$case}" );
+               $this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
+               $this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
+               $this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)", 
+                       "/^(?:{$this->mBaseRegex})$/{$case}" );
        }
        
        /**
@@ -204,10 +207,13 @@ class MagicWord {
                $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
                if ( $matchcount == 0 ) {
                        return NULL;
-               } elseif ( count($matches) == 2 ) {
+               } elseif ( count($matches) == 1 ) {
                        return $matches[0];
                } else {
-                       return $matches[2];
+                       # multiple matched parts (variable match); some will be empty because of synonyms
+                       # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
+                       $matches = array_values(array_filter($matches));
+                       return $matches[1];
                }
        }