]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryCategoryInfo.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryCategoryInfo.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
4  *
5  * Created on May 13, 2007
6  *
7  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33  * This query adds the <categories> subelement to all pages with the list of categories the page is in
34  *
35  * @ingroup API
36  */
37 class ApiQueryCategoryInfo extends ApiQueryBase {
38
39         public function __construct( $query, $moduleName ) {
40                 parent::__construct( $query, $moduleName, 'ci' );
41         }
42
43         public function execute() {
44                 $params = $this->extractRequestParams();
45                 $alltitles = $this->getPageSet()->getAllTitlesByNamespace();
46                 if ( empty( $alltitles[NS_CATEGORY] ) ) {
47                         return;
48                 }
49                 $categories = $alltitles[NS_CATEGORY];
50
51                 $titles = $this->getPageSet()->getGoodTitles() +
52                                         $this->getPageSet()->getMissingTitles();
53                 $cattitles = array();
54                 foreach ( $categories as $c ) {
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
69                 $this->addFields( array( 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files', 'pp_propname AS cat_hidden' ) );
70                 $this->addWhere( array( 'cat_title' => $cattitles ) );
71
72                 if ( !is_null( $params['continue'] ) ) {
73                         $title = $this->getDB()->addQuotes( $params['continue'] );
74                         $this->addWhere( "cat_title >= $title" );
75                 }
76                 $this->addOption( 'ORDER BY', 'cat_title' );
77
78                 $res = $this->select( __METHOD__ );
79
80                 $catids = array_flip( $cattitles );
81                 foreach ( $res as $row ) {
82                         $vals = array();
83                         $vals['size'] = intval( $row->cat_pages );
84                         $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
85                         $vals['files'] = intval( $row->cat_files );
86                         $vals['subcats'] = intval( $row->cat_subcats );
87                         if ( $row->cat_hidden ) {
88                                 $vals['hidden'] = '';
89                         }
90                         $fit = $this->addPageSubItems( $catids[$row->cat_title], $vals );
91                         if ( !$fit ) {
92                                 $this->setContinueEnumParameter( 'continue', $row->cat_title );
93                                 break;
94                         }
95                 }
96         }
97
98         public function getCacheMode( $params ) {
99                 return 'public';
100         }
101
102         public function getAllowedParams() {
103                 return array(
104                         'continue' => null,
105                 );
106         }
107
108         public function getParamDescription() {
109                 return array(
110                         'continue' => 'When more results are available, use this to continue',
111                 );
112         }
113
114         public function getDescription() {
115                 return 'Returns information about the given categories';
116         }
117
118         protected function getExamples() {
119                 return 'api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar';
120         }
121
122         public function getVersion() {
123                 return __CLASS__ . ': $Id$';
124         }
125 }