* Refactor Title::isValidMoveOperation() and Title::moveTo() to return an array of...
[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 if (!is_null($params['from']))
60 $this->addWhere('cat_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
61 if (isset ($params['prefix']))
62 $this->addWhere("cat_title LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
63
64 $this->addOption('LIMIT', $params['limit']+1);
65 $this->addOption('ORDER BY', 'cat_title' . ($params['dir'] == 'descending' ? ' DESC' : ''));
66
67 $prop = array_flip($params['prop']);
68 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset($prop['size']) );
69 //$this->addFieldsIf( 'cat_hidden', isset($prop['hidden']) );
70
71 $res = $this->select(__METHOD__);
72
73 $pages = array();
74 $categories = array();
75 $result = $this->getResult();
76 $count = 0;
77 while ($row = $db->fetchObject($res)) {
78 if (++ $count > $params['limit']) {
79 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
80 // TODO: Security issue - if the user has no right to view next title, it will still be shown
81 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->cat_title));
82 break;
83 }
84
85 // Normalize titles
86 $titleObj = Title::makeTitle(NS_CATEGORY, $row->cat_title);
87 if(!is_null($resultPageSet))
88 $pages[] = $titleObj->getPrefixedText();
89 else {
90 $item = array();
91 $result->setContent( $item, $titleObj->getText() );
92 if( isset( $prop['size'] ) ) {
93 $item['size'] = $row->cat_pages;
94 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
95 $item['files'] = $row->cat_files;
96 $item['subcats'] = $row->cat_subcats;
97 }
98 //Isn't populated, so doesn't work
99 //if( isset( $prop['hidden'] ) && $row->cat_hidden )
100 // $item['hidden'] = '';
101 $categories[] = $item;
102 }
103 }
104 $db->freeResult($res);
105
106 if (is_null($resultPageSet)) {
107 $result->setIndexedTagName($categories, 'c');
108 $result->addValue('query', $this->getModuleName(), $categories);
109 } else {
110 $resultPageSet->populateFromTitles($pages);
111 }
112 }
113
114 public function getAllowedParams() {
115 return array (
116 'from' => null,
117 'prefix' => null,
118 'dir' => array(
119 ApiBase :: PARAM_DFLT => 'ascending',
120 ApiBase :: PARAM_TYPE => array(
121 'ascending',
122 'descending'
123 ),
124 ),
125 'limit' => array (
126 ApiBase :: PARAM_DFLT => 10,
127 ApiBase :: PARAM_TYPE => 'limit',
128 ApiBase :: PARAM_MIN => 1,
129 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
130 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
131 ),
132 'prop' => array (
133 ApiBase :: PARAM_TYPE => array( 'size', /*'hidden'*/ ),
134 ApiBase :: PARAM_DFLT => '',
135 ApiBase :: PARAM_ISMULTI => true
136 ),
137 );
138 }
139
140 public function getParamDescription() {
141 return array (
142 'from' => 'The category to start enumerating from.',
143 'prefix' => 'Search for all category titles that begin with this value.',
144 'dir' => 'Direction to sort in.',
145 'limit' => 'How many categories to return.',
146 'prop' => 'Which properties to get',
147 );
148 }
149
150 public function getDescription() {
151 return 'Enumerate all categories';
152 }
153
154 protected function getExamples() {
155 return array (
156 'api.php?action=query&list=allcategories&acprop=size',
157 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
158 );
159 }
160
161 public function getVersion() {
162 return __CLASS__ . ': $Id$';
163 }
164 }