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