Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 118
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryLangBacklinks
0.00% covered (danger)
0.00%
0 / 117
0.00% covered (danger)
0.00%
0 / 8
600
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 1
306
 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 / 30
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 * API for MediaWiki 1.17+
4 *
5 * Copyright © 2011 Sam Reed
6 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
7 *
8 * @license GPL-2.0-or-later
9 * @file
10 */
11
12namespace MediaWiki\Api;
13
14use MediaWiki\Title\Title;
15use Wikimedia\ParamValidator\ParamValidator;
16use Wikimedia\ParamValidator\TypeDef\IntegerDef;
17
18/**
19 * This gives links pointing to the given interwiki
20 * @ingroup API
21 */
22class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
23
24    public function __construct( ApiQuery $query, string $moduleName ) {
25        parent::__construct( $query, $moduleName, 'lbl' );
26    }
27
28    public function execute() {
29        $this->run();
30    }
31
32    /** @inheritDoc */
33    public function executeGenerator( $resultPageSet ) {
34        $this->run( $resultPageSet );
35    }
36
37    /**
38     * @param ApiPageSet|null $resultPageSet
39     * @return void
40     */
41    public function run( $resultPageSet = null ) {
42        $params = $this->extractRequestParams();
43
44        if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
45            $this->dieWithError(
46                [
47                    'apierror-invalidparammix-mustusewith',
48                    $this->encodeParamName( 'title' ),
49                    $this->encodeParamName( 'lang' )
50                ],
51                'nolang'
52            );
53        }
54
55        if ( $params['continue'] !== null ) {
56            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
57            $db = $this->getDB();
58            $op = $params['dir'] == 'descending' ? '<=' : '>=';
59            $this->addWhere( $db->buildComparison( $op, [
60                'll_lang' => $cont[0],
61                'll_title' => $cont[1],
62                'll_from' => $cont[2],
63            ] ) );
64        }
65
66        $prop = array_fill_keys( $params['prop'], true );
67        $lllang = isset( $prop['lllang'] );
68        $lltitle = isset( $prop['lltitle'] );
69
70        $this->addTables( [ 'langlinks', 'page' ] );
71        $this->addWhere( 'll_from = page_id' );
72
73        $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
74            'll_from', 'll_lang', 'll_title' ] );
75
76        $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
77        if ( isset( $params['lang'] ) ) {
78            $this->addWhereFld( 'll_lang', $params['lang'] );
79            if ( isset( $params['title'] ) ) {
80                $this->addWhereFld( 'll_title', $params['title'] );
81                $this->addOption( 'ORDER BY', 'll_from' . $sort );
82            } else {
83                $this->addOption( 'ORDER BY', [
84                    'll_title' . $sort,
85                    'll_from' . $sort
86                ] );
87            }
88        } else {
89            $this->addOption( 'ORDER BY', [
90                'll_lang' . $sort,
91                'll_title' . $sort,
92                'll_from' . $sort
93            ] );
94        }
95
96        $this->addOption( 'LIMIT', $params['limit'] + 1 );
97
98        $res = $this->select( __METHOD__ );
99
100        $pages = [];
101
102        $count = 0;
103        $result = $this->getResult();
104
105        if ( $resultPageSet === null ) {
106            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
107        }
108
109        foreach ( $res as $row ) {
110            if ( ++$count > $params['limit'] ) {
111                // We've reached the one extra which shows that there are
112                // additional pages to be had. Stop here... Continue string
113                // preserved in case the redirect query doesn't pass the limit.
114                $this->setContinueEnumParameter(
115                    'continue',
116                    "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
117                );
118                break;
119            }
120
121            if ( $resultPageSet !== null ) {
122                $pages[] = Title::newFromRow( $row );
123            } else {
124                $entry = [ 'pageid' => (int)$row->page_id ];
125
126                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
127                ApiQueryBase::addTitleInfo( $entry, $title );
128
129                if ( $row->page_is_redirect ) {
130                    $entry['redirect'] = true;
131                }
132
133                if ( $lllang ) {
134                    $entry['lllang'] = $row->ll_lang;
135                }
136
137                if ( $lltitle ) {
138                    $entry['lltitle'] = $row->ll_title;
139                }
140
141                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
142                if ( !$fit ) {
143                    $this->setContinueEnumParameter(
144                        'continue',
145                        "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
146                    );
147                    break;
148                }
149            }
150        }
151
152        if ( $resultPageSet === null ) {
153            $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
154        } else {
155            $resultPageSet->populateFromTitles( $pages );
156        }
157    }
158
159    /** @inheritDoc */
160    public function getCacheMode( $params ) {
161        return 'public';
162    }
163
164    /** @inheritDoc */
165    public function getAllowedParams() {
166        return [
167            'lang' => null,
168            'title' => null,
169            'continue' => [
170                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
171            ],
172            'limit' => [
173                ParamValidator::PARAM_DEFAULT => 10,
174                ParamValidator::PARAM_TYPE => 'limit',
175                IntegerDef::PARAM_MIN => 1,
176                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
177                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
178            ],
179            'prop' => [
180                ParamValidator::PARAM_ISMULTI => true,
181                ParamValidator::PARAM_DEFAULT => '',
182                ParamValidator::PARAM_TYPE => [
183                    'lllang',
184                    'lltitle',
185                ],
186                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
187            ],
188            'dir' => [
189                ParamValidator::PARAM_DEFAULT => 'ascending',
190                ParamValidator::PARAM_TYPE => [
191                    'ascending',
192                    'descending'
193                ]
194            ],
195        ];
196    }
197
198    /** @inheritDoc */
199    protected function getExamplesMessages() {
200        return [
201            'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
202                => 'apihelp-query+langbacklinks-example-simple',
203            'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
204                => 'apihelp-query+langbacklinks-example-generator',
205        ];
206    }
207
208    /** @inheritDoc */
209    public function getHelpUrls() {
210        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
211    }
212}
213
214/** @deprecated class alias since 1.43 */
215class_alias( ApiQueryLangBacklinks::class, 'ApiQueryLangBacklinks' );