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