Optimized regex in Linker.php
authorKevin Israel <pleasestand@live.com>
Thu, 25 Oct 2012 13:54:52 +0000 (09:54 -0400)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 29 Nov 2012 22:33:38 +0000 (22:33 +0000)
Optimized the regex in Linker::formatLinksInComment().

Change-Id: Idea5554bcb8d7e78ddf972ce4c9827fa738ff837

includes/Linker.php

index 12edcc8..9b49015 100644 (file)
@@ -1376,7 +1376,7 @@ class Linker {
                self::$commentContextTitle = $title;
                self::$commentLocal = $local;
                $html = preg_replace_callback(
-                       '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
+                       '/\[\[:?([^\]|]+)(?:\|((?:]?[^\]|])*+))*\]\]([^[]*)/',
                        array( 'Linker', 'formatLinksInCommentCallback' ),
                        $comment );
                self::$commentContextTitle = null;
@@ -1402,8 +1402,8 @@ class Linker {
                }
 
                # Handle link renaming [[foo|text]] will show link as "text"
-               if ( $match[3] != "" ) {
-                       $text = $match[3];
+               if ( $match[2] != "" ) {
+                       $text = $match[2];
                } else {
                        $text = $match[1];
                }
@@ -1418,7 +1418,7 @@ class Linker {
                        }
                } else {
                        # Other kind of link
-                       if ( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
+                       if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
                                $trail = $submatch[1];
                        } else {
                                $trail = "";