resourceloader: Keep module_deps handling inside module base class
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 17 Sep 2015 22:14:46 +0000 (23:14 +0100)
committerOri.livneh <ori@wikimedia.org>
Thu, 24 Sep 2015 07:43:51 +0000 (07:43 +0000)
Most of it was already there (SELECT in preloadInfo, and in-class holder via
setFileDependencies) but the logic to write to the database was within
the file module class.

Moving it the other way around may make more sense in the future, but for
the moment ResourceLoader.php makes assumptions about all modules being
able to store setFileDependencies as read from the database. Whether or not
a class currently implements a way to write to that table.

Change-Id: I06b5da4144798197478c66e8b9ccd4cc62cf6fb6

includes/resourceloader/ResourceLoaderFileModule.php
includes/resourceloader/ResourceLoaderModule.php

index 51118b1..ca10ab7 100644 (file)
@@ -417,21 +417,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                );
                // Collect referenced files
                $this->localFileRefs = array_unique( $this->localFileRefs );
-               // If the list has been modified since last time we cached it, update the cache
-               try {
-                       if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) ) {
-                               $dbw = wfGetDB( DB_MASTER );
-                               $dbw->replace( 'module_deps',
-                                       array( array( 'md_module', 'md_skin' ) ), array(
-                                               'md_module' => $this->getName(),
-                                               'md_skin' => $context->getSkin(),
-                                               'md_deps' => FormatJson::encode( $this->localFileRefs ),
-                                       )
-                               );
-                       }
-               } catch ( Exception $e ) {
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
-               }
+               $this->saveFileDependencies( $context->getSkin(), $this->localFileRefs );
+
                return $styles;
        }
 
index 376b62c..80c8220 100644 (file)
@@ -374,12 +374,13 @@ abstract class ResourceLoaderModule {
 
        /**
         * Get the files this module depends on indirectly for a given skin.
-        * Currently these are only image files referenced by the module's CSS.
+        *
+        * These are only image files referenced by the module's stylesheet.
         *
         * @param string $skin Skin name
         * @return array List of files
         */
-       public function getFileDependencies( $skin ) {
+       protected function getFileDependencies( $skin ) {
                // Try in-object cache first
                if ( isset( $this->fileDeps[$skin] ) ) {
                        return $this->fileDeps[$skin];
@@ -405,8 +406,11 @@ abstract class ResourceLoaderModule {
        }
 
        /**
-        * Set preloaded file dependency information. Used so we can load this
-        * information for all modules at once.
+        * Set in-object cache for file dependencies.
+        *
+        * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
+        * To save the data, use saveFileDependencies().
+        *
         * @param string $skin Skin name
         * @param array $deps Array of file names
         */
@@ -414,6 +418,31 @@ abstract class ResourceLoaderModule {
                $this->fileDeps[$skin] = $deps;
        }
 
+       /**
+        * Set the files this module depends on indirectly for a given skin.
+        *
+        * @since 1.26
+        * @param string $skin Skin name
+        * @param array $localFileRefs List of files
+        */
+       protected function saveFileDependencies( $skin, $localFileRefs ) {
+               try {
+                       // If the list has been modified since last time we cached it, update the cache
+                       if ( $localFileRefs !== $this->getFileDependencies( $skin ) ) {
+                               $dbw = wfGetDB( DB_MASTER );
+                               $dbw->replace( 'module_deps',
+                                       array( array( 'md_module', 'md_skin' ) ), array(
+                                               'md_module' => $this->getName(),
+                                               'md_skin' => $skin,
+                                               'md_deps' => FormatJson::encode( $localFileRefs ),
+                                       )
+                               );
+                       }
+               } catch ( Exception $e ) {
+                       wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
+               }
+       }
+
        /**
         * Get the last modification timestamp of the messages in this module for a given language.
         * @param string $lang Language code
@@ -445,8 +474,10 @@ abstract class ResourceLoaderModule {
        }
 
        /**
-        * Set a preloaded message blob last modification timestamp. Used so we
-        * can load this information for all modules at once.
+        * Set in-object cache for message blob time.
+        *
+        * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
+        *
         * @param string $lang Language code
         * @param int $mtime UNIX timestamp
         */