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