API: Changing all modules' getParamDescription(), getAllowedParams() and getDescripti...
[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 * @addtogroup 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('categorylinks');
57 $this->addFields('cl_to');
58
59 if (!is_null($params['from']))
60 $this->addWhere('cl_to>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
61 if (isset ($params['prefix']))
62 $this->addWhere("cl_to LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
63
64 $this->addOption('LIMIT', $params['limit']+1);
65 $this->addOption('ORDER BY', 'cl_to' . ($params['dir'] == 'descending' ? ' DESC' : ''));
66 $this->addOption('DISTINCT');
67
68 $res = $this->select(__METHOD__);
69
70 $pages = array();
71 $count = 0;
72 while ($row = $db->fetchObject($res)) {
73 if (++ $count > $params['limit']) {
74 // We've reached the one extra which shows that there are additional cats to be had. Stop here...
75 // TODO: Security issue - if the user has no right to view next title, it will still be shown
76 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->cl_to));
77 break;
78 }
79
80 // Normalize titles
81 $titleObj = Title::makeTitle(NS_CATEGORY, $row->cl_to);
82 if(!is_null($resultPageSet))
83 $pages[] = $titleObj->getPrefixedText();
84 else
85 // Don't show "Category:" everywhere in non-generator mode
86 $pages[] = $titleObj->getText();
87 }
88 $db->freeResult($res);
89
90 if (is_null($resultPageSet)) {
91 $result = $this->getResult();
92 $result->setIndexedTagName($pages, 'c');
93 $result->addValue('query', $this->getModuleName(), $pages);
94 } else {
95 $resultPageSet->populateFromTitles($pages);
96 }
97 }
98
99 public function getAllowedParams() {
100 return array (
101 'from' => null,
102 'prefix' => null,
103 'dir' => array(
104 ApiBase :: PARAM_DFLT => 'ascending',
105 ApiBase :: PARAM_TYPE => array(
106 'ascending',
107 'descending'
108 ),
109 ),
110 'limit' => array (
111 ApiBase :: PARAM_DFLT => 10,
112 ApiBase :: PARAM_TYPE => 'limit',
113 ApiBase :: PARAM_MIN => 1,
114 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
115 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
116 )
117 );
118 }
119
120 public function getParamDescription() {
121 return array (
122 'from' => 'The category to start enumerating from.',
123 'prefix' => 'Search for all category titles that begin with this value.',
124 'dir' => 'Direction to sort in.',
125 'limit' => 'How many categories to return.'
126 );
127 }
128
129 public function getDescription() {
130 return 'Enumerate all categories';
131 }
132
133 protected function getExamples() {
134 return array (
135 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
136 );
137 }
138
139 public function getVersion() {
140 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 28216 2007-12-06 18:33:18Z vasilievvv $';
141 }
142 }