]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryExtLinksUsage.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.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 (C) 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  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
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                 {
63                         foreach ($wgUrlProtocols as $p) {
64                                 if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
65                                         $protocol = $p;
66                                         break;
67                                 }
68                         }
69                 }
70                 else
71                         $protocol = null;
72
73                 $db = $this->getDB();
74                 $this->addTables(array('page','externallinks'));        // must be in this order for 'USE INDEX'
75                 $this->addOption('USE INDEX', 'el_index');
76                 $this->addWhere('page_id=el_from');
77                 $this->addWhereFld('page_namespace', $params['namespace']);
78
79                 if(!is_null($query) || $query != '')
80                 {
81                         if(is_null($protocol))
82                                 $protocol = 'http://';
83
84                         $likeQuery = LinkFilter::makeLike($query, $protocol);
85                         if (!$likeQuery)
86                                 $this->dieUsage('Invalid query', 'bad_query');
87                         $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
88                         $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
89                 }
90                 else if(!is_null($protocol))
91                         $this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
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                 $res = $this->select(__METHOD__);
116
117                 $result = $this->getResult();
118                 $count = 0;
119                 while ($row = $db->fetchObject($res)) {
120                         if (++ $count > $limit) {
121                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
122                                 $this->setContinueEnumParameter('offset', $offset+$limit);
123                                 break;
124                         }
125
126                         if (is_null($resultPageSet)) {
127                                 $vals = array();
128                                 if ($fld_ids)
129                                         $vals['pageid'] = intval($row->page_id);
130                                 if ($fld_title) {
131                                         $title = Title :: makeTitle($row->page_namespace, $row->page_title);
132                                         ApiQueryBase::addTitleInfo($vals, $title);
133                                 }
134                                 if ($fld_url)
135                                         $vals['url'] = $row->el_to;
136                                 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
137                                 if(!$fit)
138                                 {
139                                         $this->setContinueEnumParameter('offset', $offset + $count - 1);
140                                         break;
141                                 }
142                         } else {
143                                 $resultPageSet->processDbRow($row);
144                         }
145                 }
146                 $db->freeResult($res);
147
148                 if (is_null($resultPageSet)) {
149                         $result->setIndexedTagName_internal(array('query', $this->getModuleName()),
150                                         $this->getModulePrefix());
151                 }
152         }
153
154         public function getAllowedParams() {
155                 global $wgUrlProtocols;
156                 $protocols = array('');
157                 foreach ($wgUrlProtocols as $p) {
158                         $protocols[] = substr($p, 0, strpos($p,':'));
159                 }
160
161                 return array (
162                         'prop' => array (
163                                 ApiBase :: PARAM_ISMULTI => true,
164                                 ApiBase :: PARAM_DFLT => 'ids|title|url',
165                                 ApiBase :: PARAM_TYPE => array (
166                                         'ids',
167                                         'title',
168                                         'url'
169                                 )
170                         ),
171                         'offset' => array (
172                                 ApiBase :: PARAM_TYPE => 'integer'
173                         ),
174                         'protocol' => array (
175                                 ApiBase :: PARAM_TYPE => $protocols,
176                                 ApiBase :: PARAM_DFLT => '',
177                         ),
178                         'query' => null,
179                         'namespace' => array (
180                                 ApiBase :: PARAM_ISMULTI => true,
181                                 ApiBase :: PARAM_TYPE => 'namespace'
182                         ),
183                         'limit' => array (
184                                 ApiBase :: PARAM_DFLT => 10,
185                                 ApiBase :: PARAM_TYPE => 'limit',
186                                 ApiBase :: PARAM_MIN => 1,
187                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
188                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
189                         )
190                 );
191         }
192
193         public function getParamDescription() {
194                 return array (
195                         'prop' => 'What pieces of information to include',
196                         'offset' => 'Used for paging. Use the value returned for "continue"',
197                         'protocol' => array(    'Protocol of the url. If empty and euquery set, the protocol is http.',
198                                                 'Leave both this and euquery empty to list all external links'),
199                         'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
200                         'namespace' => 'The page namespace(s) to enumerate.',
201                         'limit' => 'How many pages to return.'
202                 );
203         }
204
205         public function getDescription() {
206                 return 'Enumerate pages that contain a given URL';
207         }
208
209         protected function getExamples() {
210                 return array (
211                         'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
212                 );
213         }
214
215         public function getVersion() {
216                 return __CLASS__ . ': $Id: ApiQueryExtLinksUsage.php 69986 2010-07-27 03:57:39Z tstarling $';
217         }
218 }