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