4 * Created on July 7, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
34 class ApiQueryExtLinksUsage
extends ApiQueryGeneratorBase
{
36 public function __construct($query, $moduleName) {
37 parent
:: __construct($query, $moduleName, 'eu');
40 public function execute() {
44 public function executeGenerator($resultPageSet) {
45 $this->run($resultPageSet);
48 private function run($resultPageSet = null) {
50 $params = $this->extractRequestParams();
52 $protocol = $params['protocol'];
53 $query = $params['query'];
55 // Find the right prefix
56 global $wgUrlProtocols;
57 if($protocol && !in_array($protocol, $wgUrlProtocols))
59 foreach ($wgUrlProtocols as $p) {
60 if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
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']);
75 if(!is_null($query) ||
$query != '')
77 if(is_null($protocol))
78 $protocol = 'http://';
80 $likeQuery = LinkFilter
::makeLike($query, $protocol);
82 $this->dieUsage('Invalid query', 'bad_query');
83 $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+
1);
84 $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
86 else if(!is_null($protocol))
87 $this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
89 $prop = array_flip($params['prop']);
90 $fld_ids = isset($prop['ids']);
91 $fld_title = isset($prop['title']);
92 $fld_url = isset($prop['url']);
94 if (is_null($resultPageSet)) {
95 $this->addFields(array (
100 $this->addFieldsIf('el_to', $fld_url);
102 $this->addFields($resultPageSet->getPageTableFields());
105 $limit = $params['limit'];
106 $offset = $params['offset'];
107 $this->addOption('LIMIT', $limit +
1);
109 $this->addOption('OFFSET', $offset);
111 $res = $this->select(__METHOD__
);
113 $result = $this->getResult();
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);
122 if (is_null($resultPageSet)) {
125 $vals['pageid'] = intval($row->page_id
);
127 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
128 ApiQueryBase
::addTitleInfo($vals, $title);
131 $vals['url'] = $row->el_to
;
132 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
135 $this->setContinueEnumParameter('offset', $offset +
$count - 1);
139 $resultPageSet->processDbRow($row);
142 $db->freeResult($res);
144 if (is_null($resultPageSet)) {
145 $result->setIndexedTagName_internal(array('query', $this->getModuleName()),
146 $this->getModulePrefix());
150 public function getAllowedParams() {
151 global $wgUrlProtocols;
152 $protocols = array('');
153 foreach ($wgUrlProtocols as $p) {
154 $protocols[] = substr($p, 0, strpos($p,':'));
159 ApiBase
:: PARAM_ISMULTI
=> true,
160 ApiBase
:: PARAM_DFLT
=> 'ids|title|url',
161 ApiBase
:: PARAM_TYPE
=> array (
168 ApiBase
:: PARAM_TYPE
=> 'integer'
170 'protocol' => array (
171 ApiBase
:: PARAM_TYPE
=> $protocols,
172 ApiBase
:: PARAM_DFLT
=> '',
175 'namespace' => array (
176 ApiBase
:: PARAM_ISMULTI
=> true,
177 ApiBase
:: PARAM_TYPE
=> 'namespace'
180 ApiBase
:: PARAM_DFLT
=> 10,
181 ApiBase
:: PARAM_TYPE
=> 'limit',
182 ApiBase
:: PARAM_MIN
=> 1,
183 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
184 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
189 public function getParamDescription() {
191 'prop' => 'What pieces of information to include',
192 'offset' => 'Used for paging. Use the value returned for "continue"',
193 'protocol' => array( 'Protocol of the url. If empty and euquery set, the protocol is http.',
194 'Leave both this and euquery empty to list all external links'),
195 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
196 'namespace' => 'The page namespace(s) to enumerate.',
197 'limit' => 'How many pages to return.'
201 public function getDescription() {
202 return 'Enumerate pages that contain a given URL';
205 protected function getExamples() {
207 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
211 public function getVersion() {
212 return __CLASS__
. ': $Id$';