jenkins-bot has submitted this change and it was merged. Change subject: Turn MessageBlobStore into a singleton instead of static functions ......................................................................
Turn MessageBlobStore into a singleton instead of static functions For easier testability and other things. There are no uses of this class in any extensions in gerrit. Change-Id: I606de4259239e128ed7e0477fc98b84c647430c4 --- M includes/MessageBlobStore.php M includes/cache/LocalisationCache.php M includes/cache/MessageCache.php M includes/installer/DatabaseUpdater.php M includes/resourceloader/ResourceLoader.php 5 files changed, 35 insertions(+), 20 deletions(-) Approvals: Krinkle: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/MessageBlobStore.php b/includes/MessageBlobStore.php index 5725898..30d4fda 100644 --- a/includes/MessageBlobStore.php +++ b/includes/MessageBlobStore.php @@ -33,6 +33,21 @@ */ class MessageBlobStore { /** + * Get the singleton instance + * + * @since 1.24 + * @return MessageBlobStore + */ + public static function getInstance() { + static $instance = null; + if ( $instance === null ) { + $instance = new self; + } + + return $instance; + } + + /** * Get the message blobs for a set of modules * * @param ResourceLoader $resourceLoader @@ -40,19 +55,19 @@ * @param string $lang Language code * @return array An array mapping module names to message blobs */ - public static function get( ResourceLoader $resourceLoader, $modules, $lang ) { + public function get( ResourceLoader $resourceLoader, $modules, $lang ) { wfProfileIn( __METHOD__ ); if ( !count( $modules ) ) { wfProfileOut( __METHOD__ ); return array(); } // Try getting from the DB first - $blobs = self::getFromDB( $resourceLoader, array_keys( $modules ), $lang ); + $blobs = $this->getFromDB( $resourceLoader, array_keys( $modules ), $lang ); // Generate blobs for any missing modules and store them in the DB $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) ); foreach ( $missing as $name ) { - $blob = self::insertMessageBlob( $name, $modules[$name], $lang ); + $blob = $this->insertMessageBlob( $name, $modules[$name], $lang ); if ( $blob ) { $blobs[$name] = $blob; } @@ -72,8 +87,8 @@ * @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 ) { - $blob = self::generateMessageBlob( $module, $lang ); + public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) { + $blob = $this->generateMessageBlob( $module, $lang ); if ( !$blob ) { return false; @@ -130,7 +145,7 @@ * @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 ) { + public function updateModule( $name, ResourceLoaderModule $module, $lang ) { $dbw = wfGetDB( DB_MASTER ); $row = $dbw->selectRow( 'msg_resource', 'mr_blob', array( 'mr_resource' => $name, 'mr_lang' => $lang ), @@ -142,7 +157,7 @@ // Save the old and new blobs for later $oldBlob = $row->mr_blob; - $newBlob = self::generateMessageBlob( $module, $lang ); + $newBlob = $this->generateMessageBlob( $module, $lang ); try { $newRow = array( @@ -197,7 +212,7 @@ * * @param string $key Message key */ - public static function updateMessage( $key ) { + public function updateMessage( $key ) { try { $dbw = wfGetDB( DB_MASTER ); @@ -206,7 +221,7 @@ // in one iteration. $updates = null; do { - $updates = self::getUpdatesForMessage( $key, $updates ); + $updates = $this->getUpdatesForMessage( $key, $updates ); foreach ( $updates as $k => $update ) { // Update the row on the condition that it @@ -240,7 +255,7 @@ } } - public static function clear() { + public function clear() { // TODO: Give this some more thought try { // Not using TRUNCATE, because that needs extra permissions, @@ -260,7 +275,7 @@ * @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 ) { + private function getUpdatesForMessage( $key, $prevUpdates = null ) { $dbw = wfGetDB( DB_MASTER ); if ( is_null( $prevUpdates ) ) { @@ -297,7 +312,7 @@ 'resource' => $row->mr_resource, 'lang' => $row->mr_lang, 'timestamp' => $row->mr_timestamp, - 'newBlob' => self::reencodeBlob( $row->mr_blob, $key, $row->mr_lang ) + 'newBlob' => $this->reencodeBlob( $row->mr_blob, $key, $row->mr_lang ) ); } @@ -312,7 +327,7 @@ * @param string $lang Language code * @return string Message blob with $key replaced with its new value */ - private static function reencodeBlob( $blob, $key, $lang ) { + private function reencodeBlob( $blob, $key, $lang ) { $decoded = FormatJson::decode( $blob, true ); $decoded[$key] = wfMessage( $key )->inLanguage( $lang )->plain(); @@ -329,7 +344,7 @@ * @throws MWException * @return array Array mapping module names to blobs */ - private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) { + private function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) { global $wgCacheEpoch; $retval = array(); @@ -354,7 +369,7 @@ if ( $keys !== $values || wfTimestamp( TS_MW, $row->mr_timestamp ) <= $wgCacheEpoch ) { - $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $module, $lang ); + $retval[$row->mr_resource] = $this->updateModule( $row->mr_resource, $module, $lang ); } else { $retval[$row->mr_resource] = $row->mr_blob; } @@ -370,7 +385,7 @@ * @param string $lang Language code * @return string JSON object */ - private static function generateMessageBlob( ResourceLoaderModule $module, $lang ) { + private function generateMessageBlob( ResourceLoaderModule $module, $lang ) { $messages = array(); foreach ( $module->getMessages() as $key ) { diff --git a/includes/cache/LocalisationCache.php b/includes/cache/LocalisationCache.php index e2e304a..5214ab2 100644 --- a/includes/cache/LocalisationCache.php +++ b/includes/cache/LocalisationCache.php @@ -983,7 +983,7 @@ # HACK: If using a null (i.e. disabled) storage backend, we # can't write to the MessageBlobStore either if ( $purgeBlobs && !$this->store instanceof LCStoreNull ) { - MessageBlobStore::clear(); + MessageBlobStore::getInstance()->clear(); } wfProfileOut( __METHOD__ ); diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index e34961c..1ef7cc5 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -573,7 +573,7 @@ // Update the message in the message blob store global $wgContLang; - MessageBlobStore::updateMessage( $wgContLang->lcfirst( $msg ) ); + MessageBlobStore::getInstance()->updateMessage( $wgContLang->lcfirst( $msg ) ); wfRunHooks( 'MessageCacheReplace', array( $title, $text ) ); diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index 427ded4..cd34f3a 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -907,7 +907,7 @@ if ( $wgLocalisationCacheConf['manualRecache'] ) { $this->rebuildLocalisationCache(); } - MessageBlobStore::clear(); + MessageBlobStore::getInstance()->clear(); $this->output( "done.\n" ); } diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 5f72d8d..08340c9 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -855,7 +855,7 @@ // Pre-fetch blobs if ( $context->shouldIncludeMessages() ) { try { - $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() ); + $blobs = MessageBlobStore::getInstance()->get( $this, $modules, $context->getLanguage() ); } catch ( Exception $e ) { MWExceptionHandler::logException( $e ); wfDebugLog( -- To view, visit https://gerrit.wikimedia.org/r/155873 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I606de4259239e128ed7e0477fc98b84c647430c4 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits