Followup r81583, break some of the long lines and centralize the regex.
[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 private function run( $resultPageSet = null ) {
56 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
57 return; // nothing to do
58 }
59
60 $params = $this->extractRequestParams();
61 $prop = array_flip( (array)$params['prop'] );
62 $show = array_flip( (array)$params['show'] );
63
64 $this->addFields( array(
65 'cl_from',
66 'cl_to'
67 ) );
68
69 $this->addFieldsIf( 'cl_sortkey', isset( $prop['sortkey'] ) );
70 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
71
72 $this->addTables( 'categorylinks' );
73 $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
74 if ( !is_null( $params['categories'] ) ) {
75 $cats = array();
76 foreach ( $params['categories'] as $cat ) {
77 $title = Title::newFromText( $cat );
78 if ( !$title || $title->getNamespace() != NS_CATEGORY ) {
79 $this->setWarning( "``$cat'' is not a category" );
80 } else {
81 $cats[] = $title->getDBkey();
82 }
83 }
84 $this->addWhereFld( 'cl_to', $cats );
85 }
86
87 if ( !is_null( $params['continue'] ) ) {
88 $cont = explode( '|', $params['continue'] );
89 if ( count( $cont ) != 2 ) {
90 $this->dieUsage( "Invalid continue param. You should pass the " .
91 "original value returned by the previous query", "_badcontinue" );
92 }
93 $clfrom = intval( $cont[0] );
94 $clto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
95 $this->addWhere(
96 "cl_from > $clfrom OR " .
97 "(cl_from = $clfrom AND " .
98 "cl_to >= '$clto')"
99 );
100 }
101
102 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
103 $this->dieUsageMsg( array( 'show' ) );
104 }
105 if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) )
106 {
107 $this->addOption( 'STRAIGHT_JOIN' );
108 $this->addTables( array( 'page', 'page_props' ) );
109 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
110 $this->addJoinConds( array(
111 'page' => array( 'LEFT JOIN', array(
112 'page_namespace' => NS_CATEGORY,
113 'page_title = cl_to' ) ),
114 'page_props' => array( 'LEFT JOIN', array(
115 'pp_page=page_id',
116 'pp_propname' => 'hiddencat' ) )
117 ) );
118 if ( isset( $show['hidden'] ) ) {
119 $this->addWhere( array( 'pp_propname IS NOT NULL' ) );
120 } elseif ( isset( $show['!hidden'] ) ) {
121 $this->addWhere( array( 'pp_propname IS NULL' ) );
122 }
123 }
124
125 $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
126 // Don't order by cl_from if it's constant in the WHERE clause
127 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
128 $this->addOption( 'ORDER BY', 'cl_to' );
129 } else {
130 $this->addOption( 'ORDER BY', "cl_from, cl_to" );
131 }
132
133 $res = $this->select( __METHOD__ );
134
135 if ( is_null( $resultPageSet ) ) {
136 $count = 0;
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 .
142 '|' . $this->keyToTitle( $row->cl_to ) );
143 break;
144 }
145
146 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
147 $vals = array();
148 ApiQueryBase::addTitleInfo( $vals, $title );
149 if ( isset( $prop['sortkey'] ) ) {
150 $vals['sortkey'] = $row->cl_sortkey;
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 .
162 '|' . $this->keyToTitle( $row->cl_to ) );
163 break;
164 }
165 }
166 } else {
167 $titles = array();
168 foreach ( $res as $row ) {
169 if ( ++$count > $params['limit'] ) {
170 // We've reached the one extra which shows that
171 // there are additional pages to be had. Stop here...
172 $this->setContinueEnumParameter( 'continue', $row->cl_from .
173 '|' . $this->keyToTitle( $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 );
212 }
213
214 public function getParamDescription() {
215 return array(
216 'prop' => array(
217 'Which additional properties to get for each category',
218 ' sortkey - Adds the sortkey for the category',
219 ' timestamp - Adds timestamp of when the category was added',
220 ' hidden - Tags categories that are hidden with __HIDDENCAT__',
221 ),
222 'limit' => 'How many categories to return',
223 'show' => 'Which kind of categories to show',
224 'continue' => 'When more results are available, use this to continue',
225 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
226 );
227 }
228
229 public function getDescription() {
230 return 'List all categories the page(s) belong to';
231 }
232
233 public function getPossibleErrors() {
234 return array_merge( parent::getPossibleErrors(), array(
235 array( 'show' ),
236 ) );
237 }
238
239 protected function getExamples() {
240 return array(
241 'Get a list of categories [[Albert Einstein]] belongs to:',
242 ' api.php?action=query&prop=categories&titles=Albert%20Einstein',
243 'Get information about all categories used in the [[Albert Einstein]]:',
244 ' api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info'
245 );
246 }
247
248 public function getVersion() {
249 return __CLASS__ . ': $Id$';
250 }
251 }