resourceloader: Avoid endless module_deps write for the same value
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
index 3e94460..6d2bc0d 100644 (file)
  * @author Roan Kattouw
  */
 
+use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
+use Wikimedia\ScopedCallback;
 
 /**
  * Abstraction for ResourceLoader modules, with name registration and maxage functionality.
@@ -186,7 +188,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        public function getConfig() {
                if ( $this->config === null ) {
                        // Ugh, fall back to default
-                       $this->config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+                       $this->config = MediaWikiServices::getInstance()->getMainConfig();
                }
 
                return $this->config;
@@ -329,14 +331,13 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        }
 
        /**
-        * Where on the HTML page should this module's JS be loaded?
-        *  - 'top': in the "<head>"
-        *  - 'bottom': at the bottom of the "<body>"
+        * From where in the page HTML should this module be loaded?
         *
+        * @deprecated since 1.29 Obsolete. All modules load async from `<head>`.
         * @return string
         */
        public function getPosition() {
-               return 'bottom';
+               return 'top';
        }
 
        /**
@@ -460,29 +461,47 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * @param array $localFileRefs List of files
         */
        protected function saveFileDependencies( ResourceLoaderContext $context, $localFileRefs ) {
-               // Normalise array
-               $localFileRefs = array_values( array_unique( $localFileRefs ) );
-               sort( $localFileRefs );
 
                try {
+                       // Related bugs and performance considerations:
+                       // 1. Don't needlessly change the database value with the same list in a
+                       //    different order or with duplicates.
+                       // 2. Use relative paths to avoid ghost entries when $IP changes. (T111481)
+                       // 3. Don't needlessly replace the database with the same value
+                       //    just because $IP changed (e.g. when upgrading a wiki).
+                       // 4. Don't create an endless replace loop on every request for this
+                       //    module when '../' is used anywhere. Even though both are expanded
+                       //    (one expanded by getFileDependencies from the DB, the other is
+                       //    still raw as originally read by RL), the latter has not
+                       //    been normalized yet.
+
+                       // Normalise
+                       $localFileRefs = array_values( array_unique( $localFileRefs ) );
+                       sort( $localFileRefs );
+                       $localPaths = self::getRelativePaths( $localFileRefs );
+
+                       $storedPaths = self::getRelativePaths( $this->getFileDependencies( $context ) );
                        // If the list has been modified since last time we cached it, update the cache
-                       if ( $localFileRefs !== $this->getFileDependencies( $context ) ) {
+                       if ( $localPaths !== $storedPaths ) {
+                               $vary = $context->getSkin() . '|' . $context->getLanguage();
                                $cache = ObjectCache::getLocalClusterInstance();
-                               $key = $cache->makeKey( __METHOD__, $this->getName() );
+                               $key = $cache->makeKey( __METHOD__, $this->getName(), $vary );
                                $scopeLock = $cache->getScopedLock( $key, 0 );
                                if ( !$scopeLock ) {
                                        return; // T124649; avoid write slams
                                }
 
-                               $vary = $context->getSkin() . '|' . $context->getLanguage();
+                               $deps = FormatJson::encode( $localPaths );
                                $dbw = wfGetDB( DB_MASTER );
-                               $dbw->replace( 'module_deps',
-                                       [ [ 'md_module', 'md_skin' ] ],
+                               $dbw->upsert( 'module_deps',
                                        [
                                                'md_module' => $this->getName(),
                                                'md_skin' => $vary,
-                                               // Use relative paths to avoid ghost entries when $IP changes (T111481)
-                                               'md_deps' => FormatJson::encode( self::getRelativePaths( $localFileRefs ) ),
+                                               'md_deps' => $deps,
+                                       ],
+                                       [ 'md_module', 'md_skin' ],
+                                       [
+                                               'md_deps' => $deps,
                                        ]
                                );