Add support to only return keys in ApiAllMessages
[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 = array_keys( Language::getMessagesFor( 'en' ) );
69 sort( $message_names );
70 $messages_target = $message_names;
71 } else {
72 $messages_target = $params['messages'];
73 }
74
75 // Filter messages that have the specified prefix
76 // Because we sorted the message array earlier, they will appear in a clump:
77 if ( isset( $params['prefix'] ) ) {
78 $skip = false;
79 $messages_filtered = array();
80 foreach ( $messages_target as $message ) {
81 // === 0: must be at beginning of string (position 0)
82 if ( strpos( $message, $params['prefix'] ) === 0 ) {
83 if( !$skip ) {
84 $skip = true;
85 }
86 $messages_filtered[] = $message;
87 } elseif ( $skip ) {
88 break;
89 }
90 }
91 $messages_target = $messages_filtered;
92 }
93
94 // Filter messages that contain specified string
95 if ( isset( $params['filter'] ) ) {
96 $messages_filtered = array();
97 foreach ( $messages_target as $message ) {
98 // !== is used because filter can be at the beginning of the string
99 if ( strpos( $message, $params['filter'] ) !== false ) {
100 $messages_filtered[] = $message;
101 }
102 }
103 $messages_target = $messages_filtered;
104 }
105
106 // Whether we have any sort of message customisation filtering
107 $customiseFilterEnabled = $params['customised'] !== 'all';
108 if ( $customiseFilterEnabled ) {
109 global $wgContLang;
110 $lang = $langObj->getCode();
111
112 $customisedMessages = AllmessagesTablePager::getCustomisedStatuses(
113 array_map( array( $langObj, 'ucfirst'), $messages_target ), $lang, $lang != $wgContLang->getCode() );
114
115 $customised = $params['customised'] === 'modified';
116 }
117
118 // Get all requested messages and print the result
119 $skip = !is_null( $params['from'] );
120 $useto = !is_null( $params['to'] );
121 $result = $this->getResult();
122 foreach ( $messages_target as $message ) {
123 // Skip all messages up to $params['from']
124 if ( $skip && $message === $params['from'] ) {
125 $skip = false;
126 }
127
128 if ( $useto && $message > $params['to'] ) {
129 break;
130 }
131
132 if ( !$skip ) {
133 $a = array( 'name' => $message );
134 $args = array();
135 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
136 $args = $params['args'];
137 }
138
139 if ( $customiseFilterEnabled ) {
140 $messageIsCustomised = isset( $customisedMessages['pages'][ $langObj->ucfirst( $message ) ] );
141 if ( $customised === $messageIsCustomised ) {
142 if ( $customised ) {
143 $a['customised'] = '';
144 }
145 } else {
146 continue;
147 }
148 }
149
150 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
151
152 if ( !$msg->exists() ) {
153 $a['missing'] = '';
154 } else {
155 // Check if the parser is enabled:
156 if ( $params['enableparser'] ) {
157 $msgString = $msg->title( $title )->text();
158 } else {
159 $msgString = $msg->plain();
160 }
161 if ( !$params['nocontent'] ) {
162 ApiResult::setContent( $a, $msgString );
163 }
164 if ( isset( $prop['default'] ) ) {
165 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
166 if ( !$default->exists() ) {
167 $a['defaultmissing'] = '';
168 } elseif ( $default->plain() != $msgString ) {
169 $a['default'] = $default->plain();
170 }
171 }
172 }
173 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
174 if ( !$fit ) {
175 $this->setContinueEnumParameter( 'from', $message );
176 break;
177 }
178 }
179 }
180 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
181 }
182
183 public function getCacheMode( $params ) {
184 if ( is_null( $params['lang'] ) ) {
185 // Language not specified, will be fetched from preferences
186 return 'anon-public-user-private';
187 } elseif ( $params['enableparser'] ) {
188 // User-specific parser options will be used
189 return 'anon-public-user-private';
190 } else {
191 // OK to cache
192 return 'public';
193 }
194 }
195
196 public function getAllowedParams() {
197 return array(
198 'messages' => array(
199 ApiBase::PARAM_DFLT => '*',
200 ApiBase::PARAM_ISMULTI => true,
201 ),
202 'prop' => array(
203 ApiBase::PARAM_ISMULTI => true,
204 ApiBase::PARAM_TYPE => array(
205 'default'
206 )
207 ),
208 'enableparser' => false,
209 'nocontent' => false,
210 'args' => array(
211 ApiBase::PARAM_ISMULTI => true,
212 ApiBase::PARAM_ALLOW_DUPLICATES => true,
213 ),
214 'filter' => array(),
215 'customised' => array(
216 ApiBase::PARAM_DFLT => 'all',
217 ApiBase::PARAM_TYPE => array(
218 'all',
219 'modified',
220 'unmodified'
221 )
222 ),
223 'lang' => null,
224 'from' => null,
225 'to' => null,
226 'title' => null,
227 'prefix' => null,
228 );
229 }
230
231 public function getParamDescription() {
232 return array(
233 'messages' => 'Which messages to output. "*" (default) means all messages',
234 'prop' => 'Which properties to get',
235 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
236 'Will substitute magic words, handle templates etc.' ),
237 'nocontent' => 'Set to not include the content of the messages in the output.',
238 'title' => 'Page name to use as context when parsing message (for enableparser option)',
239 'args' => 'Arguments to be substituted into message',
240 'prefix' => 'Return messages with this prefix',
241 'filter' => 'Return only messages with names that contain this string',
242 'customised' => 'Return only messages in this customisation state',
243 'lang' => 'Return messages in this language',
244 'from' => 'Return messages starting at this message',
245 'to' => 'Return messages ending at this message',
246 );
247 }
248
249 public function getDescription() {
250 return 'Return messages from this site';
251 }
252
253 public function getExamples() {
254 return array(
255 'api.php?action=query&meta=allmessages&amprefix=ipb-',
256 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
257 );
258 }
259
260 public function getHelpUrls() {
261 return 'http://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';
262 }
263
264 public function getVersion() {
265 return __CLASS__ . ': $Id$';
266 }
267 }