Fixed dependencies for jquery.collapsibleTabs
[lhc/web/wiklou.git] / includes / logging / LogEntry.php
1 <?php
2 /**
3 * Contain classes for dealing with individual log entries
4 *
5 * This is how I see the log system history:
6 * - appending to plain wiki pages
7 * - formatting log entries based on database fields
8 * - user is now part of the action message
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @author Niklas Laxström
27 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
28 * @since 1.19
29 */
30
31 /**
32 * Interface for log entries. Every log entry has these methods.
33 * @since 1.19
34 */
35 interface LogEntry {
36
37 /**
38 * The main log type.
39 * @return string
40 */
41 public function getType();
42
43 /**
44 * The log subtype.
45 * @return string
46 */
47 public function getSubtype();
48
49 /**
50 * The full logtype in format maintype/subtype.
51 * @return string
52 */
53 public function getFullType();
54
55 /**
56 * Get the extra parameters stored for this message.
57 * @return array
58 */
59 public function getParameters();
60
61 /**
62 * Get the user for performed this action.
63 * @return User
64 */
65 public function getPerformer();
66
67 /**
68 * Get the target page of this action.
69 * @return Title
70 */
71 public function getTarget();
72
73 /**
74 * Get the timestamp when the action was executed.
75 * @return string
76 */
77 public function getTimestamp();
78
79 /**
80 * Get the user provided comment.
81 * @return string
82 */
83 public function getComment();
84
85 /**
86 * Get the access restriction.
87 * @return string
88 */
89 public function getDeleted();
90
91 /**
92 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
93 * @return Boolean
94 */
95 public function isDeleted( $field );
96 }
97
98 /**
99 * Extends the LogEntryInterface with some basic functionality
100 * @since 1.19
101 */
102 abstract class LogEntryBase implements LogEntry {
103
104 public function getFullType() {
105 return $this->getType() . '/' . $this->getSubtype();
106 }
107
108 public function isDeleted( $field ) {
109 return ( $this->getDeleted() & $field ) === $field;
110 }
111
112 /**
113 * Whether the parameters for this log are stored in new or
114 * old format.
115 * @return bool
116 */
117 public function isLegacy() {
118 return false;
119 }
120
121 }
122
123 /**
124 * This class wraps around database result row.
125 * @since 1.19
126 */
127 class DatabaseLogEntry extends LogEntryBase {
128 // Static->
129
130 /**
131 * Returns array of information that is needed for querying
132 * log entries. Array contains the following keys:
133 * tables, fields, conds, options and join_conds
134 * @return array
135 */
136 public static function getSelectQueryData() {
137 $tables = array( 'logging', 'user' );
138 $fields = array(
139 'log_id', 'log_type', 'log_action', 'log_timestamp',
140 'log_user', 'log_user_text',
141 'log_namespace', 'log_title', // unused log_page
142 'log_comment', 'log_params', 'log_deleted',
143 'user_id', 'user_name', 'user_editcount',
144 );
145
146 $joins = array(
147 // IP's don't have an entry in user table
148 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
149 );
150
151 return array(
152 'tables' => $tables,
153 'fields' => $fields,
154 'conds' => array(),
155 'options' => array(),
156 'join_conds' => $joins,
157 );
158 }
159
160 /**
161 * Constructs new LogEntry from database result row.
162 * Supports rows from both logging and recentchanges table.
163 * @param $row
164 * @return DatabaseLogEntry
165 */
166 public static function newFromRow( $row ) {
167 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
168 return new RCDatabaseLogEntry( (object) $row );
169 } else {
170 return new self( $row );
171 }
172 }
173
174 // Non-static->
175
176 /// Database result row.
177 protected $row;
178
179 protected function __construct( $row ) {
180 $this->row = $row;
181 }
182
183 /**
184 * Returns the unique database id.
185 * @return int
186 */
187 public function getId() {
188 return (int)$this->row->log_id;
189 }
190
191 /**
192 * Returns whatever is stored in the database field.
193 * @return string
194 */
195 protected function getRawParameters() {
196 return $this->row->log_params;
197 }
198
199 // LogEntryBase->
200
201 public function isLegacy() {
202 // This does the check
203 $this->getParameters();
204 return $this->legacy;
205 }
206
207 // LogEntry->
208
209 public function getType() {
210 return $this->row->log_type;
211 }
212
213 public function getSubtype() {
214 return $this->row->log_action;
215 }
216
217 public function getParameters() {
218 if ( !isset( $this->params ) ) {
219 $blob = $this->getRawParameters();
220 wfSuppressWarnings();
221 $params = unserialize( $blob );
222 wfRestoreWarnings();
223 if ( $params !== false ) {
224 $this->params = $params;
225 $this->legacy = false;
226 } else {
227 $this->params = $blob === '' ? array() : explode( "\n", $blob );
228 $this->legacy = true;
229 }
230 }
231 return $this->params;
232 }
233
234 public function getPerformer() {
235 $userId = (int) $this->row->log_user;
236 if ( $userId !== 0 ) { // logged-in users
237 if ( isset( $this->row->user_name ) ) {
238 return User::newFromRow( $this->row );
239 } else {
240 return User::newFromId( $userId );
241 }
242 } else { // IP users
243 $userText = $this->row->log_user_text;
244 return User::newFromName( $userText, false );
245 }
246 }
247
248 public function getTarget() {
249 $namespace = $this->row->log_namespace;
250 $page = $this->row->log_title;
251 $title = Title::makeTitle( $namespace, $page );
252 return $title;
253 }
254
255 public function getTimestamp() {
256 return wfTimestamp( TS_MW, $this->row->log_timestamp );
257 }
258
259 public function getComment() {
260 return $this->row->log_comment;
261 }
262
263 public function getDeleted() {
264 return $this->row->log_deleted;
265 }
266
267 }
268
269 class RCDatabaseLogEntry extends DatabaseLogEntry {
270
271 public function getId() {
272 return $this->row->rc_logid;
273 }
274
275 protected function getRawParameters() {
276 return $this->row->rc_params;
277 }
278
279 // LogEntry->
280
281 public function getType() {
282 return $this->row->rc_log_type;
283 }
284
285 public function getSubtype() {
286 return $this->row->rc_log_action;
287 }
288
289 public function getPerformer() {
290 $userId = (int) $this->row->rc_user;
291 if ( $userId !== 0 ) {
292 return User::newFromId( $userId );
293 } else {
294 $userText = $this->row->rc_user_text;
295 // Might be an IP, don't validate the username
296 return User::newFromName( $userText, false );
297 }
298 }
299
300 public function getTarget() {
301 $namespace = $this->row->rc_namespace;
302 $page = $this->row->rc_title;
303 $title = Title::makeTitle( $namespace, $page );
304 return $title;
305 }
306
307 public function getTimestamp() {
308 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
309 }
310
311 public function getComment() {
312 return $this->row->rc_comment;
313 }
314
315 public function getDeleted() {
316 return $this->row->rc_deleted;
317 }
318
319 }
320
321 /**
322 * Class for creating log entries manually, for
323 * example to inject them into the database.
324 * @since 1.19
325 */
326 class ManualLogEntry extends LogEntryBase {
327 protected $type; ///!< @var string
328 protected $subtype; ///!< @var string
329 protected $parameters = array(); ///!< @var array
330 protected $performer; ///!< @var User
331 protected $target; ///!< @var Title
332 protected $timestamp; ///!< @var string
333 protected $comment = ''; ///!< @var string
334 protected $deleted; ///!< @var int
335
336 /**
337 * Constructor.
338 *
339 * @since 1.19
340 *
341 * @param string $type
342 * @param string $subtype
343 */
344 public function __construct( $type, $subtype ) {
345 $this->type = $type;
346 $this->subtype = $subtype;
347 }
348
349 /**
350 * Set extra log parameters.
351 * You can pass params to the log action message
352 * by prefixing the keys with a number and colon.
353 * The numbering should start with number 4, the
354 * first three parameters are hardcoded for every
355 * message. Example:
356 * $entry->setParameters(
357 * '4:color' => 'blue',
358 * 'animal' => 'dog'
359 * );
360 *
361 * @since 1.19
362 *
363 * @param $parameters array Associative array
364 */
365 public function setParameters( $parameters ) {
366 $this->parameters = $parameters;
367 }
368
369 /**
370 * Set the user that performed the action being logged.
371 *
372 * @since 1.19
373 *
374 * @param User $performer
375 */
376 public function setPerformer( User $performer ) {
377 $this->performer = $performer;
378 }
379
380 /**
381 * Set the title of the object changed.
382 *
383 * @since 1.19
384 *
385 * @param Title $target
386 */
387 public function setTarget( Title $target ) {
388 $this->target = $target;
389 }
390
391 /**
392 * Set the timestamp of when the logged action took place.
393 *
394 * @since 1.19
395 *
396 * @param string $timestamp
397 */
398 public function setTimestamp( $timestamp ) {
399 $this->timestamp = $timestamp;
400 }
401
402 /**
403 * Set a comment associated with the action being logged.
404 *
405 * @since 1.19
406 *
407 * @param string $comment
408 */
409 public function setComment( $comment ) {
410 $this->comment = $comment;
411 }
412
413 /**
414 * TODO: document
415 *
416 * @since 1.19
417 *
418 * @param integer $deleted
419 */
420 public function setDeleted( $deleted ) {
421 $this->deleted = $deleted;
422 }
423
424 /**
425 * Inserts the entry into the logging table.
426 * @return int If of the log entry
427 */
428 public function insert() {
429 global $wgContLang;
430
431 $dbw = wfGetDB( DB_MASTER );
432 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
433
434 if ( $this->timestamp === null ) {
435 $this->timestamp = wfTimestampNow();
436 }
437
438 # Truncate for whole multibyte characters.
439 $comment = $wgContLang->truncate( $this->getComment(), 255 );
440
441 $data = array(
442 'log_id' => $id,
443 'log_type' => $this->getType(),
444 'log_action' => $this->getSubtype(),
445 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
446 'log_user' => $this->getPerformer()->getId(),
447 'log_user_text' => $this->getPerformer()->getName(),
448 'log_namespace' => $this->getTarget()->getNamespace(),
449 'log_title' => $this->getTarget()->getDBkey(),
450 'log_page' => $this->getTarget()->getArticleID(),
451 'log_comment' => $comment,
452 'log_params' => serialize( (array) $this->getParameters() ),
453 );
454 $dbw->insert( 'logging', $data, __METHOD__ );
455 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
456 return $this->id;
457 }
458
459 /**
460 * Publishes the log entry.
461 * @param $newId int id of the log entry.
462 * @param $to string: rcandudp (default), rc, udp
463 */
464 public function publish( $newId, $to = 'rcandudp' ) {
465 $log = new LogPage( $this->getType() );
466 if ( $log->isRestricted() ) {
467 return;
468 }
469
470 $formatter = LogFormatter::newFromEntry( $this );
471 $context = RequestContext::newExtraneousContext( $this->getTarget() );
472 $formatter->setContext( $context );
473
474 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
475 $user = $this->getPerformer();
476 $ip = "";
477 if ( $user->isAnon() ) {
478 /*
479 * "MediaWiki default" and friends may have
480 * no IP address in their name
481 */
482 if ( IP::isIPAddress( $user->getName() ) ) {
483 $ip = $user->getName();
484 }
485 }
486 $rc = RecentChange::newLogEntry(
487 $this->getTimestamp(),
488 $logpage,
489 $user,
490 $formatter->getPlainActionText(),
491 $ip,
492 $this->getType(),
493 $this->getSubtype(),
494 $this->getTarget(),
495 $this->getComment(),
496 serialize( (array) $this->getParameters() ),
497 $newId,
498 $formatter->getIRCActionComment() // Used for IRC feeds
499 );
500
501 if ( $to === 'rc' || $to === 'rcandudp' ) {
502 $rc->save( 'pleasedontudp' );
503 }
504
505 if ( $to === 'udp' || $to === 'rcandudp' ) {
506 $rc->notifyRC2UDP();
507 }
508 }
509
510 // LogEntry->
511
512 public function getType() {
513 return $this->type;
514 }
515
516 public function getSubtype() {
517 return $this->subtype;
518 }
519
520 public function getParameters() {
521 return $this->parameters;
522 }
523
524 /**
525 * @return User
526 */
527 public function getPerformer() {
528 return $this->performer;
529 }
530
531 /**
532 * @return Title
533 */
534 public function getTarget() {
535 return $this->target;
536 }
537
538 public function getTimestamp() {
539 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
540 return wfTimestamp( TS_MW, $ts );
541 }
542
543 public function getComment() {
544 return $this->comment;
545 }
546
547 public function getDeleted() {
548 return (int) $this->deleted;
549 }
550
551 }