5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
28 * Query module to enumerate all available pages.
32 class ApiQueryAllPages
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'ap' );
38 public function execute() {
42 public function getCacheMode( $params ) {
47 * @param $resultPageSet ApiPageSet
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 );
59 * @param $resultPageSet ApiPageSet
62 private function run( $resultPageSet = null ) {
65 $params = $this->extractRequestParams();
68 $this->addTables( 'page' );
70 if ( $params['filterredir'] == 'redirects' ) {
71 $this->addWhereFld( 'page_is_redirect', 1 );
72 } elseif ( $params['filterredir'] == 'nonredirects' ) {
73 $this->addWhereFld( 'page_is_redirect', 0 );
76 $this->addWhereFld( 'page_namespace', $params['namespace'] );
77 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
78 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
79 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
80 $this->addWhereRange( 'page_title', $dir, $from, $to );
82 if ( isset( $params['prefix'] ) ) {
83 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
86 if ( is_null( $resultPageSet ) ) {
87 $selectFields = array(
93 $selectFields = $resultPageSet->getPageTableFields();
96 $this->addFields( $selectFields );
97 $forceNameTitleIndex = true;
98 if ( isset( $params['minsize'] ) ) {
99 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
100 $forceNameTitleIndex = false;
103 if ( isset( $params['maxsize'] ) ) {
104 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
105 $forceNameTitleIndex = false;
108 // Page protection filtering
109 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
110 $this->addTables( 'page_restrictions' );
111 $this->addWhere( 'page_id=pr_page' );
112 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
114 if ( count( $params['prtype'] ) ) {
115 $this->addWhereFld( 'pr_type', $params['prtype'] );
117 if ( isset( $params['prlevel'] ) ) {
118 // Remove the empty string and '*' from the prlevel array
119 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
121 if ( count( $prlevel ) ) {
122 $this->addWhereFld( 'pr_level', $prlevel );
125 if ( $params['prfiltercascade'] == 'cascading' ) {
126 $this->addWhereFld( 'pr_cascade', 1 );
127 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
128 $this->addWhereFld( 'pr_cascade', 0 );
131 $this->addOption( 'DISTINCT' );
133 $forceNameTitleIndex = false;
135 if ( $params['prexpiry'] == 'indefinite' ) {
136 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
137 } elseif ( $params['prexpiry'] == 'definite' ) {
138 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
141 } elseif ( isset( $params['prlevel'] ) ) {
142 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
145 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
146 $this->addTables( 'langlinks' );
147 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
148 $this->addWhere( 'll_from IS NULL' );
149 $forceNameTitleIndex = false;
150 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
151 $this->addTables( 'langlinks' );
152 $this->addWhere( 'page_id=ll_from' );
153 $this->addOption( 'STRAIGHT_JOIN' );
154 // We have to GROUP BY all selected fields to stop
155 // PostgreSQL from whining
156 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
157 $forceNameTitleIndex = false;
160 if ( $forceNameTitleIndex ) {
161 $this->addOption( 'USE INDEX', 'name_title' );
164 $limit = $params['limit'];
165 $this->addOption( 'LIMIT', $limit +
1 );
166 $res = $this->select( __METHOD__
);
169 $result = $this->getResult();
170 foreach ( $res as $row ) {
171 if ( ++
$count > $limit ) {
172 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
173 // TODO: Security issue - if the user has no right to view next title, it will still be shown
174 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
178 if ( is_null( $resultPageSet ) ) {
179 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
181 'pageid' => intval( $row->page_id
),
182 'ns' => intval( $title->getNamespace() ),
183 'title' => $title->getPrefixedText()
185 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
187 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
191 $resultPageSet->processDbRow( $row );
195 if ( is_null( $resultPageSet ) ) {
196 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
200 public function getAllowedParams() {
201 global $wgRestrictionLevels;
207 'namespace' => array(
208 ApiBase
::PARAM_DFLT
=> 0,
209 ApiBase
::PARAM_TYPE
=> 'namespace',
211 'filterredir' => array(
212 ApiBase
::PARAM_DFLT
=> 'all',
213 ApiBase
::PARAM_TYPE
=> array(
220 ApiBase
::PARAM_TYPE
=> 'integer',
223 ApiBase
::PARAM_TYPE
=> 'integer',
226 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
227 ApiBase
::PARAM_ISMULTI
=> true
230 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
231 ApiBase
::PARAM_ISMULTI
=> true
233 'prfiltercascade' => array(
234 ApiBase
::PARAM_DFLT
=> 'all',
235 ApiBase
::PARAM_TYPE
=> array(
242 ApiBase
::PARAM_DFLT
=> 10,
243 ApiBase
::PARAM_TYPE
=> 'limit',
244 ApiBase
::PARAM_MIN
=> 1,
245 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
246 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
249 ApiBase
::PARAM_DFLT
=> 'ascending',
250 ApiBase
::PARAM_TYPE
=> array(
255 'filterlanglinks' => array(
256 ApiBase
::PARAM_TYPE
=> array(
261 ApiBase
::PARAM_DFLT
=> 'all'
264 ApiBase
::PARAM_TYPE
=> array(
269 ApiBase
::PARAM_DFLT
=> 'all'
274 public function getParamDescription() {
275 $p = $this->getModulePrefix();
277 'from' => 'The page title to start enumerating from',
278 'to' => 'The page title to stop enumerating at',
279 'prefix' => 'Search for all page titles that begin with this value',
280 'namespace' => 'The namespace to enumerate',
281 'filterredir' => 'Which pages to list',
282 'dir' => 'The direction in which to list',
283 'minsize' => 'Limit to pages with at least this many bytes',
284 'maxsize' => 'Limit to pages with at most this many bytes',
285 'prtype' => 'Limit to protected pages only',
286 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
287 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
288 'filterlanglinks' => 'Filter based on whether a page has langlinks',
289 'limit' => 'How many total pages to return.',
291 'Which protection expiry to filter the page on',
292 ' indefinite - Get only pages with indefinite protection expiry',
293 ' definite - Get only pages with a definite (specific) protection expiry',
294 ' all - Get pages with any protections expiry'
299 public function getDescription() {
300 return 'Enumerate all pages sequentially in a given namespace';
303 public function getPossibleErrors() {
304 return array_merge( parent
::getPossibleErrors(), array(
305 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
306 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
310 public function getExamples() {
312 'api.php?action=query&list=allpages&apfrom=B' => array(
314 'Show a list of pages starting at the letter "B"',
316 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
317 'Using as Generator',
318 'Show info about 4 pages starting at the letter "T"',
320 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
321 'Show content of first 2 non-redirect pages begining at "Re"',
326 public function getHelpUrls() {
327 return 'https://www.mediawiki.org/wiki/API:Allpages';
330 public function getVersion() {
331 return __CLASS__
. ': $Id$';