MediaWiki master
ApiQueryLangLinks.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
32
39
40 private LanguageNameUtils $languageNameUtils;
41 private Language $contentLanguage;
42 private UrlUtils $urlUtils;
43
44 public function __construct(
45 ApiQuery $query,
46 string $moduleName,
47 LanguageNameUtils $languageNameUtils,
48 Language $contentLanguage,
49 UrlUtils $urlUtils
50 ) {
51 parent::__construct( $query, $moduleName, 'll' );
52 $this->languageNameUtils = $languageNameUtils;
53 $this->contentLanguage = $contentLanguage;
54 $this->urlUtils = $urlUtils;
55 }
56
57 public function execute() {
58 $pages = $this->getPageSet()->getGoodPages();
59 if ( $pages === [] ) {
60 return;
61 }
62
64 $prop = array_fill_keys( (array)$params['prop'], true );
65
66 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
67 $this->dieWithError(
68 [
69 'apierror-invalidparammix-mustusewith',
70 $this->encodeParamName( 'title' ),
71 $this->encodeParamName( 'lang' ),
72 ],
73 'invalidparammix'
74 );
75 }
76
77 // Handle deprecated param
78 $this->requireMaxOneParameter( $params, 'url', 'prop' );
79 if ( $params['url'] ) {
80 $prop = [ 'url' => 1 ];
81 }
82
83 $this->addFields( [
84 'll_from',
85 'll_lang',
86 'll_title'
87 ] );
88
89 $this->addTables( 'langlinks' );
90 $this->addWhereFld( 'll_from', array_keys( $pages ) );
91 if ( $params['continue'] !== null ) {
92 $db = $this->getDB();
93 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
94 $op = $params['dir'] == 'descending' ? '<=' : '>=';
95 $this->addWhere( $db->buildComparison( $op, [
96 'll_from' => $cont[0],
97 'll_lang' => $cont[1],
98 ] ) );
99 }
100
101 // FIXME: (follow-up) To allow extensions to add to the language links, we need
102 // to load them all, add the extra links, then apply paging.
103 // Should not be terrible, it's not going to be more than a few hundred links.
104
105 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
106 // to sort by ll_title to ensure deterministic ordering.
107 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
108 if ( isset( $params['lang'] ) ) {
109 $this->addWhereFld( 'll_lang', $params['lang'] );
110 if ( isset( $params['title'] ) ) {
111 $this->addWhereFld( 'll_title', $params['title'] );
112 }
113 $this->addOption( 'ORDER BY', 'll_from' . $sort );
114 } else {
115 // Don't order by ll_from if it's constant in the WHERE clause
116 if ( count( $pages ) === 1 ) {
117 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
118 } else {
119 $this->addOption( 'ORDER BY', [
120 'll_from' . $sort,
121 'll_lang' . $sort
122 ] );
123 }
124 }
125
126 $this->addOption( 'LIMIT', $params['limit'] + 1 );
127 $res = $this->select( __METHOD__ );
128
129 $count = 0;
130 foreach ( $res as $row ) {
131 if ( ++$count > $params['limit'] ) {
132 // We've reached the one extra which shows that
133 // there are additional pages to be had. Stop here...
134 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
135 break;
136 }
137
138 $languageNameMap = $this->getConfig()->get( MainConfigNames::InterlanguageLinkCodeMap );
139 $displayLanguageCode = $languageNameMap[ $row->ll_lang ] ?? $row->ll_lang;
140
141 // This is potentially risky and confusing (request `no`, but get `nb` in the result).
142 $entry = [ 'lang' => $displayLanguageCode ];
143 if ( isset( $prop['url'] ) ) {
144 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
145 if ( $title ) {
146 $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
147 }
148 }
149
150 if ( isset( $prop['langname'] ) ) {
151 $entry['langname'] = $this->languageNameUtils
152 ->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] );
153 }
154 if ( isset( $prop['autonym'] ) ) {
155 $entry['autonym'] = $this->languageNameUtils->getLanguageName( $displayLanguageCode );
156 }
157 ApiResult::setContentValue( $entry, 'title', $row->ll_title );
158 $fit = $this->addPageSubItem( $row->ll_from, $entry );
159 if ( !$fit ) {
160 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
161 break;
162 }
163 }
164 }
165
166 public function getCacheMode( $params ) {
167 return 'public';
168 }
169
170 public function getAllowedParams() {
171 return [
172 'prop' => [
173 ParamValidator::PARAM_ISMULTI => true,
174 ParamValidator::PARAM_TYPE => [
175 'url',
176 'langname',
177 'autonym',
178 ],
180 ],
181 'lang' => null,
182 'title' => null,
183 'dir' => [
184 ParamValidator::PARAM_DEFAULT => 'ascending',
185 ParamValidator::PARAM_TYPE => [
186 'ascending',
187 'descending'
188 ]
189 ],
190 'inlanguagecode' => $this->contentLanguage->getCode(),
191 'limit' => [
192 ParamValidator::PARAM_DEFAULT => 10,
193 ParamValidator::PARAM_TYPE => 'limit',
194 IntegerDef::PARAM_MIN => 1,
195 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
196 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
197 ],
198 'continue' => [
199 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
200 ],
201 'url' => [
202 ParamValidator::PARAM_DEFAULT => false,
203 ParamValidator::PARAM_DEPRECATED => true,
204 ],
205 ];
206 }
207
208 protected function getExamplesMessages() {
209 $title = Title::newMainPage()->getPrefixedText();
210 $mp = rawurlencode( $title );
211
212 return [
213 "action=query&prop=langlinks&titles={$mp}&redirects="
214 => 'apihelp-query+langlinks-example-simple',
215 ];
216 }
217
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
220 }
221}
222
224class_alias( ApiQueryLangLinks::class, 'ApiQueryLangLinks' );
const PROTO_CURRENT
Definition Defines.php:209
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1768
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1025
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:251
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:829
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:249
This is a base class for all Query modules.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
getPageSet()
Get the PageSet object to work on.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
This is the main query class.
Definition ApiQuery.php:48
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Base class for language-specific code.
Definition Language.php:78
A service that provides utilities to do with language names and codes.
A class containing constants representing the names of configuration variables.
const InterlanguageLinkCodeMap
Name constant for the InterlanguageLinkCodeMap setting, for use with Config::get()
Represents a title within MediaWiki.
Definition Title.php:78
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Service for formatting and validating API parameters.
Type definition for integer types.