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();
43 } elseif ( !Language
::isValidCode( $params['lang'] ) ) {
44 $this->dieUsage( 'Invalid language code for parameter lang', 'invalidlang' );
46 $langObj = Language
::factory( $params['lang'] );
49 if ( $params['enableparser'] ) {
50 if ( !is_null( $params['title'] ) ) {
51 $title = Title
::newFromText( $params['title'] );
52 if ( !$title ||
$title->isExternal() ) {
53 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
56 $title = Title
::newFromText( 'API' );
60 $prop = array_flip( (array)$params['prop'] );
62 // Determine which messages should we print
63 if ( in_array( '*', $params['messages'] ) ) {
64 $message_names = Language
::getMessageKeysFor( $langObj->getCode() );
65 if ( $params['includelocal'] ) {
66 global $wgLanguageCode;
67 $message_names = array_unique( array_merge(
69 // Pass in the content language code so we get local messages that have a
70 // MediaWiki:msgkey page. We might theoretically miss messages that have no
71 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
72 // just a stupid case.
73 MessageCache
::singleton()->getAllMessageKeys( $wgLanguageCode )
76 sort( $message_names );
77 $messages_target = $message_names;
79 $messages_target = $params['messages'];
82 // Filter messages that have the specified prefix
83 // Because we sorted the message array earlier, they will appear in a clump:
84 if ( isset( $params['prefix'] ) ) {
86 $messages_filtered = array();
87 foreach ( $messages_target as $message ) {
88 // === 0: must be at beginning of string (position 0)
89 if ( strpos( $message, $params['prefix'] ) === 0 ) {
93 $messages_filtered[] = $message;
98 $messages_target = $messages_filtered;
101 // Filter messages that contain specified string
102 if ( isset( $params['filter'] ) ) {
103 $messages_filtered = array();
104 foreach ( $messages_target as $message ) {
105 // !== is used because filter can be at the beginning of the string
106 if ( strpos( $message, $params['filter'] ) !== false ) {
107 $messages_filtered[] = $message;
110 $messages_target = $messages_filtered;
113 // Whether we have any sort of message customisation filtering
114 $customiseFilterEnabled = $params['customised'] !== 'all';
115 if ( $customiseFilterEnabled ) {
117 $lang = $langObj->getCode();
119 $customisedMessages = AllmessagesTablePager
::getCustomisedStatuses(
120 array_map( array( $langObj, 'ucfirst' ), $messages_target ), $lang, $lang != $wgContLang->getCode() );
122 $customised = $params['customised'] === 'modified';
125 // Get all requested messages and print the result
126 $skip = !is_null( $params['from'] );
127 $useto = !is_null( $params['to'] );
128 $result = $this->getResult();
129 foreach ( $messages_target as $message ) {
130 // Skip all messages up to $params['from']
131 if ( $skip && $message === $params['from'] ) {
135 if ( $useto && $message > $params['to'] ) {
140 $a = array( 'name' => $message );
142 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
143 $args = $params['args'];
146 if ( $customiseFilterEnabled ) {
147 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
148 if ( $customised === $messageIsCustomised ) {
150 $a['customised'] = '';
157 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
159 if ( !$msg->exists() ) {
162 // Check if the parser is enabled:
163 if ( $params['enableparser'] ) {
164 $msgString = $msg->title( $title )->text();
166 $msgString = $msg->plain();
168 if ( !$params['nocontent'] ) {
169 ApiResult
::setContent( $a, $msgString );
171 if ( isset( $prop['default'] ) ) {
172 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
173 if ( !$default->exists() ) {
174 $a['defaultmissing'] = '';
175 } elseif ( $default->plain() != $msgString ) {
176 $a['default'] = $default->plain();
180 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
182 $this->setContinueEnumParameter( 'from', $message );
187 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
190 public function getCacheMode( $params ) {
191 if ( is_null( $params['lang'] ) ) {
192 // Language not specified, will be fetched from preferences
193 return 'anon-public-user-private';
194 } elseif ( $params['enableparser'] ) {
195 // User-specific parser options will be used
196 return 'anon-public-user-private';
203 public function getAllowedParams() {
206 ApiBase
::PARAM_DFLT
=> '*',
207 ApiBase
::PARAM_ISMULTI
=> true,
210 ApiBase
::PARAM_ISMULTI
=> true,
211 ApiBase
::PARAM_TYPE
=> array(
215 'enableparser' => false,
216 'nocontent' => false,
217 'includelocal' => false,
219 ApiBase
::PARAM_ISMULTI
=> true,
220 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
223 'customised' => array(
224 ApiBase
::PARAM_DFLT
=> 'all',
225 ApiBase
::PARAM_TYPE
=> array(
239 public function getParamDescription() {
241 'messages' => 'Which messages to output. "*" (default) means all messages',
242 'prop' => 'Which properties to get',
243 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
244 'Will substitute magic words, handle templates etc.' ),
245 'nocontent' => 'If set, do not include the content of the messages in the output.',
246 'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.",
247 "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js",
249 'title' => 'Page name to use as context when parsing message (for enableparser option)',
250 'args' => 'Arguments to be substituted into message',
251 'prefix' => 'Return messages with this prefix',
252 'filter' => 'Return only messages with names that contain this string',
253 'customised' => 'Return only messages in this customisation state',
254 'lang' => 'Return messages in this language',
255 'from' => 'Return messages starting at this message',
256 'to' => 'Return messages ending at this message',
260 public function getPossibleErrors() {
261 return array_merge( parent
::getPossibleErrors(), array(
262 array( 'code' => 'invalidlang', 'info' => 'Invalid language code for parameter lang' ),
266 public function getResultProperties() {
270 'customised' => 'boolean',
271 'missing' => 'boolean',
273 ApiBase
::PROP_TYPE
=> 'string',
274 ApiBase
::PROP_NULLABLE
=> true
278 'defaultmissing' => 'boolean',
280 ApiBase
::PROP_TYPE
=> 'string',
281 ApiBase
::PROP_NULLABLE
=> true
287 public function getDescription() {
288 return 'Return messages from this site';
291 public function getExamples() {
293 'api.php?action=query&meta=allmessages&refix=ipb-',
294 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';