Call Linker methods statically
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
36
37 public function __construct( $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'eu' );
39 }
40
41 public function execute() {
42 $this->run();
43 }
44
45 public function getCacheMode( $params ) {
46 return 'public';
47 }
48
49 public function executeGenerator( $resultPageSet ) {
50 $this->run( $resultPageSet );
51 }
52
53 /**
54 * @param $resultPageSet ApiPageSet
55 * @return void
56 */
57 private function run( $resultPageSet = null ) {
58 $params = $this->extractRequestParams();
59
60 $query = $params['query'];
61 $protocol = self::getProtocolPrefix( $params['protocol'] );
62
63 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
64 $this->addOption( 'USE INDEX', 'el_index' );
65 $this->addWhere( 'page_id=el_from' );
66
67 global $wgMiserMode;
68 $miser_ns = array();
69 if ( $wgMiserMode ) {
70 $miser_ns = $params['namespace'];
71 } else {
72 $this->addWhereFld( 'page_namespace', $params['namespace'] );
73 }
74
75 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
76
77 if ( $whereQuery !== null ) {
78 $this->addWhere( $whereQuery );
79 }
80
81 $prop = array_flip( $params['prop'] );
82 $fld_ids = isset( $prop['ids'] );
83 $fld_title = isset( $prop['title'] );
84 $fld_url = isset( $prop['url'] );
85
86 if ( is_null( $resultPageSet ) ) {
87 $this->addFields( array(
88 'page_id',
89 'page_namespace',
90 'page_title'
91 ) );
92 $this->addFieldsIf( 'el_to', $fld_url );
93 } else {
94 $this->addFields( $resultPageSet->getPageTableFields() );
95 }
96
97 $limit = $params['limit'];
98 $offset = $params['offset'];
99 $this->addOption( 'LIMIT', $limit + 1 );
100 if ( isset( $offset ) ) {
101 $this->addOption( 'OFFSET', $offset );
102 }
103
104 $res = $this->select( __METHOD__ );
105
106 $result = $this->getResult();
107 $count = 0;
108 foreach ( $res as $row ) {
109 if ( ++ $count > $limit ) {
110 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
111 $this->setContinueEnumParameter( 'offset', $offset + $limit );
112 break;
113 }
114
115 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
116 continue;
117 }
118
119 if ( is_null( $resultPageSet ) ) {
120 $vals = array();
121 if ( $fld_ids ) {
122 $vals['pageid'] = intval( $row->page_id );
123 }
124 if ( $fld_title ) {
125 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
126 ApiQueryBase::addTitleInfo( $vals, $title );
127 }
128 if ( $fld_url ) {
129 // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
130 $vals['url'] = $row->el_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 );
179 }
180
181 public static function prepareProtocols() {
182 global $wgUrlProtocols;
183 $protocols = array( '' );
184 foreach ( $wgUrlProtocols as $p ) {
185 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
186 }
187 return $protocols;
188 }
189
190 public static function getProtocolPrefix( $protocol ) {
191 // Find the right prefix
192 global $wgUrlProtocols;
193 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
194 foreach ( $wgUrlProtocols as $p ) {
195 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
196 $protocol = $p;
197 break;
198 }
199 }
200
201 return $protocol;
202 } else {
203 return null;
204 }
205 }
206
207 public function getParamDescription() {
208 global $wgMiserMode;
209 $p = $this->getModulePrefix();
210 $desc = array(
211 'prop' => array(
212 'What pieces of information to include',
213 ' ids - Adds the ID of page',
214 ' title - Adds the title and namespace ID of the page',
215 ' url - Adds the URL used in the page',
216 ),
217 'offset' => 'Used for paging. Use the value returned for "continue"',
218 'protocol' => array(
219 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
220 "Leave both this and {$p}query empty to list all external links"
221 ),
222 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
223 'namespace' => 'The page namespace(s) to enumerate.',
224 'limit' => 'How many pages to return.'
225 );
226
227 if ( $wgMiserMode ) {
228 $desc['namespace'] = array(
229 $desc['namespace'],
230 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
231 'returned before continuing; in extreme cases, zero results may be returned',
232 );
233 }
234
235 return $desc;
236 }
237
238 public function getDescription() {
239 return 'Enumerate pages that contain a given URL';
240 }
241
242 public function getPossibleErrors() {
243 return array_merge( parent::getPossibleErrors(), array(
244 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
245 ) );
246 }
247
248 public function getExamples() {
249 return array(
250 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
251 );
252 }
253
254 public function getHelpUrls() {
255 return 'http://www.mediawiki.org/wiki/API:Exturlusage';
256 }
257
258 public function getVersion() {
259 return __CLASS__ . ': $Id$';
260 }
261 }