* (bug 2271) Use faster text-only link replacement in image alt text
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 31 May 2005 08:49:03 +0000 (08:49 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 31 May 2005 08:49:03 +0000 (08:49 +0000)
  instead of rerunning expensive link lookup and HTML generation.

RELEASE-NOTES
includes/Parser.php
maintenance/parserTests.txt

index bfe233f..bbbb56a 100644 (file)
@@ -225,7 +225,8 @@ Various bugfixes, small features, and a few experimental things:
 * (bug 2267) Don't generate thumbnail at the same size as the source image.
 * Fix rebuildtextindex.inc for new schema
 * Remove linkscc table code, no longer used.
-
+* (bug 2271) Use faster text-only link replacement in image alt text
+  instead of rerunning expensive link lookup and HTML generation.
 
 === Caveats ===
 
index 7d657cc..d532f89 100644 (file)
@@ -3000,10 +3000,52 @@ class Parser
                                $text );
                        wfProfileOut( $fname.'-interwiki' );
                }
-
+               
                wfProfileOut( $fname );
                return $colours;
        }
+       
+       /**
+        * Replace <!--LINK--> link placeholders with plain text of links
+        * (not HTML-formatted).
+        * @param string $text
+        * @return string
+        */
+       function replaceLinkHoldersText( $text ) {
+               global $wgUser, $wgLinkCache;
+               global $wgOutputReplace;
+
+               $fname = 'Parser::replaceLinkHoldersText';
+               wfProfileIn( $fname );
+
+               $text = preg_replace_callback(
+                       '/<!--(LINK|IWLINK) (.*?)-->/',
+                       array( &$this, 'replaceLinkHoldersTextCallback' ),
+                       $text );
+               
+               wfProfileOut( $fname );
+               return $text;
+       }
+       
+       /**
+        * @param array $matches
+        * @return string
+        * @access private
+        */
+       function replaceLinkHoldersTextCallback( $matches ) {
+               $type = $matches[1];
+               $key  = $matches[2];
+               if( $type == 'LINK' ) {
+                       if( isset( $this->mLinkHolders['texts'][$key] ) ) {
+                               return $this->mLinkHolders['texts'][$key];
+                       }
+               } elseif( $type == 'IWLINK' ) {
+                       if( isset( $this->mInterwikiLinkHolders[$key][1] ) ) {
+                               return $this->mInterwikiLinkHolders[$key][1];
+                       }
+               }
+               return $matches[0];
+       }
 
        /**
         * Renders an image gallery from a text with one line per image.
@@ -3125,8 +3167,7 @@ class Parser
                        }
                }
                # Strip bad stuff out of the alt text
-               $alt = $caption;
-               $this->replaceLinkHolders( $alt );
+               $alt = $this->replaceLinkHoldersText( $caption );
                $alt = Sanitizer::stripAllTags( $alt );
 
                # Linker does the rest
index c321b37..16b1e36 100644 (file)
@@ -1728,6 +1728,33 @@ BUG 648: Frameless image caption with a link
 </p>
 !! end
 
+!! test
+BUG 648: Frameless image caption with a link (suffix)
+!! input
+[[Image:foobar.jpg|text with a [[link]]foo in it]]
+!! result
+<p><a href="/wiki/Image:Foobar.jpg" class="image" title="text with a linkfoo in it"><img src="/images/3/3a/Foobar.jpg" alt="text with a linkfoo in it" longdesc="/wiki/Image:Foobar.jpg" /></a>
+</p>
+!! end
+
+!! test
+BUG 648: Frameless image caption with an interwiki link
+!! input
+[[Image:foobar.jpg|text with a [[MeatBall:Link]] in it]]
+!! result
+<p><a href="/wiki/Image:Foobar.jpg" class="image" title="text with a MeatBall:Link in it"><img src="/images/3/3a/Foobar.jpg" alt="text with a MeatBall:Link in it" longdesc="/wiki/Image:Foobar.jpg" /></a>
+</p>
+!! end
+
+!! test
+BUG 648: Frameless image caption with a piped interwiki link
+!! input
+[[Image:foobar.jpg|text with a [[MeatBall:Link|link]] in it]]
+!! result
+<p><a href="/wiki/Image:Foobar.jpg" class="image" title="text with a link in it"><img src="/images/3/3a/Foobar.jpg" alt="text with a link in it" longdesc="/wiki/Image:Foobar.jpg" /></a>
+</p>
+!! end
+
 !! test
 Escape HTML special chars in image alt text
 !! input