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
49 public function executeGenerator( $resultPageSet ) {
50 if ( $resultPageSet->isResolvingRedirects() ) {
51 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
54 $this->run( $resultPageSet );
58 * @param $resultPageSet ApiPageSet
60 private function run( $resultPageSet = null ) {
63 $params = $this->extractRequestParams();
66 $this->addTables( 'page' );
68 if ( $params['filterredir'] == 'redirects' ) {
69 $this->addWhereFld( 'page_is_redirect', 1 );
70 } elseif ( $params['filterredir'] == 'nonredirects' ) {
71 $this->addWhereFld( 'page_is_redirect', 0 );
74 $this->addWhereFld( 'page_namespace', $params['namespace'] );
75 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
76 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
77 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
78 $this->addWhereRange( 'page_title', $dir, $from, $to );
80 if ( isset( $params['prefix'] ) ) {
81 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
84 if ( is_null( $resultPageSet ) ) {
85 $selectFields = array(
91 $selectFields = $resultPageSet->getPageTableFields();
94 $this->addFields( $selectFields );
95 $forceNameTitleIndex = true;
96 if ( isset( $params['minsize'] ) ) {
97 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
98 $forceNameTitleIndex = false;
101 if ( isset( $params['maxsize'] ) ) {
102 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
103 $forceNameTitleIndex = false;
106 // Page protection filtering
107 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
108 $this->addTables( 'page_restrictions' );
109 $this->addWhere( 'page_id=pr_page' );
110 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
112 if ( count( $params['prtype'] ) ) {
113 $this->addWhereFld( 'pr_type', $params['prtype'] );
115 if ( isset( $params['prlevel'] ) ) {
116 // Remove the empty string and '*' from the prlevel array
117 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
119 if ( count( $prlevel ) ) {
120 $this->addWhereFld( 'pr_level', $prlevel );
123 if ( $params['prfiltercascade'] == 'cascading' ) {
124 $this->addWhereFld( 'pr_cascade', 1 );
125 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
126 $this->addWhereFld( 'pr_cascade', 0 );
129 $this->addOption( 'DISTINCT' );
131 $forceNameTitleIndex = false;
133 if ( $params['prexpiry'] == 'indefinite' ) {
134 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
135 } elseif ( $params['prexpiry'] == 'definite' ) {
136 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
139 } elseif ( isset( $params['prlevel'] ) ) {
140 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
143 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
144 $this->addTables( 'langlinks' );
145 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
146 $this->addWhere( 'll_from IS NULL' );
147 $forceNameTitleIndex = false;
148 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
149 $this->addTables( 'langlinks' );
150 $this->addWhere( 'page_id=ll_from' );
151 $this->addOption( 'STRAIGHT_JOIN' );
152 // We have to GROUP BY all selected fields to stop
153 // PostgreSQL from whining
154 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
155 $forceNameTitleIndex = false;
158 if ( $forceNameTitleIndex ) {
159 $this->addOption( 'USE INDEX', 'name_title' );
162 $limit = $params['limit'];
163 $this->addOption( 'LIMIT', $limit +
1 );
164 $res = $this->select( __METHOD__
);
167 $result = $this->getResult();
168 foreach ( $res as $row ) {
169 if ( ++
$count > $limit ) {
170 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
171 // TODO: Security issue - if the user has no right to view next title, it will still be shown
172 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
176 if ( is_null( $resultPageSet ) ) {
177 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
179 'pageid' => intval( $row->page_id
),
180 'ns' => intval( $title->getNamespace() ),
181 'title' => $title->getPrefixedText()
183 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
185 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title
) );
189 $resultPageSet->processDbRow( $row );
193 if ( is_null( $resultPageSet ) ) {
194 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
198 public function getAllowedParams() {
199 global $wgRestrictionLevels;
205 'namespace' => array(
206 ApiBase
::PARAM_DFLT
=> 0,
207 ApiBase
::PARAM_TYPE
=> 'namespace',
209 'filterredir' => array(
210 ApiBase
::PARAM_DFLT
=> 'all',
211 ApiBase
::PARAM_TYPE
=> array(
218 ApiBase
::PARAM_TYPE
=> 'integer',
221 ApiBase
::PARAM_TYPE
=> 'integer',
224 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
225 ApiBase
::PARAM_ISMULTI
=> true
228 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
229 ApiBase
::PARAM_ISMULTI
=> true
231 'prfiltercascade' => array(
232 ApiBase
::PARAM_DFLT
=> 'all',
233 ApiBase
::PARAM_TYPE
=> array(
240 ApiBase
::PARAM_DFLT
=> 10,
241 ApiBase
::PARAM_TYPE
=> 'limit',
242 ApiBase
::PARAM_MIN
=> 1,
243 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
244 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
247 ApiBase
::PARAM_DFLT
=> 'ascending',
248 ApiBase
::PARAM_TYPE
=> array(
253 'filterlanglinks' => array(
254 ApiBase
::PARAM_TYPE
=> array(
259 ApiBase
::PARAM_DFLT
=> 'all'
262 ApiBase
::PARAM_TYPE
=> array(
267 ApiBase
::PARAM_DFLT
=> 'all'
272 public function getParamDescription() {
273 $p = $this->getModulePrefix();
275 'from' => 'The page title to start enumerating from',
276 'to' => 'The page title to stop enumerating at',
277 'prefix' => 'Search for all page titles that begin with this value',
278 'namespace' => 'The namespace to enumerate',
279 'filterredir' => 'Which pages to list',
280 'dir' => 'The direction in which to list',
281 'minsize' => 'Limit to pages with at least this many bytes',
282 'maxsize' => 'Limit to pages with at most this many bytes',
283 'prtype' => 'Limit to protected pages only',
284 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
285 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
286 'filterlanglinks' => 'Filter based on whether a page has langlinks',
287 'limit' => 'How many total pages to return.',
289 'Which protection expiry to filter the page on',
290 ' indefinite - Get only pages with indefinite protection expiry',
291 ' definite - Get only pages with a definite (specific) protection expiry',
292 ' all - Get pages with any protections expiry'
297 public function getDescription() {
298 return 'Enumerate all pages sequentially in a given namespace';
301 public function getPossibleErrors() {
302 return array_merge( parent
::getPossibleErrors(), array(
303 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
304 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
308 public function getExamples() {
310 'api.php?action=query&list=allpages&apfrom=B' => array(
312 'Show a list of pages starting at the letter "B"',
314 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
315 'Using as Generator',
316 'Show info about 4 pages starting at the letter "T"',
318 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
319 'Show content of first 2 non-redirect pages begining at "Re"',
324 public function getHelpUrls() {
325 return 'https://www.mediawiki.org/wiki/API:Allpages';
328 public function getVersion() {
329 return __CLASS__
. ': $Id$';