Merge "Title::getLinkURL(): Allow expanding PROTO_RELATIVE too"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 20 Jul 2016 21:56:21 +0000 (21:56 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 20 Jul 2016 21:56:21 +0000 (21:56 +0000)
1  2 
includes/Title.php
includes/linker/LinkRenderer.php

diff --combined includes/Title.php
@@@ -1773,12 -1773,13 +1773,13 @@@ class Title implements LinkTarget 
         *
         * @param array $query
         * @param bool $query2
-        * @param string $proto Protocol to use; setting this will cause a full URL to be used
+        * @param string|int|bool $proto A PROTO_* constant on how the URL should be expanded,
+        *                               or false (default) for no expansion
         * @see self::getLocalURL for the arguments.
         * @return string The URL
         */
-       public function getLinkURL( $query = '', $query2 = false, $proto = PROTO_RELATIVE ) {
-               if ( $this->isExternal() || $proto !== PROTO_RELATIVE ) {
+       public function getLinkURL( $query = '', $query2 = false, $proto = false ) {
+               if ( $this->isExternal() || $proto !== false ) {
                        $ret = $this->getFullURL( $query, $query2, $proto );
                } elseif ( $this->getPrefixedText() === '' && $this->hasFragment() ) {
                        $ret = $this->getFragmentForURL();
                $conds = $this->pageCond();
                $dbw->onTransactionIdle( function () use ( $dbw, $conds, $method, $purgeTime ) {
                        $dbTimestamp = $dbw->timestamp( $purgeTime ?: time() );
 -
                        $dbw->update(
                                'page',
                                [ 'page_touched' => $dbTimestamp ],
         * @return bool
         */
        public function canUseNoindex() {
 -              global $wgContentNamespaces, $wgExemptFromUserRobotsControl;
 +              global $wgExemptFromUserRobotsControl;
  
                $bannedNamespaces = is_null( $wgExemptFromUserRobotsControl )
 -                      ? $wgContentNamespaces
 +                      ? MWNamespace::getContentNamespaces()
                        : $wgExemptFromUserRobotsControl;
  
                return !in_array( $this->mNamespace, $bannedNamespaces );
@@@ -25,10 -25,8 +25,10 @@@ use DummyLinker
  use Hooks;
  use Html;
  use HtmlArmor;
 +use LinkCache;
  use Linker;
  use MediaWiki\MediaWikiServices;
 +use MWNamespace;
  use Sanitizer;
  use Title;
  use TitleFormatter;
@@@ -64,11 -62,6 +64,11 @@@ class LinkRenderer 
         */
        private $titleFormatter;
  
 +      /**
 +       * @var LinkCache
 +       */
 +      private $linkCache;
 +
        /**
         * Whether to run the legacy Linker hooks
         *
  
        /**
         * @param TitleFormatter $titleFormatter
 +       * @param LinkCache $linkCache
         */
 -      public function __construct( TitleFormatter $titleFormatter ) {
 +      public function __construct( TitleFormatter $titleFormatter, LinkCache $linkCache ) {
                $this->titleFormatter = $titleFormatter;
 +              $this->linkCache = $linkCache;
        }
  
        /**
        }
  
        /**
 -       * If you have already looked up the proper CSS classes using Linker::getLinkColour()
 +       * If you have already looked up the proper CSS classes using LinkRenderer::getLinkClasses()
         * or some other method, use this to avoid looking it up again.
         *
         * @param LinkTarget $target
                if ( $target->isExternal() ) {
                        $classes[] = 'extiw';
                }
 -              $colour = Linker::getLinkColour( $target, $this->stubThreshold );
 +              $colour = $this->getLinkClasses( $target );
                if ( $colour !== '' ) {
                        $classes[] = $colour;
                }
        private function getLinkURL( LinkTarget $target, array $query = [] ) {
                // TODO: Use a LinkTargetResolver service instead of Title
                $title = Title::newFromLinkTarget( $target );
-               $proto = $this->expandUrls !== false
-                       ? $this->expandUrls
-                       : PROTO_RELATIVE;
                if ( $this->forceArticlePath ) {
                        $realQuery = $query;
                        $query = [];
                } else {
                        $realQuery = [];
                }
-               $url = $title->getLinkURL( $query, false, $proto );
+               $url = $title->getLinkURL( $query, false, $this->expandUrls );
  
                if ( $this->forceArticlePath && $realQuery ) {
                        $url = wfAppendQuery( $url, $realQuery );
                return $ret;
        }
  
 +      /**
 +       * Return the CSS classes of a known link
 +       *
 +       * @param LinkTarget $target
 +       * @return string CSS class
 +       */
 +      public function getLinkClasses( LinkTarget $target ) {
 +              // Make sure the target is in the cache
 +              $id = $this->linkCache->addLinkObj( $target );
 +              if ( $id == 0 ) {
 +                      // Doesn't exist
 +                      return '';
 +              }
 +
 +              if ( $this->linkCache->getGoodLinkFieldObj( $target, 'redirect' ) ) {
 +                      # Page is a redirect
 +                      return 'mw-redirect';
 +              } elseif ( $this->stubThreshold > 0 && MWNamespace::isContent( $target->getNamespace() )
 +                      && $this->linkCache->getGoodLinkFieldObj( $target, 'length' ) < $this->stubThreshold
 +              ) {
 +                      # Page is a stub
 +                      return 'stub';
 +              }
 +
 +              return '';
 +      }
  }