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