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