Refactor out the duplication I left with a TODO in r79659
[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 * @para $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 $db = $this->getDB();
64 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
65 $this->addOption( 'USE INDEX', 'el_index' );
66 $this->addWhere( 'page_id=el_from' );
67 $this->addWhereFld( 'page_namespace', $params['namespace'] );
68
69 $whereQuery = $this->prepareUrlQuerySearchString( $db, $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 ( is_null( $resultPageSet ) ) {
110 $vals = array();
111 if ( $fld_ids ) {
112 $vals['pageid'] = intval( $row->page_id );
113 }
114 if ( $fld_title ) {
115 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116 ApiQueryBase::addTitleInfo( $vals, $title );
117 }
118 if ( $fld_url ) {
119 $vals['url'] = $row->el_to;
120 }
121 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
124 break;
125 }
126 } else {
127 $resultPageSet->processDbRow( $row );
128 }
129 }
130
131 if ( is_null( $resultPageSet ) ) {
132 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
133 $this->getModulePrefix() );
134 }
135 }
136
137 public function getAllowedParams() {
138 return array(
139 'prop' => array(
140 ApiBase::PARAM_ISMULTI => true,
141 ApiBase::PARAM_DFLT => 'ids|title|url',
142 ApiBase::PARAM_TYPE => array(
143 'ids',
144 'title',
145 'url'
146 )
147 ),
148 'offset' => array(
149 ApiBase::PARAM_TYPE => 'integer'
150 ),
151 'protocol' => array(
152 ApiBase::PARAM_TYPE => self::prepareProtocols(),
153 ApiBase::PARAM_DFLT => '',
154 ),
155 'query' => null,
156 'namespace' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_TYPE => 'namespace'
159 ),
160 'limit' => array(
161 ApiBase::PARAM_DFLT => 10,
162 ApiBase::PARAM_TYPE => 'limit',
163 ApiBase::PARAM_MIN => 1,
164 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
166 )
167 );
168 }
169
170 public static function prepareProtocols() {
171 global $wgUrlProtocols;
172 $protocols = array( '' );
173 foreach ( $wgUrlProtocols as $p ) {
174 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
175 }
176 return $protocols;
177 }
178
179 public static function getProtocolPrefix( $protocol ) {
180 // Find the right prefix
181 global $wgUrlProtocols;
182 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
183 foreach ( $wgUrlProtocols as $p ) {
184 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
185 $protocol = $p;
186 break;
187 }
188 }
189
190 return $protocol;
191 } else {
192 return null;
193 }
194 }
195
196 public function getParamDescription() {
197 $p = $this->getModulePrefix();
198 return array(
199 'prop' => array(
200 'What pieces of information to include',
201 ' ids - Adds the ID of page',
202 ' title - Adds the title and namespace ID of the page',
203 ' url - Adds the URL used in the page',
204 ),
205 'offset' => 'Used for paging. Use the value returned for "continue"',
206 'protocol' => array(
207 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
208 "Leave both this and {$p}query empty to list all external links"
209 ),
210 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
211 'namespace' => 'The page namespace(s) to enumerate.',
212 'limit' => 'How many pages to return.'
213 );
214 }
215
216 public function getDescription() {
217 return 'Enumerate pages that contain a given URL';
218 }
219
220 public function getPossibleErrors() {
221 return array_merge( parent::getPossibleErrors(), array(
222 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
223 ) );
224 }
225
226 protected function getExamples() {
227 return array(
228 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
229 );
230 }
231
232 public function getVersion() {
233 return __CLASS__ . ': $Id$';
234 }
235 }