Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryLangLinks
0.00% covered (danger)
0.00%
0 / 115
0.00% covered (danger)
0.00%
0 / 6
552
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
342
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Language\Language;
12use MediaWiki\Languages\LanguageNameUtils;
13use MediaWiki\MainConfigNames;
14use MediaWiki\Title\Title;
15use MediaWiki\Utils\UrlUtils;
16use Wikimedia\ParamValidator\ParamValidator;
17use Wikimedia\ParamValidator\TypeDef\IntegerDef;
18
19/**
20 * A query module to list all langlinks (links to corresponding foreign language pages).
21 *
22 * @ingroup API
23 */
24class ApiQueryLangLinks extends ApiQueryBase {
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
152    /** @inheritDoc */
153    public function getCacheMode( $params ) {
154        return 'public';
155    }
156
157    /** @inheritDoc */
158    public function getAllowedParams() {
159        return [
160            'prop' => [
161                ParamValidator::PARAM_ISMULTI => true,
162                ParamValidator::PARAM_TYPE => [
163                    'url',
164                    'langname',
165                    'autonym',
166                ],
167                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
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
196    /** @inheritDoc */
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
207    /** @inheritDoc */
208    public function getHelpUrls() {
209        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
210    }
211}
212
213/** @deprecated class alias since 1.43 */
214class_alias( ApiQueryLangLinks::class, 'ApiQueryLangLinks' );