]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryExtLinksUsage.php
MediaWiki 1.11.0
[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  * @addtogroup 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                 if (is_null($query))
55                         $this->dieUsage('Missing required query parameter', 'params');
56                 
57                 // Find the right prefix
58                 global $wgUrlProtocols;
59                 foreach ($wgUrlProtocols as $p) {
60                         if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
61                                 $protocol = $p;
62                                 break;
63                         }
64                 }
65                 
66                 $likeQuery = LinkFilter::makeLike($query , $protocol);
67                 if (!$likeQuery)
68                         $this->dieUsage('Invalid query', 'bad_query');
69                 $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
70
71                 $this->addTables(array('page','externallinks'));        // must be in this order for 'USE INDEX' 
72                 $this->addOption('USE INDEX', 'el_index');
73
74                 $db = $this->getDB();
75                 $this->addWhere('page_id=el_from');
76                 $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
77                 $this->addWhereFld('page_namespace', $params['namespace']);
78
79                 $prop = array_flip($params['prop']);
80                 $fld_ids = isset($prop['ids']);
81                 $fld_title = isset($prop['title']);
82                 $fld_url = isset($prop['url']);
83                 
84                 if (is_null($resultPageSet)) {
85                         $this->addFields(array (
86                                 'page_id',
87                                 'page_namespace',
88                                 'page_title'
89                         ));
90                         $this->addFieldsIf('el_to', $fld_url);                  
91                 } else {
92                         $this->addFields($resultPageSet->getPageTableFields());
93                 }
94
95                 $limit = $params['limit'];
96                 $offset = $params['offset'];
97                 $this->addOption('LIMIT', $limit +1);
98                 if (isset ($offset))
99                         $this->addOption('OFFSET', $offset);
100
101                 $res = $this->select(__METHOD__);
102
103                 $data = array ();
104                 $count = 0;
105                 while ($row = $db->fetchObject($res)) {
106                         if (++ $count > $limit) {
107                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
108                                 $this->setContinueEnumParameter('offset', $offset+$limit+1);
109                                 break;
110                         }
111
112                         if (is_null($resultPageSet)) {
113                                 $vals = array();
114                                 if ($fld_ids)
115                                         $vals['pageid'] = intval($row->page_id);
116                                 if ($fld_title) {
117                                         $title = Title :: makeTitle($row->page_namespace, $row->page_title);
118                                         $vals['ns'] = intval($title->getNamespace());
119                                         $vals['title'] = $title->getPrefixedText();
120                                 }
121                                 if ($fld_url)
122                                         $vals['url'] = $row->el_to;
123                                 $data[] = $vals;
124                         } else {
125                                 $resultPageSet->processDbRow($row);
126                         }
127                 }
128                 $db->freeResult($res);
129
130                 if (is_null($resultPageSet)) {
131                         $result = $this->getResult();
132                         $result->setIndexedTagName($data, $this->getModulePrefix());
133                         $result->addValue('query', $this->getModuleName(), $data);
134                 }
135         }
136
137         protected function getAllowedParams() {
138                 global $wgUrlProtocols;
139                 $protocols = array();
140                 foreach ($wgUrlProtocols as $p) {
141                         $protocols[] = substr($p, 0, strpos($p,':'));
142                 }
143                 
144                 return array (
145                         'prop' => array (
146                                 ApiBase :: PARAM_ISMULTI => true,
147                                 ApiBase :: PARAM_DFLT => 'ids|title|url',
148                                 ApiBase :: PARAM_TYPE => array (
149                                         'ids',
150                                         'title',
151                                         'url'
152                                 )
153                         ),
154                         'offset' => array (
155                                 ApiBase :: PARAM_TYPE => 'integer'
156                         ),
157                         'protocol' => array (
158                                 ApiBase :: PARAM_TYPE => $protocols,
159                                 ApiBase :: PARAM_DFLT => 'http',
160                         ),
161                         'query' => null,
162                         'namespace' => array (
163                                 ApiBase :: PARAM_ISMULTI => true,
164                                 ApiBase :: PARAM_TYPE => 'namespace'
165                         ),
166                         'limit' => array (
167                                 ApiBase :: PARAM_DFLT => 10,
168                                 ApiBase :: PARAM_TYPE => 'limit',
169                                 ApiBase :: PARAM_MIN => 1,
170                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
171                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
172                         )
173                 );
174         }
175
176         protected function getParamDescription() {
177                 return array (
178                         'prop' => 'What pieces of information to include',
179                         'offset' => 'Used for paging. Use the value returned for "continue"',
180                         'protocol' => 'Protocol of the url',
181                         'query' => 'Search string without protocol. See [[Special:LinkSearch]]',
182                         'namespace' => 'The page namespace(s) to enumerate.',
183                         'limit' => 'How many entries to return.'
184                 );
185         }
186
187         protected function getDescription() {
188                 return 'Enumerate pages that contain a given URL';
189         }
190
191         protected function getExamples() {
192                 return array (
193                         'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
194                 );
195         }
196
197         public function getVersion() {
198                 return __CLASS__ . ': $Id: ApiQueryExtLinksUsage.php 24694 2007-08-09 08:41:58Z yurik $';
199         }
200 }