Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 137 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryExtLinksUsage | |
0.00% |
0 / 137 |
|
0.00% |
0 / 9 |
1056 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 82 |
|
0.00% |
0 / 1 |
506 | |||
setContinue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getAllowedParams | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
6 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @file |
22 | */ |
23 | |
24 | use MediaWiki\ExternalLinks\LinkFilter; |
25 | use MediaWiki\MainConfigNames; |
26 | use MediaWiki\Parser\Parser; |
27 | use MediaWiki\Title\Title; |
28 | use MediaWiki\Utils\UrlUtils; |
29 | use Wikimedia\ParamValidator\ParamValidator; |
30 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
31 | use Wikimedia\Rdbms\IExpression; |
32 | use Wikimedia\Rdbms\LikeValue; |
33 | |
34 | /** |
35 | * @ingroup API |
36 | */ |
37 | class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { |
38 | |
39 | private UrlUtils $urlUtils; |
40 | |
41 | /** |
42 | * @param ApiQuery $query |
43 | * @param string $moduleName |
44 | * @param UrlUtils $urlUtils |
45 | */ |
46 | public function __construct( ApiQuery $query, $moduleName, UrlUtils $urlUtils ) { |
47 | parent::__construct( $query, $moduleName, 'eu' ); |
48 | |
49 | $this->urlUtils = $urlUtils; |
50 | } |
51 | |
52 | public function execute() { |
53 | $this->run(); |
54 | } |
55 | |
56 | public function getCacheMode( $params ) { |
57 | return 'public'; |
58 | } |
59 | |
60 | public function executeGenerator( $resultPageSet ) { |
61 | $this->run( $resultPageSet ); |
62 | } |
63 | |
64 | /** |
65 | * @param ApiPageSet|null $resultPageSet |
66 | * @return void |
67 | */ |
68 | private function run( $resultPageSet = null ) { |
69 | $params = $this->extractRequestParams(); |
70 | $db = $this->getDB(); |
71 | |
72 | $query = $params['query']; |
73 | $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] ); |
74 | |
75 | $this->addTables( [ 'externallinks', 'page' ] ); |
76 | $this->addJoinConds( [ 'page' => [ 'JOIN', 'page_id=el_from' ] ] ); |
77 | $fields = [ 'el_to_domain_index', 'el_to_path' ]; |
78 | |
79 | $miser_ns = []; |
80 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
81 | $miser_ns = $params['namespace'] ?: []; |
82 | } else { |
83 | $this->addWhereFld( 'page_namespace', $params['namespace'] ); |
84 | } |
85 | if ( $query !== null && $query !== '' ) { |
86 | // Normalize query to match the normalization applied for the externallinks table |
87 | $query = Parser::normalizeLinkUrl( $query ); |
88 | $conds = LinkFilter::getQueryConditions( $query, [ |
89 | 'protocol' => $protocol, |
90 | 'oneWildcard' => true, |
91 | 'db' => $db |
92 | ] ); |
93 | if ( !$conds ) { |
94 | $this->dieWithError( 'apierror-badquery' ); |
95 | } |
96 | $this->addWhere( $conds ); |
97 | } else { |
98 | if ( $protocol !== null ) { |
99 | $this->addWhere( |
100 | $db->expr( 'el_to_domain_index', IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) ) |
101 | ); |
102 | } |
103 | } |
104 | $orderBy = [ 'el_id' ]; |
105 | |
106 | $this->addOption( 'ORDER BY', $orderBy ); |
107 | $this->addFields( $orderBy ); // Make sure |
108 | |
109 | $prop = array_fill_keys( $params['prop'], true ); |
110 | $fld_ids = isset( $prop['ids'] ); |
111 | $fld_title = isset( $prop['title'] ); |
112 | $fld_url = isset( $prop['url'] ); |
113 | |
114 | if ( $resultPageSet === null ) { |
115 | $this->addFields( [ |
116 | 'page_id', |
117 | 'page_namespace', |
118 | 'page_title' |
119 | ] ); |
120 | foreach ( $fields as $field ) { |
121 | $this->addFieldsIf( $field, $fld_url ); |
122 | } |
123 | } else { |
124 | $this->addFields( $resultPageSet->getPageTableFields() ); |
125 | } |
126 | |
127 | $limit = $params['limit']; |
128 | $this->addOption( 'LIMIT', $limit + 1 ); |
129 | |
130 | // T244254: Avoid MariaDB deciding to scan all of `page`. |
131 | $this->addOption( 'STRAIGHT_JOIN' ); |
132 | |
133 | if ( $params['continue'] !== null ) { |
134 | $cont = $this->parseContinueParamOrDie( $params['continue'], |
135 | array_fill( 0, count( $orderBy ), 'string' ) ); |
136 | $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) ); |
137 | $this->addWhere( $db->buildComparison( '>=', $conds ) ); |
138 | } |
139 | |
140 | $res = $this->select( __METHOD__ ); |
141 | |
142 | $result = $this->getResult(); |
143 | |
144 | if ( $resultPageSet === null ) { |
145 | $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ ); |
146 | } |
147 | |
148 | $count = 0; |
149 | foreach ( $res as $row ) { |
150 | if ( ++$count > $limit ) { |
151 | // We've reached the one extra which shows that there are |
152 | // additional pages to be had. Stop here... |
153 | $this->setContinue( $orderBy, $row ); |
154 | break; |
155 | } |
156 | |
157 | if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) { |
158 | continue; |
159 | } |
160 | |
161 | if ( $resultPageSet === null ) { |
162 | $vals = [ |
163 | ApiResult::META_TYPE => 'assoc', |
164 | ]; |
165 | if ( $fld_ids ) { |
166 | $vals['pageid'] = (int)$row->page_id; |
167 | } |
168 | if ( $fld_title ) { |
169 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
170 | ApiQueryBase::addTitleInfo( $vals, $title ); |
171 | } |
172 | if ( $fld_url ) { |
173 | $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path; |
174 | // expand protocol-relative urls |
175 | if ( $params['expandurl'] ) { |
176 | $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL ); |
177 | } |
178 | $vals['url'] = $to; |
179 | } |
180 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
181 | if ( !$fit ) { |
182 | $this->setContinue( $orderBy, $row ); |
183 | break; |
184 | } |
185 | } else { |
186 | $resultPageSet->processDbRow( $row ); |
187 | } |
188 | } |
189 | |
190 | if ( $resultPageSet === null ) { |
191 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], |
192 | $this->getModulePrefix() ); |
193 | } |
194 | } |
195 | |
196 | private function setContinue( $orderBy, $row ) { |
197 | $fields = []; |
198 | foreach ( $orderBy as $field ) { |
199 | $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] ); |
200 | } |
201 | $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) ); |
202 | } |
203 | |
204 | public function getAllowedParams() { |
205 | $ret = [ |
206 | 'prop' => [ |
207 | ParamValidator::PARAM_ISMULTI => true, |
208 | ParamValidator::PARAM_DEFAULT => 'ids|title|url', |
209 | ParamValidator::PARAM_TYPE => [ |
210 | 'ids', |
211 | 'title', |
212 | 'url' |
213 | ], |
214 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
215 | ], |
216 | 'continue' => [ |
217 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
218 | ], |
219 | 'protocol' => [ |
220 | ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(), |
221 | ParamValidator::PARAM_DEFAULT => '', |
222 | ], |
223 | 'query' => null, |
224 | 'namespace' => [ |
225 | ParamValidator::PARAM_ISMULTI => true, |
226 | ParamValidator::PARAM_TYPE => 'namespace' |
227 | ], |
228 | 'limit' => [ |
229 | ParamValidator::PARAM_DEFAULT => 10, |
230 | ParamValidator::PARAM_TYPE => 'limit', |
231 | IntegerDef::PARAM_MIN => 1, |
232 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
233 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
234 | ], |
235 | 'expandurl' => [ |
236 | ParamValidator::PARAM_TYPE => 'boolean', |
237 | ParamValidator::PARAM_DEFAULT => false, |
238 | ParamValidator::PARAM_DEPRECATED => true, |
239 | ], |
240 | ]; |
241 | |
242 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
243 | $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [ |
244 | 'api-help-param-limited-in-miser-mode', |
245 | ]; |
246 | } |
247 | |
248 | return $ret; |
249 | } |
250 | |
251 | protected function getExamplesMessages() { |
252 | return [ |
253 | 'action=query&list=exturlusage&euquery=www.mediawiki.org' |
254 | => 'apihelp-query+exturlusage-example-simple', |
255 | ]; |
256 | } |
257 | |
258 | public function getHelpUrls() { |
259 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage'; |
260 | } |
261 | } |