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