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