From 9db190c7e736ec8d063187d4241b59feaf7dc2d1 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Wed, 22 Jun 2011 11:28:20 +0200 Subject: update to MediaWiki 1.17.0 --- includes/MagicWord.php | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'includes/MagicWord.php') diff --git a/includes/MagicWord.php b/includes/MagicWord.php index d741832f..31d83332 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -1,6 +1,7 @@ mId = $id; $this->mSynonyms = (array)$syn; $this->mCaseSensitive = $cs; @@ -269,13 +272,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 ); @@ -288,6 +291,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 */ @@ -513,7 +533,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; } @@ -646,7 +665,6 @@ class MagicWordArray { } // This shouldn't happen either throw new MWException( __METHOD__.': parameter not found' ); - return array( false, false ); } /** @@ -656,7 +674,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 !== '' ) { @@ -720,7 +737,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 { -- cgit v1.2.2