r82221 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82220‎ | r82221 | r82222 >
Date:07:24, 16 February 2011
Author:tstarling
Status:ok
Tags:
Comment:
* Fix for r81960: List the local magic word synonyms first, so that they will be returned by $magic->getSynonym( 0 ). Apply array_values() to fix up the keys, potentially corrupted by array_unique().
* In MagicWord::initRegex(), put the longest synonyms first in the alternation. This means that when one synonym is an initial substring of another, the longest one will match in precedence, which is generally in line with user expectations.
Modified paths:
  • /trunk/phase3/includes/LocalisationCache.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/LocalisationCache.php
@@ -448,7 +448,8 @@
449449 } else {
450450 $oldSynonyms = array_slice( $fallbackInfo, 1 );
451451 $newSynonyms = array_slice( $value[$magicName], 1 );
452 - $synonyms = array_unique( array_merge( $oldSynonyms, $newSynonyms ) );
 452+ $synonyms = array_values( array_unique( array_merge(
 453+ $newSynonyms, $oldSynonyms ) ) );
453454 $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
454455 }
455456 }
Index: trunk/phase3/includes/MagicWord.php
@@ -273,13 +273,13 @@
274274 * @private
275275 */
276276 function initRegex() {
277 - #$variableClass = Title::legalChars();
278 - # This was used for matching "$1" variables, but different uses of the feature will have
279 - # different restrictions, which should be checked *after* the MagicWord has been matched,
280 - # not here. - IMSoP
 277+ // Sort the synonyms by length, descending, so that the longest synonym
 278+ // matches in precedence to the shortest
 279+ $synonyms = $this->mSynonyms;
 280+ usort( $synonyms, array( $this, 'compareStringLength' ) );
281281
282282 $escSyn = array();
283 - foreach ( $this->mSynonyms as $synonym )
 283+ foreach ( $synonyms as $synonym )
284284 // In case a magic word contains /, like that's going to happen;)
285285 $escSyn[] = preg_quote( $synonym, '/' );
286286 $this->mBaseRegex = implode( '|', $escSyn );
@@ -293,6 +293,23 @@
294294 }
295295
296296 /**
 297+ * A comparison function that returns -1, 0 or 1 depending on whether the
 298+ * first string is longer, the same length or shorter than the second
 299+ * string.
 300+ */
 301+ function compareStringLength( $s1, $s2 ) {
 302+ $l1 = strlen( $s1 );
 303+ $l2 = strlen( $s2 );
 304+ if ( $l1 < $l2 ) {
 305+ return 1;
 306+ } elseif ( $l1 > $l2 ) {
 307+ return -1;
 308+ } else {
 309+ return 0;
 310+ }
 311+ }
 312+
 313+ /**
297314 * Gets a regex representing matching the word
298315 */
299316 function getRegex() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r822231.17wmf1: MFT r82221catrope07:37, 16 February 2011
r85151MFT: r82000, r82004, r82020, r82025, r82038, r82039, r82048, r82070, r82081, ...demon20:39, 1 April 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r81960Since 1.17 a whole lot of extensions have sprouted *.i18n.magic.php files. Mo...tstarling11:53, 11 February 2011

Status & tagging log