4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * Query module to enumerate all available pages.
36 class ApiQueryAllpages
extends ApiQueryGeneratorBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'ap' );
42 public function execute() {
46 public function getCacheMode( $params ) {
50 public function executeGenerator( $resultPageSet ) {
51 if ( $resultPageSet->isResolvingRedirects() ) {
52 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
55 $this->run( $resultPageSet );
58 private function run( $resultPageSet = null ) {
61 $params = $this->extractRequestParams();
64 $this->addTables( 'page' );
66 if ( $params['filterredir'] == 'redirects' ) {
67 $this->addWhereFld( 'page_is_redirect', 1 );
68 } elseif ( $params['filterredir'] == 'nonredirects' ) {
69 $this->addWhereFld( 'page_is_redirect', 0 );
72 $this->addWhereFld( 'page_namespace', $params['namespace'] );
73 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
74 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
75 $this->addWhereRange( 'page_title', $dir, $from, null );
77 if ( isset( $params['prefix'] ) ) {
78 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
81 if ( is_null( $resultPageSet ) ) {
82 $selectFields = array(
88 $selectFields = $resultPageSet->getPageTableFields();
91 $this->addFields( $selectFields );
92 $forceNameTitleIndex = true;
93 if ( isset( $params['minsize'] ) ) {
94 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
95 $forceNameTitleIndex = false;
98 if ( isset( $params['maxsize'] ) ) {
99 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
100 $forceNameTitleIndex = false;
103 // Page protection filtering
104 if ( !empty( $params['prtype'] ) ) {
105 $this->addTables( 'page_restrictions' );
106 $this->addWhere( 'page_id=pr_page' );
107 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
108 $this->addWhereFld( 'pr_type', $params['prtype'] );
110 if ( isset( $params['prlevel'] ) ) {
111 // Remove the empty string and '*' from the prlevel array
112 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
114 if ( !empty( $prlevel ) ) {
115 $this->addWhereFld( 'pr_level', $prlevel );
118 if ( $params['prfiltercascade'] == 'cascading' ) {
119 $this->addWhereFld( 'pr_cascade', 1 );
120 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
121 $this->addWhereFld( 'pr_cascade', 0 );
124 $this->addOption( 'DISTINCT' );
126 $forceNameTitleIndex = false;
128 } elseif ( isset( $params['prlevel'] ) ) {
129 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
132 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
133 $this->addTables( 'langlinks' );
134 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
135 $this->addWhere( 'll_from IS NULL' );
136 $forceNameTitleIndex = false;
137 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
138 $this->addTables( 'langlinks' );
139 $this->addWhere( 'page_id=ll_from' );
140 $this->addOption( 'STRAIGHT_JOIN' );
141 // We have to GROUP BY all selected fields to stop
142 // PostgreSQL from whining
143 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
144 $forceNameTitleIndex = false;
147 if ( $forceNameTitleIndex ) {
148 $this->addOption( 'USE INDEX', 'name_title' );
151 $limit = $params['limit'];
152 $this->addOption( 'LIMIT', $limit +
1 );
153 $res = $this->select( __METHOD__
);
156 $result = $this->getResult();
157 foreach ( $res as $row ) {
158 if ( ++
$count > $limit ) {
159 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
160 // TODO: Security issue - if the user has no right to view next title, it will still be shown
161 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
165 if ( is_null( $resultPageSet ) ) {
166 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
168 'pageid' => intval( $row->page_id
),
169 'ns' => intval( $title->getNamespace() ),
170 'title' => $title->getPrefixedText()
172 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
174 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
178 $resultPageSet->processDbRow( $row );
182 if ( is_null( $resultPageSet ) ) {
183 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
187 public function getAllowedParams() {
188 global $wgRestrictionTypes, $wgRestrictionLevels;
193 'namespace' => array(
194 ApiBase
::PARAM_DFLT
=> 0,
195 ApiBase
::PARAM_TYPE
=> 'namespace',
197 'filterredir' => array(
198 ApiBase
::PARAM_DFLT
=> 'all',
199 ApiBase
::PARAM_TYPE
=> array(
206 ApiBase
::PARAM_TYPE
=> 'integer',
209 ApiBase
::PARAM_TYPE
=> 'integer',
212 ApiBase
::PARAM_TYPE
=> $wgRestrictionTypes,
213 ApiBase
::PARAM_ISMULTI
=> true
216 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
217 ApiBase
::PARAM_ISMULTI
=> true
219 'prfiltercascade' => array(
220 ApiBase
::PARAM_DFLT
=> 'all',
221 ApiBase
::PARAM_TYPE
=> array(
228 ApiBase
::PARAM_DFLT
=> 10,
229 ApiBase
::PARAM_TYPE
=> 'limit',
230 ApiBase
::PARAM_MIN
=> 1,
231 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
232 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
235 ApiBase
::PARAM_DFLT
=> 'ascending',
236 ApiBase
::PARAM_TYPE
=> array(
241 'filterlanglinks' => array(
242 ApiBase
::PARAM_TYPE
=> array(
247 ApiBase
::PARAM_DFLT
=> 'all'
252 public function getParamDescription() {
253 $p = $this->getModulePrefix();
255 'from' => 'The page title to start enumerating from',
256 'prefix' => 'Search for all page titles that begin with this value',
257 'namespace' => 'The namespace to enumerate',
258 'filterredir' => 'Which pages to list',
259 'dir' => 'The direction in which to list',
260 'minsize' => 'Limit to pages with at least this many bytes',
261 'maxsize' => 'Limit to pages with at most this many bytes',
262 'prtype' => 'Limit to protected pages only',
263 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
264 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
265 'filterlanglinks' => 'Filter based on whether a page has langlinks',
266 'limit' => 'How many total pages to return.'
270 public function getDescription() {
271 return 'Enumerate all pages sequentially in a given namespace';
274 public function getPossibleErrors() {
275 return array_merge( parent
::getPossibleErrors(), array(
276 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
277 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
281 protected function getExamples() {
284 ' Show a list of pages starting at the letter "B"',
285 ' api.php?action=query&list=allpages&apfrom=B',
286 'Using as Generator',
287 ' Show info about 4 pages starting at the letter "T"',
288 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
289 ' Show content of first 2 non-redirect pages begining at "Re"',
290 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
294 public function getVersion() {
295 return __CLASS__
. ': $Id$';