EditPage::newSectionSummary should return a value in all code paths
[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( ApiQuery $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 ApiPageSet $resultPageSet
52 */
53 private function run( $resultPageSet = null ) {
54 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
55 return; // nothing to do
56 }
57
58 $params = $this->extractRequestParams();
59 $prop = array_flip( (array)$params['prop'] );
60 $show = array_flip( (array)$params['show'] );
61
62 $this->addFields( array(
63 'cl_from',
64 'cl_to'
65 ) );
66
67 $this->addFieldsIf( array( 'cl_sortkey', 'cl_sortkey_prefix' ), isset( $prop['sortkey'] ) );
68 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
69
70 $this->addTables( 'categorylinks' );
71 $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
72 if ( !is_null( $params['categories'] ) ) {
73 $cats = array();
74 foreach ( $params['categories'] as $cat ) {
75 $title = Title::newFromText( $cat );
76 if ( !$title || $title->getNamespace() != NS_CATEGORY ) {
77 $this->setWarning( "\"$cat\" is not a category" );
78 } else {
79 $cats[] = $title->getDBkey();
80 }
81 }
82 $this->addWhereFld( 'cl_to', $cats );
83 }
84
85 if ( !is_null( $params['continue'] ) ) {
86 $cont = explode( '|', $params['continue'] );
87 $this->dieContinueUsageIf( count( $cont ) != 2 );
88 $op = $params['dir'] == 'descending' ? '<' : '>';
89 $clfrom = intval( $cont[0] );
90 $clto = $this->getDB()->addQuotes( $cont[1] );
91 $this->addWhere(
92 "cl_from $op $clfrom OR " .
93 "(cl_from = $clfrom AND " .
94 "cl_to $op= $clto)"
95 );
96 }
97
98 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
99 $this->dieUsageMsg( 'show' );
100 }
101 if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) ) {
102 $this->addOption( 'STRAIGHT_JOIN' );
103 $this->addTables( array( 'page', 'page_props' ) );
104 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
105 $this->addJoinConds( array(
106 'page' => array( 'LEFT JOIN', array(
107 'page_namespace' => NS_CATEGORY,
108 'page_title = cl_to' ) ),
109 'page_props' => array( 'LEFT JOIN', array(
110 'pp_page=page_id',
111 'pp_propname' => 'hiddencat' ) )
112 ) );
113 if ( isset( $show['hidden'] ) ) {
114 $this->addWhere( array( 'pp_propname IS NOT NULL' ) );
115 } elseif ( isset( $show['!hidden'] ) ) {
116 $this->addWhere( array( 'pp_propname IS NULL' ) );
117 }
118 }
119
120 $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
121
122 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
123 // Don't order by cl_from if it's constant in the WHERE clause
124 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
125 $this->addOption( 'ORDER BY', 'cl_to' . $sort );
126 } else {
127 $this->addOption( 'ORDER BY', array(
128 'cl_from' . $sort,
129 'cl_to' . $sort
130 ) );
131 }
132
133 $res = $this->select( __METHOD__ );
134
135 $count = 0;
136 if ( is_null( $resultPageSet ) ) {
137 foreach ( $res as $row ) {
138 if ( ++$count > $params['limit'] ) {
139 // We've reached the one extra which shows that
140 // there are additional pages to be had. Stop here...
141 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $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'] = bin2hex( $row->cl_sortkey );
150 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
151 }
152 if ( isset( $prop['timestamp'] ) ) {
153 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
154 }
155 if ( isset( $prop['hidden'] ) && !is_null( $row->pp_propname ) ) {
156 $vals['hidden'] = '';
157 }
158
159 $fit = $this->addPageSubItem( $row->cl_from, $vals );
160 if ( !$fit ) {
161 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $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 . '|' . $row->cl_to );
172 break;
173 }
174
175 $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
176 }
177 $resultPageSet->populateFromTitles( $titles );
178 }
179 }
180
181 public function getAllowedParams() {
182 return array(
183 'prop' => array(
184 ApiBase::PARAM_ISMULTI => true,
185 ApiBase::PARAM_TYPE => array(
186 'sortkey',
187 'timestamp',
188 'hidden',
189 )
190 ),
191 'show' => array(
192 ApiBase::PARAM_ISMULTI => true,
193 ApiBase::PARAM_TYPE => array(
194 'hidden',
195 '!hidden',
196 )
197 ),
198 'limit' => array(
199 ApiBase::PARAM_DFLT => 10,
200 ApiBase::PARAM_TYPE => 'limit',
201 ApiBase::PARAM_MIN => 1,
202 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
203 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
204 ),
205 'continue' => null,
206 'categories' => array(
207 ApiBase::PARAM_ISMULTI => true,
208 ),
209 'dir' => array(
210 ApiBase::PARAM_DFLT => 'ascending',
211 ApiBase::PARAM_TYPE => array(
212 'ascending',
213 'descending'
214 )
215 ),
216 );
217 }
218
219 public function getParamDescription() {
220 return array(
221 'prop' => array(
222 'Which additional properties to get for each category',
223 ' sortkey - Adds the sortkey (hexadecimal string) and sortkey prefix',
224 ' (human-readable part) for the category',
225 ' timestamp - Adds timestamp of when the category was added',
226 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
227 ),
228 'limit' => 'How many categories to return',
229 'show' => 'Which kind of categories to show',
230 'continue' => 'When more results are available, use this to continue',
231 'categories' => 'Only list these categories. Useful for checking ' .
232 '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'
269 => 'Get a list of categories [[Albert Einstein]] belongs to',
270 'api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info'
271 => 'Get information about all categories used in the [[Albert Einstein]]',
272 );
273 }
274
275 public function getHelpUrls() {
276 return 'https://www.mediawiki.org/wiki/API:Properties#categories_.2F_cl';
277 }
278 }