User:IMSoP/replaceImageLinks.diff

Patch to add a replaceImageLinks() function to Parser.php, as a replacement for the second call to replaceInternalLinks(). Among other things, this allows us to pick when images are parsed: after both internal and external links (so either of these can appear in captions, without them breaking each other).

This appears to be mangled: all the tabs have been replaced with spaces. Just use http://195.137.84.82/~ron/replaceImageLinks.diff instead, if you want to actually apply it.

Index: Parser.php
===================================================================
RCS file: /cvsroot/wikipedia/phase3/includes/Parser.php,v
retrieving revision 1.313
diff -U3 -r1.313 Parser.php
--- Parser.php 27 Sep 2004 21:01:39 -0000      1.313
+++ Parser.php 1 Oct 2004 23:51:09 -0000
@@ -659,9 +659,8 @@
               }
               $text = $this->doAllQuotes( $text );
               $text = $this->replaceInternalLinks ( $text );
-              # Another call to replace links and images inside captions of images
-              $text = $this->replaceInternalLinks ( $text );
               $text = $this->replaceExternalLinks( $text );
+              $text = $this->replaceImageLinks ( $text );
               $text = $this->doMagicLinks( $text );
               $text = $this->doTableStuff( $text );
               $text = $this->formatHeadings( $text, $isMain );
@@ -1166,8 +1165,8 @@
                               }
                               }
                               if ( $ns == NS_IMAGE ) {
-                                      $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
-                                      $wgLinkCache->addImageLinkObj( $nt );
+                                      #Don't touch images here, we'll deal with them later.
+                                      $s .= $prefix . '[[' . $line;
                                       continue;
                               }
                               }
@@ -1266,6 +1265,62 @@
               return $ret;
       }
 
+      /**
+       * Process Image links - they should be treated differently from "real" internal links
+       * This is largely a much-simplified copy of replaceInternalLinks()
+       *
+       * @access private
+       */
+      /* private */ function replaceImageLinks( $s ) {
+              global $wgLang, $wgContLang, $wgLinkCache;
+              $fname = 'Parser::replaceInternalLinks' ;
+              wfProfileIn( $fname );
+              wfProfileIn( $fname );
+              $sk =& $this->mOptions->getSkin();
+              # the % is needed to support urlencoded titles as well; no # because that is meaningless for images
+              $title_chars = Title::legalChars() . '%';
+              $link_regex = "/^([{$title_chars}]+)(?:\\|([^]]+))?]](.*)\$/sD";
+              $link_regex = "/^([{$title_chars}]+)(?:\\|([^]]+))?]](.*)\$/sD";
+              $a = explode( '[[', ' ' . $s );
+              # split on "[[", and "output" the first part of that array to $s
+              # (which won't be a match, but may just be the space we added there)
+              $s = array_shift($a);
+              $s = substr( $s, 1 );
+              # now check through the rest one by one
+              # now check through the rest one by one
+              foreach ( $a as $line ) {
+                      if ( preg_match( $link_regex, $line, $m ) ) {
+                              $title = $m[1];
+                              $text = $m[2];
+                              $rest_of_line = $m[3];
+                              # fix up urlencoded title texts
+                              if(preg_match('/%/', $title )) $title = urldecode($title);
+                      } else { # Invalid form; output directly
+                              $s .= '[[' . $line;
+                              continue;
+                      }
+
+                      #make sure we don't process if there's a leading colon
+                      if(substr($title, 0, 1) == ':') {
+                              $s .= '[[' . $line;
+                              continue;
+                      }
+
+                      $nt = Title::newFromText($title);
+                      if( $nt->getNamespace() == NS_IMAGE ) {
+                              #it's an image, so do image-y things with it!
+                              $s .= $sk->makeImageLinkObj( $nt, $text ) . $rest_of_line;
+                              $wgLinkCache->addImageLinkObj( $nt );
+                              continue;
+                      } else {
+                              # there's no other reason we'd want to be processing a link at this stage
+                              $s .= '[[' . $line;
+                              continue;
+                      }
+              }
+              return $s;
+      }
+
       /**#@+
        * Used by doBlockLevels()
        * @access private