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