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  $this->getDB()
125  );
126 
127  $customised = $params['customised'] === 'modified';
128  }
129 
130  // Get all requested messages and print the result
131  $skip = $params['from'] !== null;
132  $useto = $params['to'] !== null;
133  $result = $this->getResult();
134  foreach ( $messages_target as $message ) {
135  // Skip all messages up to $params['from']
136  if ( $skip && $message === $params['from'] ) {
137  $skip = false;
138  }
139 
140  if ( $useto && $message > $params['to'] ) {
141  break;
142  }
143 
144  if ( !$skip ) {
145  $a = [
146  'name' => $message,
147  'normalizedname' => MessageCache::normalizeKey( $message ),
148  ];
149 
150  $args = [];
151  if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
152  $args = $params['args'];
153  }
154 
155  if ( $customiseFilterEnabled ) {
156  $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
157  if ( $customised === $messageIsCustomised ) {
158  if ( $customised ) {
159  $a['customised'] = true;
160  }
161  } else {
162  continue;
163  }
164  }
165 
166  $msg = wfMessage( $message, $args )->inLanguage( $langObj );
167 
168  if ( !$msg->exists() ) {
169  $a['missing'] = true;
170  } else {
171  // Check if the parser is enabled:
172  if ( $params['enableparser'] ) {
173  $msgString = $msg->title( $title )->text();
174  } else {
175  $msgString = $msg->plain();
176  }
177  if ( !$params['nocontent'] ) {
178  ApiResult::setContentValue( $a, 'content', $msgString );
179  }
180  if ( isset( $prop['default'] ) ) {
181  $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
182  if ( !$default->exists() ) {
183  $a['defaultmissing'] = true;
184  } elseif ( $default->plain() != $msgString ) {
185  $a['default'] = $default->plain();
186  }
187  }
188  }
189  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
190  if ( !$fit ) {
191  $this->setContinueEnumParameter( 'from', $message );
192  break;
193  }
194  }
195  }
196  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
197  }
198 
199  public function getCacheMode( $params ) {
200  if ( $params['lang'] === null ) {
201  // Language not specified, will be fetched from preferences
202  return 'anon-public-user-private';
203  } elseif ( $params['enableparser'] ) {
204  // User-specific parser options will be used
205  return 'anon-public-user-private';
206  } else {
207  // OK to cache
208  return 'public';
209  }
210  }
211 
212  public function getAllowedParams() {
213  return [
214  'messages' => [
215  ApiBase::PARAM_DFLT => '*',
216  ApiBase::PARAM_ISMULTI => true,
217  ],
218  'prop' => [
219  ApiBase::PARAM_ISMULTI => true,
221  'default'
222  ]
223  ],
224  'enableparser' => false,
225  'nocontent' => false,
226  'includelocal' => false,
227  'args' => [
228  ApiBase::PARAM_ISMULTI => true,
230  ],
231  'filter' => [],
232  'customised' => [
233  ApiBase::PARAM_DFLT => 'all',
235  'all',
236  'modified',
237  'unmodified'
238  ]
239  ],
240  'lang' => null,
241  'from' => null,
242  'to' => null,
243  'title' => null,
244  'prefix' => null,
245  ];
246  }
247 
248  protected function getExamplesMessages() {
249  return [
250  'action=query&meta=allmessages&amprefix=ipb-'
251  => 'apihelp-query+allmessages-example-ipb',
252  'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
253  => 'apihelp-query+allmessages-example-de',
254  ];
255  }
256 
257  public function getHelpUrls() {
258  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
259  }
260 }
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:363
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiQueryAllMessages\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryAllMessages.php:248
MessageCache\normalizeKey
static normalizeKey( $key)
Normalize message key input.
Definition: MessageCache.php:143
ApiQueryAllMessages\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryAllMessages.php:257
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1384
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:561
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:1231
ApiBase\PARAM_ALLOW_DUPLICATES
const PARAM_ALLOW_DUPLICATES
Definition: ApiBase.php:76
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:151
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:470
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:37
ApiQueryAllMessages\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryAllMessages.php:199
$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:720
$title
$title
Definition: testCompression.php:38
AllMessagesTablePager\getCustomisedStatuses
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false, IDatabase $dbr=null)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
Definition: AllMessagesTablePager.php:156
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:698
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:1505
ApiQueryAllMessages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryAllMessages.php:212
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:515