* (bug 24677) axto= parameters added to allcategories, allimages, alllinks, allmessag...
[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 $fromdir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
63 $todir = ( $params['dir'] != 'descending' ? 'older' : 'newer' );
64 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
65 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
66 $this->addWhereRange( 'cat_title', $fromdir, $from, null );
67 $this->addWhereRange( 'cat_title', $todir, $to, null );
68
69 if ( isset( $params['prefix'] ) ) {
70 $this->addWhere( 'cat_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
71 }
72
73 $this->addOption( 'LIMIT', $params['limit'] + 1 );
74 $this->addOption( 'ORDER BY', 'cat_title' . ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
75
76 $prop = array_flip( $params['prop'] );
77 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset( $prop['size'] ) );
78 if ( isset( $prop['hidden'] ) ) {
79 $this->addTables( array( 'page', 'page_props' ) );
80 $this->addJoinConds( array(
81 'page' => array( 'LEFT JOIN', array(
82 'page_namespace' => NS_CATEGORY,
83 'page_title=cat_title' ) ),
84 'page_props' => array( 'LEFT JOIN', array(
85 'pp_page=page_id',
86 'pp_propname' => 'hiddencat' ) ),
87 ) );
88 $this->addFields( 'pp_propname AS cat_hidden' );
89 }
90
91 $res = $this->select( __METHOD__ );
92
93 $pages = array();
94
95 $result = $this->getResult();
96 $count = 0;
97 foreach ( $res as $row ) {
98 if ( ++ $count > $params['limit'] ) {
99 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
100 // TODO: Security issue - if the user has no right to view next title, it will still be shown
101 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title ) );
102 break;
103 }
104
105 // Normalize titles
106 $titleObj = Title::makeTitle( NS_CATEGORY, $row->cat_title );
107 if ( !is_null( $resultPageSet ) ) {
108 $pages[] = $titleObj->getPrefixedText();
109 } else {
110 $item = array();
111 $result->setContent( $item, $titleObj->getText() );
112 if ( isset( $prop['size'] ) ) {
113 $item['size'] = intval( $row->cat_pages );
114 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
115 $item['files'] = intval( $row->cat_files );
116 $item['subcats'] = intval( $row->cat_subcats );
117 }
118 if ( isset( $prop['hidden'] ) && $row->cat_hidden ) {
119 $item['hidden'] = '';
120 }
121 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->cat_title ) );
124 break;
125 }
126 }
127 }
128
129 if ( is_null( $resultPageSet ) ) {
130 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'c' );
131 } else {
132 $resultPageSet->populateFromTitles( $pages );
133 }
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'from' => null,
139 'to' => null,
140 'prefix' => null,
141 'dir' => array(
142 ApiBase::PARAM_DFLT => 'ascending',
143 ApiBase::PARAM_TYPE => array(
144 'ascending',
145 'descending'
146 ),
147 ),
148 'limit' => array(
149 ApiBase::PARAM_DFLT => 10,
150 ApiBase::PARAM_TYPE => 'limit',
151 ApiBase::PARAM_MIN => 1,
152 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
153 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
154 ),
155 'prop' => array(
156 ApiBase::PARAM_TYPE => array( 'size', 'hidden' ),
157 ApiBase::PARAM_DFLT => '',
158 ApiBase::PARAM_ISMULTI => true
159 ),
160 );
161 }
162
163 public function getParamDescription() {
164 return array(
165 'from' => 'The category to start enumerating from',
166 'to' => 'The category to stop enumerating at',
167 'prefix' => 'Search for all category titles that begin with this value',
168 'dir' => 'Direction to sort in',
169 'limit' => 'How many categories to return',
170 'prop' => array(
171 'Which properties to get',
172 ' size - Adds number of pages in the category',
173 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
174 ),
175 );
176 }
177
178 public function getDescription() {
179 return 'Enumerate all categories';
180 }
181
182 protected function getExamples() {
183 return array(
184 'api.php?action=query&list=allcategories&acprop=size',
185 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }