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