Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryAllMessages.php
bloba1cce094a54fe607d7c750019ee9bf6cbda66e28
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use LocalisationCache;
26 use MediaWiki\Language\Language;
27 use MediaWiki\Languages\LanguageFactory;
28 use MediaWiki\Languages\LanguageNameUtils;
29 use MediaWiki\Pager\AllMessagesTablePager;
30 use MediaWiki\Title\Title;
31 use MessageCache;
32 use Wikimedia\ParamValidator\ParamValidator;
34 /**
35 * A query action to return messages from site message cache
37 * @ingroup API
39 class ApiQueryAllMessages extends ApiQueryBase {
41 private Language $contentLanguage;
42 private LanguageFactory $languageFactory;
43 private LanguageNameUtils $languageNameUtils;
44 private LocalisationCache $localisationCache;
45 private MessageCache $messageCache;
47 public function __construct(
48 ApiQuery $query,
49 string $moduleName,
50 Language $contentLanguage,
51 LanguageFactory $languageFactory,
52 LanguageNameUtils $languageNameUtils,
53 LocalisationCache $localisationCache,
54 MessageCache $messageCache
55 ) {
56 parent::__construct( $query, $moduleName, 'am' );
57 $this->contentLanguage = $contentLanguage;
58 $this->languageFactory = $languageFactory;
59 $this->languageNameUtils = $languageNameUtils;
60 $this->localisationCache = $localisationCache;
61 $this->messageCache = $messageCache;
64 public function execute() {
65 $params = $this->extractRequestParams();
66 if ( $params['lang'] === null ) {
67 $langObj = $this->getLanguage();
68 } elseif ( !$this->languageNameUtils->isValidCode( $params['lang'] ) ) {
69 $this->dieWithError(
70 [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
72 } else {
73 $langObj = $this->languageFactory->getLanguage( $params['lang'] );
76 if ( $params['enableparser'] ) {
77 if ( $params['title'] !== null ) {
78 $title = Title::newFromText( $params['title'] );
79 if ( !$title || $title->isExternal() ) {
80 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
82 } else {
83 $title = Title::newFromText( 'API' );
87 $prop = array_fill_keys( (array)$params['prop'], true );
89 // Determine which messages should we print
90 if ( in_array( '*', $params['messages'] ) ) {
91 $message_names = $this->localisationCache->getSubitemList( $langObj->getCode(), 'messages' ) ?? [];
92 if ( $params['includelocal'] ) {
93 $message_names = array_unique( array_merge(
94 $message_names,
95 // Pass in the content language code so we get local messages that have a
96 // MediaWiki:msgkey page. We might theoretically miss messages that have no
97 // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
98 // just a stupid case.
99 $this->messageCache->getAllMessageKeys( $this->contentLanguage->getCode() )
100 ) );
102 sort( $message_names );
103 $messages_target = $message_names;
104 } else {
105 $messages_target = $params['messages'];
108 // Filter messages that have the specified prefix
109 // Because we sorted the message array earlier, they will appear in a clump:
110 if ( isset( $params['prefix'] ) ) {
111 $skip = false;
112 $messages_filtered = [];
113 foreach ( $messages_target as $message ) {
114 if ( str_starts_with( $message, $params['prefix'] ) ) {
115 if ( !$skip ) {
116 $skip = true;
118 $messages_filtered[] = $message;
119 } elseif ( $skip ) {
120 break;
123 $messages_target = $messages_filtered;
126 // Filter messages that contain specified string
127 if ( isset( $params['filter'] ) ) {
128 $messages_filtered = [];
129 foreach ( $messages_target as $message ) {
130 if ( str_contains( $message, $params['filter'] ) ) {
131 $messages_filtered[] = $message;
134 $messages_target = $messages_filtered;
137 // Whether we have any sort of message customisation filtering
138 $customiseFilterEnabled = $params['customised'] !== 'all';
139 if ( $customiseFilterEnabled ) {
140 $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
141 array_map(
142 [ $langObj, 'ucfirst' ],
143 $messages_target
145 $langObj->getCode(),
146 !$langObj->equals( $this->contentLanguage ),
147 $this->getDB()
150 $customised = $params['customised'] === 'modified';
153 // Get all requested messages and print the result
154 $skip = $params['from'] !== null;
155 $useto = $params['to'] !== null;
156 $result = $this->getResult();
157 foreach ( $messages_target as $message ) {
158 // Skip all messages up to $params['from']
159 if ( $skip && $message === $params['from'] ) {
160 $skip = false;
163 if ( $useto && $message > $params['to'] ) {
164 break;
167 if ( !$skip ) {
168 $a = [
169 'name' => $message,
170 'normalizedname' => MessageCache::normalizeKey( $message ),
173 $args = [];
174 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
175 $args = $params['args'];
178 if ( $customiseFilterEnabled ) {
179 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
180 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
181 if ( $customised === $messageIsCustomised ) {
182 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable customised is set when used
183 if ( $customised ) {
184 $a['customised'] = true;
186 } else {
187 continue;
191 $msg = $this->msg( $message, $args )->inLanguage( $langObj );
193 if ( !$msg->exists() ) {
194 $a['missing'] = true;
195 } else {
196 // Check if the parser is enabled:
197 if ( $params['enableparser'] ) {
198 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable title is set when used
199 $msgString = $msg->page( $title )->text();
200 } else {
201 $msgString = $msg->plain();
203 if ( !$params['nocontent'] ) {
204 ApiResult::setContentValue( $a, 'content', $msgString );
206 if ( isset( $prop['default'] ) ) {
207 $default = $this->msg( $message )->inLanguage( $langObj )->useDatabase( false );
208 if ( !$default->exists() ) {
209 $a['defaultmissing'] = true;
210 } elseif ( $default->plain() != $msgString ) {
211 $a['default'] = $default->plain();
215 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
216 if ( !$fit ) {
217 $this->setContinueEnumParameter( 'from', $message );
218 break;
222 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
225 public function getCacheMode( $params ) {
226 if ( $params['lang'] === null ) {
227 // Language not specified, will be fetched from preferences
228 return 'anon-public-user-private';
229 } elseif ( $params['enableparser'] ) {
230 // User-specific parser options will be used
231 return 'anon-public-user-private';
232 } else {
233 // OK to cache
234 return 'public';
238 public function getAllowedParams() {
239 return [
240 'messages' => [
241 ParamValidator::PARAM_DEFAULT => '*',
242 ParamValidator::PARAM_ISMULTI => true,
244 'prop' => [
245 ParamValidator::PARAM_ISMULTI => true,
246 ParamValidator::PARAM_TYPE => [
247 'default'
250 'enableparser' => false,
251 'nocontent' => false,
252 'includelocal' => false,
253 'args' => [
254 ParamValidator::PARAM_ISMULTI => true,
255 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
257 'filter' => [],
258 'customised' => [
259 ParamValidator::PARAM_DEFAULT => 'all',
260 ParamValidator::PARAM_TYPE => [
261 'all',
262 'modified',
263 'unmodified'
266 'lang' => null,
267 'from' => null,
268 'to' => null,
269 'title' => null,
270 'prefix' => null,
274 protected function getExamplesMessages() {
275 return [
276 'action=query&meta=allmessages&amprefix=ipb-'
277 => 'apihelp-query+allmessages-example-ipb',
278 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
279 => 'apihelp-query+allmessages-example-de',
283 public function getHelpUrls() {
284 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
288 /** @deprecated class alias since 1.43 */
289 class_alias( ApiQueryAllMessages::class, 'ApiQueryAllMessages' );