MediaWiki REL1_39
ApiQueryAllMessages.php
Go to the documentation of this file.
1<?php
26
33
35 private $contentLanguage;
36
38 private $languageFactory;
39
41 private $languageNameUtils;
42
44 private $localisationCache;
45
47 private $messageCache;
48
58 public function __construct(
59 ApiQuery $query,
60 $moduleName,
61 Language $contentLanguage,
62 LanguageFactory $languageFactory,
63 LanguageNameUtils $languageNameUtils,
64 LocalisationCache $localisationCache,
65 MessageCache $messageCache
66 ) {
67 parent::__construct( $query, $moduleName, 'am' );
68 $this->contentLanguage = $contentLanguage;
69 $this->languageFactory = $languageFactory;
70 $this->languageNameUtils = $languageNameUtils;
71 $this->localisationCache = $localisationCache;
72 $this->messageCache = $messageCache;
73 }
74
75 public function execute() {
76 $params = $this->extractRequestParams();
77 if ( $params['lang'] === null ) {
78 $langObj = $this->getLanguage();
79 } elseif ( !$this->languageNameUtils->isValidCode( $params['lang'] ) ) {
80 $this->dieWithError(
81 [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
82 );
83 } else {
84 $langObj = $this->languageFactory->getLanguage( $params['lang'] );
85 }
86
87 if ( $params['enableparser'] ) {
88 if ( $params['title'] !== null ) {
89 $title = Title::newFromText( $params['title'] );
90 if ( !$title || $title->isExternal() ) {
91 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
92 }
93 } else {
94 $title = Title::newFromText( 'API' );
95 }
96 }
97
98 $prop = array_fill_keys( (array)$params['prop'], true );
99
100 // Determine which messages should we print
101 if ( in_array( '*', $params['messages'] ) ) {
102 $message_names = $this->localisationCache->getSubitemList( $langObj->getCode(), 'messages' ) ?? [];
103 if ( $params['includelocal'] ) {
104 $message_names = array_unique( array_merge(
105 $message_names,
106 // Pass in the content language code so we get local messages that have a
107 // MediaWiki:msgkey page. We might theoretically miss messages that have no
108 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
109 // just a stupid case.
110 $this->messageCache->getAllMessageKeys( $this->contentLanguage->getCode() )
111 ) );
112 }
113 sort( $message_names );
114 $messages_target = $message_names;
115 } else {
116 $messages_target = $params['messages'];
117 }
118
119 // Filter messages that have the specified prefix
120 // Because we sorted the message array earlier, they will appear in a clump:
121 if ( isset( $params['prefix'] ) ) {
122 $skip = false;
123 $messages_filtered = [];
124 foreach ( $messages_target as $message ) {
125 // === 0: must be at beginning of string (position 0)
126 if ( strpos( $message, $params['prefix'] ) === 0 ) {
127 if ( !$skip ) {
128 $skip = true;
129 }
130 $messages_filtered[] = $message;
131 } elseif ( $skip ) {
132 break;
133 }
134 }
135 $messages_target = $messages_filtered;
136 }
137
138 // Filter messages that contain specified string
139 if ( isset( $params['filter'] ) ) {
140 $messages_filtered = [];
141 foreach ( $messages_target as $message ) {
142 // !== is used because filter can be at the beginning of the string
143 if ( strpos( $message, $params['filter'] ) !== false ) {
144 $messages_filtered[] = $message;
145 }
146 }
147 $messages_target = $messages_filtered;
148 }
149
150 // Whether we have any sort of message customisation filtering
151 $customiseFilterEnabled = $params['customised'] !== 'all';
152 if ( $customiseFilterEnabled ) {
154 array_map(
155 [ $langObj, 'ucfirst' ],
156 $messages_target
157 ),
158 $langObj->getCode(),
159 !$langObj->equals( $this->contentLanguage ),
160 $this->getDB()
161 );
162
163 $customised = $params['customised'] === 'modified';
164 }
165
166 // Get all requested messages and print the result
167 $skip = $params['from'] !== null;
168 $useto = $params['to'] !== null;
169 $result = $this->getResult();
170 foreach ( $messages_target as $message ) {
171 // Skip all messages up to $params['from']
172 if ( $skip && $message === $params['from'] ) {
173 $skip = false;
174 }
175
176 if ( $useto && $message > $params['to'] ) {
177 break;
178 }
179
180 if ( !$skip ) {
181 $a = [
182 'name' => $message,
183 'normalizedname' => MessageCache::normalizeKey( $message ),
184 ];
185
186 $args = [];
187 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
188 $args = $params['args'];
189 }
190
191 if ( $customiseFilterEnabled ) {
192 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
193 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
194 if ( $customised === $messageIsCustomised ) {
195 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
196 if ( $customised ) {
197 $a['customised'] = true;
198 }
199 } else {
200 continue;
201 }
202 }
203
204 $msg = $this->msg( $message, $args )->inLanguage( $langObj );
205
206 if ( !$msg->exists() ) {
207 $a['missing'] = true;
208 } else {
209 // Check if the parser is enabled:
210 if ( $params['enableparser'] ) {
211 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable title is set when used
212 $msgString = $msg->page( $title )->text();
213 } else {
214 $msgString = $msg->plain();
215 }
216 if ( !$params['nocontent'] ) {
217 ApiResult::setContentValue( $a, 'content', $msgString );
218 }
219 if ( isset( $prop['default'] ) ) {
220 $default = $this->msg( $message )->inLanguage( $langObj )->useDatabase( false );
221 if ( !$default->exists() ) {
222 $a['defaultmissing'] = true;
223 } elseif ( $default->plain() != $msgString ) {
224 $a['default'] = $default->plain();
225 }
226 }
227 }
228 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
229 if ( !$fit ) {
230 $this->setContinueEnumParameter( 'from', $message );
231 break;
232 }
233 }
234 }
235 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
236 }
237
238 public function getCacheMode( $params ) {
239 if ( $params['lang'] === null ) {
240 // Language not specified, will be fetched from preferences
241 return 'anon-public-user-private';
242 } elseif ( $params['enableparser'] ) {
243 // User-specific parser options will be used
244 return 'anon-public-user-private';
245 } else {
246 // OK to cache
247 return 'public';
248 }
249 }
250
251 public function getAllowedParams() {
252 return [
253 'messages' => [
254 ParamValidator::PARAM_DEFAULT => '*',
255 ParamValidator::PARAM_ISMULTI => true,
256 ],
257 'prop' => [
258 ParamValidator::PARAM_ISMULTI => true,
259 ParamValidator::PARAM_TYPE => [
260 'default'
261 ]
262 ],
263 'enableparser' => false,
264 'nocontent' => false,
265 'includelocal' => false,
266 'args' => [
267 ParamValidator::PARAM_ISMULTI => true,
268 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
269 ],
270 'filter' => [],
271 'customised' => [
272 ParamValidator::PARAM_DEFAULT => 'all',
273 ParamValidator::PARAM_TYPE => [
274 'all',
275 'modified',
276 'unmodified'
277 ]
278 ],
279 'lang' => null,
280 'from' => null,
281 'to' => null,
282 'title' => null,
283 'prefix' => null,
284 ];
285 }
286
287 protected function getExamplesMessages() {
288 return [
289 'action=query&meta=allmessages&amprefix=ipb-'
290 => 'apihelp-query+allmessages-example-ipb',
291 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
292 => 'apihelp-query+allmessages-example-de',
293 ];
294 }
295
296 public function getHelpUrls() {
297 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
298 }
299}
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false, IDatabase $dbr=null)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:743
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
A query action to return messages from site message cache.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiQuery $query, $moduleName, Language $contentLanguage, LanguageFactory $languageFactory, LanguageNameUtils $languageNameUtils, LocalisationCache $localisationCache, MessageCache $messageCache)
getExamplesMessages()
Returns usage examples for this module.
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
This is the main query class.
Definition ApiQuery.php:41
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Base class for language-specific code.
Definition Language.php:53
Caching for the contents of localisation files.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
A service that provides utilities to do with language names and codes.
Cache messages that are defined by MediaWiki-namespace pages or by hooks.
Service for formatting and validating API parameters.
if( $line===false) $args
Definition mcc.php:124