MediaWiki REL1_35
ApiQueryAllMessages.php
Go to the documentation of this file.
1<?php
24
31
32 public function __construct( ApiQuery $query, $moduleName ) {
33 parent::__construct( $query, $moduleName, 'am' );
34 }
35
36 public function execute() {
37 $params = $this->extractRequestParams();
38 $services = MediaWikiServices::getInstance();
39 if ( $params['lang'] === null ) {
40 $langObj = $this->getLanguage();
41 } elseif ( !$services->getLanguageNameUtils()->isValidCode( $params['lang'] ) ) {
42 $this->dieWithError(
43 [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
44 );
45 } else {
46 $langObj = $services->getLanguageFactory()->getLanguage( $params['lang'] );
47 }
48
49 if ( $params['enableparser'] ) {
50 if ( $params['title'] !== null ) {
51 $title = Title::newFromText( $params['title'] );
52 if ( !$title || $title->isExternal() ) {
53 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $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 = $services->getLocalisationCache()
65 ->getSubitemList( $langObj->getCode(), 'messages' );
66 if ( $params['includelocal'] ) {
67 $message_names = array_unique( array_merge(
68 $message_names,
69 // Pass in the content language code so we get local messages that have a
70 // MediaWiki:msgkey page. We might theoretically miss messages that have no
71 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
72 // just a stupid case.
73 $services->getMessageCache()
74 ->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
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 // === 0: must be at beginning of string (position 0)
90 if ( strpos( $message, $params['prefix'] ) === 0 ) {
91 if ( !$skip ) {
92 $skip = true;
93 }
94 $messages_filtered[] = $message;
95 } elseif ( $skip ) {
96 break;
97 }
98 }
99 $messages_target = $messages_filtered;
100 }
101
102 // Filter messages that contain specified string
103 if ( isset( $params['filter'] ) ) {
104 $messages_filtered = [];
105 foreach ( $messages_target as $message ) {
106 // !== is used because filter can be at the beginning of the string
107 if ( strpos( $message, $params['filter'] ) !== false ) {
108 $messages_filtered[] = $message;
109 }
110 }
111 $messages_target = $messages_filtered;
112 }
113
114 // Whether we have any sort of message customisation filtering
115 $customiseFilterEnabled = $params['customised'] !== 'all';
116 if ( $customiseFilterEnabled ) {
118 array_map(
119 [ $langObj, 'ucfirst' ],
120 $messages_target
121 ),
122 $langObj->getCode(),
123 !$langObj->equals( MediaWikiServices::getInstance()->getContentLanguage() )
124 );
125
126 $customised = $params['customised'] === 'modified';
127 }
128
129 // Get all requested messages and print the result
130 $skip = $params['from'] !== null;
131 $useto = $params['to'] !== null;
132 $result = $this->getResult();
133 foreach ( $messages_target as $message ) {
134 // Skip all messages up to $params['from']
135 if ( $skip && $message === $params['from'] ) {
136 $skip = false;
137 }
138
139 if ( $useto && $message > $params['to'] ) {
140 break;
141 }
142
143 if ( !$skip ) {
144 $a = [
145 'name' => $message,
146 'normalizedname' => MessageCache::normalizeKey( $message ),
147 ];
148
149 $args = [];
150 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
151 $args = $params['args'];
152 }
153
154 if ( $customiseFilterEnabled ) {
155 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
156 if ( $customised === $messageIsCustomised ) {
157 if ( $customised ) {
158 $a['customised'] = true;
159 }
160 } else {
161 continue;
162 }
163 }
164
165 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
166
167 if ( !$msg->exists() ) {
168 $a['missing'] = true;
169 } else {
170 // Check if the parser is enabled:
171 if ( $params['enableparser'] ) {
172 $msgString = $msg->title( $title )->text();
173 } else {
174 $msgString = $msg->plain();
175 }
176 if ( !$params['nocontent'] ) {
177 ApiResult::setContentValue( $a, 'content', $msgString );
178 }
179 if ( isset( $prop['default'] ) ) {
180 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
181 if ( !$default->exists() ) {
182 $a['defaultmissing'] = true;
183 } elseif ( $default->plain() != $msgString ) {
184 $a['default'] = $default->plain();
185 }
186 }
187 }
188 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
189 if ( !$fit ) {
190 $this->setContinueEnumParameter( 'from', $message );
191 break;
192 }
193 }
194 }
195 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
196 }
197
198 public function getCacheMode( $params ) {
199 if ( $params['lang'] === null ) {
200 // Language not specified, will be fetched from preferences
201 return 'anon-public-user-private';
202 } elseif ( $params['enableparser'] ) {
203 // User-specific parser options will be used
204 return 'anon-public-user-private';
205 } else {
206 // OK to cache
207 return 'public';
208 }
209 }
210
211 public function getAllowedParams() {
212 return [
213 'messages' => [
214 ApiBase::PARAM_DFLT => '*',
216 ],
217 'prop' => [
220 'default'
221 ]
222 ],
223 'enableparser' => false,
224 'nocontent' => false,
225 'includelocal' => false,
226 'args' => [
229 ],
230 'filter' => [],
231 'customised' => [
232 ApiBase::PARAM_DFLT => 'all',
234 'all',
235 'modified',
236 'unmodified'
237 ]
238 ],
239 'lang' => null,
240 'from' => null,
241 'to' => null,
242 'title' => null,
243 'prefix' => null,
244 ];
245 }
246
247 protected function getExamplesMessages() {
248 return [
249 'action=query&meta=allmessages&amprefix=ipb-'
250 => 'apihelp-query+allmessages-example-ipb',
251 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
252 => 'apihelp-query+allmessages-example-de',
253 ];
254 }
255
256 public function getHelpUrls() {
257 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
258 }
259}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false)
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:1437
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:750
const PARAM_TYPE
Definition ApiBase.php:78
const PARAM_DFLT
Definition ApiBase.php:70
const PARAM_ALLOW_DUPLICATES
Definition ApiBase.php:94
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
const PARAM_ISMULTI
Definition ApiBase.php:74
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)
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:37
MediaWikiServices is the service locator for the application scope of MediaWiki.
if( $line===false) $args
Definition mcc.php:124