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
ApiQueryIWLinks
0.00% covered (danger)
0.00%
0 / 115
0.00% covered (danger)
0.00%
0 / 6
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 1
272
 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 / 32
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 MediaWiki\Utils\UrlUtils;
17use Wikimedia\ParamValidator\ParamValidator;
18use Wikimedia\ParamValidator\TypeDef\IntegerDef;
19
20/**
21 * A query module to list all interwiki links on a page
22 *
23 * @ingroup API
24 */
25class ApiQueryIWLinks extends ApiQueryBase {
26
27    private UrlUtils $urlUtils;
28
29    public function __construct(
30        ApiQuery $query,
31        string $moduleName,
32        UrlUtils $urlUtils
33    ) {
34        parent::__construct( $query, $moduleName, 'iw' );
35
36        $this->urlUtils = $urlUtils;
37    }
38
39    public function execute() {
40        $pages = $this->getPageSet()->getGoodPages();
41        if ( $pages === [] ) {
42            return;
43        }
44
45        $params = $this->extractRequestParams();
46        $prop = array_fill_keys( (array)$params['prop'], true );
47
48        if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
49            $this->dieWithError(
50                [
51                    'apierror-invalidparammix-mustusewith',
52                    $this->encodeParamName( 'title' ),
53                    $this->encodeParamName( 'prefix' ),
54                ],
55                'invalidparammix'
56            );
57        }
58
59        // Handle deprecated param
60        $this->requireMaxOneParameter( $params, 'url', 'prop' );
61        if ( $params['url'] ) {
62            $prop = [ 'url' => 1 ];
63        }
64
65        $this->setVirtualDomain( InterwikiLinksTable::VIRTUAL_DOMAIN );
66
67        $this->addFields( [
68            'iwl_from',
69            'iwl_prefix',
70            'iwl_title'
71        ] );
72        $this->addTables( 'iwlinks' );
73        $this->addWhereFld( 'iwl_from', array_keys( $pages ) );
74
75        if ( $params['continue'] !== null ) {
76            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'string' ] );
77            $op = $params['dir'] == 'descending' ? '<=' : '>=';
78            $this->addWhere( $this->getDB()->buildComparison( $op, [
79                'iwl_from' => $cont[0],
80                'iwl_prefix' => $cont[1],
81                'iwl_title' => $cont[2],
82            ] ) );
83        }
84
85        $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
86        if ( isset( $params['prefix'] ) ) {
87            $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
88            if ( isset( $params['title'] ) ) {
89                $this->addWhereFld( 'iwl_title', $params['title'] );
90                $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
91            } else {
92                $this->addOption( 'ORDER BY', [
93                    'iwl_from' . $sort,
94                    'iwl_title' . $sort
95                ] );
96            }
97        } else {
98            // Don't order by iwl_from if it's constant in the WHERE clause
99            if ( count( $pages ) === 1 ) {
100                $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
101            } else {
102                $this->addOption( 'ORDER BY', [
103                    'iwl_from' . $sort,
104                    'iwl_prefix' . $sort,
105                    'iwl_title' . $sort
106                ] );
107            }
108        }
109
110        $this->addOption( 'LIMIT', $params['limit'] + 1 );
111        $res = $this->select( __METHOD__ );
112
113        $count = 0;
114        foreach ( $res as $row ) {
115            if ( ++$count > $params['limit'] ) {
116                // We've reached the one extra which shows that
117                // there are additional pages to be had. Stop here...
118                $this->setContinueEnumParameter(
119                    'continue',
120                    "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
121                );
122                break;
123            }
124            $entry = [ 'prefix' => $row->iwl_prefix ];
125
126            if ( isset( $prop['url'] ) ) {
127                $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
128                if ( $title ) {
129                    $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
130                }
131            }
132
133            ApiResult::setContentValue( $entry, 'title', $row->iwl_title );
134            $fit = $this->addPageSubItem( $row->iwl_from, $entry );
135            if ( !$fit ) {
136                $this->setContinueEnumParameter(
137                    'continue',
138                    "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
139                );
140                break;
141            }
142        }
143    }
144
145    /** @inheritDoc */
146    public function getCacheMode( $params ) {
147        return 'public';
148    }
149
150    /** @inheritDoc */
151    public function getAllowedParams() {
152        return [
153            'prop' => [
154                ParamValidator::PARAM_ISMULTI => true,
155                ParamValidator::PARAM_TYPE => [
156                    'url',
157                ],
158                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
159            ],
160            'prefix' => null,
161            'title' => null,
162            'dir' => [
163                ParamValidator::PARAM_DEFAULT => 'ascending',
164                ParamValidator::PARAM_TYPE => [
165                    'ascending',
166                    'descending'
167                ]
168            ],
169            'limit' => [
170                ParamValidator::PARAM_DEFAULT => 10,
171                ParamValidator::PARAM_TYPE => 'limit',
172                IntegerDef::PARAM_MIN => 1,
173                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
174                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
175            ],
176            'continue' => [
177                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
178            ],
179            'url' => [
180                ParamValidator::PARAM_DEFAULT => false,
181                ParamValidator::PARAM_DEPRECATED => true,
182            ],
183        ];
184    }
185
186    /** @inheritDoc */
187    protected function getExamplesMessages() {
188        $title = Title::newMainPage()->getPrefixedText();
189        $mp = rawurlencode( $title );
190
191        return [
192            "action=query&prop=iwlinks&titles={$mp}"
193                => 'apihelp-query+iwlinks-example-simple',
194        ];
195    }
196
197    /** @inheritDoc */
198    public function getHelpUrls() {
199        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks';
200    }
201}
202
203/** @deprecated class alias since 1.43 */
204class_alias( ApiQueryIWLinks::class, 'ApiQueryIWLinks' );