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