5 * Created on Dec 1, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * A query action to return messages from site message cache
32 class ApiQueryAllMessages
extends ApiQueryBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'am' );
38 public function execute() {
39 $params = $this->extractRequestParams();
41 if ( is_null( $params['lang'] ) ) {
42 $langObj = $this->getLanguage();
44 $langObj = Language
::factory( $params['lang'] );
47 if ( $params['enableparser'] ) {
48 if ( !is_null( $params['title'] ) ) {
49 $title = Title
::newFromText( $params['title'] );
50 if ( !$title ||
$title->isExternal() ) {
51 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
54 $title = Title
::newFromText( 'API' );
58 $prop = array_flip( (array)$params['prop'] );
60 // Determine which messages should we print
61 if ( in_array( '*', $params['messages'] ) ) {
62 $message_names = Language
::getMessageKeysFor( $langObj->getCode() );
63 if ( $params['includelocal'] ) {
64 global $wgLanguageCode;
65 $message_names = array_unique( array_merge(
67 // Pass in the content language code so we get local messages that have a
68 // MediaWiki:msgkey page. We might theoretically miss messages that have no
69 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
70 // just a stupid case.
71 MessageCache
::singleton()->getAllMessageKeys( $wgLanguageCode )
74 sort( $message_names );
75 $messages_target = $message_names;
77 $messages_target = $params['messages'];
80 // Filter messages that have the specified prefix
81 // Because we sorted the message array earlier, they will appear in a clump:
82 if ( isset( $params['prefix'] ) ) {
84 $messages_filtered = array();
85 foreach ( $messages_target as $message ) {
86 // === 0: must be at beginning of string (position 0)
87 if ( strpos( $message, $params['prefix'] ) === 0 ) {
91 $messages_filtered[] = $message;
96 $messages_target = $messages_filtered;
99 // Filter messages that contain specified string
100 if ( isset( $params['filter'] ) ) {
101 $messages_filtered = array();
102 foreach ( $messages_target as $message ) {
103 // !== is used because filter can be at the beginning of the string
104 if ( strpos( $message, $params['filter'] ) !== false ) {
105 $messages_filtered[] = $message;
108 $messages_target = $messages_filtered;
111 // Whether we have any sort of message customisation filtering
112 $customiseFilterEnabled = $params['customised'] !== 'all';
113 if ( $customiseFilterEnabled ) {
115 $lang = $langObj->getCode();
117 $customisedMessages = AllmessagesTablePager
::getCustomisedStatuses(
118 array_map( array( $langObj, 'ucfirst' ), $messages_target ), $lang, $lang != $wgContLang->getCode() );
120 $customised = $params['customised'] === 'modified';
123 // Get all requested messages and print the result
124 $skip = !is_null( $params['from'] );
125 $useto = !is_null( $params['to'] );
126 $result = $this->getResult();
127 foreach ( $messages_target as $message ) {
128 // Skip all messages up to $params['from']
129 if ( $skip && $message === $params['from'] ) {
133 if ( $useto && $message > $params['to'] ) {
138 $a = array( 'name' => $message );
140 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
141 $args = $params['args'];
144 if ( $customiseFilterEnabled ) {
145 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
146 if ( $customised === $messageIsCustomised ) {
148 $a['customised'] = '';
155 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
157 if ( !$msg->exists() ) {
160 // Check if the parser is enabled:
161 if ( $params['enableparser'] ) {
162 $msgString = $msg->title( $title )->text();
164 $msgString = $msg->plain();
166 if ( !$params['nocontent'] ) {
167 ApiResult
::setContent( $a, $msgString );
169 if ( isset( $prop['default'] ) ) {
170 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
171 if ( !$default->exists() ) {
172 $a['defaultmissing'] = '';
173 } elseif ( $default->plain() != $msgString ) {
174 $a['default'] = $default->plain();
178 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
180 $this->setContinueEnumParameter( 'from', $message );
185 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
188 public function getCacheMode( $params ) {
189 if ( is_null( $params['lang'] ) ) {
190 // Language not specified, will be fetched from preferences
191 return 'anon-public-user-private';
192 } elseif ( $params['enableparser'] ) {
193 // User-specific parser options will be used
194 return 'anon-public-user-private';
201 public function getAllowedParams() {
204 ApiBase
::PARAM_DFLT
=> '*',
205 ApiBase
::PARAM_ISMULTI
=> true,
208 ApiBase
::PARAM_ISMULTI
=> true,
209 ApiBase
::PARAM_TYPE
=> array(
213 'enableparser' => false,
214 'nocontent' => false,
215 'includelocal' => false,
217 ApiBase
::PARAM_ISMULTI
=> true,
218 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
221 'customised' => array(
222 ApiBase
::PARAM_DFLT
=> 'all',
223 ApiBase
::PARAM_TYPE
=> array(
237 public function getParamDescription() {
239 'messages' => 'Which messages to output. "*" (default) means all messages',
240 'prop' => 'Which properties to get',
241 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
242 'Will substitute magic words, handle templates etc.' ),
243 'nocontent' => 'If set, do not include the content of the messages in the output.',
244 'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.",
245 "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js",
247 'title' => 'Page name to use as context when parsing message (for enableparser option)',
248 'args' => 'Arguments to be substituted into message',
249 'prefix' => 'Return messages with this prefix',
250 'filter' => 'Return only messages with names that contain this string',
251 'customised' => 'Return only messages in this customisation state',
252 'lang' => 'Return messages in this language',
253 'from' => 'Return messages starting at this message',
254 'to' => 'Return messages ending at this message',
258 public function getResultProperties() {
262 'customised' => 'boolean',
263 'missing' => 'boolean',
265 ApiBase
::PROP_TYPE
=> 'string',
266 ApiBase
::PROP_NULLABLE
=> true
270 'defaultmissing' => 'boolean',
272 ApiBase
::PROP_TYPE
=> 'string',
273 ApiBase
::PROP_NULLABLE
=> true
279 public function getDescription() {
280 return 'Return messages from this site';
283 public function getExamples() {
285 'api.php?action=query&meta=allmessages&refix=ipb-',
286 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
290 public function getHelpUrls() {
291 return 'https://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';