c75960cdffa83aa969b7d585a51a1019e68d3c89
[lhc/web/wiklou.git] / includes / api / ApiQueryCategories.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 * A query module to enumerate categories the set of pages belong to.
29 *
30 * @ingroup API
31 */
32 class ApiQueryCategories extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'cl' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param $resultPageSet ApiPageSet
52 * @return
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( array( 'cl_sortkey', 'cl_sortkey_prefix' ), 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( '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
126 $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
127 // Don't order by cl_from if it's constant in the WHERE clause
128 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
129 $this->addOption( 'ORDER BY', 'cl_to' . $dir );
130 } else {
131 $this->addOption( 'ORDER BY', array(
132 'cl_from' . $dir,
133 'cl_to' . $dir
134 ));
135 }
136
137 $res = $this->select( __METHOD__ );
138
139 $count = 0;
140 if ( is_null( $resultPageSet ) ) {
141 foreach ( $res as $row ) {
142 if ( ++$count > $params['limit'] ) {
143 // We've reached the one extra which shows that
144 // there are additional pages to be had. Stop here...
145 $this->setContinueEnumParameter( 'continue', $row->cl_from .
146 '|' . $this->keyToTitle( $row->cl_to ) );
147 break;
148 }
149
150 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
151 $vals = array();
152 ApiQueryBase::addTitleInfo( $vals, $title );
153 if ( isset( $prop['sortkey'] ) ) {
154 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
155 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
156 }
157 if ( isset( $prop['timestamp'] ) ) {
158 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
159 }
160 if ( isset( $prop['hidden'] ) && !is_null( $row->pp_propname ) ) {
161 $vals['hidden'] = '';
162 }
163
164 $fit = $this->addPageSubItem( $row->cl_from, $vals );
165 if ( !$fit ) {
166 $this->setContinueEnumParameter( 'continue', $row->cl_from .
167 '|' . $this->keyToTitle( $row->cl_to ) );
168 break;
169 }
170 }
171 } else {
172 $titles = array();
173 foreach ( $res as $row ) {
174 if ( ++$count > $params['limit'] ) {
175 // We've reached the one extra which shows that
176 // there are additional pages to be had. Stop here...
177 $this->setContinueEnumParameter( 'continue', $row->cl_from .
178 '|' . $this->keyToTitle( $row->cl_to ) );
179 break;
180 }
181
182 $titles[] = Title :: makeTitle( NS_CATEGORY, $row->cl_to );
183 }
184 $resultPageSet->populateFromTitles( $titles );
185 }
186 }
187
188 public function getAllowedParams() {
189 return array(
190 'prop' => array(
191 ApiBase::PARAM_ISMULTI => true,
192 ApiBase::PARAM_TYPE => array (
193 'sortkey',
194 'timestamp',
195 'hidden',
196 )
197 ),
198 'show' => array(
199 ApiBase::PARAM_ISMULTI => true,
200 ApiBase::PARAM_TYPE => array(
201 'hidden',
202 '!hidden',
203 )
204 ),
205 'limit' => array(
206 ApiBase::PARAM_DFLT => 10,
207 ApiBase::PARAM_TYPE => 'limit',
208 ApiBase::PARAM_MIN => 1,
209 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
210 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
211 ),
212 'continue' => null,
213 'categories' => array(
214 ApiBase::PARAM_ISMULTI => true,
215 ),
216 'dir' => array(
217 ApiBase::PARAM_DFLT => 'ascending',
218 ApiBase::PARAM_TYPE => array(
219 'ascending',
220 'descending'
221 )
222 ),
223 );
224 }
225
226 public function getParamDescription() {
227 return array(
228 'prop' => array(
229 'Which additional properties to get for each category',
230 ' sortkey - Adds the sortkey (hexadecimal string) and sortkey prefix (human-readable part) for the category',
231 ' timestamp - Adds timestamp of when the category was added',
232 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
233 ),
234 'limit' => 'How many categories to return',
235 'show' => 'Which kind of categories to show',
236 'continue' => 'When more results are available, use this to continue',
237 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
238 'dir' => 'The direction in which to list',
239 );
240 }
241
242 public function getDescription() {
243 return 'List all categories the page(s) belong to';
244 }
245
246 public function getPossibleErrors() {
247 return array_merge( parent::getPossibleErrors(), array(
248 array( 'show' ),
249 ) );
250 }
251
252 public function getExamples() {
253 return array(
254 'Get a list of categories [[Albert Einstein]] belongs to:',
255 ' api.php?action=query&prop=categories&titles=Albert%20Einstein',
256 'Get information about all categories used in the [[Albert Einstein]]:',
257 ' api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info'
258 );
259 }
260
261 public function getHelpUrls() {
262 return 'http://www.mediawiki.org/wiki/API:Properties#categories_.2F_cl';
263 }
264
265 public function getVersion() {
266 return __CLASS__ . ': $Id$';
267 }
268 }