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