(bug 22868) don't list infinite block expiry date as "now" in API logevents
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 list( $tbl_logging, $tbl_page, $tbl_user ) = $db->tableNamesN( 'logging', 'page', 'user' );
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( 'log_namespace', $this->fld_title );
89 $this->addFieldsIf( 'log_title', $this->fld_title );
90 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
91 $this->addFieldsIf( 'log_params', $this->fld_details );
92
93 if ( $this->fld_tags ) {
94 $this->addTables( 'tag_summary' );
95 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
96 $this->addFields( 'ts_tags' );
97 }
98
99 if ( !is_null( $params['tag'] ) ) {
100 $this->addTables( 'change_tag' );
101 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
102 $this->addWhereFld( 'ct_tag', $params['tag'] );
103 global $wgOldChangeTagsIndex;
104 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
105 }
106
107 if ( !is_null( $params['action'] ) ) {
108 list( $type, $action ) = explode( '/', $params['action'] );
109 $this->addWhereFld( 'log_type', $type );
110 $this->addWhereFld( 'log_action', $action );
111 }
112 else if ( !is_null( $params['type'] ) ) {
113 $this->addWhereFld( 'log_type', $params['type'] );
114 $index['logging'] = 'type_time';
115 }
116
117 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
118
119 $limit = $params['limit'];
120 $this->addOption( 'LIMIT', $limit + 1 );
121
122 $user = $params['user'];
123 if ( !is_null( $user ) ) {
124 $userid = User::idFromName( $user );
125 if ( !$userid ) {
126 $this->dieUsage( "User name $user not found", 'param_user' );
127 }
128 $this->addWhereFld( 'log_user', $userid );
129 $index['logging'] = 'user_time';
130 }
131
132 $title = $params['title'];
133 if ( !is_null( $title ) ) {
134 $titleObj = Title::newFromText( $title );
135 if ( is_null( $titleObj ) ) {
136 $this->dieUsage( "Bad title value '$title'", 'param_title' );
137 }
138 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
139 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
140
141 // Use the title index in preference to the user index if there is a conflict
142 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
143 }
144
145 $this->addOption( 'USE INDEX', $index );
146
147 // Paranoia: avoid brute force searches (bug 17342)
148 if ( !is_null( $title ) ) {
149 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
150 }
151 if ( !is_null( $user ) ) {
152 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
153 }
154
155 $count = 0;
156 $res = $this->select( __METHOD__ );
157 while ( $row = $db->fetchObject( $res ) ) {
158 if ( ++ $count > $limit ) {
159 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
160 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
161 break;
162 }
163
164 $vals = $this->extractRowInfo( $row );
165 if ( !$vals ) {
166 continue;
167 }
168 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
169 if ( !$fit ) {
170 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
171 break;
172 }
173 }
174 $db->freeResult( $res );
175
176 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
177 }
178
179 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
180 $params = explode( "\n", $params );
181 switch ( $type ) {
182 case 'move':
183 if ( isset( $params[0] ) ) {
184 $title = Title::newFromText( $params[0] );
185 if ( $title ) {
186 $vals2 = array();
187 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
188 $vals[$type] = $vals2;
189 }
190 }
191 if ( isset( $params[1] ) && $params[1] ) {
192 $vals[$type]['suppressedredirect'] = '';
193 }
194 $params = null;
195 break;
196 case 'patrol':
197 $vals2 = array();
198 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
199 $vals[$type] = $vals2;
200 $params = null;
201 break;
202 case 'rights':
203 $vals2 = array();
204 list( $vals2['old'], $vals2['new'] ) = $params;
205 $vals[$type] = $vals2;
206 $params = null;
207 break;
208 case 'block':
209 $vals2 = array();
210 list( $vals2['duration'], $vals2['flags'] ) = $params;
211
212 // Indefinite blocks have no expiry time
213 if ( Block::parseExpiryInput( $params[0] ) !== Block::infinity() ) {
214 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
215 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
216 }
217 $vals[$type] = $vals2;
218 $params = null;
219 break;
220 }
221 if ( !is_null( $params ) ) {
222 $result->setIndexedTagName( $params, 'param' );
223 $vals = array_merge( $vals, $params );
224 }
225 return $vals;
226 }
227
228 private function extractRowInfo( $row ) {
229 $vals = array();
230
231 if ( $this->fld_ids ) {
232 $vals['logid'] = intval( $row->log_id );
233 $vals['pageid'] = intval( $row->page_id );
234 }
235
236 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
237
238 if ( $this->fld_title ) {
239 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
240 $vals['actionhidden'] = '';
241 } else {
242 ApiQueryBase::addTitleInfo( $vals, $title );
243 }
244 }
245
246 if ( $this->fld_type || $this->fld_action ) {
247 $vals['type'] = $row->log_type;
248 $vals['action'] = $row->log_action;
249 }
250
251 if ( $this->fld_details && $row->log_params !== '' ) {
252 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
253 $vals['actionhidden'] = '';
254 } else {
255 self::addLogParams(
256 $this->getResult(), $vals,
257 $row->log_params, $row->log_type,
258 $row->log_timestamp
259 );
260 }
261 }
262
263 if ( $this->fld_user ) {
264 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
265 $vals['userhidden'] = '';
266 } else {
267 $vals['user'] = $row->user_name;
268 if ( !$row->log_user )
269 $vals['anon'] = '';
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 getAllowedParams() {
305 global $wgLogTypes, $wgLogActions;
306 return array(
307 'prop' => array(
308 ApiBase::PARAM_ISMULTI => true,
309 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
310 ApiBase::PARAM_TYPE => array(
311 'ids',
312 'title',
313 'type',
314 'user',
315 'timestamp',
316 'comment',
317 'parsedcomment',
318 'details',
319 'tags'
320 )
321 ),
322 'type' => array(
323 ApiBase::PARAM_TYPE => $wgLogTypes
324 ),
325 'action' => array(
326 ApiBase::PARAM_TYPE => array_keys( $wgLogActions )
327 ),
328 'start' => array(
329 ApiBase::PARAM_TYPE => 'timestamp'
330 ),
331 'end' => array(
332 ApiBase::PARAM_TYPE => 'timestamp'
333 ),
334 'dir' => array(
335 ApiBase::PARAM_DFLT => 'older',
336 ApiBase::PARAM_TYPE => array(
337 'newer',
338 'older'
339 )
340 ),
341 'user' => null,
342 'title' => null,
343 'tag' => null,
344 'limit' => array(
345 ApiBase::PARAM_DFLT => 10,
346 ApiBase::PARAM_TYPE => 'limit',
347 ApiBase::PARAM_MIN => 1,
348 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
349 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
350 )
351 );
352 }
353
354 public function getParamDescription() {
355 return array(
356 'prop' => 'Which properties to get',
357 'type' => 'Filter log entries to only this type(s)',
358 'action' => "Filter log actions to only this type. Overrides {$this->getModulePrefix()}type",
359 'start' => 'The timestamp to start enumerating from.',
360 'end' => 'The timestamp to end enumerating.',
361 'dir' => 'In which direction to enumerate.',
362 'user' => 'Filter entries to those made by the given user.',
363 'title' => 'Filter entries to those related to a page.',
364 'limit' => 'How many total event entries to return.',
365 'tag' => 'Only list event entries tagged with this tag.',
366 );
367 }
368
369 public function getDescription() {
370 return 'Get events from logs.';
371 }
372
373 public function getPossibleErrors() {
374 return array_merge( parent::getPossibleErrors(), array(
375 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
376 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
377 ) );
378 }
379
380 protected function getExamples() {
381 return array(
382 'api.php?action=query&list=logevents'
383 );
384 }
385
386 public function getVersion() {
387 return __CLASS__ . ': $Id$';
388 }
389 }