resourceloader: Remove use of msg_resource_links table
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 6 Nov 2015 23:20:16 +0000 (23:20 +0000)
committerKrinkle <krinklemail@gmail.com>
Mon, 9 Nov 2015 19:16:38 +0000 (19:16 +0000)
This table is not needed because module names and their messages
array are available to the runtime environment at very little cost.

The only purpose it was serving is reverse lookup from message
key to module name (e.g. when MessageCache receives update that
need to propagate to MessageBlobStore). However that is better
achieved by simply looping through modules in PHP. The overhead
of a database is not worth this minor convenience.

MessageBlobStore
* insertMessageBlob: Doesn't need to update msg_resource_links.
* updateModule: Doesn't need to update msg_resource_links.
* getUpdatesForMessage: Reimplement with list from memory
  instead of msg_resource_links.

The database table will be removed in If009e2620e59002e1.

Bug: T113092
Change-Id: Ia9131f570001f00c9800b260ac4b3469d54d2784

includes/cache/MessageBlobStore.php
includes/cache/MessageCache.php
includes/resourceloader/ResourceLoader.php
maintenance/cleanupRemovedModules.php

index 6290eae..ab7e171 100644 (file)
@@ -41,6 +41,16 @@ class MessageBlobStore {
         */
        protected $blobCache = array();
 
+       /* @var ResourceLoader */
+       protected $resourceloader;
+
+       /**
+        * @param ResourceLoader $resourceloader
+        */
+       public function __construct( ResourceLoader $resourceloader = null ) {
+               $this->resourceloader = $resourceloader;
+       }
+
        /**
         * Get the singleton instance
         *
@@ -131,29 +141,14 @@ class MessageBlobStore {
                                array( 'IGNORE' )
                        );
 
-                       if ( $success ) {
-                               if ( $dbw->affectedRows() == 0 ) {
-                                       // Blob was already present, fetch it
-                                       $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
-                                                       'mr_resource' => $name,
-                                                       'mr_lang' => $lang,
-                                               ),
-                                               __METHOD__
-                                       );
-                               } else {
-                                       // Update msg_resource_links
-                                       $rows = array();
-
-                                       foreach ( $module->getMessages() as $key ) {
-                                               $rows[] = array(
-                                                       'mrl_resource' => $name,
-                                                       'mrl_message' => $key
-                                               );
-                                       }
-                                       $dbw->insert( 'msg_resource_links', $rows,
-                                               __METHOD__, array( 'IGNORE' )
-                                       );
-                               }
+                       if ( $success && $dbw->affectedRows() == 0 ) {
+                               // Blob was already present, fetch it
+                               $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
+                                               'mr_resource' => $name,
+                                               'mr_lang' => $lang,
+                                       ),
+                                       __METHOD__
+                               );
                        }
                } catch ( DBError $e ) {
                        wfDebug( __METHOD__ . " failed to update DB: $e\n" );
@@ -180,8 +175,6 @@ class MessageBlobStore {
                        return null;
                }
 
-               // Save the old and new blobs for later
-               $oldBlob = $row->mr_blob;
                $newBlob = $this->generateMessageBlob( $module, $lang );
 
                try {
@@ -196,36 +189,6 @@ class MessageBlobStore {
                                array( array( 'mr_resource', 'mr_lang' ) ),
                                $newRow, __METHOD__
                        );
-
-                       // Figure out which messages were added and removed
-                       $oldMessages = array_keys( FormatJson::decode( $oldBlob, true ) );
-                       $newMessages = array_keys( FormatJson::decode( $newBlob, true ) );
-                       $added = array_diff( $newMessages, $oldMessages );
-                       $removed = array_diff( $oldMessages, $newMessages );
-
-                       // Delete removed messages, insert added ones
-                       if ( $removed ) {
-                               $dbw->delete( 'msg_resource_links', array(
-                                               'mrl_resource' => $name,
-                                               'mrl_message' => $removed
-                                       ), __METHOD__
-                               );
-                       }
-
-                       $newLinksRows = array();
-
-                       foreach ( $added as $message ) {
-                               $newLinksRows[] = array(
-                                       'mrl_resource' => $name,
-                                       'mrl_message' => $message
-                               );
-                       }
-
-                       if ( $newLinksRows ) {
-                               $dbw->insert( 'msg_resource_links', $newLinksRows, __METHOD__,
-                                       array( 'IGNORE' ) // just in case
-                               );
-                       }
                } catch ( Exception $e ) {
                        wfDebug( __METHOD__ . " failed to update DB: $e\n" );
                }
@@ -273,26 +236,36 @@ class MessageBlobStore {
                                }
                        } while ( count( $updates ) );
 
-                       // No need to update msg_resource_links because we didn't add
-                       // or remove any messages, we just changed their contents.
                } catch ( Exception $e ) {
                        wfDebug( __METHOD__ . " failed to update DB: $e\n" );
                }
        }
 
        public function clear() {
-               // TODO: Give this some more thought
                try {
                        // Not using TRUNCATE, because that needs extra permissions,
                        // which maybe not granted to the database user.
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->delete( 'msg_resource', '*', __METHOD__ );
-                       $dbw->delete( 'msg_resource_links', '*', __METHOD__ );
                } catch ( Exception $e ) {
                        wfDebug( __METHOD__ . " failed to update DB: $e\n" );
                }
        }
 
+       /**
+        * @return ResourceLoader
+        */
+       protected function getResourceLoader() {
+               // For back-compat this class supports instantiation without passing ResourceLoader
+               // Lazy-initialise this property because most callers don't need it.
+               if ( $this->resourceloader === null ) {
+                       wfDebug( __CLASS__ . ' created without a ResourceLoader instance' );
+                       $this->resourceloader = new ResourceLoader();
+               }
+
+               return $this->resourceloader;
+       }
+
        /**
         * Create an update queue for updateMessage()
         *
@@ -304,11 +277,15 @@ class MessageBlobStore {
                $dbw = wfGetDB( DB_MASTER );
 
                if ( is_null( $prevUpdates ) ) {
+                       $rl = $this->getResourceLoader();
+                       $moduleNames = $rl->getModulesByMessage( $key );
                        // Fetch all blobs referencing $key
                        $res = $dbw->select(
-                               array( 'msg_resource', 'msg_resource_links' ),
+                               array( 'msg_resource' ),
                                array( 'mr_resource', 'mr_lang', 'mr_blob', 'mr_timestamp' ),
-                               array( 'mrl_message' => $key, 'mr_resource=mrl_resource' ),
+                               array(
+                                       'mr_resource' => $moduleNames,
+                               ),
                                __METHOD__
                        );
                } else {
index ae746e3..2818d2f 100644 (file)
@@ -578,7 +578,8 @@ class MessageCache {
                }
 
                // Update the message in the message blob store
-               $blobStore = new MessageBlobStore();
+               $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
+               $blobStore = $resourceloader->getMessageBlobStore();
                $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
 
                Hooks::run( 'MessageCacheReplace', array( $title, $text ) );
index d9416e4..6995642 100644 (file)
@@ -274,7 +274,7 @@ class ResourceLoader implements LoggerAwareInterface {
                        $this->registerTestModules();
                }
 
-               $this->setMessageBlobStore( new MessageBlobStore() );
+               $this->setMessageBlobStore( new MessageBlobStore( $this ) );
        }
 
        /**
@@ -1069,6 +1069,23 @@ MESSAGE;
                return $out;
        }
 
+       /**
+        * Get names of modules that use a certain message.
+        *
+        * @param string $messageKey
+        * @return array List of module names
+        */
+       public function getModulesByMessage( $messageKey ) {
+               $moduleNames = array();
+               foreach ( $this->getModuleNames() as $moduleName ) {
+                       $module = $this->getModule( $moduleName );
+                       if ( in_array( $messageKey, $module->getMessages() ) ) {
+                               $moduleNames[] = $moduleName;
+                       }
+               }
+               return $moduleNames;
+       }
+
        /* Static Methods */
 
        /**
index a4e66ca..ae05930 100644 (file)
@@ -72,19 +72,6 @@ class CleanupRemovedModules extends Maintenance {
                        wfWaitForSlaves();
                } while ( $numRows > 0 );
                $this->output( "done\n" );
-
-               $this->output( "Cleaning up msg_resource_links table...\n" );
-               $i = 1;
-               $msgResLinks = $dbw->tableName( 'msg_resource_links' );
-               do {
-                       $where = $moduleList ? "mrl_resource NOT IN ($moduleList)" : '1=1';
-                       $dbw->query( "DELETE FROM $msgResLinks WHERE $where LIMIT $limit", __METHOD__ );
-                       $numRows = $dbw->affectedRows();
-                       $this->output( "Batch $i: $numRows rows\n" );
-                       $i++;
-                       wfWaitForSlaves();
-               } while ( $numRows > 0 );
-               $this->output( "done\n" );
        }
 }