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