Merge "Enable $wgVectorUseIconWatch by default."
[lhc/web/wiklou.git] / includes / MessageBlobStore.php
index 5251643..8a8142b 100644 (file)
@@ -29,7 +29,7 @@
  * 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
- * consistuent messages or the resource itself is changed.
+ * constituent messages or the resource itself is changed.
  */
 class MessageBlobStore {
 
@@ -37,8 +37,8 @@ class MessageBlobStore {
         * Get the message blobs for a set of modules
         *
         * @param $resourceLoader ResourceLoader object
-        * @param $modules array Array of module objects keyed by module name
-        * @param $lang string Language code
+        * @param array $modules Array of module objects keyed by module name
+        * @param string $lang Language code
         * @return array An array mapping module names to message blobs
         */
        public static function get( ResourceLoader $resourceLoader, $modules, $lang ) {
@@ -68,9 +68,9 @@ class MessageBlobStore {
         * present, it is not regenerated; instead, the preexisting blob
         * is fetched and returned.
         *
-        * @param $name String: module name
+        * @param string $name module name
         * @param $module ResourceLoaderModule object
-        * @param $lang String: language code
+        * @param string $lang language code
         * @return mixed Message blob or false if the module has no messages
         */
        public static function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
@@ -80,51 +80,54 @@ class MessageBlobStore {
                        return false;
                }
 
-               $dbw = wfGetDB( DB_MASTER );
-               $success = $dbw->insert( 'msg_resource', array(
-                               'mr_lang' => $lang,
-                               'mr_resource' => $name,
-                               'mr_blob' => $blob,
-                               'mr_timestamp' => $dbw->timestamp()
-                       ),
-                       __METHOD__,
-                       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();
+               try {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $success = $dbw->insert( 'msg_resource', array(
+                                       'mr_lang' => $lang,
+                                       'mr_resource' => $name,
+                                       'mr_blob' => $blob,
+                                       'mr_timestamp' => $dbw->timestamp()
+                               ),
+                               __METHOD__,
+                               array( 'IGNORE' )
+                       );
 
-                               foreach ( $module->getMessages() as $key ) {
-                                       $rows[] = array(
-                                               'mrl_resource' => $name,
-                                               'mrl_message' => $key
+                       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' )
                                        );
                                }
-                               $dbw->insert( 'msg_resource_links', $rows,
-                                       __METHOD__, array( 'IGNORE' )
-                               );
                        }
+               } catch ( Exception $e ) {
+                       wfDebug( __METHOD__ . " failed to update DB: $e\n" );
                }
-
                return $blob;
        }
 
        /**
         * Update the message blob for a given module in a given language
         *
-        * @param $name String: module name
+        * @param string $name module name
         * @param $module ResourceLoaderModule object
-        * @param $lang String: language code
+        * @param string $lang language code
         * @return String Regenerated message blob, or null if there was no blob for the given module/language pair
         */
        public static function updateModule( $name, ResourceLoaderModule $module, $lang ) {
@@ -141,111 +144,119 @@ class MessageBlobStore {
                $oldBlob = $row->mr_blob;
                $newBlob = self::generateMessageBlob( $module, $lang );
 
-               $newRow = array(
-                       'mr_resource' => $name,
-                       'mr_lang' => $lang,
-                       'mr_blob' => $newBlob,
-                       'mr_timestamp' => $dbw->timestamp()
-               );
+               try {
+                       $newRow = array(
+                               'mr_resource' => $name,
+                               'mr_lang' => $lang,
+                               'mr_blob' => $newBlob,
+                               'mr_timestamp' => $dbw->timestamp()
+                       );
 
-               $dbw->replace( 'msg_resource',
-                       array( array( 'mr_resource', 'mr_lang' ) ),
-                       $newRow, __METHOD__
-               );
+                       $dbw->replace( 'msg_resource',
+                               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 );
+                       // 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__
-                       );
-               }
+                       // Delete removed messages, insert added ones
+                       if ( $removed ) {
+                               $dbw->delete( 'msg_resource_links', array(
+                                               'mrl_resource' => $name,
+                                               'mrl_message' => $removed
+                                       ), __METHOD__
+                               );
+                       }
 
-               $newLinksRows = array();
+                       $newLinksRows = array();
 
-               foreach ( $added as $message ) {
-                       $newLinksRows[] = array(
-                               'mrl_resource' => $name,
-                               'mrl_message' => $message
-                       );
-               }
+                       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
-                       );
+                       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" );
                }
-
                return $newBlob;
        }
 
        /**
         * Update a single message in all message blobs it occurs in.
         *
-        * @param $key String: message key
+        * @param string $key message key
         */
        public static function updateMessage( $key ) {
-               $dbw = wfGetDB( DB_MASTER );
-
-               // Keep running until the updates queue is empty.
-               // Due to update conflicts, the queue might not be emptied
-               // in one iteration.
-               $updates = null;
-               do {
-                       $updates = self::getUpdatesForMessage( $key, $updates );
-
-                       foreach ( $updates as $k => $update ) {
-                               // Update the row on the condition that it
-                               // didn't change since we fetched it by putting
-                               // the timestamp in the WHERE clause.
-                               $success = $dbw->update( 'msg_resource',
-                                       array(
-                                               'mr_blob' => $update['newBlob'],
-                                               'mr_timestamp' => $dbw->timestamp() ),
-                                       array(
-                                               'mr_resource' => $update['resource'],
-                                               'mr_lang' => $update['lang'],
-                                               'mr_timestamp' => $update['timestamp'] ),
-                                       __METHOD__
-                               );
+               try {
+                       $dbw = wfGetDB( DB_MASTER );
+
+                       // Keep running until the updates queue is empty.
+                       // Due to update conflicts, the queue might not be emptied
+                       // in one iteration.
+                       $updates = null;
+                       do {
+                               $updates = self::getUpdatesForMessage( $key, $updates );
+
+                               foreach ( $updates as $k => $update ) {
+                                       // Update the row on the condition that it
+                                       // didn't change since we fetched it by putting
+                                       // the timestamp in the WHERE clause.
+                                       $success = $dbw->update( 'msg_resource',
+                                               array(
+                                                       'mr_blob' => $update['newBlob'],
+                                                       'mr_timestamp' => $dbw->timestamp() ),
+                                               array(
+                                                       'mr_resource' => $update['resource'],
+                                                       'mr_lang' => $update['lang'],
+                                                       'mr_timestamp' => $update['timestamp'] ),
+                                               __METHOD__
+                                       );
 
-                               // Only requeue conflicted updates.
-                               // If update() returned false, don't retry, for
-                               // fear of getting into an infinite loop
-                               if ( !( $success && $dbw->affectedRows() == 0 ) ) {
-                                       // Not conflicted
-                                       unset( $updates[$k] );
+                                       // Only requeue conflicted updates.
+                                       // If update() returned false, don't retry, for
+                                       // fear of getting into an infinite loop
+                                       if ( !( $success && $dbw->affectedRows() == 0 ) ) {
+                                               // Not conflicted
+                                               unset( $updates[$k] );
+                                       }
                                }
-                       }
-               } while ( count( $updates ) );
+                       } while ( count( $updates ) );
 
-               // No need to update msg_resource_links because we didn't add
-               // or remove any messages, we just changed their contents.
+                       // 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 static function clear() {
-               // HACK: disable clear() on WMF servers
-               return;
-
                // TODO: Give this some more thought
                // TODO: Is TRUNCATE better?
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'msg_resource', '*', __METHOD__ );
-               $dbw->delete( 'msg_resource_links', '*', __METHOD__ );
+               try {
+                       $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" );
+               }
        }
 
        /**
         * Create an update queue for updateMessage()
         *
-        * @param $key String: message key
-        * @param $prevUpdates Array: updates queue to refresh or null to build a fresh update queue
+        * @param string $key message key
+        * @param array $prevUpdates updates queue to refresh or null to build a fresh update queue
         * @return Array: updates queue
         */
        private static function getUpdatesForMessage( $key, $prevUpdates = null ) {
@@ -295,9 +306,9 @@ class MessageBlobStore {
        /**
         * Reencode a message blob with the updated value for a message
         *
-        * @param $blob String: message blob (JSON object)
-        * @param $key String: message key
-        * @param $lang String: language code
+        * @param string $blob message blob (JSON object)
+        * @param string $key message key
+        * @param string $lang language code
         * @return Message blob with $key replaced with its new value
         */
        private static function reencodeBlob( $blob, $key, $lang ) {
@@ -312,8 +323,8 @@ class MessageBlobStore {
         * Modules whose blobs are not in the database are silently dropped.
         *
         * @param $resourceLoader ResourceLoader object
-        * @param $modules Array of module names
-        * @param $lang String: language code
+        * @param array $modules of module names
+        * @param string $lang language code
         * @throws MWException
         * @return array Array mapping module names to blobs
         */
@@ -350,7 +361,7 @@ class MessageBlobStore {
         * Generate the message blob for a given module in a given language.
         *
         * @param $module ResourceLoaderModule object
-        * @param $lang String: language code
+        * @param string $lang language code
         * @return String: JSON object
         */
        private static function generateMessageBlob( ResourceLoaderModule $module, $lang ) {