Merge "Ticking 'Search in all namespaces' in prefs should disable other checkboxes"
[lhc/web/wiklou.git] / includes / api / ApiQueryExtLinksUsage.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
31
32 public function __construct( $query, $moduleName ) {
33 parent::__construct( $query, $moduleName, 'eu' );
34 }
35
36 public function execute() {
37 $this->run();
38 }
39
40 public function getCacheMode( $params ) {
41 return 'public';
42 }
43
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
46 }
47
48 /**
49 * @param $resultPageSet ApiPageSet
50 * @return void
51 */
52 private function run( $resultPageSet = null ) {
53 $params = $this->extractRequestParams();
54
55 $query = $params['query'];
56 $protocol = self::getProtocolPrefix( $params['protocol'] );
57
58 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
59 $this->addOption( 'USE INDEX', 'el_index' );
60 $this->addWhere( 'page_id=el_from' );
61
62 global $wgMiserMode;
63 $miser_ns = array();
64 if ( $wgMiserMode ) {
65 $miser_ns = $params['namespace'];
66 } else {
67 $this->addWhereFld( 'page_namespace', $params['namespace'] );
68 }
69
70 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
71
72 if ( $whereQuery !== null ) {
73 $this->addWhere( $whereQuery );
74 }
75
76 $prop = array_flip( $params['prop'] );
77 $fld_ids = isset( $prop['ids'] );
78 $fld_title = isset( $prop['title'] );
79 $fld_url = isset( $prop['url'] );
80
81 if ( is_null( $resultPageSet ) ) {
82 $this->addFields( array(
83 'page_id',
84 'page_namespace',
85 'page_title'
86 ) );
87 $this->addFieldsIf( 'el_to', $fld_url );
88 } else {
89 $this->addFields( $resultPageSet->getPageTableFields() );
90 }
91
92 $limit = $params['limit'];
93 $offset = $params['offset'];
94 $this->addOption( 'LIMIT', $limit + 1 );
95 if ( isset( $offset ) ) {
96 $this->addOption( 'OFFSET', $offset );
97 }
98
99 $res = $this->select( __METHOD__ );
100
101 $result = $this->getResult();
102 $count = 0;
103 foreach ( $res as $row ) {
104 if ( ++$count > $limit ) {
105 // We've reached the one extra which shows that there are
106 // additional pages to be had. Stop here...
107 $this->setContinueEnumParameter( 'offset', $offset + $limit );
108 break;
109 }
110
111 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
112 continue;
113 }
114
115 if ( is_null( $resultPageSet ) ) {
116 $vals = array();
117 if ( $fld_ids ) {
118 $vals['pageid'] = intval( $row->page_id );
119 }
120 if ( $fld_title ) {
121 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
122 ApiQueryBase::addTitleInfo( $vals, $title );
123 }
124 if ( $fld_url ) {
125 $to = $row->el_to;
126 // expand protocol-relative urls
127 if ( $params['expandurl'] ) {
128 $to = wfExpandUrl( $to, PROTO_CANONICAL );
129 }
130 $vals['url'] = $to;
131 }
132 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
133 if ( !$fit ) {
134 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
135 break;
136 }
137 } else {
138 $resultPageSet->processDbRow( $row );
139 }
140 }
141
142 if ( is_null( $resultPageSet ) ) {
143 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
144 $this->getModulePrefix() );
145 }
146 }
147
148 public function getAllowedParams() {
149 return array(
150 'prop' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_DFLT => 'ids|title|url',
153 ApiBase::PARAM_TYPE => array(
154 'ids',
155 'title',
156 'url'
157 )
158 ),
159 'offset' => array(
160 ApiBase::PARAM_TYPE => 'integer'
161 ),
162 'protocol' => array(
163 ApiBase::PARAM_TYPE => self::prepareProtocols(),
164 ApiBase::PARAM_DFLT => '',
165 ),
166 'query' => null,
167 'namespace' => array(
168 ApiBase::PARAM_ISMULTI => true,
169 ApiBase::PARAM_TYPE => 'namespace'
170 ),
171 'limit' => array(
172 ApiBase::PARAM_DFLT => 10,
173 ApiBase::PARAM_TYPE => 'limit',
174 ApiBase::PARAM_MIN => 1,
175 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
176 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
177 ),
178 'expandurl' => false,
179 );
180 }
181
182 public static function prepareProtocols() {
183 global $wgUrlProtocols;
184 $protocols = array( '' );
185 foreach ( $wgUrlProtocols as $p ) {
186 if ( $p !== '//' ) {
187 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
188 }
189 }
190
191 return $protocols;
192 }
193
194 public static function getProtocolPrefix( $protocol ) {
195 // Find the right prefix
196 global $wgUrlProtocols;
197 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
198 foreach ( $wgUrlProtocols as $p ) {
199 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
200 $protocol = $p;
201 break;
202 }
203 }
204
205 return $protocol;
206 } else {
207 return null;
208 }
209 }
210
211 public function getParamDescription() {
212 global $wgMiserMode;
213 $p = $this->getModulePrefix();
214 $desc = array(
215 'prop' => array(
216 'What pieces of information to include',
217 ' ids - Adds the ID of page',
218 ' title - Adds the title and namespace ID of the page',
219 ' url - Adds the URL used in the page',
220 ),
221 'offset' => 'Used for paging. Use the value returned for "continue"',
222 'protocol' => array(
223 "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
224 "Leave both this and {$p}query empty to list all external links"
225 ),
226 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. ' .
227 'Leave empty to list all external links',
228 'namespace' => 'The page namespace(s) to enumerate.',
229 'limit' => 'How many pages to return.',
230 'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
231 );
232
233 if ( $wgMiserMode ) {
234 $desc['namespace'] = array(
235 $desc['namespace'],
236 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
237 'returned before continuing; in extreme cases, zero results may be returned',
238 );
239 }
240
241 return $desc;
242 }
243
244 public function getResultProperties() {
245 return array(
246 'ids' => array(
247 'pageid' => 'integer'
248 ),
249 'title' => array(
250 'ns' => 'namespace',
251 'title' => 'string'
252 ),
253 'url' => array(
254 'url' => 'string'
255 )
256 );
257 }
258
259 public function getDescription() {
260 return 'Enumerate pages that contain a given URL';
261 }
262
263 public function getPossibleErrors() {
264 return array_merge( parent::getPossibleErrors(), array(
265 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
266 ) );
267 }
268
269 public function getExamples() {
270 return array(
271 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
272 );
273 }
274
275 public function getHelpUrls() {
276 return 'https://www.mediawiki.org/wiki/API:Exturlusage';
277 }
278 }