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