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 $limit = $from = $namespace = $filterredir = $prefix = null;
58 extract($this->extractRequestParams());
60 $this->addTables('page');
61 if (!$this->addWhereIf('page_is_redirect = 1', $filterredir === 'redirects'))
62 $this->addWhereIf('page_is_redirect = 0', $filterredir === 'nonredirects');
63 $this->addWhereFld('page_namespace', $namespace);
65 $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase
:: titleToKey($from)));
67 $this->addWhere("page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'");
69 if (is_null($resultPageSet)) {
70 $this->addFields(array (
76 $this->addFields($resultPageSet->getPageTableFields());
79 $this->addOption('USE INDEX', 'name_title');
80 $this->addOption('LIMIT', $limit +
1);
81 $this->addOption('ORDER BY', 'page_namespace, page_title');
83 $res = $this->select(__METHOD__
);
87 while ($row = $db->fetchObject($res)) {
88 if (++
$count > $limit) {
89 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
90 $this->setContinueEnumParameter('from', ApiQueryBase
:: keyToTitle($row->page_title
));
94 if (is_null($resultPageSet)) {
95 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
96 if ($title->userCanRead()) {
98 'pageid' => intval($row->page_id
),
99 'ns' => intval($title->getNamespace()),
100 'title' => $title->getPrefixedText());
103 $resultPageSet->processDbRow($row);
106 $db->freeResult($res);
108 if (is_null($resultPageSet)) {
109 $result = $this->getResult();
110 $result->setIndexedTagName($data, 'p');
111 $result->addValue('query', $this->getModuleName(), $data);
115 protected function getAllowedParams() {
119 'namespace' => array (
120 ApiBase
:: PARAM_DFLT
=> 0,
121 ApiBase
:: PARAM_TYPE
=> 'namespace'
123 'filterredir' => array (
124 ApiBase
:: PARAM_DFLT
=> 'all',
125 ApiBase
:: PARAM_TYPE
=> array (
132 ApiBase
:: PARAM_DFLT
=> 10,
133 ApiBase
:: PARAM_TYPE
=> 'limit',
134 ApiBase
:: PARAM_MIN
=> 1,
135 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
136 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
141 protected function getParamDescription() {
143 'from' => 'The page title to start enumerating from.',
144 'prefix' => 'Search for all page titles that begin with this value.',
145 'namespace' => 'The namespace to enumerate.',
146 'filterredir' => 'Which pages to list.',
147 'limit' => 'How many total pages to return.'
151 protected function getDescription() {
152 return 'Enumerate all pages sequentially in a given namespace';
155 protected function getExamples() {
158 ' Show a list of pages starting at the letter "B"',
159 ' api.php?action=query&list=allpages&apfrom=B',
160 'Using as Generator',
161 ' Show info about 4 pages starting at the letter "T"',
162 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
163 ' Show content of first 2 non-redirect pages begining at "Re"',
164 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
168 public function getVersion() {
169 return __CLASS__
. ': $Id$';