Merge "Move section ID fallbacks into headers themselves"
[lhc/web/wiklou.git] / includes / MagicWordArray.php
index ee5de6b..5856e21 100644 (file)
@@ -42,7 +42,7 @@ class MagicWordArray {
        /**
         * @param array $names
         */
-       function __construct( $names = [] ) {
+       public function __construct( $names = [] ) {
                $this->names = $names;
        }
 
@@ -70,7 +70,7 @@ class MagicWordArray {
         * Get a 2-d hashtable for this array
         * @return array
         */
-       function getHash() {
+       public function getHash() {
                if ( is_null( $this->hash ) ) {
                        global $wgContLang;
                        $this->hash = [ 0 => [], 1 => [] ];
@@ -92,16 +92,25 @@ class MagicWordArray {
         * Get the base regex
         * @return array
         */
-       function getBaseRegex() {
+       public function getBaseRegex() {
                if ( is_null( $this->baseRegex ) ) {
                        $this->baseRegex = [ 0 => '', 1 => '' ];
+                       $allGroups = [];
                        foreach ( $this->names as $name ) {
                                $magic = MagicWord::get( $name );
                                $case = intval( $magic->isCaseSensitive() );
                                foreach ( $magic->getSynonyms() as $i => $syn ) {
                                        // Group name must start with a non-digit in PCRE 8.34+
                                        $it = strtr( $i, '0123456789', 'abcdefghij' );
-                                       $group = "(?P<{$it}_{$name}>" . preg_quote( $syn, '/' ) . ')';
+                                       $groupName = $it . '_' . $name;
+                                       $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
+                                       // look for same group names to avoid same named subpatterns in the regex
+                                       if ( isset( $allGroups[$groupName] ) ) {
+                                               throw new MWException(
+                                                       __METHOD__ . ': duplicate internal name in magic word array: ' . $name
+                                               );
+                                       }
+                                       $allGroups[$groupName] = true;
                                        if ( $this->baseRegex[$case] === '' ) {
                                                $this->baseRegex[$case] = $group;
                                        } else {
@@ -117,7 +126,7 @@ class MagicWordArray {
         * Get an unanchored regex that does not match parameters
         * @return array
         */
-       function getRegex() {
+       public function getRegex() {
                if ( is_null( $this->regex ) ) {
                        $base = $this->getBaseRegex();
                        $this->regex = [ '', '' ];
@@ -136,7 +145,7 @@ class MagicWordArray {
         *
         * @return string
         */
-       function getVariableRegex() {
+       public function getVariableRegex() {
                return str_replace( "\\$1", "(.*?)", $this->getRegex() );
        }
 
@@ -145,7 +154,7 @@ class MagicWordArray {
         *
         * @return array
         */
-       function getRegexStart() {
+       public function getRegexStart() {
                $base = $this->getBaseRegex();
                $newRegex = [ '', '' ];
                if ( $base[0] !== '' ) {
@@ -162,7 +171,7 @@ class MagicWordArray {
         *
         * @return array
         */
-       function getVariableStartToEndRegex() {
+       public function getVariableStartToEndRegex() {
                $base = $this->getBaseRegex();
                $newRegex = [ '', '' ];
                if ( $base[0] !== '' ) {
@@ -192,7 +201,7 @@ class MagicWordArray {
         * @throws MWException
         * @return array
         */
-       function parseMatch( $m ) {
+       public function parseMatch( $m ) {
                reset( $m );
                while ( list( $key, $value ) = each( $m ) ) {
                        if ( $key === 0 || $value === '' ) {
@@ -260,7 +269,7 @@ class MagicWordArray {
         * Returns an associative array, ID => param value, for all items that match
         * Removes the matched items from the input string (passed by reference)
         *
-        * @param string $text
+        * @param string &$text
         *
         * @return array
         */
@@ -304,7 +313,7 @@ class MagicWordArray {
         * Return false if no match found and $text is not modified.
         * Does not match parameters.
         *
-        * @param string $text
+        * @param string &$text
         *
         * @return int|bool False on failure
         */