993f3de50ec5352a629de6e3cfa5c4f18383b4b8
[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 * The main log type.
38 * @return string
39 */
40 public function getType();
41
42 /**
43 * The log subtype.
44 * @return string
45 */
46 public function getSubtype();
47
48 /**
49 * The full logtype in format maintype/subtype.
50 * @return string
51 */
52 public function getFullType();
53
54 /**
55 * Get the extra parameters stored for this message.
56 * @return array
57 */
58 public function getParameters();
59
60 /**
61 * Get the user for performed this action.
62 * @return User
63 */
64 public function getPerformer();
65
66 /**
67 * Get the target page of this action.
68 * @return Title
69 */
70 public function getTarget();
71
72 /**
73 * Get the timestamp when the action was executed.
74 * @return string
75 */
76 public function getTimestamp();
77
78 /**
79 * Get the user provided comment.
80 * @return string
81 */
82 public function getComment();
83
84 /**
85 * Get the access restriction.
86 * @return string
87 */
88 public function getDeleted();
89
90 /**
91 * @param int $field One of LogPage::DELETED_* bitfield constants
92 * @return bool
93 */
94 public function isDeleted( $field );
95 }
96
97 /**
98 * Extends the LogEntryInterface with some basic functionality
99 * @since 1.19
100 */
101 abstract class LogEntryBase implements LogEntry {
102 public function getFullType() {
103 return $this->getType() . '/' . $this->getSubtype();
104 }
105
106 public function isDeleted( $field ) {
107 return ( $this->getDeleted() & $field ) === $field;
108 }
109
110 /**
111 * Whether the parameters for this log are stored in new or
112 * old format.
113 * @return bool
114 */
115 public function isLegacy() {
116 return false;
117 }
118
119 /**
120 * Create a blob from a parameter array
121 *
122 * @param array $params
123 * @return string
124 * @since 1.26
125 */
126 public static function makeParamBlob( $params ) {
127 return serialize( (array)$params );
128 }
129
130 /**
131 * Extract a parameter array from a blob
132 *
133 * @param string $blob
134 * @return array
135 * @since 1.26
136 */
137 public static function extractParams( $blob ) {
138 return unserialize( $blob );
139 }
140 }
141
142 /**
143 * This class wraps around database result row.
144 * @since 1.19
145 */
146 class DatabaseLogEntry extends LogEntryBase {
147 // Static->
148
149 /**
150 * Returns array of information that is needed for querying
151 * log entries. Array contains the following keys:
152 * tables, fields, conds, options and join_conds
153 * @return array
154 */
155 public static function getSelectQueryData() {
156 $tables = array( 'logging', 'user' );
157 $fields = array(
158 'log_id', 'log_type', 'log_action', 'log_timestamp',
159 'log_user', 'log_user_text',
160 'log_namespace', 'log_title', // unused log_page
161 'log_comment', 'log_params', 'log_deleted',
162 'user_id', 'user_name', 'user_editcount',
163 );
164
165 $joins = array(
166 // IP's don't have an entry in user table
167 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
168 );
169
170 return array(
171 'tables' => $tables,
172 'fields' => $fields,
173 'conds' => array(),
174 'options' => array(),
175 'join_conds' => $joins,
176 );
177 }
178
179 /**
180 * Constructs new LogEntry from database result row.
181 * Supports rows from both logging and recentchanges table.
182 * @param stdClass|array $row
183 * @return DatabaseLogEntry
184 */
185 public static function newFromRow( $row ) {
186 $row = (object)$row;
187 if ( isset( $row->rc_logid ) ) {
188 return new RCDatabaseLogEntry( $row );
189 } else {
190 return new self( $row );
191 }
192 }
193
194 // Non-static->
195
196 /** @var stdClass Database result row. */
197 protected $row;
198
199 /** @var User */
200 protected $performer;
201
202 /** @var bool Whether the parameters for this log entry are stored in new
203 * or old format.
204 */
205 protected $legacy;
206
207 protected function __construct( $row ) {
208 $this->row = $row;
209 }
210
211 /**
212 * Returns the unique database id.
213 * @return int
214 */
215 public function getId() {
216 return (int)$this->row->log_id;
217 }
218
219 /**
220 * Returns whatever is stored in the database field.
221 * @return string
222 */
223 protected function getRawParameters() {
224 return $this->row->log_params;
225 }
226
227 // LogEntryBase->
228
229 public function isLegacy() {
230 // This does the check
231 $this->getParameters();
232
233 return $this->legacy;
234 }
235
236 // LogEntry->
237
238 public function getType() {
239 return $this->row->log_type;
240 }
241
242 public function getSubtype() {
243 return $this->row->log_action;
244 }
245
246 public function getParameters() {
247 if ( !isset( $this->params ) ) {
248 $blob = $this->getRawParameters();
249 MediaWiki\suppressWarnings();
250 $params = LogEntryBase::extractParams( $blob );
251 MediaWiki\restoreWarnings();
252 if ( $params !== false ) {
253 $this->params = $params;
254 $this->legacy = false;
255 } else {
256 $this->params = LogPage::extractParams( $blob );
257 $this->legacy = true;
258 }
259 }
260
261 return $this->params;
262 }
263
264 public function getPerformer() {
265 if ( !$this->performer ) {
266 $userId = (int)$this->row->log_user;
267 if ( $userId !== 0 ) { // logged-in users
268 if ( isset( $this->row->user_name ) ) {
269 $this->performer = User::newFromRow( $this->row );
270 } else {
271 $this->performer = User::newFromId( $userId );
272 }
273 } else { // IP users
274 $userText = $this->row->log_user_text;
275 $this->performer = User::newFromName( $userText, false );
276 }
277 }
278
279 return $this->performer;
280 }
281
282 public function getTarget() {
283 $namespace = $this->row->log_namespace;
284 $page = $this->row->log_title;
285 $title = Title::makeTitle( $namespace, $page );
286
287 return $title;
288 }
289
290 public function getTimestamp() {
291 return wfTimestamp( TS_MW, $this->row->log_timestamp );
292 }
293
294 public function getComment() {
295 return $this->row->log_comment;
296 }
297
298 public function getDeleted() {
299 return $this->row->log_deleted;
300 }
301 }
302
303 class RCDatabaseLogEntry extends DatabaseLogEntry {
304
305 public function getId() {
306 return $this->row->rc_logid;
307 }
308
309 protected function getRawParameters() {
310 return $this->row->rc_params;
311 }
312
313 // LogEntry->
314
315 public function getType() {
316 return $this->row->rc_log_type;
317 }
318
319 public function getSubtype() {
320 return $this->row->rc_log_action;
321 }
322
323 public function getPerformer() {
324 if ( !$this->performer ) {
325 $userId = (int)$this->row->rc_user;
326 if ( $userId !== 0 ) {
327 $this->performer = User::newFromId( $userId );
328 } else {
329 $userText = $this->row->rc_user_text;
330 // Might be an IP, don't validate the username
331 $this->performer = User::newFromName( $userText, false );
332 }
333 }
334
335 return $this->performer;
336 }
337
338 public function getTarget() {
339 $namespace = $this->row->rc_namespace;
340 $page = $this->row->rc_title;
341 $title = Title::makeTitle( $namespace, $page );
342
343 return $title;
344 }
345
346 public function getTimestamp() {
347 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
348 }
349
350 public function getComment() {
351 return $this->row->rc_comment;
352 }
353
354 public function getDeleted() {
355 return $this->row->rc_deleted;
356 }
357 }
358
359 /**
360 * Class for creating log entries manually, for
361 * example to inject them into the database.
362 * @since 1.19
363 */
364 class ManualLogEntry extends LogEntryBase {
365 /** @var string Type of log entry */
366 protected $type;
367
368 /** @var string Sub type of log entry */
369 protected $subtype;
370
371 /** @var array Parameters for log entry */
372 protected $parameters = array();
373
374 /** @var array */
375 protected $relations = array();
376
377 /** @var User Performer of the action for the log entry */
378 protected $performer;
379
380 /** @var Title Target title for the log entry */
381 protected $target;
382
383 /** @var string Timestamp of creation of the log entry */
384 protected $timestamp;
385
386 /** @var string Comment for the log entry */
387 protected $comment = '';
388
389 /** @var int A rev id associated to the log entry */
390 protected $revId = 0;
391
392 /** @var int Deletion state of the log entry */
393 protected $deleted;
394
395 /** @var int ID of the log entry */
396 protected $id;
397
398 /** @var bool Whether this is a legacy log entry */
399 protected $legacy = false;
400
401 /**
402 * Constructor.
403 *
404 * @since 1.19
405 *
406 * @param string $type
407 * @param string $subtype
408 */
409 public function __construct( $type, $subtype ) {
410 $this->type = $type;
411 $this->subtype = $subtype;
412 }
413
414 /**
415 * Set extra log parameters.
416 *
417 * You can pass params to the log action message by prefixing the keys with
418 * a number and optional type, using colons to separate the fields. The
419 * numbering should start with number 4, the first three parameters are
420 * hardcoded for every message. Example:
421 * $entry->setParameters(
422 * '4::color' => 'blue',
423 * '5:number:count' => 3000,
424 * 'animal' => 'dog'
425 * );
426 *
427 * @since 1.19
428 *
429 * @param array $parameters Associative array
430 */
431 public function setParameters( $parameters ) {
432 $this->parameters = $parameters;
433 }
434
435 /**
436 * Declare arbitrary tag/value relations to this log entry.
437 * These can be used to filter log entries later on.
438 *
439 * @param array $relations Map of (tag => (list of values|value))
440 * @since 1.22
441 */
442 public function setRelations( array $relations ) {
443 $this->relations = $relations;
444 }
445
446 /**
447 * Set the user that performed the action being logged.
448 *
449 * @since 1.19
450 *
451 * @param User $performer
452 */
453 public function setPerformer( User $performer ) {
454 $this->performer = $performer;
455 }
456
457 /**
458 * Set the title of the object changed.
459 *
460 * @since 1.19
461 *
462 * @param Title $target
463 */
464 public function setTarget( Title $target ) {
465 $this->target = $target;
466 }
467
468 /**
469 * Set the timestamp of when the logged action took place.
470 *
471 * @since 1.19
472 *
473 * @param string $timestamp
474 */
475 public function setTimestamp( $timestamp ) {
476 $this->timestamp = $timestamp;
477 }
478
479 /**
480 * Set a comment associated with the action being logged.
481 *
482 * @since 1.19
483 *
484 * @param string $comment
485 */
486 public function setComment( $comment ) {
487 $this->comment = $comment;
488 }
489
490 /**
491 * Set an associated revision id.
492 *
493 * @since 1.27
494 *
495 * @param int $revId
496 */
497 public function setAssociatedRevId( $revId ) {
498 $this->revId = $revId;
499 }
500
501 /**
502 * Set the 'legacy' flag
503 *
504 * @since 1.25
505 * @param bool $legacy
506 */
507 public function setLegacy( $legacy ) {
508 $this->legacy = $legacy;
509 }
510
511 /**
512 * TODO: document
513 *
514 * @since 1.19
515 *
516 * @param int $deleted
517 */
518 public function setDeleted( $deleted ) {
519 $this->deleted = $deleted;
520 }
521
522 /**
523 * Inserts the entry into the logging table.
524 * @param IDatabase $dbw
525 * @return int ID of the log entry
526 * @throws MWException
527 */
528 public function insert( IDatabase $dbw = null ) {
529 global $wgContLang;
530
531 $dbw = $dbw ?: wfGetDB( DB_MASTER );
532 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
533
534 if ( $this->timestamp === null ) {
535 $this->timestamp = wfTimestampNow();
536 }
537
538 # Trim spaces on user supplied text
539 $comment = trim( $this->getComment() );
540
541 # Truncate for whole multibyte characters.
542 $comment = $wgContLang->truncate( $comment, 255 );
543
544 $data = array(
545 'log_id' => $id,
546 'log_type' => $this->getType(),
547 'log_action' => $this->getSubtype(),
548 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
549 'log_user' => $this->getPerformer()->getId(),
550 'log_user_text' => $this->getPerformer()->getName(),
551 'log_namespace' => $this->getTarget()->getNamespace(),
552 'log_title' => $this->getTarget()->getDBkey(),
553 'log_page' => $this->getTarget()->getArticleID(),
554 'log_comment' => $comment,
555 'log_params' => LogEntryBase::makeParamBlob( $this->getParameters() ),
556 );
557 if ( isset( $this->deleted ) ) {
558 $data['log_deleted'] = $this->deleted;
559 }
560
561 $dbw->insert( 'logging', $data, __METHOD__ );
562 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
563
564 $rows = array();
565 foreach ( $this->relations as $tag => $values ) {
566 if ( !strlen( $tag ) ) {
567 throw new MWException( "Got empty log search tag." );
568 }
569
570 if ( !is_array( $values ) ) {
571 $values = array( $values );
572 }
573
574 foreach ( $values as $value ) {
575 $rows[] = array(
576 'ls_field' => $tag,
577 'ls_value' => $value,
578 'ls_log_id' => $this->id
579 );
580 }
581 }
582 if ( count( $rows ) ) {
583 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
584 }
585
586 return $this->id;
587 }
588
589 /**
590 * Get a RecentChanges object for the log entry
591 * @param int $newId
592 * @return RecentChange
593 * @since 1.23
594 */
595 public function getRecentChange( $newId = 0 ) {
596 $formatter = LogFormatter::newFromEntry( $this );
597 $context = RequestContext::newExtraneousContext( $this->getTarget() );
598 $formatter->setContext( $context );
599
600 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
601 $user = $this->getPerformer();
602 $ip = "";
603 if ( $user->isAnon() ) {
604 /*
605 * "MediaWiki default" and friends may have
606 * no IP address in their name
607 */
608 if ( IP::isIPAddress( $user->getName() ) ) {
609 $ip = $user->getName();
610 }
611 }
612
613 return RecentChange::newLogEntry(
614 $this->getTimestamp(),
615 $logpage,
616 $user,
617 $formatter->getPlainActionText(),
618 $ip,
619 $this->getType(),
620 $this->getSubtype(),
621 $this->getTarget(),
622 $this->getComment(),
623 LogEntryBase::makeParamBlob( $this->getParameters() ),
624 $newId,
625 $formatter->getIRCActionComment(), // Used for IRC feeds
626 $this->getAssociatedRevId() // Used for e.g. moves and uploads
627 );
628 }
629
630 /**
631 * Publishes the log entry.
632 * @param int $newId Id of the log entry.
633 * @param string $to One of: rcandudp (default), rc, udp
634 */
635 public function publish( $newId, $to = 'rcandudp' ) {
636 $log = new LogPage( $this->getType() );
637 if ( $log->isRestricted() ) {
638 return;
639 }
640
641 $rc = $this->getRecentChange( $newId );
642
643 if ( $to === 'rc' || $to === 'rcandudp' ) {
644 $rc->save( 'pleasedontudp' );
645 }
646
647 if ( $to === 'udp' || $to === 'rcandudp' ) {
648 $rc->notifyRCFeeds();
649 }
650
651 // Log the autopatrol if an associated rev id was passed
652 if ( $this->getAssociatedRevId() > 0 &&
653 $rc->getAttribute( 'rc_patrolled' ) === 1 ) {
654 PatrolLog::record( $rc, true, $this->getPerformer() );
655 }
656 }
657
658 // LogEntry->
659
660 public function getType() {
661 return $this->type;
662 }
663
664 public function getSubtype() {
665 return $this->subtype;
666 }
667
668 public function getParameters() {
669 return $this->parameters;
670 }
671
672 /**
673 * @return User
674 */
675 public function getPerformer() {
676 return $this->performer;
677 }
678
679 /**
680 * @return Title
681 */
682 public function getTarget() {
683 return $this->target;
684 }
685
686 public function getTimestamp() {
687 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
688
689 return wfTimestamp( TS_MW, $ts );
690 }
691
692 public function getComment() {
693 return $this->comment;
694 }
695
696 /**
697 * @since 1.27
698 * @return int
699 */
700 public function getAssociatedRevId() {
701 return $this->revId;
702 }
703
704 /**
705 * @since 1.25
706 * @return bool
707 */
708 public function isLegacy() {
709 return $this->legacy;
710 }
711
712 public function getDeleted() {
713 return (int)$this->deleted;
714 }
715 }