Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderWikiModule.php
index 5b512af..085244a 100644 (file)
@@ -22,6 +22,7 @@
  * @author Roan Kattouw
  */
 
+use MediaWiki\Linker\LinkTarget;
 use Wikimedia\Rdbms\Database;
 use Wikimedia\Rdbms\IDatabase;
 
@@ -50,7 +51,19 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
        // Origin defaults to users with sitewide authority
        protected $origin = self::ORIGIN_USER_SITEWIDE;
 
-       // In-process cache for title info
+       // In-process cache for title info, structured as an array
+       // [
+       //  <batchKey> // Pipe-separated list of sorted keys from getPages
+       //   => [
+       //     <titleKey> => [ // Normalised title key
+       //       'page_len' => ..,
+       //       'page_latest' => ..,
+       //       'page_touched' => ..,
+       //     ]
+       //   ]
+       // ]
+       // @see self::fetchTitleInfo()
+       // @see self::makeTitleKey()
        protected $titleInfo = [];
 
        // List of page names that contain CSS
@@ -144,24 +157,22 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
 
        /**
         * @param string $titleText
+        * @param ResourceLoaderContext|null $context (but passing null is deprecated)
         * @return null|string
+        * @since 1.32 added the $context parameter
         */
-       protected function getContent( $titleText ) {
+       protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
                $title = Title::newFromText( $titleText );
                if ( !$title ) {
                        return null; // Bad title
                }
 
-               // If the page is a redirect, follow the redirect.
-               if ( $title->isRedirect() ) {
-                       $content = $this->getContentObj( $title );
-                       $title = $content ? $content->getUltimateRedirectTarget() : null;
-                       if ( !$title ) {
-                               return null; // Dead redirect
-                       }
+               $content = $this->getContentObj( $title, $context );
+               if ( !$content ) {
+                       return null; // No content found
                }
 
-               $handler = ContentHandler::getForTitle( $title );
+               $handler = $content->getContentHandler();
                if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
                        $format = CONTENT_FORMAT_CSS;
                } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
@@ -170,31 +181,81 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        return null; // Bad content model
                }
 
-               $content = $this->getContentObj( $title );
-               if ( !$content ) {
-                       return null; // No content found
-               }
-
                return $content->serialize( $format );
        }
 
        /**
         * @param Title $title
+        * @param ResourceLoaderContext|null $context (but passing null is deprecated)
+        * @param int|null $maxRedirects Maximum number of redirects to follow. If
+        *  null, uses $wgMaxRedirects
         * @return Content|null
+        * @since 1.32 added the $context and $maxRedirects parameters
         */
-       protected function getContentObj( Title $title ) {
-               $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
-               if ( !$revision ) {
-                       return null;
+       protected function getContentObj(
+               Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
+       ) {
+               if ( $context === null ) {
+                       wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
                }
-               $content = $revision->getContent( Revision::RAW );
-               if ( !$content ) {
-                       wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
-                       return null;
+
+               $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
+               $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
+               if ( $content ) {
+                       if ( !$content instanceof Content ) {
+                               $this->getLogger()->error(
+                                       'Bad content override for "{title}" in ' . __METHOD__,
+                                       [ 'title' => $title->getPrefixedText() ]
+                               );
+                               return null;
+                       }
+               } else {
+                       $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
+                       if ( !$revision ) {
+                               return null;
+                       }
+                       $content = $revision->getContent( Revision::RAW );
+
+                       if ( !$content ) {
+                               $this->getLogger()->error(
+                                       'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
+                                       [ 'title' => $title->getPrefixedText() ]
+                               );
+                               return null;
+                       }
+               }
+
+               if ( $content && $content->isRedirect() ) {
+                       if ( $maxRedirects === null ) {
+                               $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
+                       }
+                       if ( $maxRedirects > 0 ) {
+                               $newTitle = $content->getRedirectTarget();
+                               return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
+                       }
                }
+
                return $content;
        }
 
+       /**
+        * @param ResourceLoaderContext $context
+        * @return bool
+        */
+       public function shouldEmbedModule( ResourceLoaderContext $context ) {
+               $overrideCallback = $context->getContentOverrideCallback();
+               if ( $overrideCallback && $this->getSource() === 'local' ) {
+                       foreach ( $this->getPages( $context ) as $page => $info ) {
+                               $title = Title::newFromText( $page );
+                               if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
+                                       return true;
+                               }
+                       }
+               }
+
+               return parent::shouldEmbedModule( $context );
+       }
+
        /**
         * @param ResourceLoaderContext $context
         * @return string JavaScript code
@@ -205,7 +266,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        if ( $options['type'] !== 'script' ) {
                                continue;
                        }
-                       $script = $this->getContent( $titleText );
+                       $script = $this->getContent( $titleText, $context );
                        if ( strval( $script ) !== '' ) {
                                $script = $this->validateScriptFile( $titleText, $script );
                                $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
@@ -225,7 +286,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                                continue;
                        }
                        $media = isset( $options['media'] ) ? $options['media'] : 'all';
-                       $style = $this->getContent( $titleText );
+                       $style = $this->getContent( $titleText, $context );
                        if ( strval( $style ) === '' ) {
                                continue;
                        }
@@ -278,6 +339,10 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
        public function isKnownEmpty( ResourceLoaderContext $context ) {
                $revisions = $this->getTitleInfo( $context );
 
+               // If a module has dependencies it cannot be empty. An empty array will be cast to false
+               if ( $this->getDependencies() ) {
+                       return false;
+               }
                // For user modules, don't needlessly load if there are no non-empty pages
                if ( $this->getGroup() === 'user' ) {
                        foreach ( $revisions as $revision ) {
@@ -295,8 +360,13 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                return count( $revisions ) === 0;
        }
 
-       private function setTitleInfo( $key, array $titleInfo ) {
-               $this->titleInfo[$key] = $titleInfo;
+       private function setTitleInfo( $batchKey, array $titleInfo ) {
+               $this->titleInfo[$batchKey] = $titleInfo;
+       }
+
+       private static function makeTitleKey( LinkTarget $title ) {
+               // Used for keys in titleInfo.
+               return "{$title->getNamespace()}:{$title->getDBkey()}";
        }
 
        /**
@@ -313,11 +383,30 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
 
                $pageNames = array_keys( $this->getPages( $context ) );
                sort( $pageNames );
-               $key = implode( '|', $pageNames );
-               if ( !isset( $this->titleInfo[$key] ) ) {
-                       $this->titleInfo[$key] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
+               $batchKey = implode( '|', $pageNames );
+               if ( !isset( $this->titleInfo[$batchKey] ) ) {
+                       $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
+               }
+
+               $titleInfo = $this->titleInfo[$batchKey];
+
+               // Override the title info from the overrides, if any
+               $overrideCallback = $context->getContentOverrideCallback();
+               if ( $overrideCallback ) {
+                       foreach ( $pageNames as $page ) {
+                               $title = Title::newFromText( $page );
+                               $content = $title ? call_user_func( $overrideCallback, $title ) : null;
+                               if ( $content !== null ) {
+                                       $titleInfo[$title->getPrefixedText()] = [
+                                               'page_len' => $content->getSize(),
+                                               'page_latest' => 'TBD', // None available
+                                               'page_touched' => wfTimestamp( TS_MW ),
+                                       ];
+                               }
+                       }
                }
-               return $this->titleInfo[$key];
+
+               return $titleInfo;
        }
 
        protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
@@ -340,8 +429,8 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        foreach ( $res as $row ) {
                                // Avoid including ids or timestamps of revision/page tables so
                                // that versions are not wasted
-                               $title = Title::makeTitle( $row->page_namespace, $row->page_title );
-                               $titleInfo[$title->getPrefixedText()] = [
+                               $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
+                               $titleInfo[ self::makeTitleKey( $title ) ] = [
                                        'page_len' => $row->page_len,
                                        'page_latest' => $row->page_latest,
                                        'page_touched' => $row->page_touched,
@@ -410,23 +499,23 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        $pages = $wikiModule->getPages( $context );
                        // Before we intersect, map the names to canonical form (T145673).
                        $intersect = [];
-                       foreach ( $pages as $page => $unused ) {
-                               $title = Title::newFromText( $page );
+                       foreach ( $pages as $pageName => $unused ) {
+                               $title = Title::newFromText( $pageName );
                                if ( $title ) {
-                                       $intersect[ $title->getPrefixedText() ] = 1;
+                                       $intersect[ self::makeTitleKey( $title ) ] = 1;
                                } else {
                                        // Page name may be invalid if user-provided (e.g. gadgets)
                                        $rl->getLogger()->info(
                                                'Invalid wiki page title "{title}" in ' . __METHOD__,
-                                               [ 'title' => $page ]
+                                               [ 'title' => $pageName ]
                                        );
                                }
                        }
                        $info = array_intersect_key( $allInfo, $intersect );
                        $pageNames = array_keys( $pages );
                        sort( $pageNames );
-                       $key = implode( '|', $pageNames );
-                       $wikiModule->setTitleInfo( $key, $info );
+                       $batchKey = implode( '|', $pageNames );
+                       $wikiModule->setTitleInfo( $batchKey, $info );
                }
        }