ApiQueryBase::titleToKey and ApiQueryBase::keyToTitle;
[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 * @ingroup 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 $this->addTables('page');
61 if (!$this->addWhereIf('page_is_redirect = 1', $params['filterredir'] === 'redirects'))
62 $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
63 $this->addWhereFld('page_namespace', $params['namespace']);
64 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
65 $from = (is_null($params['from']) ? null : $this->titleToKey($params['from']));
66 $this->addWhereRange('page_title', $dir, $from, null);
67 if (isset ($params['prefix']))
68 $this->addWhere("page_title LIKE '" . $db->escapeLike($this->titleToKey($params['prefix'])) . "%'");
69
70 $forceNameTitleIndex = true;
71 if (isset ($params['minsize'])) {
72 $this->addWhere('page_len>=' . intval($params['minsize']));
73 $forceNameTitleIndex = false;
74 }
75
76 if (isset ($params['maxsize'])) {
77 $this->addWhere('page_len<=' . intval($params['maxsize']));
78 $forceNameTitleIndex = false;
79 }
80
81 // Page protection filtering
82 if (isset ($params['prtype'])) {
83 $this->addTables('page_restrictions');
84 $this->addWhere('page_id=pr_page');
85 $this->addWhere('pr_expiry>' . $db->addQuotes($db->timestamp()));
86 $this->addWhereFld('pr_type', $params['prtype']);
87
88 $prlevel = $params['prlevel'];
89 if (!is_null($prlevel) && $prlevel != '' && $prlevel != '*')
90 $this->addWhereFld('pr_level', $prlevel);
91
92 $this->addOption('DISTINCT');
93
94 $forceNameTitleIndex = false;
95
96 } else if (isset ($params['prlevel'])) {
97 $this->dieUsage('prlevel may not be used without prtype', 'params');
98 }
99
100 if($params['filterlanglinks'] == 'withoutlanglinks') {
101 $this->addTables('langlinks');
102 $this->addJoinConds(array('langlinks' => array('LEFT JOIN', 'page_id=ll_from')));
103 $this->addWhere('ll_from IS NULL');
104 $forceNameTitleIndex = false;
105 } else if($params['filterlanglinks'] == 'withlanglinks') {
106 $this->addTables('langlinks');
107 $this->addWhere('page_id=ll_from');
108 $forceNameTitleIndex = false;
109 }
110 if ($forceNameTitleIndex)
111 $this->addOption('USE INDEX', 'name_title');
112
113 if (is_null($resultPageSet)) {
114 $this->addFields(array (
115 'page_id',
116 'page_namespace',
117 'page_title'
118 ));
119 } else {
120 $this->addFields($resultPageSet->getPageTableFields());
121 }
122
123 $limit = $params['limit'];
124 $this->addOption('LIMIT', $limit+1);
125 $res = $this->select(__METHOD__);
126
127 $data = array ();
128 $count = 0;
129 while ($row = $db->fetchObject($res)) {
130 if (++ $count > $limit) {
131 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
132 // TODO: Security issue - if the user has no right to view next title, it will still be shown
133 $this->setContinueEnumParameter('from', $this->keyToTitle($row->page_title));
134 break;
135 }
136
137 if (is_null($resultPageSet)) {
138 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
139 $data[] = array(
140 'pageid' => intval($row->page_id),
141 'ns' => intval($title->getNamespace()),
142 'title' => $title->getPrefixedText());
143 } else {
144 $resultPageSet->processDbRow($row);
145 }
146 }
147 $db->freeResult($res);
148
149 if (is_null($resultPageSet)) {
150 $result = $this->getResult();
151 $result->setIndexedTagName($data, 'p');
152 $result->addValue('query', $this->getModuleName(), $data);
153 }
154 }
155
156 public function getAllowedParams() {
157 global $wgRestrictionTypes, $wgRestrictionLevels;
158
159 return array (
160 'from' => null,
161 'prefix' => null,
162 'namespace' => array (
163 ApiBase :: PARAM_DFLT => 0,
164 ApiBase :: PARAM_TYPE => 'namespace',
165 ),
166 'filterredir' => array (
167 ApiBase :: PARAM_DFLT => 'all',
168 ApiBase :: PARAM_TYPE => array (
169 'all',
170 'redirects',
171 'nonredirects'
172 )
173 ),
174 'minsize' => array (
175 ApiBase :: PARAM_TYPE => 'integer',
176 ),
177 'maxsize' => array (
178 ApiBase :: PARAM_TYPE => 'integer',
179 ),
180 'prtype' => array (
181 ApiBase :: PARAM_TYPE => $wgRestrictionTypes,
182 ApiBase :: PARAM_ISMULTI => true
183 ),
184 'prlevel' => array (
185 ApiBase :: PARAM_TYPE => $wgRestrictionLevels,
186 ApiBase :: PARAM_ISMULTI => true
187 ),
188 'limit' => array (
189 ApiBase :: PARAM_DFLT => 10,
190 ApiBase :: PARAM_TYPE => 'limit',
191 ApiBase :: PARAM_MIN => 1,
192 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
193 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
194 ),
195 'dir' => array (
196 ApiBase :: PARAM_DFLT => 'ascending',
197 ApiBase :: PARAM_TYPE => array (
198 'ascending',
199 'descending'
200 )
201 ),
202 'filterlanglinks' => array(
203 ApiBase :: PARAM_TYPE => array(
204 'withlanglinks',
205 'withoutlanglinks',
206 'all'
207 ),
208 ApiBase :: PARAM_DFLT => 'all'
209 )
210 );
211 }
212
213 public function getParamDescription() {
214 return array (
215 'from' => 'The page title to start enumerating from.',
216 'prefix' => 'Search for all page titles that begin with this value.',
217 'namespace' => 'The namespace to enumerate.',
218 'filterredir' => 'Which pages to list.',
219 'dir' => 'The direction in which to list',
220 'minsize' => 'Limit to pages with at least this many bytes',
221 'maxsize' => 'Limit to pages with at most this many bytes',
222 'prtype' => 'Limit to protected pages only',
223 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
224 'filterlanglinks' => 'Filter based on whether a page has langlinks',
225 'limit' => 'How many total pages to return.'
226 );
227 }
228
229 public function getDescription() {
230 return 'Enumerate all pages sequentially in a given namespace';
231 }
232
233 protected function getExamples() {
234 return array (
235 'Simple Use',
236 ' Show a list of pages starting at the letter "B"',
237 ' api.php?action=query&list=allpages&apfrom=B',
238 'Using as Generator',
239 ' Show info about 4 pages starting at the letter "T"',
240 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
241 ' Show content of first 2 non-redirect pages begining at "Re"',
242 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
243 );
244 }
245
246 public function getVersion() {
247 return __CLASS__ . ': $Id$';
248 }
249 }