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