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