MediaWiki  master
ApiQueryLanguageinfo.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\Timestamp\ConvertibleTimestamp;
26 
33 
43  private const MAX_EXECUTE_SECONDS = 2;
44 
47 
50 
53 
56 
65  public function __construct(
66  ApiQuery $queryModule,
67  $moduleName,
72  ) {
73  parent::__construct( $queryModule, $moduleName, 'li' );
74  $this->languageFactory = $languageFactory;
75  $this->languageNameUtils = $languageNameUtils;
76  $this->languageFallback = $languageFallback;
77  $this->languageConverterFactory = $languageConverterFactory;
78  }
79 
80  public function execute() {
81  // ConvertibleTimestamp::time() used so we can fake the current time in tests
82  $endTime = ConvertibleTimestamp::time() + self::MAX_EXECUTE_SECONDS;
83 
84  $props = array_fill_keys( $this->getParameter( 'prop' ), true );
85  $includeCode = isset( $props['code'] );
86  $includeBcp47 = isset( $props['bcp47'] );
87  $includeDir = isset( $props['dir'] );
88  $includeAutonym = isset( $props['autonym'] );
89  $includeName = isset( $props['name'] );
90  $includeFallbacks = isset( $props['fallbacks'] );
91  $includeVariants = isset( $props['variants'] );
92 
93  $targetLanguageCode = $this->getLanguage()->getCode();
94  $include = 'all';
95 
96  $availableLanguageCodes = array_keys( $this->languageNameUtils->getLanguageNames(
97  // MediaWiki and extensions may return different sets of language codes
98  // when asked for language names in different languages;
99  // asking for English language names is most likely to give us the full set,
100  // even though we may not need those at all
101  'en',
102  $include
103  ) );
104  $selectedLanguageCodes = $this->getParameter( 'code' );
105  if ( $selectedLanguageCodes === [ '*' ] ) {
106  $languageCodes = $availableLanguageCodes;
107  } else {
108  $languageCodes = array_values( array_intersect(
109  $availableLanguageCodes,
110  $selectedLanguageCodes
111  ) );
112  $unrecognizedCodes = array_values( array_diff(
113  $selectedLanguageCodes,
114  $availableLanguageCodes
115  ) );
116  if ( $unrecognizedCodes !== [] ) {
117  $this->addWarning( [
118  'apiwarn-unrecognizedvalues',
119  $this->encodeParamName( 'code' ),
120  Message::listParam( $unrecognizedCodes, 'comma' ),
121  count( $unrecognizedCodes ),
122  ] );
123  }
124  }
125  // order of $languageCodes is guaranteed by Language::fetchLanguageNames()
126  // and preserved by array_values() + array_intersect()
127 
128  $continue = $this->getParameter( 'continue' );
129  if ( $continue === null ) {
130  $continue = reset( $languageCodes );
131  }
132 
133  $result = $this->getResult();
134  $rootPath = [
135  $this->getQuery()->getModuleName(),
136  $this->getModuleName(),
137  ];
138  $result->addArrayType( $rootPath, 'assoc' );
139 
140  foreach ( $languageCodes as $languageCode ) {
141  if ( $languageCode < $continue ) {
142  continue;
143  }
144 
145  $now = ConvertibleTimestamp::time();
146  if ( $now >= $endTime ) {
147  $this->setContinueEnumParameter( 'continue', $languageCode );
148  break;
149  }
150 
151  $info = [];
152  ApiResult::setArrayType( $info, 'assoc' );
153 
154  if ( $includeCode ) {
155  $info['code'] = $languageCode;
156  }
157 
158  if ( $includeBcp47 ) {
159  $bcp47 = LanguageCode::bcp47( $languageCode );
160  $info['bcp47'] = $bcp47;
161  }
162 
163  if ( $includeDir ) {
164  $dir = $this->languageFactory->getLanguage( $languageCode )->getDir();
165  $info['dir'] = $dir;
166  }
167 
168  if ( $includeAutonym ) {
169  $autonym = $this->languageNameUtils->getLanguageName(
170  $languageCode,
171  LanguageNameUtils::AUTONYMS,
172  $include
173  );
174  $info['autonym'] = $autonym;
175  }
176 
177  if ( $includeName ) {
178  $name = $this->languageNameUtils->getLanguageName(
179  $languageCode,
180  $targetLanguageCode,
181  $include
182  );
183  $info['name'] = $name;
184  }
185 
186  if ( $includeFallbacks ) {
187  $fallbacks = $this->languageFallback->getAll(
188  $languageCode,
189  // allow users to distinguish between implicit and explicit 'en' fallbacks
190  LanguageFallback::STRICT
191  );
192  ApiResult::setIndexedTagName( $fallbacks, 'fb' );
193  $info['fallbacks'] = $fallbacks;
194  }
195 
196  if ( $includeVariants ) {
197  $language = $this->languageFactory->getLanguage( $languageCode );
198  $converter = $this->languageConverterFactory->getLanguageConverter( $language );
199  $variants = $converter->getVariants();
200  ApiResult::setIndexedTagName( $variants, 'var' );
201  $info['variants'] = $variants;
202  }
203 
204  $fit = $result->addValue( $rootPath, $languageCode, $info );
205  if ( !$fit ) {
206  $this->setContinueEnumParameter( 'continue', $languageCode );
207  break;
208  }
209  }
210  }
211 
212  public function getCacheMode( $params ) {
213  return 'public';
214  }
215 
216  public function getAllowedParams() {
217  return [
218  'prop' => [
219  self::PARAM_DFLT => 'code',
220  self::PARAM_ISMULTI => true,
221  self::PARAM_TYPE => [
222  'code',
223  'bcp47',
224  'dir',
225  'autonym',
226  'name',
227  'fallbacks',
228  'variants',
229  ],
230  self::PARAM_HELP_MSG_PER_VALUE => [],
231  ],
232  'code' => [
233  self::PARAM_DFLT => '*',
234  self::PARAM_ISMULTI => true,
235  ],
236  'continue' => [
237  self::PARAM_HELP_MSG => 'api-help-param-continue',
238  ],
239  ];
240  }
241 
242  protected function getExamplesMessages() {
243  $pathUrl = 'action=' . $this->getQuery()->getModuleName() .
244  '&meta=' . $this->getModuleName();
245  $pathMsg = $this->getModulePath();
246  $prefix = $this->getModulePrefix();
247 
248  return [
249  "$pathUrl"
250  => "apihelp-$pathMsg-example-simple",
251  "$pathUrl&{$prefix}prop=autonym|name&uselang=de"
252  => "apihelp-$pathMsg-example-autonym-name-de",
253  "$pathUrl&{$prefix}prop=fallbacks|variants&{$prefix}code=oc"
254  => "apihelp-$pathMsg-example-fallbacks-variants-oc",
255  "$pathUrl&{$prefix}prop=bcp47|dir"
256  => "apihelp-$pathMsg-example-bcp47-dir",
257  ];
258  }
259 
260 }
ApiQuery
This is the main query class.
Definition: ApiQuery.php:39
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1354
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:49
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiQueryBase\getQuery
getQuery()
Get the main Query module.
Definition: ApiQueryBase.php:103
MediaWiki\Languages\LanguageFallback
Definition: LanguageFallback.php:31
ApiQueryLanguageinfo\MAX_EXECUTE_SECONDS
const MAX_EXECUTE_SECONDS
The maximum time for execute(); if execution takes longer than this, apply continuation.
Definition: ApiQueryLanguageinfo.php:43
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:46
Message\listParam
static listParam(array $list, $type='text')
Definition: Message.php:1227
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:153
ApiQueryLanguageinfo\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryLanguageinfo.php:212
ApiResult\setArrayType
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:715
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:572
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:37
ApiQueryLanguageinfo\$languageFallback
LanguageFallback $languageFallback
Definition: ApiQueryLanguageinfo.php:52
ApiQueryLanguageinfo\$languageFactory
LanguageFactory $languageFactory
Definition: ApiQueryLanguageinfo.php:46
ApiQueryLanguageinfo\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryLanguageinfo.php:80
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:505
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:603
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:742
ApiQueryLanguageinfo\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryLanguageinfo.php:242
ApiQueryLanguageinfo\__construct
__construct(ApiQuery $queryModule, $moduleName, LanguageFactory $languageFactory, LanguageNameUtils $languageNameUtils, LanguageFallback $languageFallback, LanguageConverterFactory $languageConverterFactory)
Definition: ApiQueryLanguageinfo.php:65
ApiQueryLanguageinfo\$languageConverterFactory
LanguageConverterFactory $languageConverterFactory
Definition: ApiQueryLanguageinfo.php:55
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:884
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:497
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:515
ApiQueryLanguageinfo
API module to enumerate language information.
Definition: ApiQueryLanguageinfo.php:32
ApiQueryLanguageinfo\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryLanguageinfo.php:216