MediaWiki  master
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 
39  if ( is_null( $params['lang'] ) ) {
40  $langObj = $this->getLanguage();
41  } elseif ( !Language::isValidCode( $params['lang'] ) ) {
42  $this->dieWithError(
43  [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
44  );
45  } else {
46  $langObj = MediaWikiServices::getInstance()->getLanguageFactory()
47  ->getLanguage( $params['lang'] );
48  }
49 
50  if ( $params['enableparser'] ) {
51  if ( !is_null( $params['title'] ) ) {
52  $title = Title::newFromText( $params['title'] );
53  if ( !$title || $title->isExternal() ) {
54  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
55  }
56  } else {
57  $title = Title::newFromText( 'API' );
58  }
59  }
60 
61  $prop = array_flip( (array)$params['prop'] );
62 
63  // Determine which messages should we print
64  if ( in_array( '*', $params['messages'] ) ) {
65  $message_names = Language::getMessageKeysFor( $langObj->getCode() );
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  MessageCache::singleton()->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
74  ) );
75  }
76  sort( $message_names );
77  $messages_target = $message_names;
78  } else {
79  $messages_target = $params['messages'];
80  }
81 
82  // Filter messages that have the specified prefix
83  // Because we sorted the message array earlier, they will appear in a clump:
84  if ( isset( $params['prefix'] ) ) {
85  $skip = false;
86  $messages_filtered = [];
87  foreach ( $messages_target as $message ) {
88  // === 0: must be at beginning of string (position 0)
89  if ( strpos( $message, $params['prefix'] ) === 0 ) {
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  // !== is used because filter can be at the beginning of the string
106  if ( strpos( $message, $params['filter'] ) !== false ) {
107  $messages_filtered[] = $message;
108  }
109  }
110  $messages_target = $messages_filtered;
111  }
112 
113  // Whether we have any sort of message customisation filtering
114  $customiseFilterEnabled = $params['customised'] !== 'all';
115  if ( $customiseFilterEnabled ) {
116  $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
117  array_map(
118  [ $langObj, 'ucfirst' ],
119  $messages_target
120  ),
121  $langObj->getCode(),
122  !$langObj->equals( MediaWikiServices::getInstance()->getContentLanguage() )
123  );
124 
125  $customised = $params['customised'] === 'modified';
126  }
127 
128  // Get all requested messages and print the result
129  $skip = !is_null( $params['from'] );
130  $useto = !is_null( $params['to'] );
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' => 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  if ( $customised === $messageIsCustomised ) {
156  if ( $customised ) {
157  $a['customised'] = true;
158  }
159  } else {
160  continue;
161  }
162  }
163 
164  $msg = wfMessage( $message, $args )->inLanguage( $langObj );
165 
166  if ( !$msg->exists() ) {
167  $a['missing'] = true;
168  } else {
169  // Check if the parser is enabled:
170  if ( $params['enableparser'] ) {
171  $msgString = $msg->title( $title )->text();
172  } else {
173  $msgString = $msg->plain();
174  }
175  if ( !$params['nocontent'] ) {
176  ApiResult::setContentValue( $a, 'content', $msgString );
177  }
178  if ( isset( $prop['default'] ) ) {
179  $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
180  if ( !$default->exists() ) {
181  $a['defaultmissing'] = true;
182  } elseif ( $default->plain() != $msgString ) {
183  $a['default'] = $default->plain();
184  }
185  }
186  }
187  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
188  if ( !$fit ) {
189  $this->setContinueEnumParameter( 'from', $message );
190  break;
191  }
192  }
193  }
194  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
195  }
196 
197  public function getCacheMode( $params ) {
198  if ( is_null( $params['lang'] ) ) {
199  // Language not specified, will be fetched from preferences
200  return 'anon-public-user-private';
201  } elseif ( $params['enableparser'] ) {
202  // User-specific parser options will be used
203  return 'anon-public-user-private';
204  } else {
205  // OK to cache
206  return 'public';
207  }
208  }
209 
210  public function getAllowedParams() {
211  return [
212  'messages' => [
213  ApiBase::PARAM_DFLT => '*',
214  ApiBase::PARAM_ISMULTI => true,
215  ],
216  'prop' => [
217  ApiBase::PARAM_ISMULTI => true,
219  'default'
220  ]
221  ],
222  'enableparser' => false,
223  'nocontent' => false,
224  'includelocal' => false,
225  'args' => [
226  ApiBase::PARAM_ISMULTI => true,
228  ],
229  'filter' => [],
230  'customised' => [
231  ApiBase::PARAM_DFLT => 'all',
233  'all',
234  'modified',
235  'unmodified'
236  ]
237  ],
238  'lang' => null,
239  'from' => null,
240  'to' => null,
241  'title' => null,
242  'prefix' => null,
243  ];
244  }
245 
246  protected function getExamplesMessages() {
247  return [
248  'action=query&meta=allmessages&amprefix=ipb-'
249  => 'apihelp-query+allmessages-example-ipb',
250  'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
251  => 'apihelp-query+allmessages-example-de',
252  ];
253  }
254 
255  public function getHelpUrls() {
256  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
257  }
258 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
getResult()
Get the result object.
Definition: ApiBase.php:640
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
This is a base class for all Query modules.
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2006
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
__construct(ApiQuery $query, $moduleName)
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Definition: ApiResult.php:478
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
static normalizeKey( $key)
Normalize message key input.
if( $line===false) $args
Definition: mcc.php:124
static getMessageKeysFor( $code)
Get all message keys for a given language.
Definition: Language.php:4356
A query action to return messages from site message cache.
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
This is the main query class.
Definition: ApiQuery.php:37
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:739
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static isValidCode( $code)
Returns true if a language code string is of a valid form, whether or not it exists.
Definition: Language.php:326
const PARAM_ALLOW_DUPLICATES
(boolean) Allow the same value to be set more than once when PARAM_ISMULTI is true?
Definition: ApiBase.php:109
static singleton()
Get the singleton instance of this class.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.