Merge "Make sure Parsoid doesn't get snobbish and treat non-html5 tags badly."
[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 /**
28 * A query action to return messages from site message cache
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllMessages extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'am' );
36 }
37
38 public function execute() {
39 $params = $this->extractRequestParams();
40
41 if ( is_null( $params['lang'] ) ) {
42 $langObj = $this->getLanguage();
43 } else {
44 $langObj = Language::factory( $params['lang'] );
45 }
46
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'] ) );
52 }
53 } else {
54 $title = Title::newFromText( 'API' );
55 }
56 }
57
58 $prop = array_flip( (array)$params['prop'] );
59
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(
66 $message_names,
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 )
72 ) );
73 }
74 sort( $message_names );
75 $messages_target = $message_names;
76 } else {
77 $messages_target = $params['messages'];
78 }
79
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'] ) ) {
83 $skip = false;
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 ) {
88 if( !$skip ) {
89 $skip = true;
90 }
91 $messages_filtered[] = $message;
92 } elseif ( $skip ) {
93 break;
94 }
95 }
96 $messages_target = $messages_filtered;
97 }
98
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;
106 }
107 }
108 $messages_target = $messages_filtered;
109 }
110
111 // Whether we have any sort of message customisation filtering
112 $customiseFilterEnabled = $params['customised'] !== 'all';
113 if ( $customiseFilterEnabled ) {
114 global $wgContLang;
115 $lang = $langObj->getCode();
116
117 $customisedMessages = AllmessagesTablePager::getCustomisedStatuses(
118 array_map( array( $langObj, 'ucfirst' ), $messages_target ), $lang, $lang != $wgContLang->getCode() );
119
120 $customised = $params['customised'] === 'modified';
121 }
122
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'] ) {
130 $skip = false;
131 }
132
133 if ( $useto && $message > $params['to'] ) {
134 break;
135 }
136
137 if ( !$skip ) {
138 $a = array( 'name' => $message );
139 $args = array();
140 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
141 $args = $params['args'];
142 }
143
144 if ( $customiseFilterEnabled ) {
145 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
146 if ( $customised === $messageIsCustomised ) {
147 if ( $customised ) {
148 $a['customised'] = '';
149 }
150 } else {
151 continue;
152 }
153 }
154
155 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
156
157 if ( !$msg->exists() ) {
158 $a['missing'] = '';
159 } else {
160 // Check if the parser is enabled:
161 if ( $params['enableparser'] ) {
162 $msgString = $msg->title( $title )->text();
163 } else {
164 $msgString = $msg->plain();
165 }
166 if ( !$params['nocontent'] ) {
167 ApiResult::setContent( $a, $msgString );
168 }
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();
175 }
176 }
177 }
178 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
179 if ( !$fit ) {
180 $this->setContinueEnumParameter( 'from', $message );
181 break;
182 }
183 }
184 }
185 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
186 }
187
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';
195 } else {
196 // OK to cache
197 return 'public';
198 }
199 }
200
201 public function getAllowedParams() {
202 return array(
203 'messages' => array(
204 ApiBase::PARAM_DFLT => '*',
205 ApiBase::PARAM_ISMULTI => true,
206 ),
207 'prop' => array(
208 ApiBase::PARAM_ISMULTI => true,
209 ApiBase::PARAM_TYPE => array(
210 'default'
211 )
212 ),
213 'enableparser' => false,
214 'nocontent' => false,
215 'includelocal' => false,
216 'args' => array(
217 ApiBase::PARAM_ISMULTI => true,
218 ApiBase::PARAM_ALLOW_DUPLICATES => true,
219 ),
220 'filter' => array(),
221 'customised' => array(
222 ApiBase::PARAM_DFLT => 'all',
223 ApiBase::PARAM_TYPE => array(
224 'all',
225 'modified',
226 'unmodified'
227 )
228 ),
229 'lang' => null,
230 'from' => null,
231 'to' => null,
232 'title' => null,
233 'prefix' => null,
234 );
235 }
236
237 public function getParamDescription() {
238 return array(
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",
246 ),
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',
255 );
256 }
257
258 public function getResultProperties() {
259 return array(
260 '' => array(
261 'name' => 'string',
262 'customised' => 'boolean',
263 'missing' => 'boolean',
264 '*' => array(
265 ApiBase::PROP_TYPE => 'string',
266 ApiBase::PROP_NULLABLE => true
267 )
268 ),
269 'default' => array(
270 'defaultmissing' => 'boolean',
271 'default' => array(
272 ApiBase::PROP_TYPE => 'string',
273 ApiBase::PROP_NULLABLE => true
274 )
275 )
276 );
277 }
278
279 public function getDescription() {
280 return 'Return messages from this site';
281 }
282
283 public function getExamples() {
284 return array(
285 'api.php?action=query&meta=allmessages&amprefix=ipb-',
286 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
287 );
288 }
289
290 public function getHelpUrls() {
291 return 'https://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';
292 }
293 }