Legoktm has uploaded a new change for review.

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

Change subject: LinkRenderer: Re-implement noclasses as makePreloadedLink 
function
......................................................................

LinkRenderer: Re-implement noclasses as makePreloadedLink function

'noclasses' makes more sense as a per-link option rather than an
instance member of the LinkRenderer instance, since it depends entirely
on whether the calling code has preloaded the link classes.

Introduce LinkRenderer::makePreloadedLink() which makes this clear and
requires passing in the classes as a separate parameter. As a side-
effect, due to the way LinkRenderer::mergeAttribs() is implemented, the
'class' attribute will always appear before the 'title' attribute in the
final output.

Change-Id: I0545aa9d7139794bc22f9d3d6d6eccde003b2982
---
M includes/Linker.php
M includes/linker/LinkRenderer.php
M includes/linker/LinkRendererFactory.php
M tests/parser/parserTests.txt
4 files changed, 40 insertions(+), 48 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/79/290879/1

diff --git a/includes/Linker.php b/includes/Linker.php
index cee141f..10d9d76 100644
--- a/includes/Linker.php
+++ b/includes/Linker.php
@@ -235,6 +235,8 @@
                        return $linkRenderer->makeKnownLink( $target, $text, 
$customAttribs, $query );
                } elseif ( in_array( 'broken', $options, true ) ) {
                        return $linkRenderer->makeBrokenLink( $target, $text, 
$customAttribs, $query );
+               } elseif ( in_array( 'noclasses', $options, true ) ) {
+                       return $linkRenderer->makePreloadedLink( $target, 
$text, '', $customAttribs, $query );
                } else {
                        return $linkRenderer->makeLink( $target, $text, 
$customAttribs, $query );
                }
diff --git a/includes/linker/LinkRenderer.php b/includes/linker/LinkRenderer.php
index 0261365..c71075f 100644
--- a/includes/linker/LinkRenderer.php
+++ b/includes/linker/LinkRenderer.php
@@ -53,13 +53,6 @@
        private $expandUrls = false;
 
        /**
-        * Whether extra classes should be added
-        *
-        * @var bool
-        */
-       private $noClasses = false;
-
-       /**
         * @var int
         */
        private $stubThreshold = 0;
@@ -112,20 +105,6 @@
        }
 
        /**
-        * @param bool $no
-        */
-       public function setNoClasses( $no ) {
-               $this->noClasses = $no;
-       }
-
-       /**
-        * @return bool
-        */
-       public function getNoClasses() {
-               return $this->noClasses;
-       }
-
-       /**
         * @param int $threshold
         */
        public function setStubThreshold( $threshold ) {
@@ -172,9 +151,6 @@
         */
        private function getLegacyOptions( $isKnown ) {
                $options = [ 'stubThreshold' => $this->stubThreshold ];
-               if ( $this->noClasses ) {
-                       $options[] = 'noclasses';
-               }
                if ( $this->forceArticlePath ) {
                        $options[] = 'forcearticlepath';
                }
@@ -245,14 +221,18 @@
        }
 
        /**
+        * If you have already looked up the proper CSS classes using 
Linker::getLinkColour()
+        * or some other method, use this to avoid looking it up again.
+        *
         * @param LinkTarget $target
         * @param string|HtmlArmor|null $text
+        * @param string $classes CSS classes to add
         * @param array $extraAttribs
         * @param array $query
         * @return string
         */
-       public function makeKnownLink(
-               LinkTarget $target, $text = null, array $extraAttribs = [], 
array $query = []
+       public function makePreloadedLink(
+               LinkTarget $target, $text = null, $classes, array $extraAttribs 
= [], array $query = []
        ) {
                // Run begin hook
                $ret = $this->runBeginHook( $target, $text, $extraAttribs, 
$query, true );
@@ -261,22 +241,7 @@
                }
                $target = $this->normalizeTarget( $target );
                $url = $this->getLinkURL( $target, $query );
-               $attribs = [];
-               if ( !$this->noClasses ) {
-                       $classes = [];
-                       if ( $target->isExternal() ) {
-                               $classes[] = 'extiw';
-                       }
-                       $title = Title::newFromLinkTarget( $target );
-                       $colour = Linker::getLinkColour( $title, 
$this->stubThreshold );
-                       if ( $colour !== '' ) {
-                               $classes[] = $colour;
-                       }
-                       if ( $classes ) {
-                               $attribs['class'] = implode( ' ', $classes );
-                       }
-               }
-
+               $attribs = [ 'class' => $classes ];
                $prefixedText = $this->titleFormatter->getPrefixedText( $target 
);
                if ( $prefixedText !== '' ) {
                        $attribs['title'] = $prefixedText;
@@ -291,6 +256,35 @@
                }
 
                return $this->buildAElement( $target, $text, $attribs, true );
+       }
+
+       /**
+        * @param LinkTarget $target
+        * @param string|HtmlArmor|null $text
+        * @param array $extraAttribs
+        * @param array $query
+        * @return string
+        */
+       public function makeKnownLink(
+               LinkTarget $target, $text = null, array $extraAttribs = [], 
array $query = []
+       ) {
+               $classes = [];
+               if ( $target->isExternal() ) {
+                       $classes[] = 'extiw';
+               }
+               $title = Title::newFromLinkTarget( $target );
+               $colour = Linker::getLinkColour( $title, $this->stubThreshold );
+               if ( $colour !== '' ) {
+                       $classes[] = $colour;
+               }
+
+               return $this->makePreloadedLink(
+                       $target,
+                       $text,
+                       $classes ? implode( ' ', $classes ) : '',
+                       $extraAttribs,
+                       $query
+               );
        }
 
        /**
@@ -322,7 +316,7 @@
                }
 
                $url = $this->getLinkURL( $target, $query );
-               $attribs = $this->noClasses ? [] : [ 'class' => 'new' ];
+               $attribs = [ 'class' => 'new' ];
                $prefixedText = $this->titleFormatter->getPrefixedText( $target 
);
                if ( $prefixedText !== '' ) {
                        // This ends up in parser cache!
diff --git a/includes/linker/LinkRendererFactory.php 
b/includes/linker/LinkRendererFactory.php
index 3a30772..7124be1e 100644
--- a/includes/linker/LinkRendererFactory.php
+++ b/includes/linker/LinkRendererFactory.php
@@ -67,10 +67,6 @@
        public function createFromLegacyOptions( array $options ) {
                $linkRenderer = $this->create();
 
-               if ( in_array( 'noclasses', $options, true ) ) {
-                       $linkRenderer->setNoClasses( true );
-               }
-
                if ( in_array( 'forcearticlepath', $options, true ) ) {
                        $linkRenderer->setForceArticlePath( true );
                }
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index e93aa7e..930c879 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -13944,7 +13944,7 @@
 !! wikitext
 [[Image:Barfoo.jpg]]
 !! html/php
-<p><a href="/wiki/File:Barfoo.jpg" title="File:Barfoo.jpg" 
class="mw-redirect">File:Barfoo.jpg</a>
+<p><a href="/wiki/File:Barfoo.jpg" class="mw-redirect" 
title="File:Barfoo.jpg">File:Barfoo.jpg</a>
 </p>
 !! end
 

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

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

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

Reply via email to