]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryExtLinksUsage.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryExtLinksUsage.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
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                 global $wgUrlProtocols;
158                 $protocols = array( '' );
159                 foreach ( $wgUrlProtocols as $p ) {
160                         $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
161                 }
162
163                 return array(
164                         'prop' => array(
165                                 ApiBase::PARAM_ISMULTI => true,
166                                 ApiBase::PARAM_DFLT => 'ids|title|url',
167                                 ApiBase::PARAM_TYPE => array(
168                                         'ids',
169                                         'title',
170                                         'url'
171                                 )
172                         ),
173                         'offset' => array(
174                                 ApiBase::PARAM_TYPE => 'integer'
175                         ),
176                         'protocol' => array(
177                                 ApiBase::PARAM_TYPE => $protocols,
178                                 ApiBase::PARAM_DFLT => '',
179                         ),
180                         'query' => null,
181                         'namespace' => array(
182                                 ApiBase::PARAM_ISMULTI => true,
183                                 ApiBase::PARAM_TYPE => 'namespace'
184                         ),
185                         'limit' => array(
186                                 ApiBase::PARAM_DFLT => 10,
187                                 ApiBase::PARAM_TYPE => 'limit',
188                                 ApiBase::PARAM_MIN => 1,
189                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
190                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
191                         )
192                 );
193         }
194
195         public function getParamDescription() {
196                 $p = $this->getModulePrefix();
197                 return array(
198                         'prop' => array(
199                                 'What pieces of information to include',
200                                 ' ids    - Adds the id of page',
201                                 ' title  - Adds the title and namespace id of the page',
202                                 ' url    - Adds the URL used in the page',
203                         ),
204                         'offset' => 'Used for paging. Use the value returned for "continue"',
205                         'protocol' => array(
206                                 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
207                                 "Leave both this and {$p}query empty to list all external links"
208                         ),
209                         'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
210                         'namespace' => 'The page namespace(s) to enumerate.',
211                         'limit' => 'How many pages to return.'
212                 );
213         }
214
215         public function getDescription() {
216                 return 'Enumerate pages that contain a given URL';
217         }
218
219         public function getPossibleErrors() {
220                 return array_merge( parent::getPossibleErrors(), array(
221                         array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
222                 ) );
223         }
224
225         protected function getExamples() {
226                 return array(
227                         'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
228                 );
229         }
230
231         public function getVersion() {
232                 return __CLASS__ . ': $Id$';
233         }
234 }