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