* (bug 23473) - Give description of properties on all modules
[lhc/web/wiklou.git] / includes / api / ApiQueryExtLinksUsage.php
1 <?php
2
3 /**
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'eu' );
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
46 }
47
48 private function run( $resultPageSet = null ) {
49 $params = $this->extractRequestParams();
50
51 $protocol = $params['protocol'];
52 $query = $params['query'];
53
54 // Find the right prefix
55 global $wgUrlProtocols;
56 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
57 foreach ( $wgUrlProtocols as $p ) {
58 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
59 $protocol = $p;
60 break;
61 }
62 }
63 } else {
64 $protocol = null;
65 }
66
67 $db = $this->getDB();
68 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
69 $this->addOption( 'USE INDEX', 'el_index' );
70 $this->addWhere( 'page_id=el_from' );
71 $this->addWhereFld( 'page_namespace', $params['namespace'] );
72
73 if ( !is_null( $query ) || $query != '' ) {
74 if ( is_null( $protocol ) ) {
75 $protocol = 'http://';
76 }
77
78 $likeQuery = LinkFilter::makeLikeArray( $query, $protocol );
79 if ( !$likeQuery ) {
80 $this->dieUsage( 'Invalid query', 'bad_query' );
81 }
82
83 $likeQuery = LinkFilter::keepOneWildcard( $likeQuery );
84 $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) );
85 } elseif ( !is_null( $protocol ) ) {
86 $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) );
87 }
88
89 $prop = array_flip( $params['prop'] );
90 $fld_ids = isset( $prop['ids'] );
91 $fld_title = isset( $prop['title'] );
92 $fld_url = isset( $prop['url'] );
93
94 if ( is_null( $resultPageSet ) ) {
95 $this->addFields( array(
96 'page_id',
97 'page_namespace',
98 'page_title'
99 ) );
100 $this->addFieldsIf( 'el_to', $fld_url );
101 } else {
102 $this->addFields( $resultPageSet->getPageTableFields() );
103 }
104
105 $limit = $params['limit'];
106 $offset = $params['offset'];
107 $this->addOption( 'LIMIT', $limit + 1 );
108 if ( isset( $offset ) ) {
109 $this->addOption( 'OFFSET', $offset );
110 }
111
112 $res = $this->select( __METHOD__ );
113
114 $result = $this->getResult();
115 $count = 0;
116 foreach ( $res as $row ) {
117 if ( ++ $count > $limit ) {
118 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
119 $this->setContinueEnumParameter( 'offset', $offset + $limit );
120 break;
121 }
122
123 if ( is_null( $resultPageSet ) ) {
124 $vals = array();
125 if ( $fld_ids ) {
126 $vals['pageid'] = intval( $row->page_id );
127 }
128 if ( $fld_title ) {
129 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
130 ApiQueryBase::addTitleInfo( $vals, $title );
131 }
132 if ( $fld_url ) {
133 $vals['url'] = $row->el_to;
134 }
135 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
136 if ( !$fit ) {
137 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
138 break;
139 }
140 } else {
141 $resultPageSet->processDbRow( $row );
142 }
143 }
144
145 if ( is_null( $resultPageSet ) ) {
146 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
147 $this->getModulePrefix() );
148 }
149 }
150
151 public function getAllowedParams() {
152 global $wgUrlProtocols;
153 $protocols = array( '' );
154 foreach ( $wgUrlProtocols as $p ) {
155 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
156 }
157
158 return array(
159 'prop' => array(
160 ApiBase::PARAM_ISMULTI => true,
161 ApiBase::PARAM_DFLT => 'ids|title|url',
162 ApiBase::PARAM_TYPE => array(
163 'ids',
164 'title',
165 'url'
166 )
167 ),
168 'offset' => array(
169 ApiBase::PARAM_TYPE => 'integer'
170 ),
171 'protocol' => array(
172 ApiBase::PARAM_TYPE => $protocols,
173 ApiBase::PARAM_DFLT => '',
174 ),
175 'query' => null,
176 'namespace' => array(
177 ApiBase::PARAM_ISMULTI => true,
178 ApiBase::PARAM_TYPE => 'namespace'
179 ),
180 'limit' => array(
181 ApiBase::PARAM_DFLT => 10,
182 ApiBase::PARAM_TYPE => 'limit',
183 ApiBase::PARAM_MIN => 1,
184 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
185 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
186 )
187 );
188 }
189
190 public function getParamDescription() {
191 $p = $this->getModulePrefix();
192 return array(
193 'prop' => array(
194 'What pieces of information to include',
195 ' ids - Adds the id of page',
196 ' title - Adds the title and namespace id of the page',
197 ' url - Adds the URL used in the page',
198 ),
199 'offset' => 'Used for paging. Use the value returned for "continue"',
200 'protocol' => array(
201 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
202 "Leave both this and {$p}query empty to list all external links"
203 ),
204 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
205 'namespace' => 'The page namespace(s) to enumerate.',
206 'limit' => 'How many pages to return.'
207 );
208 }
209
210 public function getDescription() {
211 return 'Enumerate pages that contain a given URL';
212 }
213
214 public function getPossibleErrors() {
215 return array_merge( parent::getPossibleErrors(), array(
216 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
217 ) );
218 }
219
220 protected function getExamples() {
221 return array(
222 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
223 );
224 }
225
226 public function getVersion() {
227 return __CLASS__ . ': $Id$';
228 }
229 }