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