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 ( !is_null( $params['continue'] ) ) {
71 $cont = explode( '|', $params['continue'] );
72 $this->dieContinueUsageIf( count( $cont ) != 1 );
73 $op = $params['dir'] == 'descending' ?
'<' : '>';
74 $cont_from = $db->addQuotes( $cont[0] );
75 $this->addWhere( "page_title $op= $cont_from" );
78 if ( $params['filterredir'] == 'redirects' ) {
79 $this->addWhereFld( 'page_is_redirect', 1 );
80 } elseif ( $params['filterredir'] == 'nonredirects' ) {
81 $this->addWhereFld( 'page_is_redirect', 0 );
84 $this->addWhereFld( 'page_namespace', $params['namespace'] );
85 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
86 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
87 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
88 $this->addWhereRange( 'page_title', $dir, $from, $to );
90 if ( isset( $params['prefix'] ) ) {
91 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
94 if ( is_null( $resultPageSet ) ) {
95 $selectFields = array(
101 $selectFields = $resultPageSet->getPageTableFields();
104 $this->addFields( $selectFields );
105 $forceNameTitleIndex = true;
106 if ( isset( $params['minsize'] ) ) {
107 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
108 $forceNameTitleIndex = false;
111 if ( isset( $params['maxsize'] ) ) {
112 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
113 $forceNameTitleIndex = false;
116 // Page protection filtering
117 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
118 $this->addTables( 'page_restrictions' );
119 $this->addWhere( 'page_id=pr_page' );
120 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
122 if ( count( $params['prtype'] ) ) {
123 $this->addWhereFld( 'pr_type', $params['prtype'] );
125 if ( isset( $params['prlevel'] ) ) {
126 // Remove the empty string and '*' from the prlevel array
127 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
129 if ( count( $prlevel ) ) {
130 $this->addWhereFld( 'pr_level', $prlevel );
133 if ( $params['prfiltercascade'] == 'cascading' ) {
134 $this->addWhereFld( 'pr_cascade', 1 );
135 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
136 $this->addWhereFld( 'pr_cascade', 0 );
139 $forceNameTitleIndex = false;
141 if ( $params['prexpiry'] == 'indefinite' ) {
142 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
143 } elseif ( $params['prexpiry'] == 'definite' ) {
144 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
147 $this->addOption( 'DISTINCT' );
149 } elseif ( isset( $params['prlevel'] ) ) {
150 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
153 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
154 $this->addTables( 'langlinks' );
155 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
156 $this->addWhere( 'll_from IS NULL' );
157 $forceNameTitleIndex = false;
158 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
159 $this->addTables( 'langlinks' );
160 $this->addWhere( 'page_id=ll_from' );
161 $this->addOption( 'STRAIGHT_JOIN' );
162 // We have to GROUP BY all selected fields to stop
163 // PostgreSQL from whining
164 $this->addOption( 'GROUP BY', $selectFields );
165 $forceNameTitleIndex = false;
168 if ( $forceNameTitleIndex ) {
169 $this->addOption( 'USE INDEX', 'name_title' );
172 $limit = $params['limit'];
173 $this->addOption( 'LIMIT', $limit +
1 );
174 $res = $this->select( __METHOD__
);
176 //Get gender information
177 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
179 foreach ( $res as $row ) {
180 $users[] = $row->page_title
;
182 GenderCache
::singleton()->doQuery( $users, __METHOD__
);
183 $res->rewind(); //reset
187 $result = $this->getResult();
188 foreach ( $res as $row ) {
189 if ( ++
$count > $limit ) {
190 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
191 $this->setContinueEnumParameter( 'continue', $row->page_title
);
195 if ( is_null( $resultPageSet ) ) {
196 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
198 'pageid' => intval( $row->page_id
),
199 'ns' => intval( $title->getNamespace() ),
200 'title' => $title->getPrefixedText()
202 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
204 $this->setContinueEnumParameter( 'continue', $row->page_title
);
208 $resultPageSet->processDbRow( $row );
212 if ( is_null( $resultPageSet ) ) {
213 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
217 public function getAllowedParams() {
218 global $wgRestrictionLevels;
225 'namespace' => array(
226 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
227 ApiBase
::PARAM_TYPE
=> 'namespace',
229 'filterredir' => array(
230 ApiBase
::PARAM_DFLT
=> 'all',
231 ApiBase
::PARAM_TYPE
=> array(
238 ApiBase
::PARAM_TYPE
=> 'integer',
241 ApiBase
::PARAM_TYPE
=> 'integer',
244 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
245 ApiBase
::PARAM_ISMULTI
=> true
248 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
249 ApiBase
::PARAM_ISMULTI
=> true
251 'prfiltercascade' => array(
252 ApiBase
::PARAM_DFLT
=> 'all',
253 ApiBase
::PARAM_TYPE
=> array(
260 ApiBase
::PARAM_DFLT
=> 10,
261 ApiBase
::PARAM_TYPE
=> 'limit',
262 ApiBase
::PARAM_MIN
=> 1,
263 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
264 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
267 ApiBase
::PARAM_DFLT
=> 'ascending',
268 ApiBase
::PARAM_TYPE
=> array(
273 'filterlanglinks' => array(
274 ApiBase
::PARAM_TYPE
=> array(
279 ApiBase
::PARAM_DFLT
=> 'all'
282 ApiBase
::PARAM_TYPE
=> array(
287 ApiBase
::PARAM_DFLT
=> 'all'
292 public function getParamDescription() {
293 $p = $this->getModulePrefix();
295 'from' => 'The page title to start enumerating from',
296 'continue' => 'When more results are available, use this to continue',
297 'to' => 'The page title to stop enumerating at',
298 'prefix' => 'Search for all page titles that begin with this value',
299 'namespace' => 'The namespace to enumerate',
300 'filterredir' => 'Which pages to list',
301 'dir' => 'The direction in which to list',
302 'minsize' => 'Limit to pages with at least this many bytes',
303 'maxsize' => 'Limit to pages with at most this many bytes',
304 'prtype' => 'Limit to protected pages only',
305 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
306 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
307 'filterlanglinks' => 'Filter based on whether a page has langlinks',
308 'limit' => 'How many total pages to return.',
310 'Which protection expiry to filter the page on',
311 ' indefinite - Get only pages with indefinite protection expiry',
312 ' definite - Get only pages with a definite (specific) protection expiry',
313 ' all - Get pages with any protections expiry'
318 public function getResultProperties() {
321 'pageid' => 'integer',
328 public function getDescription() {
329 return 'Enumerate all pages sequentially in a given namespace';
332 public function getPossibleErrors() {
333 return array_merge( parent
::getPossibleErrors(), array(
334 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
335 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
339 public function getExamples() {
341 'api.php?action=query&list=allpages&apfrom=B' => array(
343 'Show a list of pages starting at the letter "B"',
345 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
346 'Using as Generator',
347 'Show info about 4 pages starting at the letter "T"',
349 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
350 'Show content of first 2 non-redirect pages beginning at "Re"',
355 public function getHelpUrls() {
356 return 'https://www.mediawiki.org/wiki/API:Allpages';