resourceloader: Update MessageBlobStore documentation and code cleanup
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 12 Nov 2015 23:54:04 +0000 (23:54 +0000)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 13 Nov 2015 00:41:57 +0000 (00:41 +0000)
* Update and improve class documentation.

* Remove comment that claims to return false if the module has no message.
  generateMessageBlob() returns '{}' in that case. And this is expected since
  we want to cache the absence of messages.

* Re-use the module objects passed to MessageBlobStore::get() in getFromDB()
  instead of using ResourceLoader::getModule(). The object is the same either
  way (since ResourceLoader re-uses the objects also) but reduces complexity
  of getFromDB() to not need the ResourceLoader object as parameter.

Change-Id: I89a14d7185877fae52791f6837883ed3a6749cd7

includes/cache/MessageBlobStore.php

index 4f0824f..624bbc9 100644 (file)
  */
 
 /**
- * This class provides access to the resource message blobs storage used
- * by ResourceLoader.
+ * This class provides access to the message blobs used by ResourceLoader modules.
  *
  * A message blob is a JSON object containing the interface messages for a
- * certain resource in a certain language. These message blobs are cached
- * in the msg_resource table and automatically invalidated when one of their
- * constituent messages or the resource itself is changed.
+ * certain module in a certain language. These message blobs are cached
+ * in the automatically invalidated when one of their constituent messages,
+ * or the module definition, is changed.
  */
 class MessageBlobStore {
        /**
@@ -72,13 +71,13 @@ class MessageBlobStore {
                        if ( isset( $this->blobCache[$lang][$name] ) ) {
                                $blobs[$name] = $this->blobCache[$lang][$name];
                        } else {
-                               $missingFromCache[] = $name;
+                               $missingFromCache[$name] = $module;
                        }
                }
 
                // Try DB cache
                if ( $missingFromCache ) {
-                       $blobs += $this->getFromDB( $resourceLoader, $missingFromCache, $lang );
+                       $blobs += $this->getFromDB( $missingFromCache, $lang );
                }
 
                // Generate new blobs for any remaining modules and store in DB
@@ -108,7 +107,7 @@ class MessageBlobStore {
         * @param string $name Module name
         * @param ResourceLoaderModule $module
         * @param string $lang Language code
-        * @return mixed Message blob or false if the module has no messages
+        * @return string JSON blob
         */
        public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
                $blob = $this->generateMessageBlob( $module, $lang );
@@ -340,13 +339,12 @@ class MessageBlobStore {
         * Get the message blobs for a set of modules from the database.
         * Modules whose blobs are not in the database are silently dropped.
         *
-        * @param ResourceLoader $resourceLoader
-        * @param array $modules Array of module names
+        * @param array $modules Array of module objects by name
         * @param string $lang Language code
         * @throws MWException
         * @return array Array mapping module names to blobs
         */
-       private function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
+       private function getFromDB( $modules, $lang ) {
                if ( !count( $modules ) ) {
                        return array();
                }
@@ -355,16 +353,16 @@ class MessageBlobStore {
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'msg_resource',
                        array( 'mr_blob', 'mr_resource', 'mr_timestamp' ),
-                       array( 'mr_resource' => $modules, 'mr_lang' => $lang ),
+                       array( 'mr_resource' => array_keys( $modules ), 'mr_lang' => $lang ),
                        __METHOD__
                );
 
                foreach ( $res as $row ) {
-                       $module = $resourceLoader->getModule( $row->mr_resource );
-                       if ( !$module ) {
+                       if ( !isset( $modules[ $row->mr_resource ] ) ) {
                                // This shouldn't be possible
                                throw new MWException( __METHOD__ . ' passed an invalid module name' );
                        }
+                       $module = $modules[ $row->mr_resource ];
 
                        // Update the module's blob if the list of messages changed
                        $blobKeys = array_keys( FormatJson::decode( $row->mr_blob, true ) );
@@ -384,7 +382,7 @@ class MessageBlobStore {
         *
         * @param ResourceLoaderModule $module
         * @param string $lang Language code
-        * @return string JSON object
+        * @return string JSON blob
         */
        private function generateMessageBlob( ResourceLoaderModule $module, $lang ) {
                $messages = array();