r71834, params and desc would be good
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Oct 16, 2006
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 * Query action to List the log events, with optional filtering by various parameters.
34 *
35 * @ingroup API
36 */
37 class ApiQueryLogEvents extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'le' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $db = $this->getDB();
46
47 $prop = array_flip( $params['prop'] );
48
49 $this->fld_ids = isset( $prop['ids'] );
50 $this->fld_title = isset( $prop['title'] );
51 $this->fld_type = isset( $prop['type'] );
52 $this->fld_action = isset ( $prop['action'] );
53 $this->fld_user = isset( $prop['user'] );
54 $this->fld_userid = isset( $prop['userid'] );
55 $this->fld_timestamp = isset( $prop['timestamp'] );
56 $this->fld_comment = isset( $prop['comment'] );
57 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
58 $this->fld_details = isset( $prop['details'] );
59 $this->fld_tags = isset( $prop['tags'] );
60
61 $hideLogs = LogEventsList::getExcludeClause( $db );
62 if ( $hideLogs !== false ) {
63 $this->addWhere( $hideLogs );
64 }
65
66 // Order is significant here
67 $this->addTables( array( 'logging', 'user', 'page' ) );
68 $this->addOption( 'STRAIGHT_JOIN' );
69 $this->addJoinConds( array(
70 'user' => array( 'JOIN',
71 'user_id=log_user' ),
72 'page' => array( 'LEFT JOIN',
73 array( 'log_namespace=page_namespace',
74 'log_title=page_title' ) ) ) );
75 $index = array( 'logging' => 'times' ); // default, may change
76
77 $this->addFields( array(
78 'log_type',
79 'log_action',
80 'log_timestamp',
81 'log_deleted',
82 ) );
83
84 $this->addFieldsIf( 'log_id', $this->fld_ids );
85 $this->addFieldsIf( 'page_id', $this->fld_ids );
86 $this->addFieldsIf( 'log_user', $this->fld_user );
87 $this->addFieldsIf( 'user_name', $this->fld_user );
88 $this->addFieldsIf( 'user_id', $this->fld_userid );
89 $this->addFieldsIf( 'log_namespace', $this->fld_title || $this->fld_parsedcomment );
90 $this->addFieldsIf( 'log_title', $this->fld_title || $this->fld_parsedcomment );
91 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
92 $this->addFieldsIf( 'log_params', $this->fld_details );
93
94 if ( $this->fld_tags ) {
95 $this->addTables( 'tag_summary' );
96 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
97 $this->addFields( 'ts_tags' );
98 }
99
100 if ( !is_null( $params['tag'] ) ) {
101 $this->addTables( 'change_tag' );
102 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
103 $this->addWhereFld( 'ct_tag', $params['tag'] );
104 global $wgOldChangeTagsIndex;
105 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
106 }
107
108 if ( !is_null( $params['action'] ) ) {
109 list( $type, $action ) = explode( '/', $params['action'] );
110 $this->addWhereFld( 'log_type', $type );
111 $this->addWhereFld( 'log_action', $action );
112 }
113 else if ( !is_null( $params['type'] ) ) {
114 $this->addWhereFld( 'log_type', $params['type'] );
115 $index['logging'] = 'type_time';
116 }
117
118 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
119
120 $limit = $params['limit'];
121 $this->addOption( 'LIMIT', $limit + 1 );
122
123 $user = $params['user'];
124 if ( !is_null( $user ) ) {
125 $userid = User::idFromName( $user );
126 if ( !$userid ) {
127 $this->dieUsage( "User name $user not found", 'param_user' );
128 }
129 $this->addWhereFld( 'log_user', $userid );
130 $index['logging'] = 'user_time';
131 }
132
133 $title = $params['title'];
134 if ( !is_null( $title ) ) {
135 $titleObj = Title::newFromText( $title );
136 if ( is_null( $titleObj ) ) {
137 $this->dieUsage( "Bad title value '$title'", 'param_title' );
138 }
139 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
140 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
141
142 // Use the title index in preference to the user index if there is a conflict
143 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
144 }
145
146 $this->addOption( 'USE INDEX', $index );
147
148 // Paranoia: avoid brute force searches (bug 17342)
149 if ( !is_null( $title ) ) {
150 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
151 }
152 if ( !is_null( $user ) ) {
153 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
154 }
155
156 $count = 0;
157 $res = $this->select( __METHOD__ );
158 foreach ( $res as $row ) {
159 if ( ++ $count > $limit ) {
160 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
161 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
162 break;
163 }
164
165 $vals = $this->extractRowInfo( $row );
166 if ( !$vals ) {
167 continue;
168 }
169 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
170 if ( !$fit ) {
171 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
172 break;
173 }
174 }
175 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
176 }
177
178 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
179 $params = explode( "\n", $params );
180 switch ( $type ) {
181 case 'move':
182 if ( isset( $params[0] ) ) {
183 $title = Title::newFromText( $params[0] );
184 if ( $title ) {
185 $vals2 = array();
186 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
187 $vals[$type] = $vals2;
188 }
189 }
190 if ( isset( $params[1] ) && $params[1] ) {
191 $vals[$type]['suppressedredirect'] = '';
192 }
193 $params = null;
194 break;
195 case 'patrol':
196 $vals2 = array();
197 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
198 $vals[$type] = $vals2;
199 $params = null;
200 break;
201 case 'rights':
202 $vals2 = array();
203 list( $vals2['old'], $vals2['new'] ) = $params;
204 $vals[$type] = $vals2;
205 $params = null;
206 break;
207 case 'block':
208 $vals2 = array();
209 list( $vals2['duration'], $vals2['flags'] ) = $params;
210
211 // Indefinite blocks have no expiry time
212 if ( Block::parseExpiryInput( $params[0] ) !== Block::infinity() ) {
213 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
214 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
215 }
216 $vals[$type] = $vals2;
217 $params = null;
218 break;
219 }
220 if ( !is_null( $params ) ) {
221 $result->setIndexedTagName( $params, 'param' );
222 $vals = array_merge( $vals, $params );
223 }
224 return $vals;
225 }
226
227 private function extractRowInfo( $row ) {
228 $vals = array();
229
230 if ( $this->fld_ids ) {
231 $vals['logid'] = intval( $row->log_id );
232 $vals['pageid'] = intval( $row->page_id );
233 }
234
235 if ( $this->fld_title || $this->fld_parsedcomment ) {
236 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
237 }
238
239 if ( $this->fld_title ) {
240 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
241 $vals['actionhidden'] = '';
242 } else {
243 ApiQueryBase::addTitleInfo( $vals, $title );
244 }
245 }
246
247 if ( $this->fld_type || $this->fld_action ) {
248 $vals['type'] = $row->log_type;
249 $vals['action'] = $row->log_action;
250 }
251
252 if ( $this->fld_details && $row->log_params !== '' ) {
253 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
254 $vals['actionhidden'] = '';
255 } else {
256 self::addLogParams(
257 $this->getResult(), $vals,
258 $row->log_params, $row->log_type,
259 $row->log_timestamp
260 );
261 }
262 }
263
264 if ( $this->fld_user || $this->fld_userid ) {
265 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
266 $vals['userhidden'] = '';
267 } else {
268 if ( $this->fld_user ) {
269 $vals['user'] = $row->user_name;
270 }
271 if ( $this->fld_userid ) {
272 $vals['userid'] = $row->user_id;
273 }
274
275 if ( !$row->log_user ) {
276 $vals['anon'] = '';
277 }
278 }
279 }
280 if ( $this->fld_timestamp ) {
281 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
282 }
283
284 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
285 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
286 $vals['commenthidden'] = '';
287 } else {
288 if ( $this->fld_comment ) {
289 $vals['comment'] = $row->log_comment;
290 }
291
292 if ( $this->fld_parsedcomment ) {
293 global $wgUser;
294 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment, $title );
295 }
296 }
297 }
298
299 if ( $this->fld_tags ) {
300 if ( $row->ts_tags ) {
301 $tags = explode( ',', $row->ts_tags );
302 $this->getResult()->setIndexedTagName( $tags, 'tag' );
303 $vals['tags'] = $tags;
304 } else {
305 $vals['tags'] = array();
306 }
307 }
308
309 return $vals;
310 }
311
312 public function getCacheMode( $params ) {
313 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
314 // formatComment() calls wfMsg() among other things
315 return 'anon-public-user-private';
316 } else {
317 return 'public';
318 }
319 }
320
321 public function getAllowedParams() {
322 global $wgLogTypes, $wgLogActions;
323 return array(
324 'prop' => array(
325 ApiBase::PARAM_ISMULTI => true,
326 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
327 ApiBase::PARAM_TYPE => array(
328 'ids',
329 'title',
330 'type',
331 'user',
332 'userid',
333 'timestamp',
334 'comment',
335 'parsedcomment',
336 'details',
337 'tags'
338 )
339 ),
340 'type' => array(
341 ApiBase::PARAM_TYPE => $wgLogTypes
342 ),
343 'action' => array(
344 ApiBase::PARAM_TYPE => array_keys( $wgLogActions )
345 ),
346 'start' => array(
347 ApiBase::PARAM_TYPE => 'timestamp'
348 ),
349 'end' => array(
350 ApiBase::PARAM_TYPE => 'timestamp'
351 ),
352 'dir' => array(
353 ApiBase::PARAM_DFLT => 'older',
354 ApiBase::PARAM_TYPE => array(
355 'newer',
356 'older'
357 )
358 ),
359 'user' => null,
360 'title' => null,
361 'tag' => null,
362 'limit' => array(
363 ApiBase::PARAM_DFLT => 10,
364 ApiBase::PARAM_TYPE => 'limit',
365 ApiBase::PARAM_MIN => 1,
366 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
367 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
368 )
369 );
370 }
371
372 public function getParamDescription() {
373 return array(
374 'prop' => array(
375 'Which properties to get',
376 ' ids - Adds the id of the log event',
377 ' title - Adds the title of the page for the log event',
378 ' type - Adds the type of log event',
379 ' user - Adds the user responsible for the log event',
380 ' userid - Adds the user id who was responsible for the log event',
381 ' timestamp - Adds the timestamp for the event',
382 ' comment - Adds the comment of the event',
383 ' parsedcomment - Adds the parsed comment of the event',
384 ' details - Lists addtional details about the event',
385 ' tags - Lists tags for the event',
386 ),
387 'type' => 'Filter log entries to only this type(s)',
388 'action' => "Filter log actions to only this type. Overrides {$this->getModulePrefix()}type",
389 'start' => 'The timestamp to start enumerating from',
390 'end' => 'The timestamp to end enumerating',
391 'dir' => 'In which direction to enumerate',
392 'user' => 'Filter entries to those made by the given user',
393 'title' => 'Filter entries to those related to a page',
394 'limit' => 'How many total event entries to return',
395 'tag' => 'Only list event entries tagged with this tag',
396 );
397 }
398
399 public function getDescription() {
400 return 'Get events from logs';
401 }
402
403 public function getPossibleErrors() {
404 return array_merge( parent::getPossibleErrors(), array(
405 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
406 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
407 ) );
408 }
409
410 protected function getExamples() {
411 return array(
412 'api.php?action=query&list=logevents'
413 );
414 }
415
416 public function getVersion() {
417 return __CLASS__ . ': $Id$';
418 }
419 }