Make Linker::formatLinksInComment format links to other wikis
authorMarius Hoch <hoo@online.de>
Fri, 2 Jan 2015 18:33:04 +0000 (19:33 +0100)
committerKunal Mehta <legoktm@gmail.com>
Fri, 9 Jan 2015 19:22:31 +0000 (11:22 -0800)
Needed for CentralAuth.

Change-Id: Ibdedf087f85046646450367cbf1811db578d8f4b

includes/Linker.php
tests/phpunit/includes/LinkerTest.php

index 10c6804..7840868 100644 (file)
@@ -1385,9 +1385,11 @@ class Linker {
         * @param string $comment Text to format links in
         * @param Title|null $title An optional title object used to links to sections
         * @param bool $local Whether section links should refer to local page
+        * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), as used by WikiMap
+        *
         * @return string
         */
-       public static function formatLinksInComment( $comment, $title = null, $local = false ) {
+       public static function formatLinksInComment( $comment, $title = null, $local = false, $wikiId = null ) {
                return preg_replace_callback(
                        '/
                                \[\[
@@ -1401,7 +1403,7 @@ class Linker {
                                \]\]
                                ([^[]*) # 3. link trail (the text up until the next link)
                        /x',
-                       function ( $match ) use ( $title, $local ) {
+                       function ( $match ) use ( $title, $local, $wikiId ) {
                                global $wgContLang;
 
                                $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
@@ -1457,11 +1459,22 @@ class Linker {
                                                        $newTarget = clone ( $title );
                                                        $newTarget->setFragment( '#' . $target->getFragment() );
                                                        $target = $newTarget;
+
+                                               }
+
+                                               if ( $wikiId !== null ) {
+                                                       $thelink = Linker::makeExternalLink(
+                                                               WikiMap::getForeignURL( $wikiId, $target->getPrefixedURL() ),
+                                                               $linkText . $inside,
+                                                               true
+                                                       ) . $trail;
+                                               } else {
+                                                       $thelink = Linker::link(
+                                                               $target,
+                                                               $linkText . $inside
+                                                       ) . $trail;
                                                }
-                                               $thelink = Linker::link(
-                                                       $target,
-                                                       $linkText . $inside
-                                               ) . $trail;
+
                                        }
                                }
                                if ( $thelink ) {
index fc88a55..6341bf0 100644 (file)
@@ -193,4 +193,49 @@ class LinkerTest extends MediaWikiLangTestCase {
                        ),
                );
        }
+
+       /**
+        * @covers Linker::formatLinksInComment
+        * @dataProvider provideCasesForFormatLinksInComment
+        */
+       public function testFormatLinksInComment( $expected, $input, $wiki ) {
+
+               $conf = new SiteConfiguration();
+               $conf->settings = array(
+                       'wgServer' => array(
+                               'enwiki' => '//en.example.org'
+                       ),
+                       'wgArticlePath' => array(
+                               'enwiki' => '/w/$1',
+                       ),
+               );
+               $conf->suffixes = array( 'wiki' );
+               $this->setMwGlobals( array(
+                       'wgScript' => '/wiki/index.php',
+                       'wgArticlePath' => '/wiki/$1',
+                       'wgWellFormedXml' => true,
+                       'wgCapitalLinks' => true,
+                       'wgConf' => $conf,
+               ) );
+
+               $this->assertEquals(
+                       $expected,
+                       Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
+               );
+       }
+
+       public static function provideCasesForFormatLinksInComment() {
+               return array(
+                       array(
+                               'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
+                               'foo bar [[Special:BlankPage]]',
+                               null,
+                       ),
+                       array(
+                               'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
+                               'foo bar [[Special:BlankPage]]',
+                               'enwiki',
+                       ),
+               );
+       }
 }