Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 115
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 * 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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23use MediaWiki\Languages\LanguageNameUtils;
24use MediaWiki\MainConfigNames;
25use MediaWiki\Title\Title;
26use MediaWiki\Utils\UrlUtils;
27use Wikimedia\ParamValidator\ParamValidator;
28use Wikimedia\ParamValidator\TypeDef\IntegerDef;
29
30/**
31 * A query module to list all langlinks (links to corresponding foreign language pages).
32 *
33 * @ingroup API
34 */
35class ApiQueryLangLinks extends ApiQueryBase {
36
37    private LanguageNameUtils $languageNameUtils;
38    private Language $contentLanguage;
39    private UrlUtils $urlUtils;
40
41    public function __construct(
42        ApiQuery $query,
43        $moduleName,
44        LanguageNameUtils $languageNameUtils,
45        Language $contentLanguage,
46        UrlUtils $urlUtils
47    ) {
48        parent::__construct( $query, $moduleName, 'll' );
49        $this->languageNameUtils = $languageNameUtils;
50        $this->contentLanguage = $contentLanguage;
51        $this->urlUtils = $urlUtils;
52    }
53
54    public function execute() {
55        $pages = $this->getPageSet()->getGoodPages();
56        if ( $pages === [] ) {
57            return;
58        }
59
60        $params = $this->extractRequestParams();
61        $prop = array_fill_keys( (array)$params['prop'], true );
62
63        if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
64            $this->dieWithError(
65                [
66                    'apierror-invalidparammix-mustusewith',
67                    $this->encodeParamName( 'title' ),
68                    $this->encodeParamName( 'lang' ),
69                ],
70                'invalidparammix'
71            );
72        }
73
74        // Handle deprecated param
75        $this->requireMaxOneParameter( $params, 'url', 'prop' );
76        if ( $params['url'] ) {
77            $prop = [ 'url' => 1 ];
78        }
79
80        $this->addFields( [
81            'll_from',
82            'll_lang',
83            'll_title'
84        ] );
85
86        $this->addTables( 'langlinks' );
87        $this->addWhereFld( 'll_from', array_keys( $pages ) );
88        if ( $params['continue'] !== null ) {
89            $db = $this->getDB();
90            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
91            $op = $params['dir'] == 'descending' ? '<=' : '>=';
92            $this->addWhere( $db->buildComparison( $op, [
93                'll_from' => $cont[0],
94                'll_lang' => $cont[1],
95            ] ) );
96        }
97
98        // FIXME: (follow-up) To allow extensions to add to the language links, we need
99        //       to load them all, add the extra links, then apply paging.
100        //       Should not be terrible, it's not going to be more than a few hundred links.
101
102        // Note that, since (ll_from, ll_lang) is a unique key, we don't need
103        // to sort by ll_title to ensure deterministic ordering.
104        $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
105        if ( isset( $params['lang'] ) ) {
106            $this->addWhereFld( 'll_lang', $params['lang'] );
107            if ( isset( $params['title'] ) ) {
108                $this->addWhereFld( 'll_title', $params['title'] );
109            }
110            $this->addOption( 'ORDER BY', 'll_from' . $sort );
111        } else {
112            // Don't order by ll_from if it's constant in the WHERE clause
113            if ( count( $pages ) === 1 ) {
114                $this->addOption( 'ORDER BY', 'll_lang' . $sort );
115            } else {
116                $this->addOption( 'ORDER BY', [
117                    'll_from' . $sort,
118                    'll_lang' . $sort
119                ] );
120            }
121        }
122
123        $this->addOption( 'LIMIT', $params['limit'] + 1 );
124        $res = $this->select( __METHOD__ );
125
126        $count = 0;
127        foreach ( $res as $row ) {
128            if ( ++$count > $params['limit'] ) {
129                // We've reached the one extra which shows that
130                // there are additional pages to be had. Stop here...
131                $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
132                break;
133            }
134
135            $languageNameMap = $this->getConfig()->get( MainConfigNames::InterlanguageLinkCodeMap );
136            $displayLanguageCode = $languageNameMap[ $row->ll_lang ] ?? $row->ll_lang;
137
138            // This is potentially risky and confusing (request `no`, but get `nb` in the result).
139            $entry = [ 'lang' => $displayLanguageCode ];
140            if ( isset( $prop['url'] ) ) {
141                $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
142                if ( $title ) {
143                    $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
144                }
145            }
146
147            if ( isset( $prop['langname'] ) ) {
148                $entry['langname'] = $this->languageNameUtils
149                    ->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] );
150            }
151            if ( isset( $prop['autonym'] ) ) {
152                $entry['autonym'] = $this->languageNameUtils->getLanguageName( $displayLanguageCode );
153            }
154            ApiResult::setContentValue( $entry, 'title', $row->ll_title );
155            $fit = $this->addPageSubItem( $row->ll_from, $entry );
156            if ( !$fit ) {
157                $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
158                break;
159            }
160        }
161    }
162
163    public function getCacheMode( $params ) {
164        return 'public';
165    }
166
167    public function getAllowedParams() {
168        return [
169            'prop' => [
170                ParamValidator::PARAM_ISMULTI => true,
171                ParamValidator::PARAM_TYPE => [
172                    'url',
173                    'langname',
174                    'autonym',
175                ],
176                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
177            ],
178            'lang' => null,
179            'title' => null,
180            'dir' => [
181                ParamValidator::PARAM_DEFAULT => 'ascending',
182                ParamValidator::PARAM_TYPE => [
183                    'ascending',
184                    'descending'
185                ]
186            ],
187            'inlanguagecode' => $this->contentLanguage->getCode(),
188            'limit' => [
189                ParamValidator::PARAM_DEFAULT => 10,
190                ParamValidator::PARAM_TYPE => 'limit',
191                IntegerDef::PARAM_MIN => 1,
192                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
193                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
194            ],
195            'continue' => [
196                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
197            ],
198            'url' => [
199                ParamValidator::PARAM_DEFAULT => false,
200                ParamValidator::PARAM_DEPRECATED => true,
201            ],
202        ];
203    }
204
205    protected function getExamplesMessages() {
206        $title = Title::newMainPage()->getPrefixedText();
207        $mp = rawurlencode( $title );
208
209        return [
210            "action=query&prop=langlinks&titles={$mp}&redirects="
211                => 'apihelp-query+langlinks-example-simple',
212        ];
213    }
214
215    public function getHelpUrls() {
216        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
217    }
218}