Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiQueryAllMessages.php
blob244effc5238a3dacb235fb4d08a7ba16704e0998
1 <?php
2 /**
5 * Created on Dec 1, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * A query action to return messages from site message cache
30 * @ingroup API
32 class ApiQueryAllMessages extends ApiQueryBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'am' );
38 public function execute() {
39 $params = $this->extractRequestParams();
41 if ( is_null( $params['lang'] ) ) {
42 $langObj = $this->getLanguage();
43 } elseif ( !Language::isValidCode( $params['lang'] ) ) {
44 $this->dieWithError(
45 [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
47 } else {
48 $langObj = Language::factory( $params['lang'] );
51 if ( $params['enableparser'] ) {
52 if ( !is_null( $params['title'] ) ) {
53 $title = Title::newFromText( $params['title'] );
54 if ( !$title || $title->isExternal() ) {
55 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
57 } else {
58 $title = Title::newFromText( 'API' );
62 $prop = array_flip( (array)$params['prop'] );
64 // Determine which messages should we print
65 if ( in_array( '*', $params['messages'] ) ) {
66 $message_names = Language::getMessageKeysFor( $langObj->getCode() );
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 MessageCache::singleton()->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
75 ) );
77 sort( $message_names );
78 $messages_target = $message_names;
79 } else {
80 $messages_target = $params['messages'];
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;
94 $messages_filtered[] = $message;
95 } elseif ( $skip ) {
96 break;
99 $messages_target = $messages_filtered;
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;
111 $messages_target = $messages_filtered;
114 // Whether we have any sort of message customisation filtering
115 $customiseFilterEnabled = $params['customised'] !== 'all';
116 if ( $customiseFilterEnabled ) {
117 global $wgContLang;
119 $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
120 array_map(
121 [ $langObj, 'ucfirst' ],
122 $messages_target
124 $langObj->getCode(),
125 !$langObj->equals( $wgContLang )
128 $customised = $params['customised'] === 'modified';
131 // Get all requested messages and print the result
132 $skip = !is_null( $params['from'] );
133 $useto = !is_null( $params['to'] );
134 $result = $this->getResult();
135 foreach ( $messages_target as $message ) {
136 // Skip all messages up to $params['from']
137 if ( $skip && $message === $params['from'] ) {
138 $skip = false;
141 if ( $useto && $message > $params['to'] ) {
142 break;
145 if ( !$skip ) {
146 $a = [
147 'name' => $message,
148 'normalizedname' => MessageCache::normalizeKey( $message ),
151 $args = [];
152 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
153 $args = $params['args'];
156 if ( $customiseFilterEnabled ) {
157 $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
158 if ( $customised === $messageIsCustomised ) {
159 if ( $customised ) {
160 $a['customised'] = true;
162 } else {
163 continue;
167 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
169 if ( !$msg->exists() ) {
170 $a['missing'] = true;
171 } else {
172 // Check if the parser is enabled:
173 if ( $params['enableparser'] ) {
174 $msgString = $msg->title( $title )->text();
175 } else {
176 $msgString = $msg->plain();
178 if ( !$params['nocontent'] ) {
179 ApiResult::setContentValue( $a, 'content', $msgString );
181 if ( isset( $prop['default'] ) ) {
182 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
183 if ( !$default->exists() ) {
184 $a['defaultmissing'] = true;
185 } elseif ( $default->plain() != $msgString ) {
186 $a['default'] = $default->plain();
190 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'from', $message );
193 break;
197 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
200 public function getCacheMode( $params ) {
201 if ( is_null( $params['lang'] ) ) {
202 // Language not specified, will be fetched from preferences
203 return 'anon-public-user-private';
204 } elseif ( $params['enableparser'] ) {
205 // User-specific parser options will be used
206 return 'anon-public-user-private';
207 } else {
208 // OK to cache
209 return 'public';
213 public function getAllowedParams() {
214 return [
215 'messages' => [
216 ApiBase::PARAM_DFLT => '*',
217 ApiBase::PARAM_ISMULTI => true,
219 'prop' => [
220 ApiBase::PARAM_ISMULTI => true,
221 ApiBase::PARAM_TYPE => [
222 'default'
225 'enableparser' => false,
226 'nocontent' => false,
227 'includelocal' => false,
228 'args' => [
229 ApiBase::PARAM_ISMULTI => true,
230 ApiBase::PARAM_ALLOW_DUPLICATES => true,
232 'filter' => [],
233 'customised' => [
234 ApiBase::PARAM_DFLT => 'all',
235 ApiBase::PARAM_TYPE => [
236 'all',
237 'modified',
238 'unmodified'
241 'lang' => null,
242 'from' => null,
243 'to' => null,
244 'title' => null,
245 'prefix' => null,
249 protected function getExamplesMessages() {
250 return [
251 'action=query&meta=allmessages&amprefix=ipb-'
252 => 'apihelp-query+allmessages-example-ipb',
253 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
254 => 'apihelp-query+allmessages-example-de',
258 public function getHelpUrls() {
259 return 'https://www.mediawiki.org/wiki/API:Allmessages';