Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/236568

Change subject: Allow Linker::formatComment to link to another wiki per default.
......................................................................

Allow Linker::formatComment to link to another wiki per default.

This also adds support for fragment links to WikiMap.

Bug: T111676
Change-Id: I757f2b91f3b50d789d04e530049c34b4628253e3
---
M includes/Linker.php
M includes/WikiMap.php
M includes/site/MediaWikiSite.php
M tests/phpunit/includes/LinkerTest.php
4 files changed, 118 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/68/236568/1

diff --git a/includes/Linker.php b/includes/Linker.php
index d6a4056..ebecfdf 100644
--- a/includes/Linker.php
+++ b/includes/Linker.php
@@ -1276,9 +1276,11 @@
         * @param string $comment
         * @param Title|null $title Title object (to generate link to the 
section in autocomment) or null
         * @param bool $local Whether section links should refer to local page
+        * @param string|null $wikiId Id (as used by WikiMap) of the wiki to 
generate links to. For use with external changes.
+        *
         * @return mixed|string
         */
-       public static function formatComment( $comment, $title = null, $local = 
false ) {
+       public static function formatComment( $comment, $title = null, $local = 
false, $wikiId = null ) {
 
                # Sanitize text a bit:
                $comment = str_replace( "\n", " ", $comment );
@@ -1287,7 +1289,7 @@
 
                # Render autocomments and make links:
                $comment = self::formatAutocomments( $comment, $title, $local );
-               $comment = self::formatLinksInComment( $comment, $title, $local 
);
+               $comment = self::formatLinksInComment( $comment, $title, 
$local, $wikiId );
 
                return $comment;
        }
@@ -1304,7 +1306,7 @@
         * @param string $comment Comment text
         * @param Title|null $title An optional title object used to links to 
sections
         * @param bool $local Whether section links should refer to local page
-        * @return string Formatted comment
+        * @return string Formatted comment (wikitext)
         */
        private static function formatAutocomments( $comment, $title = null, 
$local = false ) {
                // @todo $append here is something of a hack to preserve the 
status
@@ -1349,9 +1351,8 @@
                                                                
$title->getDBkey(), $section );
                                                }
                                                if ( $sectionTitle ) {
-                                                       $link = Linker::link( 
$sectionTitle,
-                                                               
$wgLang->getArrow(), array(), array(),
-                                                               'noclasses' );
+                                                       // the wiki link will 
be formatted by formatLinksInComment()
+                                                       $link = '[[' . 
$sectionTitle->getFullText() . '|' .  $wgLang->getArrow() . ']]';
                                                } else {
                                                        $link = '';
                                                }
@@ -1384,7 +1385,7 @@
         * @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
+        * @param string|null $wikiId Id of the wiki to link to (if not the 
local wiki), as used by WikiMap.
         *
         * @return string
         */
@@ -1459,12 +1460,11 @@
                                                        $newTarget = clone ( 
$title );
                                                        
$newTarget->setFragment( '#' . $target->getFragment() );
                                                        $target = $newTarget;
-
                                                }
 
-                                               if ( $wikiId !== null ) {
+                                               if ( $wikiId !== null && 
!$target->isExternal() ) {
                                                        $thelink = 
Linker::makeExternalLink(
-                                                               
WikiMap::getForeignURL( $wikiId, $target->getFullText() ),
+                                                               
WikiMap::getForeignURL( $wikiId, $target->getPrefixedText(), 
$target->getFragment() ),
                                                                $linkText . 
$inside,
                                                                /* escape = */ 
false // Already escaped
                                                        ) . $trail;
@@ -1579,17 +1579,18 @@
         * @param string $comment
         * @param Title|null $title Title object (to generate link to section 
in autocomment) or null
         * @param bool $local Whether section links should refer to local page
+        * @param string|null $wikiId Id (as used by WikiMap) of the wiki to 
generate links to. For use with external changes.
         *
         * @return string
         */
-       public static function commentBlock( $comment, $title = null, $local = 
false ) {
+       public static function commentBlock( $comment, $title = null, $local = 
false, $wikiId = null ) {
                // '*' used to be the comment inserted by the software way back
                // in antiquity in case none was provided, here for backwards
                // compatibility, acc. to brion -ævar
                if ( $comment == '' || $comment == '*' ) {
                        return '';
                } else {
-                       $formatted = self::formatComment( $comment, $title, 
$local );
+                       $formatted = self::formatComment( $comment, $title, 
$local, $wikiId );
                        $formatted = wfMessage( 'parentheses' )->rawParams( 
$formatted )->escaped();
                        return " <span class=\"comment\">$formatted</span>";
                }
diff --git a/includes/WikiMap.php b/includes/WikiMap.php
index f16f5aa..8fc7eb2 100644
--- a/includes/WikiMap.php
+++ b/includes/WikiMap.php
@@ -108,13 +108,15 @@
         *
         * @param string $wikiID Wiki'd id (generally database name)
         * @param string $page Page name (must be normalised before calling 
this function!)
+        * @param string|null $fragment
+        *
         * @return string|bool URL or false if the wiki was not found
         */
-       public static function getForeignURL( $wikiID, $page ) {
+       public static function getForeignURL( $wikiID, $page, $fragment = null 
) {
                $wiki = WikiMap::getWiki( $wikiID );
 
                if ( $wiki ) {
-                       return $wiki->getFullUrl( $page );
+                       return $wiki->getFullUrl( $page, $fragment );
                }
 
                return false;
@@ -181,21 +183,32 @@
         * Helper function for getUrl()
         *
         * @todo FIXME: This may be generalized...
-        * @param string $page Page name (must be normalised before calling 
this function!)
+        *
+        * @param string $page Page name (must be normalised before calling 
this function! May contain a section part.)
+        * @param string|null $fragment
+        *
         * @return string Url fragment
         */
-       private function getLocalUrl( $page ) {
-               return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', 
$page ) ), $this->mPath );
+       private function getLocalUrl( $page, $fragment = null ) {
+               $page = wfUrlEncode( str_replace( ' ', '_', $page ) );
+
+               if ( is_string( $fragment ) && $fragment !== '' ) {
+                       $page .= '#' . wfUrlEncode( $fragment );
+               }
+
+               return str_replace( '$1', $page, $this->mPath );
        }
 
        /**
         * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on 
this foreign wiki
         *
         * @param string $page Page name (must be normalised before calling 
this function!)
+        * @param string|null $fragment
+        *
         * @return string Url
         */
-       public function getCanonicalUrl( $page ) {
-               return $this->mCanonicalServer . $this->getLocalUrl( $page );
+       public function getCanonicalUrl( $page, $fragment = null ) {
+               return $this->mCanonicalServer . $this->getLocalUrl( $page, 
$fragment );
        }
 
        /**
@@ -209,10 +222,12 @@
        /**
         * Alias for getCanonicalUrl(), for backwards compatibility.
         * @param string $page
+        * @param string|null $fragment
+        *
         * @return string
         */
-       public function getUrl( $page ) {
-               return $this->getCanonicalUrl( $page );
+       public function getUrl( $page, $fragment = null ) {
+               return $this->getCanonicalUrl( $page, $fragment );
        }
 
        /**
@@ -220,10 +235,12 @@
         * when called locally on the wiki.
         *
         * @param string $page Page name (must be normalized before calling 
this function!)
+        * @param string|null $fragment
+        *
         * @return string URL
         */
-       public function getFullUrl( $page ) {
+       public function getFullUrl( $page, $fragment = null ) {
                return $this->mServer .
-                       $this->getLocalUrl( $page );
+                       $this->getLocalUrl( $page, $fragment );
        }
 }
diff --git a/includes/site/MediaWikiSite.php b/includes/site/MediaWikiSite.php
index 95631f8..6ce8884 100644
--- a/includes/site/MediaWikiSite.php
+++ b/includes/site/MediaWikiSite.php
@@ -136,8 +136,13 @@
                        $url = wfAppendQuery( $this->getFileUrl( 'api.php' ), 
$args );
 
                        // Go on call the external site
-                       // @todo we need a good way to specify a timeout here.
-                       $ret = Http::get( $url, array(), __METHOD__ );
+                       // XXX: what timeout should we use here?
+                       $options = array(
+                               'followRedirects' => true,
+                               'timeout' => 'default',
+                               'connectTimeout' => 'default',
+                       );
+                       $ret = Http::get( $url, $options, __METHOD__ );
                }
 
                if ( $ret === false ) {
diff --git a/tests/phpunit/includes/LinkerTest.php 
b/tests/phpunit/includes/LinkerTest.php
index 823c933..4412c60 100644
--- a/tests/phpunit/includes/LinkerTest.php
+++ b/tests/phpunit/includes/LinkerTest.php
@@ -93,12 +93,27 @@
         * @covers Linker::formatAutocomments
         * @covers Linker::formatLinksInComment
         */
-       public function testFormatComment( $expected, $comment, $title = false, 
$local = false ) {
+       public function testFormatComment( $expected, $comment, $title = false, 
$local = false, $wikiId = null ) {
+
+               $conf = new SiteConfiguration();
+               $conf->settings = array(
+                       'wgServer' => array(
+                               'enwiki' => '//en.example.org',
+                               'dewiki' => '//de.example.org',
+                       ),
+                       'wgArticlePath' => array(
+                               'enwiki' => '/w/$1',
+                               'dewiki' => '/w/$1',
+                       ),
+               );
+               $conf->suffixes = array( 'wiki' );
+
                $this->setMwGlobals( array(
                        'wgScript' => '/wiki/index.php',
                        'wgArticlePath' => '/wiki/$1',
                        'wgWellFormedXml' => true,
                        'wgCapitalLinks' => true,
+                       'wgConf' => $conf,
                ) );
 
                if ( $title === false ) {
@@ -108,11 +123,19 @@
 
                $this->assertEquals(
                        $expected,
-                       Linker::formatComment( $comment, $title, $local )
+                       Linker::formatComment( $comment, $title, $local, 
$wikiId )
                );
        }
 
-       public static function provideCasesForFormatComment() {
+       public function provideCasesForFormatComment() {
+               $wikiId = 'enwiki'; // $wgConf has a fake entry for this
+
+               $testTitle = Title::makeTitle( $this->getDefaultWikitextNS(), 
'FormatCommentTest' );
+               $testTitleText = $testTitle->getPrefixedDBkey();
+
+               $testPage = WikiPage::factory( $testTitle );
+               $testPage->doEditContent( 
$testPage->getContentHandler()->makeEmptyContent(), 'testing', EDIT_NEW );
+
                return array(
                        // Linker::formatComment
                        array(
@@ -126,6 +149,10 @@
                        array(
                                "&#039;&#039;&#039;not 
bolded&#039;&#039;&#039;",
                                "'''not bolded'''",
+                       ),
+                       array(
+                               "try &lt;script&gt;evil&lt;/scipt&gt; things",
+                               "try <script>evil</scipt> things",
                        ),
                        // Linker::formatAutocomments
                        array(
@@ -157,6 +184,14 @@
                                "/* autocomment containing /* */ T70361"
                        ),
                        array(
+                               '<a 
href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" 
title="Special:BlankPage">→</a>‎<span dir="auto"><span 
class="autocomment">autocomment containing &quot;quotes&quot;</span></span>',
+                               "/* autocomment containing \"quotes\" */"
+                       ),
+                       array(
+                               '<a 
href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E"
 title="Special:BlankPage">→</a>‎<span dir="auto"><span 
class="autocomment">autocomment containing 
&lt;script&gt;tags&lt;/script&gt;</span></span>',
+                               "/* autocomment containing 
<script>tags</script> */"
+                       ),
+                       array(
                                '<a href="#autocomment">→</a>‎<span 
dir="auto"><span class="autocomment">autocomment</span></span>',
                                "/* autocomment */",
                                false, true
@@ -165,6 +200,16 @@
                                '‎<span dir="auto"><span 
class="autocomment">autocomment</span></span>',
                                "/* autocomment */",
                                null
+                       ),
+                       array(
+                               '<a href="/wiki/' . wfUrlencode( $testTitleText 
) . '#autocomment" title="' . $testTitleText . '">→</a>‎<span dir="auto"><span 
class="autocomment">autocomment</span></span>',
+                               "/* autocomment */",
+                               $testTitle, false
+                       ),
+                       array(
+                               '<a class="external" rel="nofollow" 
href="//en.example.org/w/' . wfUrlencode( $testTitleText ) . 
'#autocomment">→</a>‎<span dir="auto"><span 
class="autocomment">autocomment</span></span>',
+                               "/* autocomment */",
+                               $testTitle, false, $wikiId
                        ),
                        // Linker::formatLinksInComment
                        array(
@@ -191,6 +236,28 @@
                                'abc <a 
href="/wiki/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" 
title="/subpage (page does not exist)">/subpage</a> def',
                                "abc [[/subpage]] def",
                        ),
+                       array(
+                               'abc <a 
href="/wiki/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" 
class="new" title="&quot;evil!&quot; (page does not 
exist)">&quot;evil!&quot;</a> def',
+                               "abc [[\"evil!\"]] def",
+                       ),
+                       array(
+                               'abc [[&lt;script&gt;very evil&lt;/script&gt;]] 
def',
+                               "abc [[<script>very evil</script>]] def",
+                       ),
+                       array(
+                               'abc [[|]] def',
+                               "abc [[|]] def",
+                       ),
+                       array(
+                               'abc <a 
href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" 
title="Link (page does not exist)">link</a> def',
+                               "abc [[link]] def",
+                               $testTitle, false
+                       ),
+                       array(
+                               'abc <a class="external" rel="nofollow" 
href="//en.example.org/w/Link">link</a> def',
+                               "abc [[link]] def",
+                               $testTitle, false, $wikiId
+                       )
                );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/236568
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I757f2b91f3b50d789d04e530049c34b4628253e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to