Removed the 'eclipse helper' bit on top of every API module
[lhc/web/wiklou.git] / includes / api / ApiQueryAllCategories.php
1 <?php
2 /**
3 *
4 *
5 * Created on December 12, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate all categories, even the ones that don't have
29 * category pages.
30 *
31 * @ingroup API
32 */
33 class ApiQueryAllCategories extends ApiQueryGeneratorBase {
34
35 public function __construct( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'ac' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function getCacheMode( $params ) {
44 return 'public';
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 private function run( $resultPageSet = null ) {
56 $db = $this->getDB();
57 $params = $this->extractRequestParams();
58
59 $this->addTables( 'category' );
60 $this->addFields( 'cat_title' );
61
62 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
63 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
64 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
65 $this->addWhereRange( 'cat_title', $dir, $from, $to );
66
67 $min = $params['min'];
68 $max = $params['max'];
69 $this->addWhereRange( 'cat_pages', $dir, $min, $max );
70
71 if ( isset( $params['prefix'] ) ) {
72 $this->addWhere( 'cat_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
73 }
74
75 $this->addOption( 'LIMIT', $params['limit'] + 1 );
76 $this->addOption( 'ORDER BY', 'cat_title' . ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
77
78 $prop = array_flip( $params['prop'] );
79 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset( $prop['size'] ) );
80 if ( isset( $prop['hidden'] ) ) {
81 $this->addTables( array( 'page', 'page_props' ) );
82 $this->addJoinConds( array(
83 'page' => array( 'LEFT JOIN', array(
84 'page_namespace' => NS_CATEGORY,
85 'page_title=cat_title' ) ),
86 'page_props' => array( 'LEFT JOIN', array(
87 'pp_page=page_id',
88 'pp_propname' => 'hiddencat' ) ),
89 ) );
90 $this->addFields( 'pp_propname AS cat_hidden' );
91 }
92
93 $res = $this->select( __METHOD__ );
94
95 $pages = array();
96
97 $result = $this->getResult();
98 $count = 0;
99 foreach ( $res as $row ) {
100 if ( ++ $count > $params['limit'] ) {
101 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
102 // TODO: Security issue - if the user has no right to view next title, it will still be shown
103 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title ) );
104 break;
105 }
106
107 // Normalize titles
108 $titleObj = Title::makeTitle( NS_CATEGORY, $row->cat_title );
109 if ( !is_null( $resultPageSet ) ) {
110 $pages[] = $titleObj->getPrefixedText();
111 } else {
112 $item = array();
113 $result->setContent( $item, $titleObj->getText() );
114 if ( isset( $prop['size'] ) ) {
115 $item['size'] = intval( $row->cat_pages );
116 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
117 $item['files'] = intval( $row->cat_files );
118 $item['subcats'] = intval( $row->cat_subcats );
119 }
120 if ( isset( $prop['hidden'] ) && $row->cat_hidden ) {
121 $item['hidden'] = '';
122 }
123 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title ) );
126 break;
127 }
128 }
129 }
130
131 if ( is_null( $resultPageSet ) ) {
132 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'c' );
133 } else {
134 $resultPageSet->populateFromTitles( $pages );
135 }
136 }
137
138 public function getAllowedParams() {
139 return array(
140 'from' => null,
141 'to' => null,
142 'prefix' => null,
143 'dir' => array(
144 ApiBase::PARAM_DFLT => 'ascending',
145 ApiBase::PARAM_TYPE => array(
146 'ascending',
147 'descending'
148 ),
149 ),
150 'min' => array(
151 ApiBase::PARAM_DFLT => null,
152 ApiBase::PARAM_TYPE => 'integer'
153 ),
154 'max' => array(
155 ApiBase::PARAM_DFLT => null,
156 ApiBase::PARAM_TYPE => 'integer'
157 ),
158 'limit' => array(
159 ApiBase::PARAM_DFLT => 10,
160 ApiBase::PARAM_TYPE => 'limit',
161 ApiBase::PARAM_MIN => 1,
162 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
163 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
164 ),
165 'prop' => array(
166 ApiBase::PARAM_TYPE => array( 'size', 'hidden' ),
167 ApiBase::PARAM_DFLT => '',
168 ApiBase::PARAM_ISMULTI => true
169 ),
170 );
171 }
172
173 public function getParamDescription() {
174 return array(
175 'from' => 'The category to start enumerating from',
176 'to' => 'The category to stop enumerating at',
177 'prefix' => 'Search for all category titles that begin with this value',
178 'dir' => 'Direction to sort in',
179 'min' => 'Minimum number of category members',
180 'max' => 'Maximum number of category members',
181 'limit' => 'How many categories to return',
182 'prop' => array(
183 'Which properties to get',
184 ' size - Adds number of pages in the category',
185 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
186 ),
187 );
188 }
189
190 public function getDescription() {
191 return 'Enumerate all categories';
192 }
193
194 public function getExamples() {
195 return array(
196 'api.php?action=query&list=allcategories&acprop=size',
197 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
198 );
199 }
200
201 public function getHelpUrls() {
202 return 'http://www.mediawiki.org/wiki/API:Allcategories';
203 }
204
205 public function getVersion() {
206 return __CLASS__ . ': $Id$';
207 }
208 }