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