From 3cc53e8ff237261d928d6f40e196bf94fd132c2e Mon Sep 17 00:00:00 2001 From: Victor Vasiliev Date: Sat, 1 Dec 2007 17:04:13 +0000 Subject: [PATCH] API: add meta=allmessages --- RELEASE-NOTES | 1 + includes/api/ApiQuery.php | 1 + includes/api/ApiQueryAllmessages.php | 120 +++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 includes/api/ApiQueryAllmessages.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 4b1ea7cb84..b6b036ea32 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -312,6 +312,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * Add limit=max to use maximal limit * Add action=parse to render parser output. Use it instead of action=render which is deprecated. * Add rvtoken=rollback to prop=revisions +* Add meta=allmessages to get messages from site's messages cache. === Languages updated in 1.12 === diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 1b1c3b3fb6..f1a5488d45 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -76,6 +76,7 @@ class ApiQuery extends ApiBase { private $mQueryMetaModules = array ( 'siteinfo' => 'ApiQuerySiteinfo', 'userinfo' => 'ApiQueryUserInfo', + 'allmessages' => 'ApiQueryAllmessages', ); private $mSlaveDB = null; diff --git a/includes/api/ApiQueryAllmessages.php b/includes/api/ApiQueryAllmessages.php new file mode 100644 index 0000000000..9fe74e662b --- /dev/null +++ b/includes/api/ApiQueryAllmessages.php @@ -0,0 +1,120 @@ +@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ('ApiQueryBase.php'); +} + +/** + * A query action to return messages from site message cache + * + * @addtogroup API + */ +class ApiQueryAllmessages extends ApiQueryBase { + + public function __construct($query, $moduleName) { + parent :: __construct($query, $moduleName, 'am'); + } + + public function execute() { + global $wgMessageCache; + $params = $this->extractRequestParams(); + + //Determine which messages should we print + $messages_target = array(); + if( $params['messages'] == '*' ) { + $wgMessageCache->loadAllMessages(); + $message_names = array_keys( array_merge( Language::getMessagesFor( 'en' ), $wgMessageCache->getExtensionMessagesFor( 'en' ) ) ); + sort( $message_names ); + $messages_target = $message_names; + } else { + $messages_target = explode( '|', $params['messages'] ); + } + + //Filter messages + if( isset( $params['filter'] ) ) { + $messages_filtered = array(); + foreach( $messages_target as $message ) { + if( strpos( $message, $params['filter'] ) !== false ) { //!== is used because filter can be at the beginnig of the string + $messages_filtered[] = $message; + } + } + $messages_target = $messages_filtered; + } + + $wgMessageCache->disableTransform(); + + //Get all requested messages + $messages = array(); + foreach( $messages_target as $message ) { + $message = trim( $message ); //Message list can be formatted like "msg1 | msg2 | msg3", so let's trim() it + $messages[$message] = wfMsg( $message ); + } + + //Print the result + $result = $this->getResult(); + $messages_out = array(); + foreach( $messages as $name => $value ) { + $message = array(); + $message['name'] = $name; + $result->setContent( $message, $value ); + $messages_out[] = $message; + } + $result->setIndexedTagName( $messages_out, 'message' ); + $result->addValue( null, $this->getModuleName(), $messages_out ); + } + + protected function getAllowedParams() { + return array ( + 'messages' => array ( + ApiBase :: PARAM_DFLT => '*', + ), + 'filter' => array(), + ); + } + + protected function getParamDescription() { + return array ( + 'messages' => 'Which messages to output. "*" means all messages', + 'filter' => 'Return only messages that contains specified string', + ); + } + + protected function getDescription() { + return 'Return messages from this site.'; + } + + protected function getExamples() { + return array( + 'api.php?action=query&meta=allmessages&amfilter=ipb-', + 'api.php?action=query&meta=allmessages&ammessages=august|mainpage', + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} -- 2.20.1