Stylize API up to date
[lhc/web/wiklou.git] / includes / api / ApiQueryCategories.php
1 <?php
2
3 /**
4 * Created on May 13, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 * A query module to enumerate categories the set of pages belong to.
33 *
34 * @ingroup API
35 */
36 class ApiQueryCategories extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'cl' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function getCacheMode( $params ) {
47 return 'public';
48 }
49
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
52 }
53
54 private function run( $resultPageSet = null ) {
55 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
56 return; // nothing to do
57 }
58
59 $params = $this->extractRequestParams();
60 $prop = array_flip( (array)$params['prop'] );
61 $show = array_flip( (array)$params['show'] );
62
63 $this->addFields( array(
64 'cl_from',
65 'cl_to'
66 ) );
67
68 $this->addFieldsIf( 'cl_sortkey', isset( $prop['sortkey'] ) );
69 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
70
71 $this->addTables( 'categorylinks' );
72 $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
73 if ( !is_null( $params['categories'] ) ) {
74 $cats = array();
75 foreach ( $params['categories'] as $cat ) {
76 $title = Title::newFromText( $cat );
77 if ( !$title || $title->getNamespace() != NS_CATEGORY ) {
78 $this->setWarning( "``$cat'' is not a category" );
79 } else {
80 $cats[] = $title->getDBkey();
81 }
82 }
83 $this->addWhereFld( 'cl_to', $cats );
84 }
85
86 if ( !is_null( $params['continue'] ) ) {
87 $cont = explode( '|', $params['continue'] );
88 if ( count( $cont ) != 2 ) {
89 $this->dieUsage( "Invalid continue param. You should pass the " .
90 "original value returned by the previous query", "_badcontinue" );
91 }
92 $clfrom = intval( $cont[0] );
93 $clto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
94 $this->addWhere(
95 "cl_from > $clfrom OR " .
96 "(cl_from = $clfrom AND " .
97 "cl_to >= '$clto')"
98 );
99 }
100
101 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
102 $this->dieUsageMsg( array( 'show' ) );
103 }
104 if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) )
105 {
106 $this->addOption( 'STRAIGHT_JOIN' );
107 $this->addTables( array( 'page', 'page_props' ) );
108 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
109 $this->addJoinConds( array(
110 'page' => array( 'LEFT JOIN', array(
111 'page_namespace' => NS_CATEGORY,
112 'page_title = cl_to' ) ),
113 'page_props' => array( 'LEFT JOIN', array(
114 'pp_page=page_id',
115 'pp_propname' => 'hiddencat' ) )
116 ) );
117 if ( isset( $show['hidden'] ) ) {
118 $this->addWhere( array( 'pp_propname IS NOT NULL' ) );
119 } elseif ( isset( $show['!hidden'] ) ) {
120 $this->addWhere( array( 'pp_propname IS NULL' ) );
121 }
122 }
123
124 $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
125 // Don't order by cl_from if it's constant in the WHERE clause
126 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
127 $this->addOption( 'ORDER BY', 'cl_to' );
128 } else {
129 $this->addOption( 'ORDER BY', "cl_from, cl_to" );
130 }
131
132 $res = $this->select( __METHOD__ );
133
134 if ( is_null( $resultPageSet ) ) {
135 $count = 0;
136 foreach ( $res as $row ) {
137 if ( ++$count > $params['limit'] ) {
138 // We've reached the one extra which shows that
139 // there are additional pages to be had. Stop here...
140 $this->setContinueEnumParameter( 'continue', $row->cl_from .
141 '|' . $this->keyToTitle( $row->cl_to ) );
142 break;
143 }
144
145 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
146 $vals = array();
147 ApiQueryBase::addTitleInfo( $vals, $title );
148 if ( isset( $prop['sortkey'] ) ) {
149 $vals['sortkey'] = $row->cl_sortkey;
150 }
151 if ( isset( $prop['timestamp'] ) ) {
152 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
153 }
154 if ( isset( $prop['hidden'] ) && !is_null( $row->pp_propname ) ) {
155 $vals['hidden'] = '';
156 }
157
158 $fit = $this->addPageSubItem( $row->cl_from, $vals );
159 if ( !$fit ) {
160 $this->setContinueEnumParameter( 'continue', $row->cl_from .
161 '|' . $this->keyToTitle( $row->cl_to ) );
162 break;
163 }
164 }
165 } else {
166 $titles = array();
167 foreach ( $res as $row ) {
168 if ( ++$count > $params['limit'] ) {
169 // We've reached the one extra which shows that
170 // there are additional pages to be had. Stop here...
171 $this->setContinueEnumParameter( 'continue', $row->cl_from .
172 '|' . $this->keyToTitle( $row->cl_to ) );
173 break;
174 }
175
176 $titles[] = Title :: makeTitle( NS_CATEGORY, $row->cl_to );
177 }
178 $resultPageSet->populateFromTitles( $titles );
179 }
180 }
181
182 public function getAllowedParams() {
183 return array(
184 'prop' => array(
185 ApiBase::PARAM_ISMULTI => true,
186 ApiBase::PARAM_TYPE => array (
187 'sortkey',
188 'timestamp',
189 'hidden',
190 )
191 ),
192 'show' => array(
193 ApiBase::PARAM_ISMULTI => true,
194 ApiBase::PARAM_TYPE => array(
195 'hidden',
196 '!hidden',
197 )
198 ),
199 'limit' => array(
200 ApiBase::PARAM_DFLT => 10,
201 ApiBase::PARAM_TYPE => 'limit',
202 ApiBase::PARAM_MIN => 1,
203 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
204 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
205 ),
206 'continue' => null,
207 'categories' => array(
208 ApiBase::PARAM_ISMULTI => true,
209 ),
210 );
211 }
212
213 public function getParamDescription() {
214 return array(
215 'prop' => array(
216 'Which additional properties to get for each category',
217 ' sortkey - Adds the sortkey for the category',
218 ' timestamp - Adds timestamp of when the category was added',
219 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
220 ),
221 'limit' => 'How many categories to return',
222 'show' => 'Which kind of categories to show',
223 'continue' => 'When more results are available, use this to continue',
224 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
225 );
226 }
227
228 public function getDescription() {
229 return 'List all categories the page(s) belong to';
230 }
231
232 public function getPossibleErrors() {
233 return array_merge( parent::getPossibleErrors(), array(
234 array( 'show' ),
235 ) );
236 }
237
238 protected function getExamples() {
239 return array(
240 'Get a list of categories [[Albert Einstein]] belongs to:',
241 ' api.php?action=query&prop=categories&titles=Albert%20Einstein',
242 'Get information about all categories used in the [[Albert Einstein]]:',
243 ' api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info'
244 );
245 }
246
247 public function getVersion() {
248 return __CLASS__ . ': $Id$';
249 }
250 }