Adding apihighlimits permission. Users with this permission can request 10 times...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryAllpages extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'ap');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
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');
49
50 $this->run($resultPageSet);
51 }
52
53 private function run($resultPageSet = null) {
54
55 $db = $this->getDB();
56
57 $params = $this->extractRequestParams();
58
59 // Page filters
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'])) . "%'");
67
68 $forceNameTitleIndex = true;
69 if (isset ($params['minsize'])) {
70 $this->addWhere('page_len>=' . intval($params['minsize']));
71 $forceNameTitleIndex = false;
72 }
73
74 if (isset ($params['maxsize'])) {
75 $this->addWhere('page_len<=' . intval($params['maxsize']));
76 $forceNameTitleIndex = false;
77 }
78
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']);
85
86 $prlevel = $params['prlevel'];
87 if (!is_null($prlevel) && $prlevel != '' && $prlevel != '*')
88 $this->addWhereFld('pr_level', $prlevel);
89
90 $this->addOption('DISTINCT');
91
92 $forceNameTitleIndex = false;
93
94 } else if (isset ($params['prlevel'])) {
95 $this->dieUsage('prlevel may not be used without prtype', 'params');
96 }
97
98 $this->addTables('page');
99 if ($forceNameTitleIndex)
100 $this->addOption('USE INDEX', 'name_title');
101
102
103 if (is_null($resultPageSet)) {
104 $this->addFields(array (
105 'page_id',
106 'page_namespace',
107 'page_title'
108 ));
109 } else {
110 $this->addFields($resultPageSet->getPageTableFields());
111 }
112
113 $limit = $params['limit'];
114 $this->addOption('LIMIT', $limit+1);
115 $this->addOption('ORDER BY', 'page_namespace, page_title' .
116 ($params['dir'] == 'ZtoA' ? ' DESC' : ''));
117
118 $res = $this->select(__METHOD__);
119
120 $data = array ();
121 $count = 0;
122 while ($row = $db->fetchObject($res)) {
123 if (++ $count > $limit) {
124 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
125 // TODO: Security issue - if the user has no right to view next title, it will still be shown
126 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
127 break;
128 }
129
130 if (is_null($resultPageSet)) {
131 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
132 $data[] = array(
133 'pageid' => intval($row->page_id),
134 'ns' => intval($title->getNamespace()),
135 'title' => $title->getPrefixedText());
136 } else {
137 $resultPageSet->processDbRow($row);
138 }
139 }
140 $db->freeResult($res);
141
142 if (is_null($resultPageSet)) {
143 $result = $this->getResult();
144 $result->setIndexedTagName($data, 'p');
145 $result->addValue('query', $this->getModuleName(), $data);
146 }
147 }
148
149 protected function getAllowedParams() {
150 global $wgRestrictionTypes, $wgRestrictionLevels;
151
152 return array (
153 'from' => null,
154 'prefix' => null,
155 'namespace' => array (
156 ApiBase :: PARAM_DFLT => 0,
157 ApiBase :: PARAM_TYPE => 'namespace',
158 ),
159 'filterredir' => array (
160 ApiBase :: PARAM_DFLT => 'all',
161 ApiBase :: PARAM_TYPE => array (
162 'all',
163 'redirects',
164 'nonredirects'
165 )
166 ),
167 'minsize' => array (
168 ApiBase :: PARAM_TYPE => 'integer',
169 ),
170 'maxsize' => array (
171 ApiBase :: PARAM_TYPE => 'integer',
172 ),
173 'prtype' => array (
174 ApiBase :: PARAM_TYPE => $wgRestrictionTypes,
175 ApiBase :: PARAM_ISMULTI => true
176 ),
177 'prlevel' => array (
178 ApiBase :: PARAM_TYPE => $wgRestrictionLevels,
179 ApiBase :: PARAM_ISMULTI => true
180 ),
181 'limit' => array (
182 ApiBase :: PARAM_DFLT => 10,
183 ApiBase :: PARAM_TYPE => 'limit',
184 ApiBase :: PARAM_MIN => 1,
185 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
186 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
187 ),
188 'dir' => array (
189 ApiBase :: PARAM_DFLT => 'AtoZ',
190 ApiBase :: PARAM_TYPE => array (
191 'AtoZ',
192 'ZtoA'
193 )
194 )
195 );
196 }
197
198 protected function getParamDescription() {
199 return array (
200 'from' => 'The page title to start enumerating from.',
201 'prefix' => 'Search for all page titles that begin with this value.',
202 'namespace' => 'The namespace to enumerate.',
203 'filterredir' => 'Which pages to list.',
204 'dir' => 'The direction in which to list',
205 'minsize' => 'Limit to pages with at least this many bytes',
206 'maxsize' => 'Limit to pages with at most this many bytes',
207 'prtype' => 'Limit to protected pages only',
208 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
209 'limit' => 'How many total pages to return.'
210 );
211 }
212
213 protected function getDescription() {
214 return 'Enumerate all pages sequentially in a given namespace';
215 }
216
217 protected function getExamples() {
218 return array (
219 'Simple Use',
220 ' Show a list of pages starting at the letter "B"',
221 ' api.php?action=query&list=allpages&apfrom=B',
222 'Using as Generator',
223 ' Show info about 4 pages starting at the letter "T"',
224 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
225 ' Show content of first 2 non-redirect pages begining at "Re"',
226 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
227 );
228 }
229
230 public function getVersion() {
231 return __CLASS__ . ': $Id$';
232 }
233 }
234