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