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