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
ApiQueryIWBacklinks
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 © 2010 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\InterwikiLinksTable;
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 ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
24
25    public function __construct( ApiQuery $query, string $moduleName ) {
26        parent::__construct( $query, $moduleName, 'iwbl' );
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['prefix'] ) ) {
46            $this->dieWithError(
47                [
48                    'apierror-invalidparammix-mustusewith',
49                    $this->encodeParamName( 'title' ),
50                    $this->encodeParamName( 'prefix' ),
51                ],
52                'invalidparammix'
53            );
54        }
55
56        $this->setVirtualDomain( InterwikiLinksTable::VIRTUAL_DOMAIN );
57        $db = $this->getDB();
58
59        if ( $params['continue'] !== null ) {
60            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
61            $op = $params['dir'] == 'descending' ? '<=' : '>=';
62            $this->addWhere( $db->buildComparison( $op, [
63                'iwl_prefix' => $cont[0],
64                'iwl_title' => $cont[1],
65                'iwl_from' => $cont[2],
66            ] ) );
67        }
68
69        $prop = array_fill_keys( $params['prop'], true );
70        $iwprefix = isset( $prop['iwprefix'] );
71        $iwtitle = isset( $prop['iwtitle'] );
72
73        $this->addTables( [ 'iwlinks', 'page' ] );
74        $this->addWhere( 'iwl_from = page_id' );
75
76        $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
77            'iwl_from', 'iwl_prefix', 'iwl_title' ] );
78
79        $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
80        if ( isset( $params['prefix'] ) ) {
81            $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
82            if ( isset( $params['title'] ) ) {
83                $this->addWhereFld( 'iwl_title', $params['title'] );
84                $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
85            } else {
86                $this->addOption( 'ORDER BY', [
87                    'iwl_title' . $sort,
88                    'iwl_from' . $sort
89                ] );
90            }
91        } else {
92            $this->addOption( 'ORDER BY', [
93                'iwl_prefix' . $sort,
94                'iwl_title' . $sort,
95                'iwl_from' . $sort
96            ] );
97        }
98
99        $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101        $res = $this->select( __METHOD__ );
102
103        $pages = [];
104
105        $count = 0;
106        $result = $this->getResult();
107
108        if ( $resultPageSet === null ) {
109            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
110        }
111
112        foreach ( $res as $row ) {
113            if ( ++$count > $params['limit'] ) {
114                // We've reached the one extra which shows that there are
115                // additional pages to be had. Stop here...
116                // Continue string preserved in case the redirect query doesn't
117                // pass the limit
118                $this->setContinueEnumParameter(
119                    'continue',
120                    "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
121                );
122                break;
123            }
124
125            if ( $resultPageSet !== null ) {
126                $pages[] = Title::newFromRow( $row );
127            } else {
128                $entry = [ 'pageid' => (int)$row->page_id ];
129
130                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
131                ApiQueryBase::addTitleInfo( $entry, $title );
132
133                if ( $row->page_is_redirect ) {
134                    $entry['redirect'] = true;
135                }
136
137                if ( $iwprefix ) {
138                    $entry['iwprefix'] = $row->iwl_prefix;
139                }
140
141                if ( $iwtitle ) {
142                    $entry['iwtitle'] = $row->iwl_title;
143                }
144
145                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
146                if ( !$fit ) {
147                    $this->setContinueEnumParameter(
148                        'continue',
149                        "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
150                    );
151                    break;
152                }
153            }
154        }
155
156        if ( $resultPageSet === null ) {
157            $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
158        } else {
159            $resultPageSet->populateFromTitles( $pages );
160        }
161    }
162
163    /** @inheritDoc */
164    public function getCacheMode( $params ) {
165        return 'public';
166    }
167
168    /** @inheritDoc */
169    public function getAllowedParams() {
170        return [
171            'prefix' => null,
172            'title' => null,
173            'continue' => [
174                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
175            ],
176            'limit' => [
177                ParamValidator::PARAM_DEFAULT => 10,
178                ParamValidator::PARAM_TYPE => 'limit',
179                IntegerDef::PARAM_MIN => 1,
180                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
181                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
182            ],
183            'prop' => [
184                ParamValidator::PARAM_ISMULTI => true,
185                ParamValidator::PARAM_DEFAULT => '',
186                ParamValidator::PARAM_TYPE => [
187                    'iwprefix',
188                    'iwtitle',
189                ],
190                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
191            ],
192            'dir' => [
193                ParamValidator::PARAM_DEFAULT => 'ascending',
194                ParamValidator::PARAM_TYPE => [
195                    'ascending',
196                    'descending'
197                ]
198            ],
199        ];
200    }
201
202    /** @inheritDoc */
203    protected function getExamplesMessages() {
204        return [
205            'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
206                => 'apihelp-query+iwbacklinks-example-simple',
207            'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
208                => 'apihelp-query+iwbacklinks-example-generator',
209        ];
210    }
211
212    /** @inheritDoc */
213    public function getHelpUrls() {
214        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwbacklinks';
215    }
216}
217
218/** @deprecated class alias since 1.43 */
219class_alias( ApiQueryIWBacklinks::class, 'ApiQueryIWBacklinks' );