Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 1, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query action to return messages from site message cache
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllmessages extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'am' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45
46 if ( is_null( $params['lang'] ) ) {
47 global $wgLang;
48 $langObj = $wgLang;
49 } else {
50 $langObj = Language::factory( $params['lang'] );
51 }
52
53 if ( $params['enableparser'] ) {
54 if ( !is_null( $params['title'] ) ) {
55 $title = Title::newFromText( $params['title'] );
56 if ( !$title ) {
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 }
59 } else {
60 $title = Title::newFromText( 'API' );
61 }
62 }
63
64 $prop = array_flip( (array)$params['prop'] );
65
66 // Determine which messages should we print
67 if ( in_array( '*', $params['messages'] ) ) {
68 $message_names = Language::getMessageKeysFor( $langObj->getCode() );
69 if ( $params['includelocal'] ) {
70 global $wgLanguageCode;
71 $message_names = array_unique( array_merge(
72 $message_names,
73 // Pass in the content language code so we get local messages that have a
74 // MediaWiki:msgkey page. We might theoretically miss messages that have no
75 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
76 // just a stupid case.
77 MessageCache::singleton()->getAllMessageKeys( $wgLanguageCode )
78 ) );
79 }
80 sort( $message_names );
81 $messages_target = $message_names;
82 } else {
83 $messages_target = $params['messages'];
84 }
85
86 // Filter messages that have the specified prefix
87 // Because we sorted the message array earlier, they will appear in a clump:
88 if ( isset( $params['prefix'] ) ) {
89 $skip = false;
90 $messages_filtered = array();
91 foreach ( $messages_target as $message ) {
92 // === 0: must be at beginning of string (position 0)
93 if ( strpos( $message, $params['prefix'] ) === 0 ) {
94 if( !$skip ) {
95 $skip = true;
96 }
97 $messages_filtered[] = $message;
98 } elseif ( $skip ) {
99 break;
100 }
101 }
102 $messages_target = $messages_filtered;
103 }
104
105 // Filter messages that contain specified string
106 if ( isset( $params['filter'] ) ) {
107 $messages_filtered = array();
108 foreach ( $messages_target as $message ) {
109 // !== is used because filter can be at the beginning of the string
110 if ( strpos( $message, $params['filter'] ) !== false ) {
111 $messages_filtered[] = $message;
112 }
113 }
114 $messages_target = $messages_filtered;
115 }
116
117 // Whether we have any sort of message customisation filtering
118 $customiseFilterEnabled = $params['customised'] !== 'all';
119 if ( $customiseFilterEnabled ) {
120 global $wgContLang;
121 $lang = $langObj->getCode();
122
123 $customisedMessages = AllmessagesTablePager::getCustomisedStatuses(
124 array_map( array( $langObj, 'ucfirst'), $messages_target ), $lang, $lang != $wgContLang->getCode() );
125
126 $customised = $params['customised'] === 'modified';
127 }
128
129 // Get all requested messages and print the result
130 $skip = !is_null( $params['from'] );
131 $useto = !is_null( $params['to'] );
132 $result = $this->getResult();
133 foreach ( $messages_target as $message ) {
134 // Skip all messages up to $params['from']
135 if ( $skip && $message === $params['from'] ) {
136 $skip = false;
137 }
138
139 if ( $useto && $message > $params['to'] ) {
140 break;
141 }
142
143 if ( !$skip ) {
144 $a = array( 'name' => $message );
145 $args = array();
146 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
147 $args = $params['args'];
148 }
149
150 if ( $customiseFilterEnabled ) {
151 $messageIsCustomised = isset( $customisedMessages['pages'][ $langObj->ucfirst( $message ) ] );
152 if ( $customised === $messageIsCustomised ) {
153 if ( $customised ) {
154 $a['customised'] = '';
155 }
156 } else {
157 continue;
158 }
159 }
160
161 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
162
163 if ( !$msg->exists() ) {
164 $a['missing'] = '';
165 } else {
166 // Check if the parser is enabled:
167 if ( $params['enableparser'] ) {
168 $msgString = $msg->title( $title )->text();
169 } else {
170 $msgString = $msg->plain();
171 }
172 if ( !$params['nocontent'] ) {
173 ApiResult::setContent( $a, $msgString );
174 }
175 if ( isset( $prop['default'] ) ) {
176 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
177 if ( !$default->exists() ) {
178 $a['defaultmissing'] = '';
179 } elseif ( $default->plain() != $msgString ) {
180 $a['default'] = $default->plain();
181 }
182 }
183 }
184 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
185 if ( !$fit ) {
186 $this->setContinueEnumParameter( 'from', $message );
187 break;
188 }
189 }
190 }
191 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
192 }
193
194 public function getCacheMode( $params ) {
195 if ( is_null( $params['lang'] ) ) {
196 // Language not specified, will be fetched from preferences
197 return 'anon-public-user-private';
198 } elseif ( $params['enableparser'] ) {
199 // User-specific parser options will be used
200 return 'anon-public-user-private';
201 } else {
202 // OK to cache
203 return 'public';
204 }
205 }
206
207 public function getAllowedParams() {
208 return array(
209 'messages' => array(
210 ApiBase::PARAM_DFLT => '*',
211 ApiBase::PARAM_ISMULTI => true,
212 ),
213 'prop' => array(
214 ApiBase::PARAM_ISMULTI => true,
215 ApiBase::PARAM_TYPE => array(
216 'default'
217 )
218 ),
219 'enableparser' => false,
220 'nocontent' => false,
221 'includelocal' => false,
222 'args' => array(
223 ApiBase::PARAM_ISMULTI => true,
224 ApiBase::PARAM_ALLOW_DUPLICATES => true,
225 ),
226 'filter' => array(),
227 'customised' => array(
228 ApiBase::PARAM_DFLT => 'all',
229 ApiBase::PARAM_TYPE => array(
230 'all',
231 'modified',
232 'unmodified'
233 )
234 ),
235 'lang' => null,
236 'from' => null,
237 'to' => null,
238 'title' => null,
239 'prefix' => null,
240 );
241 }
242
243 public function getParamDescription() {
244 return array(
245 'messages' => 'Which messages to output. "*" (default) means all messages',
246 'prop' => 'Which properties to get',
247 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
248 'Will substitute magic words, handle templates etc.' ),
249 'nocontent' => 'If set, do not include the content of the messages in the output.',
250 'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.",
251 "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js",
252 ),
253 'title' => 'Page name to use as context when parsing message (for enableparser option)',
254 'args' => 'Arguments to be substituted into message',
255 'prefix' => 'Return messages with this prefix',
256 'filter' => 'Return only messages with names that contain this string',
257 'customised' => 'Return only messages in this customisation state',
258 'lang' => 'Return messages in this language',
259 'from' => 'Return messages starting at this message',
260 'to' => 'Return messages ending at this message',
261 );
262 }
263
264 public function getDescription() {
265 return 'Return messages from this site';
266 }
267
268 public function getExamples() {
269 return array(
270 'api.php?action=query&meta=allmessages&amprefix=ipb-',
271 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
272 );
273 }
274
275 public function getHelpUrls() {
276 return 'http://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';
277 }
278
279 public function getVersion() {
280 return __CLASS__ . ': $Id$';
281 }
282 }