X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FMagicWord.php;h=3e66b7997a81d0a8deca78a2b3e0cb0ae1db743f;hb=5c479c9a3daf3c5f88fd17757cf3234ac6d0a940;hp=d05bf19da2460fd8a5210318ee16b3c5dd2267b6;hpb=449b936efdd82678cb20971328e46f5740ba9999;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/MagicWord.php b/includes/MagicWord.php index d05bf19da2..3e66b7997a 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -1,6 +1,7 @@ mId = $id; $this->mSynonyms = (array)$syn; $this->mCaseSensitive = $cs; @@ -190,6 +192,7 @@ class MagicWord { /** * Factory: creates an object representing an ID * @static + * @return MagicWord */ static function &get( $id ) { wfProfileIn( __METHOD__ ); @@ -270,13 +273,13 @@ class MagicWord { * @private */ function initRegex() { - #$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 + // Sort the synonyms by length, descending, so that the longest synonym + // matches in precedence to the shortest + $synonyms = $this->mSynonyms; + usort( $synonyms, array( $this, 'compareStringLength' ) ); $escSyn = array(); - foreach ( $this->mSynonyms as $synonym ) + foreach ( $synonyms as $synonym ) // In case a magic word contains /, like that's going to happen;) $escSyn[] = preg_quote( $synonym, '/' ); $this->mBaseRegex = implode( '|', $escSyn ); @@ -289,6 +292,23 @@ class MagicWord { "/^(?:{$this->mBaseRegex})$/{$case}" ); } + /** + * A comparison function that returns -1, 0 or 1 depending on whether the + * first string is longer, the same length or shorter than the second + * string. + */ + function compareStringLength( $s1, $s2 ) { + $l1 = strlen( $s1 ); + $l2 = strlen( $s2 ); + if ( $l1 < $l2 ) { + return 1; + } elseif ( $l1 > $l2 ) { + return -1; + } else { + return 0; + } + } + /** * Gets a regex representing matching the word */ @@ -514,7 +534,6 @@ class MagicWordArray { * Add a magic word by name */ public function add( $name ) { - global $wgContLang; $this->names[] = $name; $this->hash = $this->baseRegex = $this->regex = null; } @@ -647,7 +666,6 @@ class MagicWordArray { } // This shouldn't happen either throw new MWException( __METHOD__.': parameter not found' ); - return array( false, false ); } /** @@ -657,7 +675,6 @@ class MagicWordArray { * Both elements are false if there was no match. */ public function matchVariableStartToEnd( $text ) { - global $wgContLang; $regexes = $this->getVariableStartToEndRegex(); foreach ( $regexes as $regex ) { if ( $regex !== '' ) { @@ -721,7 +738,7 @@ class MagicWordArray { continue; } if ( preg_match( $regex, $text, $m ) ) { - list( $id, $param ) = $this->parseMatch( $m ); + list( $id, ) = $this->parseMatch( $m ); if ( strlen( $m[0] ) >= strlen( $text ) ) { $text = ''; } else {