From ae46d7fcfd3681e25791413d4500b2b1729f5476 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Wed, 14 Sep 2011 19:59:50 +0000 Subject: [PATCH] Introduce MessageCache::getAllMessageKeys(), which returns all message keys stored in the MessageCache, and use this to power the amincludelocal parameter in meta=allmessages which adds messages that don't exist in PHP but exist as MediaWiki: pages only. --- includes/api/ApiQueryAllmessages.php | 15 +++++++++++++++ includes/cache/MessageCache.php | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/includes/api/ApiQueryAllmessages.php b/includes/api/ApiQueryAllmessages.php index d900d542de..ddd849195a 100644 --- a/includes/api/ApiQueryAllmessages.php +++ b/includes/api/ApiQueryAllmessages.php @@ -66,6 +66,17 @@ class ApiQueryAllmessages extends ApiQueryBase { // Determine which messages should we print if ( in_array( '*', $params['messages'] ) ) { $message_names = Language::getMessageKeysFor( $langObj->getCode() ); + if ( $params['includelocal'] ) { + global $wgLanguageCode; + $message_names = array_unique( array_merge( + $message_names, + // Pass in the content language code so we get local messages that have a + // MediaWiki:msgkey page. We might theoretically miss messages that have no + // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's + // just a stupid case. + MessageCache::singleton()->getAllMessageKeys( $wgLanguageCode ) + ) ); + } sort( $message_names ); $messages_target = $message_names; } else { @@ -207,6 +218,7 @@ class ApiQueryAllmessages extends ApiQueryBase { ), 'enableparser' => false, 'nocontent' => false, + 'includelocal' => false, 'args' => array( ApiBase::PARAM_ISMULTI => true, ApiBase::PARAM_ALLOW_DUPLICATES => true, @@ -235,6 +247,9 @@ class ApiQueryAllmessages extends ApiQueryBase { 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message', 'Will substitute magic words, handle templates etc.' ), 'nocontent' => 'If set, do not include the content of the messages in the output.', + 'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.", + "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js", + ), 'title' => 'Page name to use as context when parsing message (for enableparser option)', 'args' => 'Arguments to be substituted into message', 'prefix' => 'Return messages with this prefix', diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index d16f63e2d9..69644792af 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -961,4 +961,25 @@ class MessageCache { return array_keys( $list ); } + /** + * Get all message keys stored in the message cache for a given language. + * If $code is the content language code, this will return all message keys + * for which MediaWiki:msgkey exists. If $code is another language code, this + * will ONLY return message keys for which MediaWiki:msgkey/$code exists. + * @param $code string + * @return array of message keys (strings) + */ + public function getAllMessageKeys( $code ) { + global $wgContLang; + $this->load( $code ); + if ( !isset( $this->mCache[$code] ) ) { + // Apparently load() failed + return null; + } + $cache = $this->mCache[$code]; // Copy the cache + unset( $cache['VERSION'] ); // Remove the VERSION key + $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys + // Keys may appear with a capital first letter. lcfirst them. + return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) ); + } } -- 2.20.1