4 * Created on Sep 25, 2006
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 all available pages.
36 class ApiQueryAllpages
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'ap');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 if ($resultPageSet->isResolvingRedirects())
48 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
50 $this->run($resultPageSet);
53 private function run($resultPageSet = null) {
57 $params = $this->extractRequestParams();
60 if (!$this->addWhereIf('page_is_redirect = 1', $params['filterredir'] === 'redirects'))
61 $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
62 $this->addWhereFld('page_namespace', $params['namespace']);
63 if (!is_null($params['from']))
64 $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase
:: titleToKey($params['from'])));
65 if (isset ($params['prefix']))
66 $this->addWhere("page_title LIKE '" . $db->escapeLike(ApiQueryBase
:: titleToKey($params['prefix'])) . "%'");
68 $forceNameTitleIndex = true;
69 if (isset ($params['minsize'])) {
70 $this->addWhere('page_len>=' . intval($params['minsize']));
71 $forceNameTitleIndex = false;
74 if (isset ($params['maxsize'])) {
75 $this->addWhere('page_len<=' . intval($params['maxsize']));
76 $forceNameTitleIndex = false;
79 // Page protection filtering
80 if (isset ($params['prtype'])) {
81 $this->addTables('page_restrictions');
82 $this->addWhere('page_id=pr_page');
83 $this->addWhere('pr_expiry>' . $db->addQuotes($db->timestamp()));
84 $this->addWhereFld('pr_type', $params['prtype']);
86 $prlevel = $params['prlevel'];
87 if (!is_null($prlevel) && $prlevel != '' && $prlevel != '*')
88 $this->addWhereFld('pr_level', $prlevel);
90 $this->addOption('DISTINCT');
92 $forceNameTitleIndex = false;
94 } else if (isset ($params['prlevel'])) {
95 $this->dieUsage('prlevel may not be used without prtype', 'params');
98 if($params['filterlanglinks'] == 'withoutlanglinks') {
99 $pageName = $this->getDB()->tableName('page');
100 $llName = $this->getDB()->tableName('langlinks');
101 $tables = "$pageName LEFT JOIN $llName ON page_id=ll_from";
102 $this->addWhere('ll_from IS NULL');
103 $this->addTables($tables);
104 $forceNameTitleIndex = false;
105 } else if($params['filterlanglinks'] == 'withlanglinks') {
106 $this->addTables(array('page', 'langlinks'));
107 $this->addWhere('page_id=ll_from');
108 $forceNameTitleIndex = false;
110 $this->addTables('page');
112 if ($forceNameTitleIndex)
113 $this->addOption('USE INDEX', 'name_title');
115 if (is_null($resultPageSet)) {
116 $this->addFields(array (
122 $this->addFields($resultPageSet->getPageTableFields());
125 $limit = $params['limit'];
126 $this->addOption('LIMIT', $limit+
1);
127 $this->addOption('ORDER BY', 'page_namespace, page_title' .
128 ($params['dir'] == 'descending' ?
' DESC' : ''));
130 $res = $this->select(__METHOD__
);
134 while ($row = $db->fetchObject($res)) {
135 if (++
$count > $limit) {
136 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
137 // TODO: Security issue - if the user has no right to view next title, it will still be shown
138 $this->setContinueEnumParameter('from', ApiQueryBase
:: keyToTitle($row->page_title
));
142 if (is_null($resultPageSet)) {
143 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
145 'pageid' => intval($row->page_id
),
146 'ns' => intval($title->getNamespace()),
147 'title' => $title->getPrefixedText());
149 $resultPageSet->processDbRow($row);
152 $db->freeResult($res);
154 if (is_null($resultPageSet)) {
155 $result = $this->getResult();
156 $result->setIndexedTagName($data, 'p');
157 $result->addValue('query', $this->getModuleName(), $data);
161 protected function getAllowedParams() {
162 global $wgRestrictionTypes, $wgRestrictionLevels;
167 'namespace' => array (
168 ApiBase
:: PARAM_DFLT
=> 0,
169 ApiBase
:: PARAM_TYPE
=> 'namespace',
171 'filterredir' => array (
172 ApiBase
:: PARAM_DFLT
=> 'all',
173 ApiBase
:: PARAM_TYPE
=> array (
180 ApiBase
:: PARAM_TYPE
=> 'integer',
183 ApiBase
:: PARAM_TYPE
=> 'integer',
186 ApiBase
:: PARAM_TYPE
=> $wgRestrictionTypes,
187 ApiBase
:: PARAM_ISMULTI
=> true
190 ApiBase
:: PARAM_TYPE
=> $wgRestrictionLevels,
191 ApiBase
:: PARAM_ISMULTI
=> true
194 ApiBase
:: PARAM_DFLT
=> 10,
195 ApiBase
:: PARAM_TYPE
=> 'limit',
196 ApiBase
:: PARAM_MIN
=> 1,
197 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
198 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
201 ApiBase
:: PARAM_DFLT
=> 'ascending',
202 ApiBase
:: PARAM_TYPE
=> array (
207 'filterlanglinks' => array(
208 ApiBase
:: PARAM_TYPE
=> array(
213 ApiBase
:: PARAM_DFLT
=> 'all'
218 protected function getParamDescription() {
220 'from' => 'The page title to start enumerating from.',
221 'prefix' => 'Search for all page titles that begin with this value.',
222 'namespace' => 'The namespace to enumerate.',
223 'filterredir' => 'Which pages to list.',
224 'dir' => 'The direction in which to list',
225 'minsize' => 'Limit to pages with at least this many bytes',
226 'maxsize' => 'Limit to pages with at most this many bytes',
227 'prtype' => 'Limit to protected pages only',
228 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
229 'filterlanglinks' => 'Filter based on whether a page has langlinks',
230 'limit' => 'How many total pages to return.'
234 protected function getDescription() {
235 return 'Enumerate all pages sequentially in a given namespace';
238 protected function getExamples() {
241 ' Show a list of pages starting at the letter "B"',
242 ' api.php?action=query&list=allpages&apfrom=B',
243 'Using as Generator',
244 ' Show info about 4 pages starting at the letter "T"',
245 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
246 ' Show content of first 2 non-redirect pages begining at "Re"',
247 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
251 public function getVersion() {
252 return __CLASS__
. ': $Id$';