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