raise timeout for CdbTest::testCdb()
[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 $this->dieContinueUsageIf( count( $cont ) != 2 );
89 $op = $params['dir'] == 'descending' ? '<' : '>';
90 $clfrom = intval( $cont[0] );
91 $clto = $this->getDB()->addQuotes( $cont[1] );
92 $this->addWhere(
93 "cl_from $op $clfrom OR " .
94 "(cl_from = $clfrom AND " .
95 "cl_to $op= $clto)"
96 );
97 }
98
99 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
100 $this->dieUsageMsg( 'show' );
101 }
102 if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) )
103 {
104 $this->addOption( 'STRAIGHT_JOIN' );
105 $this->addTables( array( 'page', 'page_props' ) );
106 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
107 $this->addJoinConds( array(
108 'page' => array( 'LEFT JOIN', array(
109 'page_namespace' => NS_CATEGORY,
110 'page_title = cl_to' ) ),
111 'page_props' => array( 'LEFT JOIN', array(
112 'pp_page=page_id',
113 'pp_propname' => 'hiddencat' ) )
114 ) );
115 if ( isset( $show['hidden'] ) ) {
116 $this->addWhere( array( 'pp_propname IS NOT NULL' ) );
117 } elseif ( isset( $show['!hidden'] ) ) {
118 $this->addWhere( array( 'pp_propname IS NULL' ) );
119 }
120 }
121
122 $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
123
124 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
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' . $sort );
128 } else {
129 $this->addOption( 'ORDER BY', array(
130 'cl_from' . $sort,
131 'cl_to' . $sort
132 ));
133 }
134
135 $res = $this->select( __METHOD__ );
136
137 $count = 0;
138 if ( is_null( $resultPageSet ) ) {
139 foreach ( $res as $row ) {
140 if ( ++$count > $params['limit'] ) {
141 // We've reached the one extra which shows that
142 // there are additional pages to be had. Stop here...
143 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
144 break;
145 }
146
147 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
148 $vals = array();
149 ApiQueryBase::addTitleInfo( $vals, $title );
150 if ( isset( $prop['sortkey'] ) ) {
151 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
152 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
153 }
154 if ( isset( $prop['timestamp'] ) ) {
155 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
156 }
157 if ( isset( $prop['hidden'] ) && !is_null( $row->pp_propname ) ) {
158 $vals['hidden'] = '';
159 }
160
161 $fit = $this->addPageSubItem( $row->cl_from, $vals );
162 if ( !$fit ) {
163 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
164 break;
165 }
166 }
167 } else {
168 $titles = array();
169 foreach ( $res as $row ) {
170 if ( ++$count > $params['limit'] ) {
171 // We've reached the one extra which shows that
172 // there are additional pages to be had. Stop here...
173 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
174 break;
175 }
176
177 $titles[] = Title :: makeTitle( NS_CATEGORY, $row->cl_to );
178 }
179 $resultPageSet->populateFromTitles( $titles );
180 }
181 }
182
183 public function getAllowedParams() {
184 return array(
185 'prop' => array(
186 ApiBase::PARAM_ISMULTI => true,
187 ApiBase::PARAM_TYPE => array (
188 'sortkey',
189 'timestamp',
190 'hidden',
191 )
192 ),
193 'show' => array(
194 ApiBase::PARAM_ISMULTI => true,
195 ApiBase::PARAM_TYPE => array(
196 'hidden',
197 '!hidden',
198 )
199 ),
200 'limit' => array(
201 ApiBase::PARAM_DFLT => 10,
202 ApiBase::PARAM_TYPE => 'limit',
203 ApiBase::PARAM_MIN => 1,
204 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
205 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
206 ),
207 'continue' => null,
208 'categories' => array(
209 ApiBase::PARAM_ISMULTI => true,
210 ),
211 'dir' => array(
212 ApiBase::PARAM_DFLT => 'ascending',
213 ApiBase::PARAM_TYPE => array(
214 'ascending',
215 'descending'
216 )
217 ),
218 );
219 }
220
221 public function getParamDescription() {
222 return array(
223 'prop' => array(
224 'Which additional properties to get for each category',
225 ' sortkey - Adds the sortkey (hexadecimal string) and sortkey prefix (human-readable part) for the category',
226 ' timestamp - Adds timestamp of when the category was added',
227 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
228 ),
229 'limit' => 'How many categories to return',
230 'show' => 'Which kind of categories to show',
231 'continue' => 'When more results are available, use this to continue',
232 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
233 'dir' => 'The direction in which to list',
234 );
235 }
236
237 public function getResultProperties() {
238 return array(
239 '' => array(
240 'ns' => 'namespace',
241 'title' => 'string'
242 ),
243 'sortkey' => array(
244 'sortkey' => 'string',
245 'sortkeyprefix' => 'string'
246 ),
247 'timestamp' => array(
248 'timestamp' => 'timestamp'
249 ),
250 'hidden' => array(
251 'hidden' => 'boolean'
252 )
253 );
254 }
255
256 public function getDescription() {
257 return 'List all categories the page(s) belong to';
258 }
259
260 public function getPossibleErrors() {
261 return array_merge( parent::getPossibleErrors(), array(
262 array( 'show' ),
263 ) );
264 }
265
266 public function getExamples() {
267 return array(
268 'api.php?action=query&prop=categories&titles=Albert%20Einstein' => 'Get a list of categories [[Albert Einstein]] belongs to',
269 'api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info' => 'Get information about all categories used in the [[Albert Einstein]]',
270 );
271 }
272
273 public function getHelpUrls() {
274 return 'https://www.mediawiki.org/wiki/API:Properties#categories_.2F_cl';
275 }
276 }