Start finding more bugs in Linker::link() by having the Parser use it, and running...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 1 Aug 2008 15:02:46 +0000 (15:02 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 1 Aug 2008 15:02:46 +0000 (15:02 +0000)
* Support class="extiw"
* Do not double the fragment for external links with fragments.  Move the code for this into a new Title::getLinkUrl() instead of a Linker method, because it seems like a useful concept to be able to get a *usable* link to the current Title.
* Fix a few parser tests that expected attributes in the opposite order.
* Don't overwrite actions for broken links.
* Style

If you want me to stop doing this, by the way, please say so before I spend too many more hours of my life on it.

includes/Linker.php
includes/Title.php
includes/parser/Parser.php
maintenance/parserTests.txt

index 0bd1daa..8255e1a 100644 (file)
@@ -161,8 +161,9 @@ class Linker {
         *     'known': Page is known to exist, so don't check if it does.
         *     'broken': Page is known not to exist, so don't check if it does.
         *     'noclasses': Don't add any classes automatically (includes "new",
-        *       "stub", "mw-redirect").  Only use the class attribute provided, if
-        *       any.
+        *       "stub", "mw-redirect", "extiw").  Only use the class attribute
+        *       provided, if any, so you get a simple blue link with no funny i-
+        *       cons.
         * @return string HTML <a> attribute
         */
        public function link( $target, $text = null, $customAttribs = array(), $query = array(), $options = array() ) {
@@ -217,27 +218,13 @@ class Linker {
        }
 
        private function linkUrl( $target, $query, $options ) {
-               # If it's a broken link, add the appropriate query pieces.  This over-
-               # writes the default action!
-               if( in_array( 'broken', $options ) ) {
+               # If it's a broken link, add the appropriate query pieces, unless
+               # there's already an action specified.
+               if( in_array( 'broken', $options ) and empty( $query['action'] ) ) {
                        $query['action'] = 'edit';
                        $query['redlink'] = '1';
                }
-               $fragment = $target->getFragmentForURL();
-               $title = $target->getPrefixedText();
-               # A couple of things to be concerned about here.  First of all,
-               # getLocalURL() ignores fragments.  Second of all, if the Title is
-               # *only* a fragment, it returns something like "/".
-               if( $fragment === '' and $title !== '' ) {
-                       return $target->getLocalURL( $query );
-               }
-               if( $title === '' ) {
-                       # Just a fragment.  There had better be one, anyway, or this is a
-                       # pretty silly Title.
-                       return $fragment;
-               }
-               # Then we must have a fragment *and* some Title text.
-               return $target->getLocalURL( $query ) . $fragment;
+               return $target->getLinkUrl( $query );
        }
 
        private function linkAttribs( $target, $attribs, $options ) {
@@ -252,20 +239,25 @@ class Linker {
                }
 
                if( !in_array( 'noclasses', $options ) ) {
-                       # Now build the classes.  This is the bulk of what we're doing.
+                       # Now build the classes.
                        $classes = array();
 
                        if( in_array( 'broken', $options ) ) {
-                               $classes []= 'new';
+                               $classes[] = 'new';
+                       }
+
+                       if( $target->isExternal() ) {
+                               $classes[] = 'extiw';
                        }
 
                        # Note that redirects never count as stubs here.
                        if ( $target->isRedirect() ) {
-                               $classes []= 'mw-redirect';
+                               $classes[] = 'mw-redirect';
                        } elseif( $target->isContentPage() ) {
+                               # Check for stub.
                                $threshold = $wgUser->getOption( 'stubthreshold' );
                                if( $threshold > 0 and $target->getLength() < $threshold ) {
-                                       $classes []= 'stub';
+                                       $classes[] = 'stub';
                                }
                        }
                        if( $classes != array() ) {
index b874b15..859d98f 100644 (file)
@@ -867,6 +867,36 @@ class Title {
                return $url;
        }
 
+       /**
+        * Get a URL that's the simplest URL that will be valid to link, locally,
+        * to the current Title.  It includes the fragment, but does not include
+        * the server unless action=render is used (or the link is external).  If
+        * there's a fragment but the prefixed text is empty, we just return a link
+        * to the fragment.
+        *
+        * @param array $query An associative array of key => value pairs for the
+        *   query string.  Keys and values will be escaped.
+        * @param string $variant Language variant of URL (for sr, zh..).  Ignored
+        *   for external links.  Default is "false" (same variant as current page,
+        *   for anonymous users).
+        * @return string the URL
+        */
+       public function getLinkUrl( $query = array(), $variant = false ) {
+               if( !is_array( $query ) ) {
+                       throw new MWException( 'Title::getLinkUrl passed a non-array for '.
+                       '$query' );
+               }
+               if( $this->isExternal() ) {
+                       return $this->getFullURL( $query );
+               } elseif( $this->getPrefixedText() === ''
+               and $this->getFragment() !== '' ) {
+                       return $this->getFragmentForURL();
+               } else {
+                       return $this->getLocalURL( $query, $variant )
+                               . $this->getFragmentForURL();
+               }
+       }
+
        /**
         * Get an HTML-escaped version of the URL form, suitable for
         * using in a link, without a server name or fragment
index 6102259..45a5689 100644 (file)
@@ -4291,7 +4291,7 @@ class Parser
                        $replacePairs = array();
                        foreach( $this->mInterwikiLinkHolders['texts'] as $key => $link ) {
                                $title = $this->mInterwikiLinkHolders['titles'][$key];
-                               $replacePairs[$key] = $sk->makeLinkObj( $title, $link );
+                               $replacePairs[$key] = $sk->link( $title, $link );
                        }
                        $replacer = new HashtableReplacer( $replacePairs, 1 );
 
index a9f4e0a..b8ef259 100644 (file)
@@ -1573,7 +1573,7 @@ Inline interwiki link
 !! input
 [[MeatBall:SoftSecurity]]
 !! result
-<p><a href="http://www.usemod.com/cgi-bin/mb.pl?SoftSecurity" class="extiw" title="meatball:SoftSecurity">MeatBall:SoftSecurity</a>
+<p><a href="http://www.usemod.com/cgi-bin/mb.pl?SoftSecurity" title="meatball:SoftSecurity" class="extiw">MeatBall:SoftSecurity</a>
 </p>
 !! end
 
@@ -1582,7 +1582,7 @@ Inline interwiki link with empty title (bug 2372)
 !! input
 [[MeatBall:]]
 !! result
-<p><a href="http://www.usemod.com/cgi-bin/mb.pl?" class="extiw" title="meatball:">MeatBall:</a>
+<p><a href="http://www.usemod.com/cgi-bin/mb.pl?" title="meatball:" class="extiw">MeatBall:</a>
 </p>
 !! end
 
@@ -1592,8 +1592,8 @@ Interwiki link encoding conversion (bug 1636)
 *[[Wikipedia:ro:Olteni&#0355;a]]
 *[[Wikipedia:ro:Olteni&#355;a]]
 !! result
-<ul><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a>
-</li><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a>
+<ul><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" title="wikipedia:ro:Olteniţa" class="extiw">Wikipedia:ro:Olteni&#355;a</a>
+</li><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" title="wikipedia:ro:Olteniţa" class="extiw">Wikipedia:ro:Olteni&#355;a</a>
 </li></ul>
 
 !! end
@@ -1603,7 +1603,7 @@ Interwiki link with fragment (bug 2130)
 !! input
 [[MeatBall:SoftSecurity#foo]]
 !! result
-<p><a href="http://www.usemod.com/cgi-bin/mb.pl?SoftSecurity#foo" class="extiw" title="meatball:SoftSecurity">MeatBall:SoftSecurity#foo</a>
+<p><a href="http://www.usemod.com/cgi-bin/mb.pl?SoftSecurity#foo" title="meatball:SoftSecurity" class="extiw">MeatBall:SoftSecurity#foo</a>
 </p>
 !! end