MediaWiki  master
ApiQueryLanguageinfo.php
Go to the documentation of this file.
1 <?php
25 
32 
44  private const MAX_EXECUTE_SECONDS = 2.0;
45 
48 
51 
54 
57 
60 
71  public function __construct(
72  ApiQuery $queryModule,
73  $moduleName,
78  $microtimeFunction = null
79  ) {
80  parent::__construct( $queryModule, $moduleName, 'li' );
81  $this->languageFactory = $languageFactory;
82  $this->languageNameUtils = $languageNameUtils;
83  $this->languageFallback = $languageFallback;
84  $this->languageConverterFactory = $languageConverterFactory;
85  $this->microtimeFunction = $microtimeFunction;
86  }
87 
89  private function microtime() {
90  if ( $this->microtimeFunction ) {
91  return ( $this->microtimeFunction )();
92  } else {
93  return microtime( true );
94  }
95  }
96 
97  public function execute() {
98  $endTime = $this->microtime() + self::MAX_EXECUTE_SECONDS;
99 
100  $props = array_flip( $this->getParameter( 'prop' ) );
101  $includeCode = isset( $props['code'] );
102  $includeBcp47 = isset( $props['bcp47'] );
103  $includeDir = isset( $props['dir'] );
104  $includeAutonym = isset( $props['autonym'] );
105  $includeName = isset( $props['name'] );
106  $includeFallbacks = isset( $props['fallbacks'] );
107  $includeVariants = isset( $props['variants'] );
108 
109  $targetLanguageCode = $this->getLanguage()->getCode();
110  $include = 'all';
111 
112  $availableLanguageCodes = array_keys( $this->languageNameUtils->getLanguageNames(
113  // MediaWiki and extensions may return different sets of language codes
114  // when asked for language names in different languages;
115  // asking for English language names is most likely to give us the full set,
116  // even though we may not need those at all
117  'en',
118  $include
119  ) );
120  $selectedLanguageCodes = $this->getParameter( 'code' );
121  if ( $selectedLanguageCodes === [ '*' ] ) {
122  $languageCodes = $availableLanguageCodes;
123  } else {
124  $languageCodes = array_values( array_intersect(
125  $availableLanguageCodes,
126  $selectedLanguageCodes
127  ) );
128  $unrecognizedCodes = array_values( array_diff(
129  $selectedLanguageCodes,
130  $availableLanguageCodes
131  ) );
132  if ( $unrecognizedCodes !== [] ) {
133  $this->addWarning( [
134  'apiwarn-unrecognizedvalues',
135  $this->encodeParamName( 'code' ),
136  Message::listParam( $unrecognizedCodes, 'comma' ),
137  count( $unrecognizedCodes ),
138  ] );
139  }
140  }
141  // order of $languageCodes is guaranteed by Language::fetchLanguageNames()
142  // and preserved by array_values() + array_intersect()
143 
144  $continue = $this->getParameter( 'continue' );
145  if ( $continue === null ) {
146  $continue = reset( $languageCodes );
147  }
148 
149  $result = $this->getResult();
150  $rootPath = [
151  $this->getQuery()->getModuleName(),
152  $this->getModuleName(),
153  ];
154  $result->addArrayType( $rootPath, 'assoc' );
155 
156  foreach ( $languageCodes as $languageCode ) {
157  if ( $languageCode < $continue ) {
158  continue;
159  }
160 
161  $now = $this->microtime();
162  if ( $now >= $endTime ) {
163  $this->setContinueEnumParameter( 'continue', $languageCode );
164  break;
165  }
166 
167  $info = [];
168  ApiResult::setArrayType( $info, 'assoc' );
169 
170  if ( $includeCode ) {
171  $info['code'] = $languageCode;
172  }
173 
174  if ( $includeBcp47 ) {
175  $bcp47 = LanguageCode::bcp47( $languageCode );
176  $info['bcp47'] = $bcp47;
177  }
178 
179  if ( $includeDir ) {
180  $dir = $this->languageFactory->getLanguage( $languageCode )->getDir();
181  $info['dir'] = $dir;
182  }
183 
184  if ( $includeAutonym ) {
185  $autonym = $this->languageNameUtils->getLanguageName(
186  $languageCode,
187  LanguageNameUtils::AUTONYMS,
188  $include
189  );
190  $info['autonym'] = $autonym;
191  }
192 
193  if ( $includeName ) {
194  $name = $this->languageNameUtils->getLanguageName(
195  $languageCode,
196  $targetLanguageCode,
197  $include
198  );
199  $info['name'] = $name;
200  }
201 
202  if ( $includeFallbacks ) {
203  $fallbacks = $this->languageFallback->getAll(
204  $languageCode,
205  // allow users to distinguish between implicit and explicit 'en' fallbacks
206  LanguageFallback::STRICT
207  );
208  ApiResult::setIndexedTagName( $fallbacks, 'fb' );
209  $info['fallbacks'] = $fallbacks;
210  }
211 
212  if ( $includeVariants ) {
213  $language = $this->languageFactory->getLanguage( $languageCode );
214  $converter = $this->languageConverterFactory->getLanguageConverter( $language );
215  $variants = $converter->getVariants();
216  ApiResult::setIndexedTagName( $variants, 'var' );
217  $info['variants'] = $variants;
218  }
219 
220  $fit = $result->addValue( $rootPath, $languageCode, $info );
221  if ( !$fit ) {
222  $this->setContinueEnumParameter( 'continue', $languageCode );
223  break;
224  }
225  }
226  }
227 
228  public function getCacheMode( $params ) {
229  return 'public';
230  }
231 
232  public function getAllowedParams() {
233  return [
234  'prop' => [
235  self::PARAM_DFLT => 'code',
236  self::PARAM_ISMULTI => true,
237  self::PARAM_TYPE => [
238  'code',
239  'bcp47',
240  'dir',
241  'autonym',
242  'name',
243  'fallbacks',
244  'variants',
245  ],
246  self::PARAM_HELP_MSG_PER_VALUE => [],
247  ],
248  'code' => [
249  self::PARAM_DFLT => '*',
250  self::PARAM_ISMULTI => true,
251  ],
252  'continue' => [
253  self::PARAM_HELP_MSG => 'api-help-param-continue',
254  ],
255  ];
256  }
257 
258  protected function getExamplesMessages() {
259  $pathUrl = 'action=' . $this->getQuery()->getModuleName() .
260  '&meta=' . $this->getModuleName();
261  $pathMsg = $this->getModulePath();
262  $prefix = $this->getModulePrefix();
263 
264  return [
265  "$pathUrl"
266  => "apihelp-$pathMsg-example-simple",
267  "$pathUrl&{$prefix}prop=autonym|name&uselang=de"
268  => "apihelp-$pathMsg-example-autonym-name-de",
269  "$pathUrl&{$prefix}prop=fallbacks|variants&{$prefix}code=oc"
270  => "apihelp-$pathMsg-example-fallbacks-variants-oc",
271  "$pathUrl&{$prefix}prop=bcp47|dir"
272  => "apihelp-$pathMsg-example-bcp47-dir",
273  ];
274  }
275 
276 }
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1300
MediaWiki\Languages\LanguageFactory
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: LanguageFactory.php:42
ApiQueryLanguageinfo\$languageNameUtils
LanguageNameUtils $languageNameUtils
Definition: ApiQueryLanguageinfo.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:564
ApiQueryBase\getQuery
getQuery()
Get the main Query module.
Definition: ApiQueryBase.php:105
MediaWiki\Languages\LanguageFallback
Definition: LanguageFallback.php:31
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:44
Message\listParam
static listParam(array $list, $type='text')
Definition: Message.php:1112
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
ApiQueryLanguageinfo\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryLanguageinfo.php:228
ApiResult\setArrayType
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:716
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:508
ApiQueryLanguageinfo\__construct
__construct(ApiQuery $queryModule, $moduleName, LanguageFactory $languageFactory, LanguageNameUtils $languageNameUtils, LanguageFallback $languageFallback, LanguageConverterFactory $languageConverterFactory, $microtimeFunction=null)
Definition: ApiQueryLanguageinfo.php:71
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:37
ApiQueryLanguageinfo\$microtimeFunction
callable null $microtimeFunction
Definition: ApiQueryLanguageinfo.php:59
ApiQueryLanguageinfo\$languageFallback
LanguageFallback $languageFallback
Definition: ApiQueryLanguageinfo.php:53
ApiQueryLanguageinfo\microtime
microtime()
Definition: ApiQueryLanguageinfo.php:89
ApiQueryLanguageinfo\$languageFactory
LanguageFactory $languageFactory
Definition: ApiQueryLanguageinfo.php:33
ApiQueryLanguageinfo\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryLanguageinfo.php:97
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:451
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:694
ApiQueryLanguageinfo\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryLanguageinfo.php:258
ApiQueryLanguageinfo\$languageConverterFactory
LanguageConverterFactory $languageConverterFactory
Definition: ApiQueryLanguageinfo.php:56
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:836
LanguageCode\bcp47
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
Definition: LanguageCode.php:175
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:443
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:519
ApiQueryLanguageinfo
API module to enumerate language information.
Definition: ApiQueryLanguageinfo.php:31
ApiQueryLanguageinfo\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryLanguageinfo.php:232