Swap else if for elseif
[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 ApiResult::setContent( $a, $msgString );
162 if ( isset( $prop['default'] ) ) {
163 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
164 if ( !$default->exists() ) {
165 $a['defaultmissing'] = '';
166 } elseif ( $default->plain() != $msgString ) {
167 $a['default'] = $default->plain();
168 }
169 }
170 }
171 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
172 if ( !$fit ) {
173 $this->setContinueEnumParameter( 'from', $message );
174 break;
175 }
176 }
177 }
178 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
179 }
180
181 public function getCacheMode( $params ) {
182 if ( is_null( $params['lang'] ) ) {
183 // Language not specified, will be fetched from preferences
184 return 'anon-public-user-private';
185 } elseif ( $params['enableparser'] ) {
186 // User-specific parser options will be used
187 return 'anon-public-user-private';
188 } else {
189 // OK to cache
190 return 'public';
191 }
192 }
193
194 public function getAllowedParams() {
195 return array(
196 'messages' => array(
197 ApiBase::PARAM_DFLT => '*',
198 ApiBase::PARAM_ISMULTI => true,
199 ),
200 'prop' => array(
201 ApiBase::PARAM_ISMULTI => true,
202 ApiBase::PARAM_TYPE => array(
203 'default'
204 )
205 ),
206 'enableparser' => false,
207 'args' => array(
208 ApiBase::PARAM_ISMULTI => true,
209 ApiBase::PARAM_ALLOW_DUPLICATES => true,
210 ),
211 'filter' => array(),
212 'customised' => array(
213 ApiBase::PARAM_DFLT => 'all',
214 ApiBase::PARAM_TYPE => array(
215 'all',
216 'modified',
217 'unmodified'
218 )
219 ),
220 'lang' => null,
221 'from' => null,
222 'to' => null,
223 'title' => null,
224 'prefix' => null,
225 );
226 }
227
228 public function getParamDescription() {
229 return array(
230 'messages' => 'Which messages to output. "*" (default) means all messages',
231 'prop' => 'Which properties to get',
232 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
233 'Will substitute magic words, handle templates etc.' ),
234 'title' => 'Page name to use as context when parsing message (for enableparser option)',
235 'args' => 'Arguments to be substituted into message',
236 'prefix' => 'Return messages with this prefix',
237 'filter' => 'Return only messages with names that contain this string',
238 'customised' => 'Return only messages in this customisation state',
239 'lang' => 'Return messages in this language',
240 'from' => 'Return messages starting at this message',
241 'to' => 'Return messages ending at this message',
242 );
243 }
244
245 public function getDescription() {
246 return 'Return messages from this site';
247 }
248
249 protected function getExamples() {
250 return array(
251 'api.php?action=query&meta=allmessages&amprefix=ipb-',
252 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
253 );
254 }
255
256 public function getVersion() {
257 return __CLASS__ . ': $Id$';
258 }
259 }