Reduced some master queries by adding flags to Revision functions.
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryInfo.php
1 <?php
2 /**
3 *
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 /**
28 * This query adds the <categories> subelement to all pages with the list of categories the page is in
29 *
30 * @ingroup API
31 */
32 class ApiQueryCategoryInfo extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'ci' );
36 }
37
38 public function execute() {
39 $params = $this->extractRequestParams();
40 $alltitles = $this->getPageSet()->getAllTitlesByNamespace();
41 if ( empty( $alltitles[NS_CATEGORY] ) ) {
42 return;
43 }
44 $categories = $alltitles[NS_CATEGORY];
45
46 $titles = $this->getPageSet()->getGoodTitles() +
47 $this->getPageSet()->getMissingTitles();
48 $cattitles = array();
49 foreach ( $categories as $c ) {
50 $t = $titles[$c];
51 $cattitles[$c] = $t->getDBkey();
52 }
53
54 $this->addTables( array( 'category', 'page', 'page_props' ) );
55 $this->addJoinConds( array(
56 'page' => array( 'LEFT JOIN', array(
57 'page_namespace' => NS_CATEGORY,
58 'page_title=cat_title' ) ),
59 'page_props' => array( 'LEFT JOIN', array(
60 'pp_page=page_id',
61 'pp_propname' => 'hiddencat' ) ),
62 ) );
63
64 $this->addFields( array( 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files', 'pp_propname AS cat_hidden' ) );
65 $this->addWhere( array( 'cat_title' => $cattitles ) );
66
67 if ( !is_null( $params['continue'] ) ) {
68 $title = $this->getDB()->addQuotes( $params['continue'] );
69 $this->addWhere( "cat_title >= $title" );
70 }
71 $this->addOption( 'ORDER BY', 'cat_title' );
72
73 $res = $this->select( __METHOD__ );
74
75 $catids = array_flip( $cattitles );
76 foreach ( $res as $row ) {
77 $vals = array();
78 $vals['size'] = intval( $row->cat_pages );
79 $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
80 $vals['files'] = intval( $row->cat_files );
81 $vals['subcats'] = intval( $row->cat_subcats );
82 if ( $row->cat_hidden ) {
83 $vals['hidden'] = '';
84 }
85 $fit = $this->addPageSubItems( $catids[$row->cat_title], $vals );
86 if ( !$fit ) {
87 $this->setContinueEnumParameter( 'continue', $row->cat_title );
88 break;
89 }
90 }
91 }
92
93 public function getCacheMode( $params ) {
94 return 'public';
95 }
96
97 public function getAllowedParams() {
98 return array(
99 'continue' => null,
100 );
101 }
102
103 public function getParamDescription() {
104 return array(
105 'continue' => 'When more results are available, use this to continue',
106 );
107 }
108
109 public function getDescription() {
110 return 'Returns information about the given categories';
111 }
112
113 public function getExamples() {
114 return 'api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar';
115 }
116
117 public function getHelpUrls() {
118 return 'https://www.mediawiki.org/wiki/API:Properties#categoryinfo_.2F_ci';
119 }
120
121 public function getVersion() {
122 return __CLASS__ . ': $Id$';
123 }
124 }