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