* (bug 12584) Don't reset cl_timestamp when auto-updating sort key on move
[mediawiki.git] / includes / api / ApiQueryAllCategories.php
blobe65ffe0e7bbfbb5f0b9cce85ee8a0a1f9c0c5e67
1 <?php
3 /*
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');
31 /**
32 * Query module to enumerate all categories, even the ones that don't have
33 * category pages.
35 * @addtogroup API
37 class ApiQueryAllCategories extends ApiQueryGeneratorBase {
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'ac');
43 public function execute() {
44 $this->run();
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
51 private function run($resultPageSet = null) {
53 $db = $this->getDB();
54 $params = $this->extractRequestParams();
56 $this->addTables('categorylinks');
57 $this->addFields('cl_to');
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'])) . "%'");
64 $this->addOption('LIMIT', $params['limit']+1);
65 $this->addOption('ORDER BY', 'cl_to' . ($params['dir'] == 'descending' ? ' DESC' : ''));
66 $this->addOption('DISTINCT');
68 $res = $this->select(__METHOD__);
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;
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();
88 $db->freeResult($res);
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);
99 protected 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'
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
120 protected 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.'
129 protected function getDescription() {
130 return 'Enumerate all categories';
133 protected function getExamples() {
134 return array (
135 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
139 public function getVersion() {
140 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 28216 2007-12-06 18:33:18Z vasilievvv $';