]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryCategoryInfo.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / api / ApiQueryCategoryInfo.php
1 <?php
2
3 /*
4  * Created on May 13, 2007
5  *
6  * API for MediaWiki 1.8+
7  *
8  * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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  * This query adds the <categories> subelement to all pages with the list of categories the page is in
33  *
34  * @ingroup API
35  */
36 class ApiQueryCategoryInfo extends ApiQueryBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'ci');
40         }
41
42         public function execute() {
43                 $params = $this->extractRequestParams();
44                 $alltitles = $this->getPageSet()->getAllTitlesByNamespace();
45                 if ( empty( $alltitles[NS_CATEGORY] ) ) {
46                         return;
47                 }
48                 $categories = $alltitles[NS_CATEGORY];
49
50                 $titles = $this->getPageSet()->getGoodTitles() +
51                                         $this->getPageSet()->getMissingTitles();
52                 $cattitles = array();
53                 foreach($categories as $c)
54                 {
55                         $t = $titles[$c];
56                         $cattitles[$c] = $t->getDBKey();
57                 }
58
59                 $this->addTables(array('category', 'page', 'page_props'));
60                 $this->addJoinConds(array(
61                         'page' => array('LEFT JOIN', array(
62                                 'page_namespace' => NS_CATEGORY,
63                                 'page_title=cat_title')),
64                         'page_props' => array('LEFT JOIN', array(
65                                 'pp_page=page_id',
66                                 'pp_propname' => 'hiddencat')),
67                 ));
68                 $this->addFields(array('cat_title', 'cat_pages', 'cat_subcats', 'cat_files', 'pp_propname AS cat_hidden'));
69                 $this->addWhere(array('cat_title' => $cattitles));
70                 if(!is_null($params['continue']))
71                 {
72                         $title = $this->getDB()->addQuotes($params['continue']);
73                         $this->addWhere("cat_title >= $title");
74                 } 
75                 $this->addOption('ORDER BY', 'cat_title');
76
77                 $db = $this->getDB();
78                 $res = $this->select(__METHOD__);
79
80                 $catids = array_flip($cattitles);
81                 while($row = $db->fetchObject($res))
82                 {
83                         $vals = array();
84                         $vals['size'] = intval($row->cat_pages);
85                         $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
86                         $vals['files'] = intval($row->cat_files);
87                         $vals['subcats'] = intval($row->cat_subcats);
88                         if($row->cat_hidden)
89                                 $vals['hidden'] = '';
90                         $fit = $this->addPageSubItems($catids[$row->cat_title], $vals);
91                         if(!$fit)
92                         {
93                                 $this->setContinueEnumParameter('continue', $row->cat_title);
94                                 break;
95                         }
96                 }
97                 $db->freeResult($res);
98         }
99
100         public function getAllowedParams() {
101                 return array (
102                         'continue' => null,
103                 );
104         }
105
106         public function getParamDescription() {
107                 return array (
108                         'continue' => 'When more results are available, use this to continue',
109                 );
110         }
111
112         public function getDescription() {
113                 return 'Returns information about the given categories';
114         }
115
116         protected function getExamples() {
117                 return "api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar";
118         }
119
120         public function getVersion() {
121                 return __CLASS__ . ': $Id: ApiQueryCategoryInfo.php 47865 2009-02-27 16:03:01Z catrope $';
122         }
123 }