Fix Generic.Files.LineLength phpcs failure in 11 files under includes/
[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( ApiQuery $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 } elseif ( !Language::isValidCode( $params['lang'] ) ) {
44 $this->dieUsage( 'Invalid language code for parameter lang', 'invalidlang' );
45 } else {
46 $langObj = Language::factory( $params['lang'] );
47 }
48
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'] ) );
54 }
55 } else {
56 $title = Title::newFromText( 'API' );
57 }
58 }
59
60 $prop = array_flip( (array)$params['prop'] );
61
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 $message_names = array_unique( array_merge(
67 $message_names,
68 // Pass in the content language code so we get local messages that have a
69 // MediaWiki:msgkey page. We might theoretically miss messages that have no
70 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
71 // just a stupid case.
72 MessageCache::singleton()->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
73 ) );
74 }
75 sort( $message_names );
76 $messages_target = $message_names;
77 } else {
78 $messages_target = $params['messages'];
79 }
80
81 // Filter messages that have the specified prefix
82 // Because we sorted the message array earlier, they will appear in a clump:
83 if ( isset( $params['prefix'] ) ) {
84 $skip = false;
85 $messages_filtered = array();
86 foreach ( $messages_target as $message ) {
87 // === 0: must be at beginning of string (position 0)
88 if ( strpos( $message, $params['prefix'] ) === 0 ) {
89 if ( !$skip ) {
90 $skip = true;
91 }
92 $messages_filtered[] = $message;
93 } elseif ( $skip ) {
94 break;
95 }
96 }
97 $messages_target = $messages_filtered;
98 }
99
100 // Filter messages that contain specified string
101 if ( isset( $params['filter'] ) ) {
102 $messages_filtered = array();
103 foreach ( $messages_target as $message ) {
104 // !== is used because filter can be at the beginning of the string
105 if ( strpos( $message, $params['filter'] ) !== false ) {
106 $messages_filtered[] = $message;
107 }
108 }
109 $messages_target = $messages_filtered;
110 }
111
112 // Whether we have any sort of message customisation filtering
113 $customiseFilterEnabled = $params['customised'] !== 'all';
114 if ( $customiseFilterEnabled ) {
115 global $wgContLang;
116 $lang = $langObj->getCode();
117
118 $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
119 array_map(
120 array( $langObj, 'ucfirst' ),
121 $messages_target
122 ),
123 $lang,
124 $lang != $wgContLang->getCode()
125 );
126
127 $customised = $params['customised'] === 'modified';
128 }
129
130 // Get all requested messages and print the result
131 $skip = !is_null( $params['from'] );
132 $useto = !is_null( $params['to'] );
133 $result = $this->getResult();
134 foreach ( $messages_target as $message ) {
135 // Skip all messages up to $params['from']
136 if ( $skip && $message === $params['from'] ) {
137 $skip = false;
138 }
139
140 if ( $useto && $message > $params['to'] ) {
141 break;
142 }
143
144 if ( !$skip ) {
145 $a = array(
146 'name' => $message,
147 'normalizedname' => MessageCache::normalizeKey( $message ),
148 );
149
150 $args = array();
151 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
152 $args = $params['args'];
153 }
154
155 if ( $customiseFilterEnabled ) {
156 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
157 if ( $customised === $messageIsCustomised ) {
158 if ( $customised ) {
159 $a['customised'] = true;
160 }
161 } else {
162 continue;
163 }
164 }
165
166 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
167
168 if ( !$msg->exists() ) {
169 $a['missing'] = true;
170 } else {
171 // Check if the parser is enabled:
172 if ( $params['enableparser'] ) {
173 $msgString = $msg->title( $title )->text();
174 } else {
175 $msgString = $msg->plain();
176 }
177 if ( !$params['nocontent'] ) {
178 ApiResult::setContentValue( $a, 'content', $msgString );
179 }
180 if ( isset( $prop['default'] ) ) {
181 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
182 if ( !$default->exists() ) {
183 $a['defaultmissing'] = true;
184 } elseif ( $default->plain() != $msgString ) {
185 $a['default'] = $default->plain();
186 }
187 }
188 }
189 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
190 if ( !$fit ) {
191 $this->setContinueEnumParameter( 'from', $message );
192 break;
193 }
194 }
195 }
196 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'message' );
197 }
198
199 public function getCacheMode( $params ) {
200 if ( is_null( $params['lang'] ) ) {
201 // Language not specified, will be fetched from preferences
202 return 'anon-public-user-private';
203 } elseif ( $params['enableparser'] ) {
204 // User-specific parser options will be used
205 return 'anon-public-user-private';
206 } else {
207 // OK to cache
208 return 'public';
209 }
210 }
211
212 public function getAllowedParams() {
213 return array(
214 'messages' => array(
215 ApiBase::PARAM_DFLT => '*',
216 ApiBase::PARAM_ISMULTI => true,
217 ),
218 'prop' => array(
219 ApiBase::PARAM_ISMULTI => true,
220 ApiBase::PARAM_TYPE => array(
221 'default'
222 )
223 ),
224 'enableparser' => false,
225 'nocontent' => false,
226 'includelocal' => false,
227 'args' => array(
228 ApiBase::PARAM_ISMULTI => true,
229 ApiBase::PARAM_ALLOW_DUPLICATES => true,
230 ),
231 'filter' => array(),
232 'customised' => array(
233 ApiBase::PARAM_DFLT => 'all',
234 ApiBase::PARAM_TYPE => array(
235 'all',
236 'modified',
237 'unmodified'
238 )
239 ),
240 'lang' => null,
241 'from' => null,
242 'to' => null,
243 'title' => null,
244 'prefix' => null,
245 );
246 }
247
248 protected function getExamplesMessages() {
249 return array(
250 'action=query&meta=allmessages&amprefix=ipb-'
251 => 'apihelp-query+allmessages-example-ipb',
252 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
253 => 'apihelp-query+allmessages-example-de',
254 );
255 }
256
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Allmessages';
259 }
260 }