From 89971cdee6a81ec307540e91197ef103da2623b7 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 4 May 2005 07:49:42 +0000 Subject: [PATCH] * (bug 688) MagicWord.php uses incorrect regex and logic for "$1" variable capturing Fixes cyrillic 'pks' form for image widths (eg 150pks instead of 150px) in Russian localization. --- includes/MagicWord.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/includes/MagicWord.php b/includes/MagicWord.php index e5a389c074..5c53c8bba7 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -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]; } } -- 2.20.1