Kill "* @return void"
[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 */
51 private function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
53
54 $query = $params['query'];
55 $protocol = self::getProtocolPrefix( $params['protocol'] );
56
57 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
58 $this->addOption( 'USE INDEX', 'el_index' );
59 $this->addWhere( 'page_id=el_from' );
60
61 global $wgMiserMode;
62 $miser_ns = array();
63 if ( $wgMiserMode ) {
64 $miser_ns = $params['namespace'];
65 } else {
66 $this->addWhereFld( 'page_namespace', $params['namespace'] );
67 }
68
69 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
70
71 if ( $whereQuery !== null ) {
72 $this->addWhere( $whereQuery );
73 }
74
75 $prop = array_flip( $params['prop'] );
76 $fld_ids = isset( $prop['ids'] );
77 $fld_title = isset( $prop['title'] );
78 $fld_url = isset( $prop['url'] );
79
80 if ( is_null( $resultPageSet ) ) {
81 $this->addFields( array(
82 'page_id',
83 'page_namespace',
84 'page_title'
85 ) );
86 $this->addFieldsIf( 'el_to', $fld_url );
87 } else {
88 $this->addFields( $resultPageSet->getPageTableFields() );
89 }
90
91 $limit = $params['limit'];
92 $offset = $params['offset'];
93 $this->addOption( 'LIMIT', $limit + 1 );
94 if ( isset( $offset ) ) {
95 $this->addOption( 'OFFSET', $offset );
96 }
97
98 $res = $this->select( __METHOD__ );
99
100 $result = $this->getResult();
101 $count = 0;
102 foreach ( $res as $row ) {
103 if ( ++ $count > $limit ) {
104 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
105 $this->setContinueEnumParameter( 'offset', $offset + $limit );
106 break;
107 }
108
109 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
110 continue;
111 }
112
113 if ( is_null( $resultPageSet ) ) {
114 $vals = array();
115 if ( $fld_ids ) {
116 $vals['pageid'] = intval( $row->page_id );
117 }
118 if ( $fld_title ) {
119 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
120 ApiQueryBase::addTitleInfo( $vals, $title );
121 }
122 if ( $fld_url ) {
123 // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
124 $vals['url'] = $row->el_to;
125 }
126 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
127 if ( !$fit ) {
128 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
129 break;
130 }
131 } else {
132 $resultPageSet->processDbRow( $row );
133 }
134 }
135
136 if ( is_null( $resultPageSet ) ) {
137 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
138 $this->getModulePrefix() );
139 }
140 }
141
142 public function getAllowedParams() {
143 return array(
144 'prop' => array(
145 ApiBase::PARAM_ISMULTI => true,
146 ApiBase::PARAM_DFLT => 'ids|title|url',
147 ApiBase::PARAM_TYPE => array(
148 'ids',
149 'title',
150 'url'
151 )
152 ),
153 'offset' => array(
154 ApiBase::PARAM_TYPE => 'integer'
155 ),
156 'protocol' => array(
157 ApiBase::PARAM_TYPE => self::prepareProtocols(),
158 ApiBase::PARAM_DFLT => '',
159 ),
160 'query' => null,
161 'namespace' => array(
162 ApiBase::PARAM_ISMULTI => true,
163 ApiBase::PARAM_TYPE => 'namespace'
164 ),
165 'limit' => array(
166 ApiBase::PARAM_DFLT => 10,
167 ApiBase::PARAM_TYPE => 'limit',
168 ApiBase::PARAM_MIN => 1,
169 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
170 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
171 )
172 );
173 }
174
175 public static function prepareProtocols() {
176 global $wgUrlProtocols;
177 $protocols = array( '' );
178 foreach ( $wgUrlProtocols as $p ) {
179 if ( $p !== '//' ) {
180 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
181 }
182 }
183 return $protocols;
184 }
185
186 public static function getProtocolPrefix( $protocol ) {
187 // Find the right prefix
188 global $wgUrlProtocols;
189 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
190 foreach ( $wgUrlProtocols as $p ) {
191 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
192 $protocol = $p;
193 break;
194 }
195 }
196
197 return $protocol;
198 } else {
199 return null;
200 }
201 }
202
203 public function getParamDescription() {
204 global $wgMiserMode;
205 $p = $this->getModulePrefix();
206 $desc = array(
207 'prop' => array(
208 'What pieces of information to include',
209 ' ids - Adds the ID of page',
210 ' title - Adds the title and namespace ID of the page',
211 ' url - Adds the URL used in the page',
212 ),
213 'offset' => 'Used for paging. Use the value returned for "continue"',
214 'protocol' => array(
215 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
216 "Leave both this and {$p}query empty to list all external links"
217 ),
218 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
219 'namespace' => 'The page namespace(s) to enumerate.',
220 'limit' => 'How many pages to return.'
221 );
222
223 if ( $wgMiserMode ) {
224 $desc['namespace'] = array(
225 $desc['namespace'],
226 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
227 'returned before continuing; in extreme cases, zero results may be returned',
228 );
229 }
230
231 return $desc;
232 }
233
234 public function getDescription() {
235 return 'Enumerate pages that contain a given URL';
236 }
237
238 public function getPossibleErrors() {
239 return array_merge( parent::getPossibleErrors(), array(
240 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
241 ) );
242 }
243
244 public function getExamples() {
245 return array(
246 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
247 );
248 }
249
250 public function getHelpUrls() {
251 return 'https://www.mediawiki.org/wiki/API:Exturlusage';
252 }
253
254 public function getVersion() {
255 return __CLASS__ . ': $Id$';
256 }
257 }