MediaWiki master
ApiQueryAllMessages.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
19
26
27 public function __construct(
28 ApiQuery $query,
29 string $moduleName,
30 private readonly Language $contentLanguage,
31 private readonly LanguageFactory $languageFactory,
32 private readonly LanguageNameUtils $languageNameUtils,
33 private readonly LocalisationCache $localisationCache,
34 private readonly MessageCache $messageCache,
35 ) {
36 parent::__construct( $query, $moduleName, 'am' );
37 }
38
39 public function execute() {
40 $params = $this->extractRequestParams();
41 if ( $params['lang'] === null ) {
42 $langObj = $this->getLanguage();
43 } elseif ( !$this->languageNameUtils->isValidCode( $params['lang'] ) ) {
44 $this->dieWithError(
45 [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
46 );
47 } else {
48 $langObj = $this->languageFactory->getLanguage( $params['lang'] );
49 }
50
51 if ( $params['enableparser'] ) {
52 if ( $params['title'] !== null ) {
53 $title = Title::newFromText( $params['title'] );
54 if ( !$title || $title->isExternal() ) {
55 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
56 }
57 } else {
58 $title = Title::newFromText( 'API' );
59 }
60 }
61
62 $prop = array_fill_keys( (array)$params['prop'], true );
63
64 // Determine which messages should we print
65 if ( in_array( '*', $params['messages'] ) ) {
66 $message_names = $this->localisationCache->getSubitemList( $langObj->getCode(), 'messages' ) ?? [];
67 if ( $params['includelocal'] ) {
68 $message_names = array_unique( array_merge(
69 $message_names,
70 // Pass in the content language code so we get local messages that have a
71 // MediaWiki:msgkey page. We might theoretically miss messages that have no
72 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
73 // just a stupid case.
74 $this->messageCache->getAllMessageKeys( $this->contentLanguage->getCode() )
75 ) );
76 }
77 sort( $message_names );
78 $messages_target = $message_names;
79 } else {
80 $messages_target = $params['messages'];
81 }
82
83 // Filter messages that have the specified prefix
84 // Because we sorted the message array earlier, they will appear in a clump:
85 if ( isset( $params['prefix'] ) ) {
86 $skip = false;
87 $messages_filtered = [];
88 foreach ( $messages_target as $message ) {
89 if ( str_starts_with( $message, $params['prefix'] ) ) {
90 if ( !$skip ) {
91 $skip = true;
92 }
93 $messages_filtered[] = $message;
94 } elseif ( $skip ) {
95 break;
96 }
97 }
98 $messages_target = $messages_filtered;
99 }
100
101 // Filter messages that contain specified string
102 if ( isset( $params['filter'] ) ) {
103 $messages_filtered = [];
104 foreach ( $messages_target as $message ) {
105 if ( str_contains( $message, $params['filter'] ) ) {
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 ) {
116 array_map(
117 $langObj->ucfirst( ... ),
118 $messages_target
119 ),
120 $langObj->getCode(),
121 !$langObj->equals( $this->contentLanguage ),
122 $this->getDB()
123 );
124
125 $customised = $params['customised'] === 'modified';
126 }
127
128 // Get all requested messages and print the result
129 $skip = $params['from'] !== null;
130 $useto = $params['to'] !== null;
131 $result = $this->getResult();
132 foreach ( $messages_target as $message ) {
133 // Skip all messages up to $params['from']
134 if ( $skip && $message === $params['from'] ) {
135 $skip = false;
136 }
137
138 if ( $useto && $message > $params['to'] ) {
139 break;
140 }
141
142 if ( !$skip ) {
143 $a = [
144 'name' => $message,
145 'normalizedname' => $this->messageCache->normalizeKey( $message ),
146 ];
147
148 $args = [];
149 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
150 $args = $params['args'];
151 }
152
153 if ( $customiseFilterEnabled ) {
154 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
155 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
156 if ( $customised === $messageIsCustomised ) {
157 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
158 if ( $customised ) {
159 $a['customised'] = true;
160 }
161 } else {
162 continue;
163 }
164 }
165
166 $msg = $this->msg( $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 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable title is set when used
174 $msgString = $msg->page( $title )->text();
175 } else {
176 $msgString = $msg->plain();
177 }
178 if ( !$params['nocontent'] ) {
179 ApiResult::setContentValue( $a, 'content', $msgString );
180 }
181 if ( isset( $prop['default'] ) ) {
182 $default = $this->msg( $message )->inLanguage( $langObj )->useDatabase( false );
183 if ( !$default->exists() ) {
184 $a['defaultmissing'] = true;
185 } elseif ( $default->plain() != $msgString ) {
186 $a['default'] = $default->plain();
187 }
188 }
189 }
190 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'from', $message );
193 break;
194 }
195 }
196 }
197 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
198 }
199
201 public function getCacheMode( $params ) {
202 if ( $params['lang'] === null ) {
203 // Language not specified, will be fetched from preferences
204 return 'anon-public-user-private';
205 } elseif ( $params['enableparser'] ) {
206 // User-specific parser options will be used
207 return 'anon-public-user-private';
208 } else {
209 // OK to cache
210 return 'public';
211 }
212 }
213
215 public function getAllowedParams() {
216 return [
217 'messages' => [
218 ParamValidator::PARAM_DEFAULT => '*',
219 ParamValidator::PARAM_ISMULTI => true,
220 ],
221 'prop' => [
222 ParamValidator::PARAM_ISMULTI => true,
223 ParamValidator::PARAM_TYPE => [
224 'default'
225 ]
226 ],
227 'enableparser' => false,
228 'nocontent' => false,
229 'includelocal' => false,
230 'args' => [
231 ParamValidator::PARAM_ISMULTI => true,
232 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
233 ],
234 'filter' => [],
235 'customised' => [
236 ParamValidator::PARAM_DEFAULT => 'all',
237 ParamValidator::PARAM_TYPE => [
238 'all',
239 'modified',
240 'unmodified'
241 ]
242 ],
243 'lang' => null,
244 'from' => null,
245 'to' => null,
246 'title' => null,
247 'prefix' => null,
248 ];
249 }
250
252 protected function getExamplesMessages() {
253 return [
254 'action=query&meta=allmessages&amprefix=ipb-'
255 => 'apihelp-query+allmessages-example-ipb',
256 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
257 => 'apihelp-query+allmessages-example-de',
258 ];
259 }
260
262 public function getHelpUrls() {
263 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
264 }
265}
266
268class_alias( ApiQueryAllMessages::class, 'ApiQueryAllMessages' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
A query action to return messages from site message cache.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
__construct(ApiQuery $query, string $moduleName, private readonly Language $contentLanguage, private readonly LanguageFactory $languageFactory, private readonly LanguageNameUtils $languageNameUtils, private readonly LocalisationCache $localisationCache, private readonly MessageCache $messageCache,)
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getCacheMode( $params)
Get the cache mode for the data generated by this module.Override this in the module subclass....
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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:36
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
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.
Base class for language-specific code.
Definition Language.php:65
Caching for the contents of localisation files.
Cache messages that are defined by MediaWiki-namespace pages or by hooks.
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false, ?IReadableDatabase $dbr=null)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.