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