Fix for r58399 (missing index on change_tag table), using the patch from bug 22032...
[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['logging'] = '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 global $wgOldChangeTagsIndex;
101 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
102 }
103
104 if ( !is_null( $params['type'] ) ) {
105 $this->addWhereFld( 'log_type', $params['type'] );
106 $index['logging'] = 'type_time';
107 }
108
109 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
110
111 $limit = $params['limit'];
112 $this->addOption( 'LIMIT', $limit + 1 );
113
114 $user = $params['user'];
115 if ( !is_null( $user ) ) {
116 $userid = User::idFromName( $user );
117 if ( !$userid )
118 $this->dieUsage( "User name $user not found", 'param_user' );
119 $this->addWhereFld( 'log_user', $userid );
120 $index['logging'] = 'user_time';
121 }
122
123 $title = $params['title'];
124 if ( !is_null( $title ) ) {
125 $titleObj = Title :: newFromText( $title );
126 if ( is_null( $titleObj ) )
127 $this->dieUsage( "Bad title value '$title'", 'param_title' );
128 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
129 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
130
131 // Use the title index in preference to the user index if there is a conflict
132 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
133 }
134
135 $this->addOption( 'USE INDEX', $index );
136
137 // Paranoia: avoid brute force searches (bug 17342)
138 if ( !is_null( $title ) ) {
139 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
140 }
141 if ( !is_null( $user ) ) {
142 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
143 }
144
145 $count = 0;
146 $res = $this->select( __METHOD__ );
147 while ( $row = $db->fetchObject( $res ) ) {
148 if ( ++ $count > $limit ) {
149 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
151 break;
152 }
153
154 $vals = $this->extractRowInfo( $row );
155 if ( !$vals )
156 continue;
157 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
158 if ( !$fit )
159 {
160 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
161 break;
162 }
163 }
164 $db->freeResult( $res );
165
166 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
167 }
168
169 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
170 $params = explode( "\n", $params );
171 switch ( $type ) {
172 case 'move':
173 if ( isset ( $params[0] ) ) {
174 $title = Title :: newFromText( $params[0] );
175 if ( $title ) {
176 $vals2 = array();
177 ApiQueryBase :: addTitleInfo( $vals2, $title, "new_" );
178 $vals[$type] = $vals2;
179 }
180 }
181 if ( isset ( $params[1] ) && $params[1] ) {
182 $vals[$type]['suppressedredirect'] = '';
183 }
184 $params = null;
185 break;
186 case 'patrol':
187 $vals2 = array();
188 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
189 $vals[$type] = $vals2;
190 $params = null;
191 break;
192 case 'rights':
193 $vals2 = array();
194 list( $vals2['old'], $vals2['new'] ) = $params;
195 $vals[$type] = $vals2;
196 $params = null;
197 break;
198 case 'block':
199 $vals2 = array();
200 list( $vals2['duration'], $vals2['flags'] ) = $params;
201 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
202 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
203 $vals[$type] = $vals2;
204 $params = null;
205 break;
206 }
207 if ( !is_null( $params ) ) {
208 $result->setIndexedTagName( $params, 'param' );
209 $vals = array_merge( $vals, $params );
210 }
211 return $vals;
212 }
213
214 private function extractRowInfo( $row ) {
215 $vals = array();
216
217 if ( $this->fld_ids ) {
218 $vals['logid'] = intval( $row->log_id );
219 $vals['pageid'] = intval( $row->page_id );
220 }
221
222 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
223
224 if ( $this->fld_title ) {
225 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
226 $vals['actionhidden'] = '';
227 } else {
228 ApiQueryBase::addTitleInfo( $vals, $title );
229 }
230 }
231
232 if ( $this->fld_type ) {
233 $vals['type'] = $row->log_type;
234 $vals['action'] = $row->log_action;
235 }
236
237 if ( $this->fld_details && $row->log_params !== '' ) {
238 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
239 $vals['actionhidden'] = '';
240 } else {
241 self::addLogParams( $this->getResult(), $vals,
242 $row->log_params, $row->log_type,
243 $row->log_timestamp );
244 }
245 }
246
247 if ( $this->fld_user ) {
248 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
249 $vals['userhidden'] = '';
250 } else {
251 $vals['user'] = $row->user_name;
252 if ( !$row->log_user )
253 $vals['anon'] = '';
254 }
255 }
256 if ( $this->fld_timestamp ) {
257 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
258 }
259
260 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
261 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
262 $vals['commenthidden'] = '';
263 } else {
264 if ( $this->fld_comment )
265 $vals['comment'] = $row->log_comment;
266
267 if ( $this->fld_parsedcomment ) {
268 global $wgUser;
269 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment, $title );
270 }
271 }
272 }
273
274 if ( $this->fld_tags ) {
275 if ( $row->ts_tags ) {
276 $tags = explode( ',', $row->ts_tags );
277 $this->getResult()->setIndexedTagName( $tags, 'tag' );
278 $vals['tags'] = $tags;
279 } else {
280 $vals['tags'] = array();
281 }
282 }
283
284 return $vals;
285 }
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 protected function getExamples() {
354 return array (
355 'api.php?action=query&list=logevents'
356 );
357 }
358
359 public function getVersion() {
360 return __CLASS__ . ': $Id$';
361 }
362 }