Merge "HTML escape parameter 'text' of hook 'SkinEditSectionLinks'"
[lhc/web/wiklou.git] / includes / cache / LinkCache.php
index b9944a8..1bcf948 100644 (file)
@@ -45,17 +45,29 @@ class LinkCache {
        /** @var TitleFormatter */
        private $titleFormatter;
 
+       /** @var NamespaceInfo */
+       private $nsInfo;
+
        /**
         * How many Titles to store. There are two caches, so the amount actually
         * stored in memory can be up to twice this.
         */
        const MAX_SIZE = 10000;
 
-       public function __construct( TitleFormatter $titleFormatter, WANObjectCache $cache ) {
+       public function __construct(
+               TitleFormatter $titleFormatter,
+               WANObjectCache $cache,
+               NamespaceInfo $nsInfo = null
+       ) {
+               if ( !$nsInfo ) {
+                       wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
+                       $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
+               }
                $this->goodLinks = new MapCacheLRU( self::MAX_SIZE );
                $this->badLinks = new MapCacheLRU( self::MAX_SIZE );
                $this->wanCache = $cache;
                $this->titleFormatter = $titleFormatter;
+               $this->nsInfo = $nsInfo;
        }
 
        /**
@@ -141,6 +153,7 @@ class LinkCache {
                        'revision' => (int)$revision,
                        'model' => $model ? (string)$model : null,
                        'lang' => $lang ? (string)$lang : null,
+                       'restrictions' => null
                ] );
        }
 
@@ -158,8 +171,15 @@ class LinkCache {
                        'length' => intval( $row->page_len ),
                        'redirect' => intval( $row->page_is_redirect ),
                        'revision' => intval( $row->page_latest ),
-                       'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
-                       'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
+                       'model' => !empty( $row->page_content_model )
+                               ? strval( $row->page_content_model )
+                               : null,
+                       'lang' => !empty( $row->page_lang )
+                               ? strval( $row->page_lang )
+                               : null,
+                       'restrictions' => !empty( $row->page_restrictions )
+                               ? strval( $row->page_restrictions )
+                               : null
                ] );
        }
 
@@ -189,22 +209,6 @@ class LinkCache {
                $this->goodLinks->clear( $dbkey );
        }
 
-       /**
-        * Add a title to the link cache, return the page_id or zero if non-existent
-        *
-        * @deprecated since 1.27, unused
-        * @param string $title Prefixed DB key
-        * @return int Page ID or zero
-        */
-       public function addLink( $title ) {
-               wfDeprecated( __METHOD__, '1.27' );
-               $nt = Title::newFromDBkey( $title );
-               if ( !$nt ) {
-                       return 0;
-               }
-               return $this->addLinkObj( $nt );
-       }
-
        /**
         * Fields that LinkCache needs to select
         *
@@ -214,7 +218,13 @@ class LinkCache {
        public static function getSelectFields() {
                global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
 
-               $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
+               $fields = [
+                       'page_id',
+                       'page_len',
+                       'page_is_redirect',
+                       'page_latest',
+                       'page_restrictions'
+               ];
                if ( $wgContentHandlerUseDB ) {
                        $fields[] = 'page_content_model';
                }
@@ -233,9 +243,7 @@ class LinkCache {
         */
        public function addLinkObj( LinkTarget $nt ) {
                $key = $this->titleFormatter->getPrefixedDBkey( $nt );
-               if ( $this->isBadLink( $key ) || $nt->isExternal()
-                       || $nt->inNamespace( NS_SPECIAL )
-               ) {
+               if ( $this->isBadLink( $key ) || $nt->isExternal() || $nt->getNamespace() < 0 ) {
                        return 0;
                }
                $id = $this->getGoodLinkID( $key );
@@ -297,7 +305,16 @@ class LinkCache {
        }
 
        private function isCacheable( LinkTarget $title ) {
-               return ( $title->inNamespace( NS_TEMPLATE ) || $title->inNamespace( NS_FILE ) );
+               $ns = $title->getNamespace();
+               if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY ] ) ) {
+                       return true;
+               }
+               // Focus on transcluded pages more than the main content
+               if ( $this->nsInfo->isContent( $ns ) ) {
+                       return false;
+               }
+               // Non-talk extension namespaces (e.g. NS_MODULE)
+               return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
        }
 
        private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {