Parser: Replace Linker::link() with LinkRenderer
authorKunal Mehta <legoktm@member.fsf.org>
Fri, 13 May 2016 00:37:17 +0000 (17:37 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 26 May 2016 21:05:47 +0000 (14:05 -0700)
Replaces usage of Linker::link() in Parser and LinkHolderArray with the
new LinkRenderer.

Change-Id: Icb796ef08d70926728732ab5468940c09ba5eaf8

includes/parser/LinkHolderArray.php
includes/parser/Parser.php

index 8575e69..1c6f404 100644 (file)
@@ -287,9 +287,10 @@ class LinkHolderArray {
                $colours = [];
                $linkCache = LinkCache::singleton();
                $output = $this->parent->getOutput();
+               $linkRenderer = $this->parent->getLinkRenderer();
+               $threshold = $linkRenderer->getStubThreshold();
 
                $dbr = wfGetDB( DB_SLAVE );
-               $threshold = $this->parent->getOptions()->getStubThreshold();
 
                # Sort by namespace
                ksort( $this->internals );
@@ -352,9 +353,6 @@ class LinkHolderArray {
                                $pdbk = $title->getPrefixedDBkey();
                                $linkCache->addGoodLinkObjFromRow( $title, $s );
                                $output->addLink( $title, $s->page_id );
-                               # @todo FIXME: Convoluted data flow
-                               # The redirect status and length is passed to getLinkColour via the LinkCache
-                               # Use formal parameters instead
                                $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
                                // add id to the extension todolist
                                $linkcolour_ids[$s->page_id] = $pdbk;
@@ -387,6 +385,8 @@ class LinkHolderArray {
                                }
                                if ( $displayText === '' ) {
                                        $displayText = null;
+                               } else {
+                                       $displayText = new HtmlArmor( $displayText );
                                }
                                if ( !isset( $colours[$pdbk] ) ) {
                                        $colours[$pdbk] = 'new';
@@ -395,15 +395,16 @@ class LinkHolderArray {
                                if ( $colours[$pdbk] == 'new' ) {
                                        $linkCache->addBadLinkObj( $title );
                                        $output->addLink( $title, 0 );
-                                       $type = [ 'broken' ];
+                                       $link = $linkRenderer->makeBrokenLink(
+                                               $title, $displayText, $attribs, $query
+                                       );
                                } else {
-                                       if ( $colours[$pdbk] != '' ) {
-                                               $attribs['class'] = $colours[$pdbk];
-                                       }
-                                       $type = [ 'known', 'noclasses' ];
+                                       $link = $linkRenderer->makePreloadedLink(
+                                               $title, $displayText, $colours[$pdbk], $attribs, $query
+                                       );
                                }
-                               $replacePairs[$searchkey] = Linker::link( $title, $displayText,
-                                               $attribs, $query, $type );
+
+                               $replacePairs[$searchkey] = $link;
                        }
                }
                $replacer = new HashtableReplacer( $replacePairs, 1 );
@@ -429,11 +430,12 @@ class LinkHolderArray {
                # Make interwiki link HTML
                $output = $this->parent->getOutput();
                $replacePairs = [];
-               $options = [
-                       'stubThreshold' => $this->parent->getOptions()->getStubThreshold(),
-               ];
+               $linkRenderer = $this->parent->getLinkRenderer();
                foreach ( $this->interwikis as $key => $link ) {
-                       $replacePairs[$key] = Linker::link( $link['title'], $link['text'], [], [], $options );
+                       $replacePairs[$key] = $linkRenderer->makeLink(
+                               $link['title'],
+                               new HtmlArmor( $link['text'] )
+                       );
                        $output->addInterwikiLink( $link['title'] );
                }
                $replacer = new HashtableReplacer( $replacePairs, 1 );
@@ -573,9 +575,6 @@ class LinkHolderArray {
                                                $entry['pdbk'] = $varPdbk;
 
                                                // set pdbk and colour
-                                               # @todo FIXME: Convoluted data flow
-                                               # The redirect status and length is passed to getLinkColour via the LinkCache
-                                               # Use formal parameters instead
                                                $colours[$varPdbk] = Linker::getLinkColour( $variantTitle, $threshold );
                                                $linkcolour_ids[$s->page_id] = $pdbk;
                                        }
index b5e5d80..8f55f01 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  * @ingroup Parser
  */
+use MediaWiki\Linker\LinkRenderer;
+use MediaWiki\MediaWikiServices;
 
 /**
  * @defgroup Parser Parser
@@ -248,6 +250,11 @@ class Parser {
        /** @var SectionProfiler */
        protected $mProfiler;
 
+       /**
+        * @var LinkRenderer
+        */
+       protected $mLinkRenderer;
+
        /**
         * @param array $conf
         */
@@ -888,6 +895,24 @@ class Parser {
                return $this->mPreprocessor;
        }
 
+       /**
+        * Get a LinkRenderer instance to make links with
+        *
+        * @since 1.28
+        * @return LinkRenderer
+        */
+       public function getLinkRenderer() {
+               if ( !$this->mLinkRenderer ) {
+                       $this->mLinkRenderer = MediaWikiServices::getInstance()
+                               ->getLinkRendererFactory()->create();
+                       $this->mLinkRenderer->setStubThreshold(
+                               $this->getOptions()->getStubThreshold()
+                       );
+               }
+
+               return $this->mLinkRenderer;
+       }
+
        /**
         * Replaces all occurrences of HTML-style comments and the given tags
         * in the text with a random marker and returns the next text. The output
@@ -2362,7 +2387,9 @@ class Parser {
                        $text = htmlspecialchars( $nt->getPrefixedText() );
                }
 
-               $link = Linker::linkKnown( $nt, "$prefix$text$inside", [], $query );
+               $link = $this->getLinkRenderer()->makeKnownLink(
+                       $nt, new HtmlArmor( "$prefix$text$inside" ), [], $query
+               );
 
                return $this->armorLinks( $link ) . $trail;
        }