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