4 * Created on December 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 categories, even the ones that don't have
37 class ApiQueryAllCategories
extends ApiQueryGeneratorBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'ac' );
43 public function execute() {
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
51 private function run( $resultPageSet = null ) {
53 $params = $this->extractRequestParams();
55 $this->addTables( 'category' );
56 $this->addFields( 'cat_title' );
58 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
59 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
60 $this->addWhereRange( 'cat_title', $dir, $from, null );
61 if ( isset( $params['prefix'] ) ) {
62 $this->addWhere( 'cat_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
65 $this->addOption( 'LIMIT', $params['limit'] +
1 );
66 $this->addOption( 'ORDER BY', 'cat_title' . ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
68 $prop = array_flip( $params['prop'] );
69 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset( $prop['size'] ) );
70 if ( isset( $prop['hidden'] ) ) {
71 $this->addTables( array( 'page', 'page_props' ) );
72 $this->addJoinConds( array(
73 'page' => array( 'LEFT JOIN', array(
74 'page_namespace' => NS_CATEGORY
,
75 'page_title=cat_title' ) ),
76 'page_props' => array( 'LEFT JOIN', array(
78 'pp_propname' => 'hiddencat' ) ),
80 $this->addFields( 'pp_propname AS cat_hidden' );
83 $res = $this->select( __METHOD__
);
87 $result = $this->getResult();
89 foreach ( $res as $row ) {
90 if ( ++
$count > $params['limit'] ) {
91 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
92 // TODO: Security issue - if the user has no right to view next title, it will still be shown
93 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title
) );
98 $titleObj = Title
::makeTitle( NS_CATEGORY
, $row->cat_title
);
99 if ( !is_null( $resultPageSet ) ) {
100 $pages[] = $titleObj->getPrefixedText();
103 $result->setContent( $item, $titleObj->getText() );
104 if ( isset( $prop['size'] ) ) {
105 $item['size'] = intval( $row->cat_pages
);
106 $item['pages'] = $row->cat_pages
- $row->cat_subcats
- $row->cat_files
;
107 $item['files'] = intval( $row->cat_files
);
108 $item['subcats'] = intval( $row->cat_subcats
);
110 if ( isset( $prop['hidden'] ) && $row->cat_hidden
) {
111 $item['hidden'] = '';
113 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item );
115 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title
) );
121 if ( is_null( $resultPageSet ) ) {
122 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'c' );
124 $resultPageSet->populateFromTitles( $pages );
128 public function getAllowedParams() {
133 ApiBase
::PARAM_DFLT
=> 'ascending',
134 ApiBase
::PARAM_TYPE
=> array(
140 ApiBase
::PARAM_DFLT
=> 10,
141 ApiBase
::PARAM_TYPE
=> 'limit',
142 ApiBase
::PARAM_MIN
=> 1,
143 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
144 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
147 ApiBase
::PARAM_TYPE
=> array( 'size', 'hidden' ),
148 ApiBase
::PARAM_DFLT
=> '',
149 ApiBase
::PARAM_ISMULTI
=> true
154 public function getParamDescription() {
156 'from' => 'The category to start enumerating from',
157 'prefix' => 'Search for all category titles that begin with this value',
158 'dir' => 'Direction to sort in',
159 'limit' => 'How many categories to return',
161 'Which properties to get',
162 ' size - Adds number of pages in the category',
163 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
168 public function getDescription() {
169 return 'Enumerate all categories';
172 protected function getExamples() {
174 'api.php?action=query&list=allcategories&acprop=size',
175 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
179 public function getVersion() {
180 return __CLASS__
. ': $Id$';