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  $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 ) {
117  $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
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 => '*',
215  ApiBase::PARAM_ISMULTI => true,
216  ],
217  'prop' => [
218  ApiBase::PARAM_ISMULTI => true,
220  'default'
221  ]
222  ],
223  'enableparser' => false,
224  'nocontent' => false,
225  'includelocal' => false,
226  'args' => [
227  ApiBase::PARAM_ISMULTI => true,
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 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:332
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiQueryAllMessages\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryAllMessages.php:247
MessageCache\normalizeKey
static normalizeKey( $key)
Normalize message key input.
Definition: MessageCache.php:138
ApiQueryAllMessages\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryAllMessages.php:256
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:137
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:60
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:538
ApiQueryAllMessages\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryAllMessages.php:32
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1201
ApiBase\PARAM_ALLOW_DUPLICATES
const PARAM_ALLOW_DUPLICATES
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:64
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
ApiQueryAllMessages\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryAllMessages.php:36
ApiResult\setContentValue
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:466
AllMessagesTablePager\getCustomisedStatuses
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
Definition: AllMessagesTablePager.php:140
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiQueryAllMessages\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryAllMessages.php:198
$args
if( $line===false) $args
Definition: mcc.php:124
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:659
$title
$title
Definition: testCompression.php:38
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:637
ApiQueryAllMessages
A query action to return messages from site message cache.
Definition: ApiQueryAllMessages.php:30
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1488
ApiQueryAllMessages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryAllMessages.php:211
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:58
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:418
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:59
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:511