]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllpages.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryAllpages.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
4  *
5  * Created on Sep 25, 2006
6  *
7  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33  * Query module to enumerate all available pages.
34  *
35  * @ingroup API
36  */
37 class ApiQueryAllpages extends ApiQueryGeneratorBase {
38
39         public function __construct( $query, $moduleName ) {
40                 parent::__construct( $query, $moduleName, 'ap' );
41         }
42
43         public function execute() {
44                 $this->run();
45         }
46
47         public function getCacheMode( $params ) {
48                 return 'public';
49         }
50
51         public function executeGenerator( $resultPageSet ) {
52                 if ( $resultPageSet->isResolvingRedirects() ) {
53                         $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
54                 }
55
56                 $this->run( $resultPageSet );
57         }
58
59         private function run( $resultPageSet = null ) {
60                 $db = $this->getDB();
61
62                 $params = $this->extractRequestParams();
63
64                 // Page filters
65                 $this->addTables( 'page' );
66
67                 if ( $params['filterredir'] == 'redirects' ) {
68                         $this->addWhereFld( 'page_is_redirect', 1 );
69                 } elseif ( $params['filterredir'] == 'nonredirects' ) {
70                         $this->addWhereFld( 'page_is_redirect', 0 );
71                 }
72
73                 $this->addWhereFld( 'page_namespace', $params['namespace'] );
74                 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
75                 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
76                 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
77                 $this->addWhereRange( 'page_title', $dir, $from, $to );
78                 
79                 if ( isset( $params['prefix'] ) ) {
80                         $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
81                 }
82
83                 if ( is_null( $resultPageSet ) ) {
84                         $selectFields = array(
85                                 'page_namespace',
86                                 'page_title',
87                                 'page_id'
88                         );
89                 } else {
90                         $selectFields = $resultPageSet->getPageTableFields();
91                 }
92
93                 $this->addFields( $selectFields );
94                 $forceNameTitleIndex = true;
95                 if ( isset( $params['minsize'] ) ) {
96                         $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
97                         $forceNameTitleIndex = false;
98                 }
99
100                 if ( isset( $params['maxsize'] ) ) {
101                         $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
102                         $forceNameTitleIndex = false;
103                 }
104
105                 // Page protection filtering
106                 if ( !empty( $params['prtype'] ) ) {
107                         $this->addTables( 'page_restrictions' );
108                         $this->addWhere( 'page_id=pr_page' );
109                         $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
110                         $this->addWhereFld( 'pr_type', $params['prtype'] );
111
112                         if ( isset( $params['prlevel'] ) ) {
113                                 // Remove the empty string and '*' from the prlevel array
114                                 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
115
116                                 if ( !empty( $prlevel ) ) {
117                                         $this->addWhereFld( 'pr_level', $prlevel );
118                                 }
119                         }
120                         if ( $params['prfiltercascade'] == 'cascading' ) {
121                                 $this->addWhereFld( 'pr_cascade', 1 );
122                         } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
123                                 $this->addWhereFld( 'pr_cascade', 0 );
124                         }
125
126                         $this->addOption( 'DISTINCT' );
127
128                         $forceNameTitleIndex = false;
129
130                 } elseif ( isset( $params['prlevel'] ) ) {
131                         $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
132                 }
133
134                 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
135                         $this->addTables( 'langlinks' );
136                         $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
137                         $this->addWhere( 'll_from IS NULL' );
138                         $forceNameTitleIndex = false;
139                 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
140                         $this->addTables( 'langlinks' );
141                         $this->addWhere( 'page_id=ll_from' );
142                         $this->addOption( 'STRAIGHT_JOIN' );
143                         // We have to GROUP BY all selected fields to stop
144                         // PostgreSQL from whining
145                         $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
146                         $forceNameTitleIndex = false;
147                 }
148
149                 if ( $forceNameTitleIndex ) {
150                         $this->addOption( 'USE INDEX', 'name_title' );
151                 }
152
153                 $limit = $params['limit'];
154                 $this->addOption( 'LIMIT', $limit + 1 );
155                 $res = $this->select( __METHOD__ );
156
157                 $count = 0;
158                 $result = $this->getResult();
159                 foreach ( $res as $row ) {
160                         if ( ++ $count > $limit ) {
161                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
162                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
163                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
164                                 break;
165                         }
166
167                         if ( is_null( $resultPageSet ) ) {
168                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
169                                 $vals = array(
170                                         'pageid' => intval( $row->page_id ),
171                                         'ns' => intval( $title->getNamespace() ),
172                                         'title' => $title->getPrefixedText()
173                                 );
174                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
175                                 if ( !$fit ) {
176                                         $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
177                                         break;
178                                 }
179                         } else {
180                                 $resultPageSet->processDbRow( $row );
181                         }
182                 }
183
184                 if ( is_null( $resultPageSet ) ) {
185                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
186                 }
187         }
188
189         public function getAllowedParams() {
190                 global $wgRestrictionLevels;
191
192                 return array(
193                         'from' => null,
194                         'to' => null,
195                         'prefix' => null,
196                         'namespace' => array(
197                                 ApiBase::PARAM_DFLT => 0,
198                                 ApiBase::PARAM_TYPE => 'namespace',
199                         ),
200                         'filterredir' => array(
201                                 ApiBase::PARAM_DFLT => 'all',
202                                 ApiBase::PARAM_TYPE => array(
203                                         'all',
204                                         'redirects',
205                                         'nonredirects'
206                                 )
207                         ),
208                         'minsize' => array(
209                                 ApiBase::PARAM_TYPE => 'integer',
210                         ),
211                         'maxsize' => array(
212                                 ApiBase::PARAM_TYPE => 'integer',
213                         ),
214                         'prtype' => array(
215                                 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
216                                 ApiBase::PARAM_ISMULTI => true
217                         ),
218                         'prlevel' => array(
219                                 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
220                                 ApiBase::PARAM_ISMULTI => true
221                         ),
222                         'prfiltercascade' => array(
223                                 ApiBase::PARAM_DFLT => 'all',
224                                 ApiBase::PARAM_TYPE => array(
225                                         'cascading',
226                                         'noncascading',
227                                         'all'
228                                 ),
229                         ),
230                         'limit' => array(
231                                 ApiBase::PARAM_DFLT => 10,
232                                 ApiBase::PARAM_TYPE => 'limit',
233                                 ApiBase::PARAM_MIN => 1,
234                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
235                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
236                         ),
237                         'dir' => array(
238                                 ApiBase::PARAM_DFLT => 'ascending',
239                                 ApiBase::PARAM_TYPE => array(
240                                         'ascending',
241                                         'descending'
242                                 )
243                         ),
244                         'filterlanglinks' => array(
245                                 ApiBase::PARAM_TYPE => array(
246                                         'withlanglinks',
247                                         'withoutlanglinks',
248                                         'all'
249                                 ),
250                                 ApiBase::PARAM_DFLT => 'all'
251                         )
252                 );
253         }
254
255         public function getParamDescription() {
256                 $p = $this->getModulePrefix();
257                 return array(
258                         'from' => 'The page title to start enumerating from',
259                         'to' => 'The page title to stop enumerating at',
260                         'prefix' => 'Search for all page titles that begin with this value',
261                         'namespace' => 'The namespace to enumerate',
262                         'filterredir' => 'Which pages to list',
263                         'dir' => 'The direction in which to list',
264                         'minsize' => 'Limit to pages with at least this many bytes',
265                         'maxsize' => 'Limit to pages with at most this many bytes',
266                         'prtype' => 'Limit to protected pages only',
267                         'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
268                         'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
269                         'filterlanglinks' => 'Filter based on whether a page has langlinks',
270                         'limit' => 'How many total pages to return.'
271                 );
272         }
273
274         public function getDescription() {
275                 return 'Enumerate all pages sequentially in a given namespace';
276         }
277
278         public function getPossibleErrors() {
279                 return array_merge( parent::getPossibleErrors(), array(
280                         array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
281                         array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
282                 ) );
283         }
284
285         protected function getExamples() {
286                 return array(
287                         'Simple Use',
288                         ' Show a list of pages starting at the letter "B"',
289                         '  api.php?action=query&list=allpages&apfrom=B',
290                         'Using as Generator',
291                         ' Show info about 4 pages starting at the letter "T"',
292                         '  api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
293                         ' Show content of first 2 non-redirect pages begining at "Re"',
294                         '  api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
295                 );
296         }
297
298         public function getVersion() {
299                 return __CLASS__ . ': $Id$';
300         }
301 }