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 * 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
23namespace MediaWiki\Api;
24
25use MediaWiki\Language\Language;
26use MediaWiki\Languages\LanguageNameUtils;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Title\Title;
29use MediaWiki\Utils\UrlUtils;
30use Wikimedia\ParamValidator\ParamValidator;
31use Wikimedia\ParamValidator\TypeDef\IntegerDef;
32
33/**
34 * A query module to list all langlinks (links to corresponding foreign language pages).
35 *
36 * @ingroup API
37 */
38class ApiQueryLangLinks extends ApiQueryBase {
39
40    private LanguageNameUtils $languageNameUtils;
41    private Language $contentLanguage;
42    private UrlUtils $urlUtils;
43
44    public function __construct(
45        ApiQuery $query,
46        string $moduleName,
47        LanguageNameUtils $languageNameUtils,
48        Language $contentLanguage,
49        UrlUtils $urlUtils
50    ) {
51        parent::__construct( $query, $moduleName, 'll' );
52        $this->languageNameUtils = $languageNameUtils;
53        $this->contentLanguage = $contentLanguage;
54        $this->urlUtils = $urlUtils;
55    }
56
57    public function execute() {
58        $pages = $this->getPageSet()->getGoodPages();
59        if ( $pages === [] ) {
60            return;
61        }
62
63        $params = $this->extractRequestParams();
64        $prop = array_fill_keys( (array)$params['prop'], true );
65
66        if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
67            $this->dieWithError(
68                [
69                    'apierror-invalidparammix-mustusewith',
70                    $this->encodeParamName( 'title' ),
71                    $this->encodeParamName( 'lang' ),
72                ],
73                'invalidparammix'
74            );
75        }
76
77        // Handle deprecated param
78        $this->requireMaxOneParameter( $params, 'url', 'prop' );
79        if ( $params['url'] ) {
80            $prop = [ 'url' => 1 ];
81        }
82
83        $this->addFields( [
84            'll_from',
85            'll_lang',
86            'll_title'
87        ] );
88
89        $this->addTables( 'langlinks' );
90        $this->addWhereFld( 'll_from', array_keys( $pages ) );
91        if ( $params['continue'] !== null ) {
92            $db = $this->getDB();
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],
98            ] ) );
99        }
100
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.
104
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'] );
112            }
113            $this->addOption( 'ORDER BY', 'll_from' . $sort );
114        } else {
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 );
118            } else {
119                $this->addOption( 'ORDER BY', [
120                    'll_from' . $sort,
121                    'll_lang' . $sort
122                ] );
123            }
124        }
125
126        $this->addOption( 'LIMIT', $params['limit'] + 1 );
127        $res = $this->select( __METHOD__ );
128
129        $count = 0;
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}" );
135                break;
136            }
137
138            $languageNameMap = $this->getConfig()->get( MainConfigNames::InterlanguageLinkCodeMap );
139            $displayLanguageCode = $languageNameMap[ $row->ll_lang ] ?? $row->ll_lang;
140
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}" );
145                if ( $title ) {
146                    $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
147                }
148            }
149
150            if ( isset( $prop['langname'] ) ) {
151                $entry['langname'] = $this->languageNameUtils
152                    ->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] );
153            }
154            if ( isset( $prop['autonym'] ) ) {
155                $entry['autonym'] = $this->languageNameUtils->getLanguageName( $displayLanguageCode );
156            }
157            ApiResult::setContentValue( $entry, 'title', $row->ll_title );
158            $fit = $this->addPageSubItem( $row->ll_from, $entry );
159            if ( !$fit ) {
160                $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
161                break;
162            }
163        }
164    }
165
166    public function getCacheMode( $params ) {
167        return 'public';
168    }
169
170    public function getAllowedParams() {
171        return [
172            'prop' => [
173                ParamValidator::PARAM_ISMULTI => true,
174                ParamValidator::PARAM_TYPE => [
175                    'url',
176                    'langname',
177                    'autonym',
178                ],
179                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
180            ],
181            'lang' => null,
182            'title' => null,
183            'dir' => [
184                ParamValidator::PARAM_DEFAULT => 'ascending',
185                ParamValidator::PARAM_TYPE => [
186                    'ascending',
187                    'descending'
188                ]
189            ],
190            'inlanguagecode' => $this->contentLanguage->getCode(),
191            'limit' => [
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
197            ],
198            'continue' => [
199                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
200            ],
201            'url' => [
202                ParamValidator::PARAM_DEFAULT => false,
203                ParamValidator::PARAM_DEPRECATED => true,
204            ],
205        ];
206    }
207
208    protected function getExamplesMessages() {
209        $title = Title::newMainPage()->getPrefixedText();
210        $mp = rawurlencode( $title );
211
212        return [
213            "action=query&prop=langlinks&titles={$mp}&redirects="
214                => 'apihelp-query+langlinks-example-simple',
215        ];
216    }
217
218    public function getHelpUrls() {
219        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
220    }
221}
222
223/** @deprecated class alias since 1.43 */
224class_alias( ApiQueryLangLinks::class, 'ApiQueryLangLinks' );