API:
[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'] == 'ZtoA' ? ' 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 if(!is_null($resultPageSet))
82 $titleObj = Title::newFromText('Category:' . $row->cl_to);
83 else
84 $titleObj = Title::newFromText($row->cl_to);
85 $pages[] = $titleObj->getPrefixedText();
86 }
87 $db->freeResult($res);
88
89 if (is_null($resultPageSet)) {
90 $result = $this->getResult();
91 $result->setIndexedTagName($pages, 'c');
92 $result->addValue('query', $this->getModuleName(), $pages);
93 } else {
94 $resultPageSet->populateFromTitles($pages);
95 }
96 }
97
98 protected function getAllowedParams() {
99 return array (
100 'from' => null,
101 'prefix' => null,
102 'dir' => array(
103 ApiBase :: PARAM_DFLT => 'AtoZ',
104 ApiBase :: PARAM_TYPE => array(
105 'AtoZ',
106 'ZtoA'
107 ),
108 ),
109 'limit' => array (
110 ApiBase :: PARAM_DFLT => 10,
111 ApiBase :: PARAM_TYPE => 'limit',
112 ApiBase :: PARAM_MIN => 1,
113 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
114 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
115 )
116 );
117 }
118
119 protected function getParamDescription() {
120 return array (
121 'from' => 'The category to start enumerating from.',
122 'prefix' => 'Search for all category titles that begin with this value.',
123 'dir' => 'Direction to sort in.',
124 'limit' => 'How many categories to return.'
125 );
126 }
127
128 protected function getDescription() {
129 return 'Enumerate all categories';
130 }
131
132 protected function getExamples() {
133 return array (
134 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
135 );
136 }
137
138 public function getVersion() {
139 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 28216 2007-12-06 18:33:18Z vasilievvv $';
140 }
141 }