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');
32 * Query module to enumerate links from all pages together.
36 class ApiQueryAllLinks
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'al');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
50 private function run($resultPageSet = null) {
53 $params = $this->extractRequestParams();
55 $prop = array_flip($params['prop']);
56 $fld_ids = isset($prop['ids']);
57 $fld_title = isset($prop['title']);
59 if ($params['unique']) {
60 if (!is_null($resultPageSet))
61 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
63 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
64 $this->addOption('DISTINCT');
67 $this->addTables('pagelinks');
68 $this->addWhereFld('pl_namespace', $params['namespace']);
70 if (!is_null($params['from']) && !is_null($params['continue']))
71 $this->dieUsage('alcontinue and alfrom cannot be used together', 'params');
72 if (!is_null($params['continue']))
74 $arr = explode('|', $params['continue']);
76 $this->dieUsage("Invalid continue parameter", 'badcontinue');
77 $from = $this->getDB()->strencode($this->titleToKey($arr[0]));
78 $id = intval($arr[1]);
79 $this->addWhere("pl_title > '$from' OR " .
80 "(pl_title = '$from' AND " .
84 if (!is_null($params['from']))
85 $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
86 if (isset ($params['prefix']))
87 $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
89 $this->addFields(array (
92 $this->addFieldsIf('pl_from', !$params['unique']);
94 $this->addOption('USE INDEX', 'pl_namespace');
95 $limit = $params['limit'];
96 $this->addOption('LIMIT', $limit+
1);
98 $this->addOption('ORDER BY', 'pl_title');
100 $this->addOption('ORDER BY', 'pl_title, pl_from');
102 $res = $this->select(__METHOD__
);
106 $result = $this->getResult();
107 while ($row = $db->fetchObject($res)) {
108 if (++
$count > $limit) {
109 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110 // TODO: Security issue - if the user has no right to view next title, it will still be shown
111 if($params['unique'])
112 $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title
));
114 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title
) . "|" . $row->pl_from
);
118 if (is_null($resultPageSet)) {
121 $vals['fromid'] = intval($row->pl_from
);
123 $title = Title
:: makeTitle($params['namespace'], $row->pl_title
);
124 ApiQueryBase
::addTitleInfo($vals, $title);
126 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
129 if($params['unique'])
130 $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title
));
132 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title
) . "|" . $row->pl_from
);
136 $pageids[] = $row->pl_from
;
139 $db->freeResult($res);
141 if (is_null($resultPageSet)) {
142 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'l');
144 $resultPageSet->populateFromPageIDs($pageids);
148 public function getAllowedParams() {
155 ApiBase
:: PARAM_ISMULTI
=> true,
156 ApiBase
:: PARAM_DFLT
=> 'title',
157 ApiBase
:: PARAM_TYPE
=> array (
162 'namespace' => array (
163 ApiBase
:: PARAM_DFLT
=> 0,
164 ApiBase
:: PARAM_TYPE
=> 'namespace'
167 ApiBase
:: PARAM_DFLT
=> 10,
168 ApiBase
:: PARAM_TYPE
=> 'limit',
169 ApiBase
:: PARAM_MIN
=> 1,
170 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
171 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
176 public function getParamDescription() {
178 'from' => 'The page title to start enumerating from.',
179 'prefix' => 'Search for all page titles that begin with this value.',
180 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
181 'prop' => 'What pieces of information to include',
182 'namespace' => 'The namespace to enumerate.',
183 'limit' => 'How many total links to return.',
184 'continue' => 'When more results are available, use this to continue.',
188 public function getDescription() {
189 return 'Enumerate all links that point to a given namespace';
192 protected function getExamples() {
194 'api.php?action=query&list=alllinks&alunique&alfrom=B',
198 public function getVersion() {
199 return __CLASS__
. ': $Id$';