Make multiple colons escaping interlanguage links invalid, consistently
authorArlo Breault <abreault@wikimedia.org>
Mon, 26 Jun 2017 23:20:31 +0000 (19:20 -0400)
committerArlo Breault <abreault@wikimedia.org>
Thu, 6 Jul 2017 21:09:25 +0000 (17:09 -0400)
 * Right now, one or two are permitted.  This patch limits it to one.
   The current behaviour seems more a byproduct of refactoring than an
   explicit goal.

 * Note that this will break links on a handful of pages surfaced in
   Parsoid's roundtrip testing.

Change-Id: Icabd34bbf15781bb891bd8e0c079d1a65eb28595

includes/parser/Parser.php
includes/title/MediaWikiTitleCodec.php
tests/parser/parserTests.txt

index 9ea65e0..0cc2aac 100644 (file)
@@ -2240,12 +2240,6 @@ class Parser {
                                $link = $origLink;
                        }
 
-                       $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
-                       if ( !$noforce ) {
-                               # Strip off leading ':'
-                               $link = substr( $link, 1 );
-                       }
-
                        $unstrip = $this->mStripState->unstripNoWiki( $link );
                        $nt = is_string( $unstrip ) ? Title::newFromText( $unstrip ) : null;
                        if ( $nt === null ) {
@@ -2256,6 +2250,8 @@ class Parser {
                        $ns = $nt->getNamespace();
                        $iw = $nt->getInterwiki();
 
+                       $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
+
                        if ( $might_be_img ) { # if this is actually an invalid link
                                if ( $ns == NS_FILE && $noforce ) { # but might be an image
                                        $found = false;
@@ -2300,6 +2296,10 @@ class Parser {
                        $wasblank = ( $text == '' );
                        if ( $wasblank ) {
                                $text = $link;
+                               if ( !$noforce ) {
+                                       # Strip off leading ':'
+                                       $text = substr( $text, 1 );
+                               }
                        } else {
                                # T6598 madness. Handle the quotes only if they come from the alternate part
                                # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
@@ -2324,7 +2324,7 @@ class Parser {
                                        }
 
                                        $s = rtrim( $s . $prefix );
-                                       $s .= trim( $trail, "\n" ) == '' ? '': $prefix . $trail;
+                                       $s .= trim( $trail, "\n" ) == '' ? '' : $prefix . $trail;
                                        continue;
                                }
 
index 0fff97c..dd8b975 100644 (file)
@@ -301,7 +301,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
 
                # Initial colon indicates main namespace rather than specified default
                # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
-               if ( $dbkey !== '' && ':' == $dbkey[0] ) {
+               if ( $dbkey !== '' && $dbkey[0] == ':' ) {
                        $parts['namespace'] = NS_MAIN;
                        $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
                        $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
@@ -368,6 +368,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                                        if ( $dbkey !== '' && $dbkey[0] == ':' ) {
                                                $parts['namespace'] = NS_MAIN;
                                                $dbkey = substr( $dbkey, 1 );
+                                               $dbkey = trim( $dbkey, '_' );
                                        }
                                }
                                # If there's no recognized interwiki or namespace,
index 44bcdff..f7629cd 100644 (file)
@@ -8741,6 +8741,19 @@ Blah blah blah
 <a rel="mw:ExtLink" href="http://zh.wikipedia.org/wiki/Chinese" title="zh:Chinese"> zh : Chinese </a></p>
 !! end
 
+!! test
+Multiple colons escaping interlanguage links
+!! wikitext
+[[:es:Spanish]]
+[[::es:Spanish]]
+[[:::es:Spanish]]
+!! html/php
+<p><a href="http://es.wikipedia.org/wiki/Spanish" class="extiw" title="es:Spanish">es:Spanish</a>
+[[::es:Spanish]]
+[[:::es:Spanish]]
+</p>
+!! end
+
 ## parsoid html2wt will normalize the space to _
 !! test
 Space and question mark encoding in interlanguage links (T95473)