2007-05-14 05:28:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on May 12, 2007
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-05-20 23:31:44 +00:00
|
|
|
* Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2007-05-14 05:28:06 +00:00
|
|
|
*
|
|
|
|
|
* 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2007-05-20 23:31:44 +00:00
|
|
|
* A query module to list all wiki links on a given set of pages.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2007-05-14 05:28:06 +00:00
|
|
|
* @addtogroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiQueryLinks extends ApiQueryGeneratorBase {
|
|
|
|
|
|
|
|
|
|
const LINKS = 'links';
|
|
|
|
|
const TEMPLATES = 'templates';
|
|
|
|
|
|
|
|
|
|
private $table, $prefix, $description;
|
|
|
|
|
|
|
|
|
|
public function __construct($query, $moduleName) {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-14 05:28:06 +00:00
|
|
|
switch ($moduleName) {
|
|
|
|
|
case self::LINKS :
|
|
|
|
|
$this->table = 'pagelinks';
|
|
|
|
|
$this->prefix = 'pl';
|
|
|
|
|
$this->description = 'link';
|
|
|
|
|
break;
|
|
|
|
|
case self::TEMPLATES :
|
|
|
|
|
$this->table = 'templatelinks';
|
|
|
|
|
$this->prefix = 'tl';
|
|
|
|
|
$this->description = 'template';
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Unknown module name');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent :: __construct($query, $moduleName, $this->prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function executeGenerator($resultPageSet) {
|
|
|
|
|
$this->run($resultPageSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function run($resultPageSet = null) {
|
|
|
|
|
|
2007-05-19 22:56:42 +00:00
|
|
|
if ($this->getPageSet()->getGoodTitleCount() == 0)
|
|
|
|
|
return; // nothing to do
|
|
|
|
|
|
2007-06-17 08:50:35 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2007-05-14 05:28:06 +00:00
|
|
|
$this->addFields(array (
|
|
|
|
|
$this->prefix . '_from pl_from',
|
|
|
|
|
$this->prefix . '_namespace pl_namespace',
|
|
|
|
|
$this->prefix . '_title pl_title'
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->addTables($this->table);
|
|
|
|
|
$this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
|
2007-06-17 08:50:35 +00:00
|
|
|
$this->addWhereFld($this->prefix . '_namespace', $params['namespace']);
|
2008-05-13 10:39:01 +00:00
|
|
|
|
|
|
|
|
# Here's some MySQL craziness going on: if you use WHERE foo='bar'
|
|
|
|
|
# and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
|
|
|
|
|
# but instead goes and filesorts, because the index for foo was used
|
|
|
|
|
# already. To work around this, we drop constant fields in the WHERE
|
|
|
|
|
# clause from the ORDER BY clause
|
|
|
|
|
$order = array();
|
|
|
|
|
if(count($this->getPageSet()->getGoodTitles()) != 1)
|
|
|
|
|
$order[] = "{$this->prefix}_from";
|
|
|
|
|
// pl_namespace is always constant
|
|
|
|
|
$order[] = "{$this->prefix}_title";
|
|
|
|
|
$this->addOption('ORDER BY', implode(", ", $order));
|
2007-05-14 05:28:06 +00:00
|
|
|
|
|
|
|
|
$db = $this->getDB();
|
|
|
|
|
$res = $this->select(__METHOD__);
|
|
|
|
|
|
|
|
|
|
if (is_null($resultPageSet)) {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-14 05:28:06 +00:00
|
|
|
$data = array();
|
2008-04-14 07:45:50 +00:00
|
|
|
$lastId = 0; // database has no ID 0
|
2007-05-14 05:28:06 +00:00
|
|
|
while ($row = $db->fetchObject($res)) {
|
|
|
|
|
if ($lastId != $row->pl_from) {
|
|
|
|
|
if($lastId != 0) {
|
|
|
|
|
$this->addPageSubItems($lastId, $data);
|
|
|
|
|
$data = array();
|
|
|
|
|
}
|
|
|
|
|
$lastId = $row->pl_from;
|
|
|
|
|
}
|
2007-05-19 22:56:42 +00:00
|
|
|
|
|
|
|
|
$vals = array();
|
2007-07-14 19:04:31 +00:00
|
|
|
ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->pl_namespace, $row->pl_title));
|
2007-05-19 22:56:42 +00:00
|
|
|
$data[] = $vals;
|
2007-05-14 05:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($lastId != 0) {
|
|
|
|
|
$this->addPageSubItems($lastId, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
$titles = array();
|
|
|
|
|
while ($row = $db->fetchObject($res)) {
|
2007-07-14 19:04:31 +00:00
|
|
|
$titles[] = Title :: makeTitle($row->pl_namespace, $row->pl_title);
|
2007-05-14 05:28:06 +00:00
|
|
|
}
|
|
|
|
|
$resultPageSet->populateFromTitles($titles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db->freeResult($res);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams()
|
2007-06-17 08:50:35 +00:00
|
|
|
{
|
|
|
|
|
return array(
|
|
|
|
|
'namespace' => array(
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'namespace',
|
|
|
|
|
ApiBase :: PARAM_ISMULTI => true
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription()
|
2007-06-17 08:50:35 +00:00
|
|
|
{
|
|
|
|
|
return array(
|
|
|
|
|
'namespace' => "Show {$this->description}s in this namespace(s) only"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2007-05-14 05:28:06 +00:00
|
|
|
return "Returns all {$this->description}s from the given page(s)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getExamples() {
|
|
|
|
|
return array (
|
|
|
|
|
"Get {$this->description}s from the [[Main Page]]:",
|
|
|
|
|
" api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
|
|
|
|
|
"Get information about the {$this->description} pages in the [[Main Page]]:",
|
2007-06-17 08:50:35 +00:00
|
|
|
" api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
|
|
|
|
|
"Get {$this->description}s from the Main Page in the User and Template namespaces:",
|
|
|
|
|
" api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
|
2007-05-14 05:28:06 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
2007-12-06 18:33:18 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2007-05-14 05:28:06 +00:00
|
|
|
}
|
|
|
|
|
}
|