Follow-up r84814: revert redundant summary message addition.
[lhc/web/wiklou.git] / includes / MagicWord.php
index d05bf19..3e66b79 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
  * File for magic words
+ *
  * See docs/magicword.txt
  *
  * @file
@@ -60,6 +61,7 @@ class MagicWord {
                'numberofarticles',
                'numberoffiles',
                'numberofedits',
+               'articlepath',
                'sitename',
                'server',
                'servername',
@@ -176,7 +178,7 @@ class MagicWord {
 
        /**#@-*/
 
-       function __construct($id = 0, $syn = '', $cs = false) {
+       function __construct($id = 0, $syn = array(), $cs = false) {
                $this->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 {