2007-07-07 09:35:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on July 7, 2007
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
|
|
|
|
require_once ('ApiQueryBase.php');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-07-07 09:35:05 +00:00
|
|
|
*/
|
|
|
|
|
class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
|
|
|
|
|
|
|
|
|
|
public function __construct($query, $moduleName) {
|
|
|
|
|
parent :: __construct($query, $moduleName, 'eu');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function executeGenerator($resultPageSet) {
|
|
|
|
|
$this->run($resultPageSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function run($resultPageSet = null) {
|
|
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
|
|
$protocol = $params['protocol'];
|
|
|
|
|
$query = $params['query'];
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-07 09:35:05 +00:00
|
|
|
// Find the right prefix
|
|
|
|
|
global $wgUrlProtocols;
|
2008-10-25 14:04:43 +00:00
|
|
|
if($protocol && !in_array($protocol, $wgUrlProtocols))
|
2008-04-04 11:51:55 +00:00
|
|
|
{
|
|
|
|
|
foreach ($wgUrlProtocols as $p) {
|
|
|
|
|
if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
|
|
|
|
|
$protocol = $p;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-07-07 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-07-15 22:03:34 +00:00
|
|
|
else
|
|
|
|
|
$protocol = null;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-04-04 11:51:55 +00:00
|
|
|
$db = $this->getDb();
|
2008-04-14 07:45:50 +00:00
|
|
|
$this->addTables(array('page','externallinks')); // must be in this order for 'USE INDEX'
|
2008-04-04 11:51:55 +00:00
|
|
|
$this->addOption('USE INDEX', 'el_index');
|
|
|
|
|
$this->addWhere('page_id=el_from');
|
|
|
|
|
$this->addWhereFld('page_namespace', $params['namespace']);
|
|
|
|
|
|
|
|
|
|
if(!is_null($query) || $query != '')
|
|
|
|
|
{
|
2008-07-15 22:03:34 +00:00
|
|
|
if(is_null($protocol))
|
|
|
|
|
$protocol = 'http://';
|
|
|
|
|
|
|
|
|
|
$likeQuery = LinkFilter::makeLike($query, $protocol);
|
2008-04-04 11:22:58 +00:00
|
|
|
if (!$likeQuery)
|
|
|
|
|
$this->dieUsage('Invalid query', 'bad_query');
|
|
|
|
|
$likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
|
2008-04-04 11:49:20 +00:00
|
|
|
$this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
|
2008-04-04 11:51:55 +00:00
|
|
|
}
|
2008-07-15 22:03:34 +00:00
|
|
|
else if(!is_null($protocol))
|
|
|
|
|
$this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
|
2008-04-04 11:49:20 +00:00
|
|
|
|
2007-07-07 09:35:05 +00:00
|
|
|
$prop = array_flip($params['prop']);
|
|
|
|
|
$fld_ids = isset($prop['ids']);
|
|
|
|
|
$fld_title = isset($prop['title']);
|
|
|
|
|
$fld_url = isset($prop['url']);
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-07 09:35:05 +00:00
|
|
|
if (is_null($resultPageSet)) {
|
|
|
|
|
$this->addFields(array (
|
|
|
|
|
'page_id',
|
|
|
|
|
'page_namespace',
|
|
|
|
|
'page_title'
|
|
|
|
|
));
|
2008-04-14 07:45:50 +00:00
|
|
|
$this->addFieldsIf('el_to', $fld_url);
|
2007-07-07 09:35:05 +00:00
|
|
|
} else {
|
|
|
|
|
$this->addFields($resultPageSet->getPageTableFields());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$limit = $params['limit'];
|
|
|
|
|
$offset = $params['offset'];
|
|
|
|
|
$this->addOption('LIMIT', $limit +1);
|
|
|
|
|
if (isset ($offset))
|
|
|
|
|
$this->addOption('OFFSET', $offset);
|
|
|
|
|
|
|
|
|
|
$res = $this->select(__METHOD__);
|
|
|
|
|
|
|
|
|
|
$data = array ();
|
|
|
|
|
$count = 0;
|
|
|
|
|
while ($row = $db->fetchObject($res)) {
|
|
|
|
|
if (++ $count > $limit) {
|
|
|
|
|
// We've reached the one extra which shows that there are additional pages to be had. Stop here...
|
2008-04-22 12:46:00 +00:00
|
|
|
$this->setContinueEnumParameter('offset', $offset+$limit);
|
2007-07-07 09:35:05 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_null($resultPageSet)) {
|
2007-07-14 19:04:31 +00:00
|
|
|
$vals = array();
|
|
|
|
|
if ($fld_ids)
|
|
|
|
|
$vals['pageid'] = intval($row->page_id);
|
|
|
|
|
if ($fld_title) {
|
|
|
|
|
$title = Title :: makeTitle($row->page_namespace, $row->page_title);
|
|
|
|
|
$vals['ns'] = intval($title->getNamespace());
|
|
|
|
|
$vals['title'] = $title->getPrefixedText();
|
2007-07-07 09:35:05 +00:00
|
|
|
}
|
2007-07-14 19:04:31 +00:00
|
|
|
if ($fld_url)
|
|
|
|
|
$vals['url'] = $row->el_to;
|
|
|
|
|
$data[] = $vals;
|
2007-07-07 09:35:05 +00:00
|
|
|
} else {
|
|
|
|
|
$resultPageSet->processDbRow($row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$db->freeResult($res);
|
|
|
|
|
|
|
|
|
|
if (is_null($resultPageSet)) {
|
|
|
|
|
$result = $this->getResult();
|
2007-08-09 08:41:58 +00:00
|
|
|
$result->setIndexedTagName($data, $this->getModulePrefix());
|
2007-07-07 09:35:05 +00:00
|
|
|
$result->addValue('query', $this->getModuleName(), $data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2007-07-07 09:35:05 +00:00
|
|
|
global $wgUrlProtocols;
|
2008-07-15 22:03:34 +00:00
|
|
|
$protocols = array('');
|
2007-07-07 09:35:05 +00:00
|
|
|
foreach ($wgUrlProtocols as $p) {
|
|
|
|
|
$protocols[] = substr($p, 0, strpos($p,':'));
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-07 09:35:05 +00:00
|
|
|
return array (
|
|
|
|
|
'prop' => array (
|
|
|
|
|
ApiBase :: PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase :: PARAM_DFLT => 'ids|title|url',
|
|
|
|
|
ApiBase :: PARAM_TYPE => array (
|
|
|
|
|
'ids',
|
|
|
|
|
'title',
|
|
|
|
|
'url'
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
'offset' => array (
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
|
|
|
|
'protocol' => array (
|
|
|
|
|
ApiBase :: PARAM_TYPE => $protocols,
|
2008-07-15 22:03:34 +00:00
|
|
|
ApiBase :: PARAM_DFLT => '',
|
2007-07-07 09:35:05 +00:00
|
|
|
),
|
|
|
|
|
'query' => null,
|
|
|
|
|
'namespace' => array (
|
|
|
|
|
ApiBase :: PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'namespace'
|
|
|
|
|
),
|
|
|
|
|
'limit' => array (
|
|
|
|
|
ApiBase :: PARAM_DFLT => 10,
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'limit',
|
|
|
|
|
ApiBase :: PARAM_MIN => 1,
|
|
|
|
|
ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
|
|
|
|
|
ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2007-07-07 09:35:05 +00:00
|
|
|
return array (
|
|
|
|
|
'prop' => 'What pieces of information to include',
|
|
|
|
|
'offset' => 'Used for paging. Use the value returned for "continue"',
|
2008-07-15 22:03:34 +00:00
|
|
|
'protocol' => array( 'Protocol of the url. If empty and euquery set, the protocol is http.',
|
|
|
|
|
'Leave both this and euquery empty to list all external links'),
|
|
|
|
|
'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
|
2007-07-07 09:35:05 +00:00
|
|
|
'namespace' => 'The page namespace(s) to enumerate.',
|
2008-07-22 13:26:15 +00:00
|
|
|
'limit' => 'How many pages to return.'
|
2007-07-07 09:35:05 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2007-07-07 09:35:05 +00:00
|
|
|
return 'Enumerate pages that contain a given URL';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getExamples() {
|
|
|
|
|
return array (
|
|
|
|
|
'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
2007-12-06 18:33:18 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2007-07-07 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
}
|