(bug 15745) Make the edit summary link parser detect links more like the real parser...
authorAlex Z <mrzman@users.mediawiki.org>
Mon, 20 Jul 2009 06:29:46 +0000 (06:29 +0000)
committerAlex Z <mrzman@users.mediawiki.org>
Mon, 20 Jul 2009 06:29:46 +0000 (06:29 +0000)
RELEASE-NOTES
includes/Linker.php
maintenance/parserTests.txt

index bd7f1b2..d7db796 100644 (file)
@@ -304,6 +304,8 @@ this. Was used when mwEmbed was going to be an extension.
 * (bug 17374) Special:Export no longer exports multiple copies of pages
 * (bug 19818) Edits to user CSS/JS subpages can now be marked as patrolled by
   users who can't edit them
+* (bug 15745) The edit summary link parser now handles mismatched brackets
+  better
 
 == API changes in 1.16 ==
 
index d059d76..3cdb545 100644 (file)
@@ -1012,10 +1012,22 @@ class Linker {
        public function formatLinksInComment( $comment, $title = null, $local = false ) {
                $this->commentContextTitle = $title;
                $this->commentLocal = $local;
-               $html = preg_replace_callback(
-                       '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
-                       array( $this, 'formatLinksInCommentCallback' ),
-                       $comment );
+               # Borrowed from Parser::replaceInternalLinks2
+               $parts = StringUtils::explode( '[[', ' ' . $comment );
+               $start = $parts->current();
+               $parts->next();
+               $line = $parts->current();
+               $html = substr( $start, 1 );
+               for ( ; $line !== false && $line !== null ; $parts->next(), $line = $parts->current() ) {                       
+                       $linked = preg_replace_callback(
+                               '/^:?(.*?)(\|(.*?))*\]\]([^[]*)/',
+                               array( $this, 'formatLinksInCommentCallback' ),
+                               $line, -1, $count );
+                       if( !$count ) { // No valid link found, put the brackets back
+                               $linked = '[[' . $linked;
+                       }
+                       $html .= $linked;
+               }
                unset( $this->commentContextTitle );
                unset( $this->commentLocal );
                return $html;
@@ -1043,7 +1055,7 @@ class Linker {
                $submatch = array();
                if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
                        # Media link; trail not supported.
-                       $linkRegexp = '/\[\[(.*?)\]\]/';
+                       $linkRegexp = '/^(.*?)\]\]/';
                        $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
                        $thelink = $this->makeMediaLinkObj( $title, $text );
                } else {
@@ -1053,7 +1065,7 @@ class Linker {
                        } else {
                                $trail = "";
                        }
-                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                       $linkRegexp = '/^(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
                        if (isset($match[1][0]) && $match[1][0] == ':')
                                $match[1] = substr($match[1], 1);
                        list( $inside, $trail ) = Linker::splitTrail( $trail );
index 2b2d2a2..099ee3d 100644 (file)
@@ -7456,6 +7456,17 @@ title=[[Main Page]]
 <a href="/wiki/Main_Page#section" title="Main Page">#section</a>
 !! end
 
+!! test
+Edit comment with mismatched brackets (bug 15745)
+!! options
+comment
+title=[[Main Page]]
+!!input
+Some text, a [[broken link|bad and a [[good link|good]]
+!! result
+Some text, a [[broken link|bad and a <a href="/index.php?title=Good_link&amp;action=edit&amp;redlink=1" class="new" title="Good link (page does not exist)">good</a>
+!! end
+
 TODO:
 more images
 more tables