Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 115 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryIWLinks | |
0.00% |
0 / 115 |
|
0.00% |
0 / 6 |
462 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 73 |
|
0.00% |
0 / 1 |
272 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
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 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | * http://www.gnu.org/copyleft/gpl.html |
22 | * |
23 | * @file |
24 | */ |
25 | |
26 | use MediaWiki\Title\Title; |
27 | use MediaWiki\Utils\UrlUtils; |
28 | use Wikimedia\ParamValidator\ParamValidator; |
29 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
30 | |
31 | /** |
32 | * A query module to list all interwiki links on a page |
33 | * |
34 | * @ingroup API |
35 | */ |
36 | class ApiQueryIWLinks extends ApiQueryBase { |
37 | |
38 | private UrlUtils $urlUtils; |
39 | |
40 | /** |
41 | * @param ApiQuery $query |
42 | * @param string $moduleName |
43 | * @param UrlUtils $urlUtils |
44 | */ |
45 | public function __construct( ApiQuery $query, $moduleName, UrlUtils $urlUtils ) { |
46 | parent::__construct( $query, $moduleName, 'iw' ); |
47 | |
48 | $this->urlUtils = $urlUtils; |
49 | } |
50 | |
51 | public function execute() { |
52 | $pages = $this->getPageSet()->getGoodPages(); |
53 | if ( $pages === [] ) { |
54 | return; |
55 | } |
56 | |
57 | $params = $this->extractRequestParams(); |
58 | $prop = array_fill_keys( (array)$params['prop'], true ); |
59 | |
60 | if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) { |
61 | $this->dieWithError( |
62 | [ |
63 | 'apierror-invalidparammix-mustusewith', |
64 | $this->encodeParamName( 'title' ), |
65 | $this->encodeParamName( 'prefix' ), |
66 | ], |
67 | 'invalidparammix' |
68 | ); |
69 | } |
70 | |
71 | // Handle deprecated param |
72 | $this->requireMaxOneParameter( $params, 'url', 'prop' ); |
73 | if ( $params['url'] ) { |
74 | $prop = [ 'url' => 1 ]; |
75 | } |
76 | |
77 | $this->addFields( [ |
78 | 'iwl_from', |
79 | 'iwl_prefix', |
80 | 'iwl_title' |
81 | ] ); |
82 | |
83 | $this->addTables( 'iwlinks' ); |
84 | $this->addWhereFld( 'iwl_from', array_keys( $pages ) ); |
85 | |
86 | if ( $params['continue'] !== null ) { |
87 | $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'string' ] ); |
88 | $op = $params['dir'] == 'descending' ? '<=' : '>='; |
89 | $db = $this->getDB(); |
90 | $this->addWhere( $db->buildComparison( $op, [ |
91 | 'iwl_from' => $cont[0], |
92 | 'iwl_prefix' => $cont[1], |
93 | 'iwl_title' => $cont[2], |
94 | ] ) ); |
95 | } |
96 | |
97 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
98 | if ( isset( $params['prefix'] ) ) { |
99 | $this->addWhereFld( 'iwl_prefix', $params['prefix'] ); |
100 | if ( isset( $params['title'] ) ) { |
101 | $this->addWhereFld( 'iwl_title', $params['title'] ); |
102 | $this->addOption( 'ORDER BY', 'iwl_from' . $sort ); |
103 | } else { |
104 | $this->addOption( 'ORDER BY', [ |
105 | 'iwl_from' . $sort, |
106 | 'iwl_title' . $sort |
107 | ] ); |
108 | } |
109 | } else { |
110 | // Don't order by iwl_from if it's constant in the WHERE clause |
111 | if ( count( $pages ) === 1 ) { |
112 | $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort ); |
113 | } else { |
114 | $this->addOption( 'ORDER BY', [ |
115 | 'iwl_from' . $sort, |
116 | 'iwl_prefix' . $sort, |
117 | 'iwl_title' . $sort |
118 | ] ); |
119 | } |
120 | } |
121 | |
122 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
123 | $res = $this->select( __METHOD__ ); |
124 | |
125 | $count = 0; |
126 | foreach ( $res as $row ) { |
127 | if ( ++$count > $params['limit'] ) { |
128 | // We've reached the one extra which shows that |
129 | // there are additional pages to be had. Stop here... |
130 | $this->setContinueEnumParameter( |
131 | 'continue', |
132 | "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" |
133 | ); |
134 | break; |
135 | } |
136 | $entry = [ 'prefix' => $row->iwl_prefix ]; |
137 | |
138 | if ( isset( $prop['url'] ) ) { |
139 | $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" ); |
140 | if ( $title ) { |
141 | $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ); |
142 | } |
143 | } |
144 | |
145 | ApiResult::setContentValue( $entry, 'title', $row->iwl_title ); |
146 | $fit = $this->addPageSubItem( $row->iwl_from, $entry ); |
147 | if ( !$fit ) { |
148 | $this->setContinueEnumParameter( |
149 | 'continue', |
150 | "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" |
151 | ); |
152 | break; |
153 | } |
154 | } |
155 | } |
156 | |
157 | public function getCacheMode( $params ) { |
158 | return 'public'; |
159 | } |
160 | |
161 | public function getAllowedParams() { |
162 | return [ |
163 | 'prop' => [ |
164 | ParamValidator::PARAM_ISMULTI => true, |
165 | ParamValidator::PARAM_TYPE => [ |
166 | 'url', |
167 | ], |
168 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
169 | ], |
170 | 'prefix' => null, |
171 | 'title' => null, |
172 | 'dir' => [ |
173 | ParamValidator::PARAM_DEFAULT => 'ascending', |
174 | ParamValidator::PARAM_TYPE => [ |
175 | 'ascending', |
176 | 'descending' |
177 | ] |
178 | ], |
179 | 'limit' => [ |
180 | ParamValidator::PARAM_DEFAULT => 10, |
181 | ParamValidator::PARAM_TYPE => 'limit', |
182 | IntegerDef::PARAM_MIN => 1, |
183 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
184 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
185 | ], |
186 | 'continue' => [ |
187 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
188 | ], |
189 | 'url' => [ |
190 | ParamValidator::PARAM_DEFAULT => false, |
191 | ParamValidator::PARAM_DEPRECATED => true, |
192 | ], |
193 | ]; |
194 | } |
195 | |
196 | protected function getExamplesMessages() { |
197 | $title = Title::newMainPage()->getPrefixedText(); |
198 | $mp = rawurlencode( $title ); |
199 | |
200 | return [ |
201 | "action=query&prop=iwlinks&titles={$mp}" |
202 | => 'apihelp-query+iwlinks-example-simple', |
203 | ]; |
204 | } |
205 | |
206 | public function getHelpUrls() { |
207 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks'; |
208 | } |
209 | } |