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