4 * Created on December 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to enumerate all categories, even the ones that don't have
37 class ApiQueryAllCategories
extends ApiQueryGeneratorBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'ac');
43 public function execute() {
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
51 private function run($resultPageSet = null) {
54 $params = $this->extractRequestParams();
56 $this->addTables('category');
57 $this->addFields('cat_title');
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'])) . "%'");
64 $this->addOption('LIMIT', $params['limit']+
1);
65 $this->addOption('ORDER BY', 'cat_title' . ($params['dir'] == 'descending' ?
' DESC' : ''));
66 $this->addOption('DISTINCT');
68 $prop = array_flip($params['prop']);
69 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset($prop['size']) );
70 //$this->addFieldsIf( 'cat_hidden', isset($prop['hidden']) );
72 $res = $this->select(__METHOD__
);
75 $categories = array();
76 $result = $this->getResult();
78 while ($row = $db->fetchObject($res)) {
79 if (++
$count > $params['limit']) {
80 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
81 // TODO: Security issue - if the user has no right to view next title, it will still be shown
82 $this->setContinueEnumParameter('from', ApiQueryBase
:: keyToTitle($row->cl_to
));
87 $titleObj = Title
::makeTitle(NS_CATEGORY
, $row->cat_title
);
88 if(!is_null($resultPageSet))
89 $pages[] = $titleObj->getPrefixedText();
92 $result->setContent( $item, $titleObj->getText() );
93 if( isset( $prop['size'] ) ) {
94 $item['size'] = $row->cat_pages
;
95 $item['pages'] = $row->cat_pages
- $row->cat_subcats
- $row->cat_files
;
96 $item['files'] = $row->cat_files
;
97 $item['subcats'] = $row->cat_subcats
;
99 //Isn't populated, so doesn't work
100 //if( isset( $prop['hidden'] ) && $row->cat_hidden )
101 // $item['hidden'] = '';
102 $categories[] = $item;
105 $db->freeResult($res);
107 if (is_null($resultPageSet)) {
108 $result->setIndexedTagName($categories, 'c');
109 $result->addValue('query', $this->getModuleName(), $categories);
111 $resultPageSet->populateFromTitles($pages);
115 public function getAllowedParams() {
120 ApiBase
:: PARAM_DFLT
=> 'ascending',
121 ApiBase
:: PARAM_TYPE
=> array(
127 ApiBase
:: PARAM_DFLT
=> 10,
128 ApiBase
:: PARAM_TYPE
=> 'limit',
129 ApiBase
:: PARAM_MIN
=> 1,
130 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
131 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
134 ApiBase
:: PARAM_TYPE
=> array( 'size', /*'hidden'*/ ),
135 ApiBase
:: PARAM_DFLT
=> '',
136 ApiBase
:: PARAM_ISMULTI
=> true
141 public function getParamDescription() {
143 'from' => 'The category to start enumerating from.',
144 'prefix' => 'Search for all category titles that begin with this value.',
145 'dir' => 'Direction to sort in.',
146 'limit' => 'How many categories to return.',
147 'prop' => 'Which properties to get',
151 public function getDescription() {
152 return 'Enumerate all categories';
155 protected function getExamples() {
157 'api.php?action=query&list=allcategories&acprop=size',
158 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
162 public function getVersion() {
163 return __CLASS__
. ': $Id$';