4 * Created on May 12, 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");
32 * A query module to list all wiki links on a given set of pages.
36 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
38 const LINKS
= 'links';
39 const TEMPLATES
= 'templates';
41 private $table, $prefix, $description;
43 public function __construct($query, $moduleName) {
45 switch ($moduleName) {
47 $this->table
= 'pagelinks';
49 $this->description
= 'link';
51 case self
::TEMPLATES
:
52 $this->table
= 'templatelinks';
54 $this->description
= 'template';
57 ApiBase
:: dieDebug(__METHOD__
, 'Unknown module name');
60 parent
:: __construct($query, $moduleName, $this->prefix
);
63 public function execute() {
67 public function executeGenerator($resultPageSet) {
68 $this->run($resultPageSet);
71 private function run($resultPageSet = null) {
73 if ($this->getPageSet()->getGoodTitleCount() == 0)
74 return; // nothing to do
76 $params = $this->extractRequestParams();
78 $this->addFields(array (
79 $this->prefix
. '_from AS pl_from',
80 $this->prefix
. '_namespace AS pl_namespace',
81 $this->prefix
. '_title AS pl_title'
84 $this->addTables($this->table
);
85 $this->addWhereFld($this->prefix
. '_from', array_keys($this->getPageSet()->getGoodTitles()));
86 $this->addWhereFld($this->prefix
. '_namespace', $params['namespace']);
88 if(!is_null($params['continue'])) {
89 $cont = explode('|', $params['continue']);
91 $this->dieUsage("Invalid continue param. You should pass the " .
92 "original value returned by the previous query", "_badcontinue");
93 $plfrom = intval($cont[0]);
94 $plns = intval($cont[1]);
95 $pltitle = $this->getDB()->strencode($this->titleToKey($cont[2]));
96 $this->addWhere("{$this->prefix}_from > $plfrom OR ".
97 "({$this->prefix}_from = $plfrom AND ".
98 "({$this->prefix}_namespace > $plns OR ".
99 "({$this->prefix}_namespace = $plns AND ".
100 "{$this->prefix}_title >= '$pltitle')))");
103 # Here's some MySQL craziness going on: if you use WHERE foo='bar'
104 # and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
105 # but instead goes and filesorts, because the index for foo was used
106 # already. To work around this, we drop constant fields in the WHERE
107 # clause from the ORDER BY clause
109 if(count($this->getPageSet()->getGoodTitles()) != 1)
110 $order[] = "{$this->prefix}_from";
111 if(count($params['namespace']) != 1)
112 $order[] = "{$this->prefix}_namespace";
113 $order[] = "{$this->prefix}_title";
114 $this->addOption('ORDER BY', implode(", ", $order));
115 $this->addOption('USE INDEX', "{$this->prefix}_from");
116 $this->addOption('LIMIT', $params['limit'] +
1);
118 $db = $this->getDB();
119 $res = $this->select(__METHOD__
);
121 if (is_null($resultPageSet)) {
123 while ($row = $db->fetchObject($res)) {
124 if(++
$count > $params['limit']) {
125 // We've reached the one extra which shows that
126 // there are additional pages to be had. Stop here...
127 $this->setContinueEnumParameter('continue',
128 "{$row->pl_from}|{$row->pl_namespace}|" .
129 $this->keyToTitle($row->pl_title
));
133 ApiQueryBase
:: addTitleInfo($vals, Title
:: makeTitle($row->pl_namespace
, $row->pl_title
));
134 $fit = $this->addPageSubItem($row->pl_from
, $vals);
137 $this->setContinueEnumParameter('continue',
138 "{$row->pl_from}|{$row->pl_namespace}|" .
139 $this->keyToTitle($row->pl_title
));
147 while ($row = $db->fetchObject($res)) {
148 if(++
$count > $params['limit']) {
149 // We've reached the one extra which shows that
150 // there are additional pages to be had. Stop here...
151 $this->setContinueEnumParameter('continue',
152 "{$row->pl_from}|{$row->pl_namespace}|" .
153 $this->keyToTitle($row->pl_title
));
156 $titles[] = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
158 $resultPageSet->populateFromTitles($titles);
161 $db->freeResult($res);
164 public function getAllowedParams()
167 'namespace' => array(
168 ApiBase
:: PARAM_TYPE
=> 'namespace',
169 ApiBase
:: PARAM_ISMULTI
=> true
172 ApiBase
:: PARAM_DFLT
=> 10,
173 ApiBase
:: PARAM_TYPE
=> 'limit',
174 ApiBase
:: PARAM_MIN
=> 1,
175 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
176 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
182 public function getParamDescription()
185 'namespace' => "Show {$this->description}s in this namespace(s) only",
186 'limit' => "How many {$this->description}s to return",
187 'continue' => 'When more results are available, use this to continue',
191 public function getDescription() {
192 return "Returns all {$this->description}s from the given page(s)";
195 protected function getExamples() {
197 "Get {$this->description}s from the [[Main Page]]:",
198 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
199 "Get information about the {$this->description} pages in the [[Main Page]]:",
200 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
201 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
202 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
206 public function getVersion() {
207 return __CLASS__
. ': $Id$';