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