3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
25 use MediaWiki\Language\Language
;
26 use MediaWiki\Languages\LanguageNameUtils
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\Title\Title
;
29 use MediaWiki\Utils\UrlUtils
;
30 use Wikimedia\ParamValidator\ParamValidator
;
31 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
34 * A query module to list all langlinks (links to corresponding foreign language pages).
38 class ApiQueryLangLinks
extends ApiQueryBase
{
40 private LanguageNameUtils
$languageNameUtils;
41 private Language
$contentLanguage;
42 private UrlUtils
$urlUtils;
44 public function __construct(
47 LanguageNameUtils
$languageNameUtils,
48 Language
$contentLanguage,
51 parent
::__construct( $query, $moduleName, 'll' );
52 $this->languageNameUtils
= $languageNameUtils;
53 $this->contentLanguage
= $contentLanguage;
54 $this->urlUtils
= $urlUtils;
57 public function execute() {
58 $pages = $this->getPageSet()->getGoodPages();
59 if ( $pages === [] ) {
63 $params = $this->extractRequestParams();
64 $prop = array_fill_keys( (array)$params['prop'], true );
66 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
69 'apierror-invalidparammix-mustusewith',
70 $this->encodeParamName( 'title' ),
71 $this->encodeParamName( 'lang' ),
77 // Handle deprecated param
78 $this->requireMaxOneParameter( $params, 'url', 'prop' );
79 if ( $params['url'] ) {
80 $prop = [ 'url' => 1 ];
89 $this->addTables( 'langlinks' );
90 $this->addWhereFld( 'll_from', array_keys( $pages ) );
91 if ( $params['continue'] !== null ) {
93 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
94 $op = $params['dir'] == 'descending' ?
'<=' : '>=';
95 $this->addWhere( $db->buildComparison( $op, [
96 'll_from' => $cont[0],
97 'll_lang' => $cont[1],
101 // FIXME: (follow-up) To allow extensions to add to the language links, we need
102 // to load them all, add the extra links, then apply paging.
103 // Should not be terrible, it's not going to be more than a few hundred links.
105 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
106 // to sort by ll_title to ensure deterministic ordering.
107 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
108 if ( isset( $params['lang'] ) ) {
109 $this->addWhereFld( 'll_lang', $params['lang'] );
110 if ( isset( $params['title'] ) ) {
111 $this->addWhereFld( 'll_title', $params['title'] );
113 $this->addOption( 'ORDER BY', 'll_from' . $sort );
115 // Don't order by ll_from if it's constant in the WHERE clause
116 if ( count( $pages ) === 1 ) {
117 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
119 $this->addOption( 'ORDER BY', [
126 $this->addOption( 'LIMIT', $params['limit'] +
1 );
127 $res = $this->select( __METHOD__
);
130 foreach ( $res as $row ) {
131 if ( ++
$count > $params['limit'] ) {
132 // We've reached the one extra which shows that
133 // there are additional pages to be had. Stop here...
134 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
138 $languageNameMap = $this->getConfig()->get( MainConfigNames
::InterlanguageLinkCodeMap
);
139 $displayLanguageCode = $languageNameMap[ $row->ll_lang
] ??
$row->ll_lang
;
141 // This is potentially risky and confusing (request `no`, but get `nb` in the result).
142 $entry = [ 'lang' => $displayLanguageCode ];
143 if ( isset( $prop['url'] ) ) {
144 $title = Title
::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
146 $entry['url'] = (string)$this->urlUtils
->expand( $title->getFullURL(), PROTO_CURRENT
);
150 if ( isset( $prop['langname'] ) ) {
151 $entry['langname'] = $this->languageNameUtils
152 ->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] );
154 if ( isset( $prop['autonym'] ) ) {
155 $entry['autonym'] = $this->languageNameUtils
->getLanguageName( $displayLanguageCode );
157 ApiResult
::setContentValue( $entry, 'title', $row->ll_title
);
158 $fit = $this->addPageSubItem( $row->ll_from
, $entry );
160 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
166 public function getCacheMode( $params ) {
170 public function getAllowedParams() {
173 ParamValidator
::PARAM_ISMULTI
=> true,
174 ParamValidator
::PARAM_TYPE
=> [
179 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
184 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
185 ParamValidator
::PARAM_TYPE
=> [
190 'inlanguagecode' => $this->contentLanguage
->getCode(),
192 ParamValidator
::PARAM_DEFAULT
=> 10,
193 ParamValidator
::PARAM_TYPE
=> 'limit',
194 IntegerDef
::PARAM_MIN
=> 1,
195 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
196 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
199 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
202 ParamValidator
::PARAM_DEFAULT
=> false,
203 ParamValidator
::PARAM_DEPRECATED
=> true,
208 protected function getExamplesMessages() {
209 $title = Title
::newMainPage()->getPrefixedText();
210 $mp = rawurlencode( $title );
213 "action=query&prop=langlinks&titles={$mp}&redirects="
214 => 'apihelp-query+langlinks-example-simple',
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
223 /** @deprecated class alias since 1.43 */
224 class_alias( ApiQueryLangLinks
::class, 'ApiQueryLangLinks' );