SessionManager: Save user name to metadata even if the user doesn't exist locally
[mediawiki.git] / includes / logging / LogEntry.php
blobddcb6365ece7e92ee15e5bfaea823927f19a313b
1 <?php
2 /**
3 * Contain classes for dealing with individual log entries
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
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.
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.
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
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
31 /**
32 * Interface for log entries. Every log entry has these methods.
34 * @since 1.19
36 interface LogEntry {
38 /**
39 * The main log type.
41 * @return string
43 public function getType();
45 /**
46 * The log subtype.
48 * @return string
50 public function getSubtype();
52 /**
53 * The full logtype in format maintype/subtype.
55 * @return string
57 public function getFullType();
59 /**
60 * Get the extra parameters stored for this message.
62 * @return array
64 public function getParameters();
66 /**
67 * Get the user for performed this action.
69 * @return User
71 public function getPerformer();
73 /**
74 * Get the target page of this action.
76 * @return Title
78 public function getTarget();
80 /**
81 * Get the timestamp when the action was executed.
83 * @return string
85 public function getTimestamp();
87 /**
88 * Get the user provided comment.
90 * @return string
92 public function getComment();
94 /**
95 * Get the access restriction.
97 * @return string
99 public function getDeleted();
102 * @param int $field One of LogPage::DELETED_* bitfield constants
103 * @return bool
105 public function isDeleted( $field );
109 * Extends the LogEntryInterface with some basic functionality
111 * @since 1.19
113 abstract class LogEntryBase implements LogEntry {
115 public function getFullType() {
116 return $this->getType() . '/' . $this->getSubtype();
119 public function isDeleted( $field ) {
120 return ( $this->getDeleted() & $field ) === $field;
124 * Whether the parameters for this log are stored in new or
125 * old format.
127 * @return bool
129 public function isLegacy() {
130 return false;
134 * Create a blob from a parameter array
136 * @since 1.26
137 * @param array $params
138 * @return string
140 public static function makeParamBlob( $params ) {
141 return serialize( (array)$params );
145 * Extract a parameter array from a blob
147 * @since 1.26
148 * @param string $blob
149 * @return array
151 public static function extractParams( $blob ) {
152 return unserialize( $blob );
157 * This class wraps around database result row.
159 * @since 1.19
161 class DatabaseLogEntry extends LogEntryBase {
164 * Returns array of information that is needed for querying
165 * log entries. Array contains the following keys:
166 * tables, fields, conds, options and join_conds
168 * @return array
170 public static function getSelectQueryData() {
171 $tables = array( 'logging', 'user' );
172 $fields = array(
173 'log_id', 'log_type', 'log_action', 'log_timestamp',
174 'log_user', 'log_user_text',
175 'log_namespace', 'log_title', // unused log_page
176 'log_comment', 'log_params', 'log_deleted',
177 'user_id', 'user_name', 'user_editcount',
180 $joins = array(
181 // IPs don't have an entry in user table
182 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
185 return array(
186 'tables' => $tables,
187 'fields' => $fields,
188 'conds' => array(),
189 'options' => array(),
190 'join_conds' => $joins,
195 * Constructs new LogEntry from database result row.
196 * Supports rows from both logging and recentchanges table.
198 * @param stdClass|array $row
199 * @return DatabaseLogEntry
201 public static function newFromRow( $row ) {
202 $row = (object)$row;
203 if ( isset( $row->rc_logid ) ) {
204 return new RCDatabaseLogEntry( $row );
205 } else {
206 return new self( $row );
210 /** @var stdClass Database result row. */
211 protected $row;
213 /** @var User */
214 protected $performer;
216 /** @var array Parameters for log entry */
217 protected $params;
219 /** @var int A rev id associated to the log entry */
220 protected $revId = null;
222 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
223 protected $legacy;
225 protected function __construct( $row ) {
226 $this->row = $row;
230 * Returns the unique database id.
232 * @return int
234 public function getId() {
235 return (int)$this->row->log_id;
239 * Returns whatever is stored in the database field.
241 * @return string
243 protected function getRawParameters() {
244 return $this->row->log_params;
247 public function isLegacy() {
248 // This extracts the property
249 $this->getParameters();
250 return $this->legacy;
253 public function getType() {
254 return $this->row->log_type;
257 public function getSubtype() {
258 return $this->row->log_action;
261 public function getParameters() {
262 if ( !isset( $this->params ) ) {
263 $blob = $this->getRawParameters();
264 MediaWiki\suppressWarnings();
265 $params = LogEntryBase::extractParams( $blob );
266 MediaWiki\restoreWarnings();
267 if ( $params !== false ) {
268 $this->params = $params;
269 $this->legacy = false;
270 } else {
271 $this->params = LogPage::extractParams( $blob );
272 $this->legacy = true;
275 if ( isset( $this->params['associated_rev_id'] ) ) {
276 $this->revId = $this->params['associated_rev_id'];
277 unset( $this->params['associated_rev_id'] );
281 return $this->params;
284 public function getAssociatedRevId() {
285 // This extracts the property
286 $this->getParameters();
287 return $this->revId;
290 public function getPerformer() {
291 if ( !$this->performer ) {
292 $userId = (int)$this->row->log_user;
293 if ( $userId !== 0 ) {
294 // logged-in users
295 if ( isset( $this->row->user_name ) ) {
296 $this->performer = User::newFromRow( $this->row );
297 } else {
298 $this->performer = User::newFromId( $userId );
300 } else {
301 // IP users
302 $userText = $this->row->log_user_text;
303 $this->performer = User::newFromName( $userText, false );
307 return $this->performer;
310 public function getTarget() {
311 $namespace = $this->row->log_namespace;
312 $page = $this->row->log_title;
313 $title = Title::makeTitle( $namespace, $page );
315 return $title;
318 public function getTimestamp() {
319 return wfTimestamp( TS_MW, $this->row->log_timestamp );
322 public function getComment() {
323 return $this->row->log_comment;
326 public function getDeleted() {
327 return $this->row->log_deleted;
331 class RCDatabaseLogEntry extends DatabaseLogEntry {
333 public function getId() {
334 return $this->row->rc_logid;
337 protected function getRawParameters() {
338 return $this->row->rc_params;
341 public function getAssociatedRevId() {
342 return $this->row->rc_this_oldid;
345 public function getType() {
346 return $this->row->rc_log_type;
349 public function getSubtype() {
350 return $this->row->rc_log_action;
353 public function getPerformer() {
354 if ( !$this->performer ) {
355 $userId = (int)$this->row->rc_user;
356 if ( $userId !== 0 ) {
357 $this->performer = User::newFromId( $userId );
358 } else {
359 $userText = $this->row->rc_user_text;
360 // Might be an IP, don't validate the username
361 $this->performer = User::newFromName( $userText, false );
365 return $this->performer;
368 public function getTarget() {
369 $namespace = $this->row->rc_namespace;
370 $page = $this->row->rc_title;
371 $title = Title::makeTitle( $namespace, $page );
373 return $title;
376 public function getTimestamp() {
377 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
380 public function getComment() {
381 return $this->row->rc_comment;
384 public function getDeleted() {
385 return $this->row->rc_deleted;
390 * Class for creating log entries manually, to inject them into the database.
392 * @since 1.19
394 class ManualLogEntry extends LogEntryBase {
395 /** @var string Type of log entry */
396 protected $type;
398 /** @var string Sub type of log entry */
399 protected $subtype;
401 /** @var array Parameters for log entry */
402 protected $parameters = array();
404 /** @var array */
405 protected $relations = array();
407 /** @var User Performer of the action for the log entry */
408 protected $performer;
410 /** @var Title Target title for the log entry */
411 protected $target;
413 /** @var string Timestamp of creation of the log entry */
414 protected $timestamp;
416 /** @var string Comment for the log entry */
417 protected $comment = '';
419 /** @var int A rev id associated to the log entry */
420 protected $revId = 0;
422 /** @var int Deletion state of the log entry */
423 protected $deleted;
425 /** @var int ID of the log entry */
426 protected $id;
428 /** @var bool Whether this is a legacy log entry */
429 protected $legacy = false;
432 * Constructor.
434 * @since 1.19
435 * @param string $type
436 * @param string $subtype
438 public function __construct( $type, $subtype ) {
439 $this->type = $type;
440 $this->subtype = $subtype;
444 * Set extra log parameters.
446 * You can pass params to the log action message by prefixing the keys with
447 * a number and optional type, using colons to separate the fields. The
448 * numbering should start with number 4, the first three parameters are
449 * hardcoded for every message.
451 * If you want to store stuff that should not be available in messages, don't
452 * prefix the array key with a number and don't use the colons.
454 * Example:
455 * $entry->setParameters(
456 * '4::color' => 'blue',
457 * '5:number:count' => 3000,
458 * 'animal' => 'dog'
459 * );
461 * @since 1.19
462 * @param array $parameters Associative array
464 public function setParameters( $parameters ) {
465 $this->parameters = $parameters;
469 * Declare arbitrary tag/value relations to this log entry.
470 * These can be used to filter log entries later on.
472 * @param array $relations Map of (tag => (list of values|value))
473 * @since 1.22
475 public function setRelations( array $relations ) {
476 $this->relations = $relations;
480 * Set the user that performed the action being logged.
482 * @since 1.19
483 * @param User $performer
485 public function setPerformer( User $performer ) {
486 $this->performer = $performer;
490 * Set the title of the object changed.
492 * @since 1.19
493 * @param Title $target
495 public function setTarget( Title $target ) {
496 $this->target = $target;
500 * Set the timestamp of when the logged action took place.
502 * @since 1.19
503 * @param string $timestamp
505 public function setTimestamp( $timestamp ) {
506 $this->timestamp = $timestamp;
510 * Set a comment associated with the action being logged.
512 * @since 1.19
513 * @param string $comment
515 public function setComment( $comment ) {
516 $this->comment = $comment;
520 * Set an associated revision id.
522 * For example, the ID of the revision that was inserted to mark a page move
523 * or protection, file upload, etc.
525 * @since 1.27
526 * @param int $revId
528 public function setAssociatedRevId( $revId ) {
529 $this->revId = $revId;
533 * Set the 'legacy' flag
535 * @since 1.25
536 * @param bool $legacy
538 public function setLegacy( $legacy ) {
539 $this->legacy = $legacy;
543 * Set the 'deleted' flag.
545 * @since 1.19
546 * @param int $deleted One of LogPage::DELETED_* bitfield constants
548 public function setDeleted( $deleted ) {
549 $this->deleted = $deleted;
553 * Insert the entry into the `logging` table.
555 * @param IDatabase $dbw
556 * @return int ID of the log entry
557 * @throws MWException
559 public function insert( IDatabase $dbw = null ) {
560 global $wgContLang;
562 $dbw = $dbw ?: wfGetDB( DB_MASTER );
563 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
565 if ( $this->timestamp === null ) {
566 $this->timestamp = wfTimestampNow();
569 // Trim spaces on user supplied text
570 $comment = trim( $this->getComment() );
572 // Truncate for whole multibyte characters.
573 $comment = $wgContLang->truncate( $comment, 255 );
575 $params = $this->getParameters();
576 $relations = $this->relations;
578 // Additional fields for which there's no space in the database table schema
579 $revId = $this->getAssociatedRevId();
580 if ( $revId ) {
581 $params['associated_rev_id'] = $revId;
582 $relations['associated_rev_id'] = $revId;
585 $data = array(
586 'log_id' => $id,
587 'log_type' => $this->getType(),
588 'log_action' => $this->getSubtype(),
589 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
590 'log_user' => $this->getPerformer()->getId(),
591 'log_user_text' => $this->getPerformer()->getName(),
592 'log_namespace' => $this->getTarget()->getNamespace(),
593 'log_title' => $this->getTarget()->getDBkey(),
594 'log_page' => $this->getTarget()->getArticleID(),
595 'log_comment' => $comment,
596 'log_params' => LogEntryBase::makeParamBlob( $params ),
598 if ( isset( $this->deleted ) ) {
599 $data['log_deleted'] = $this->deleted;
602 $dbw->insert( 'logging', $data, __METHOD__ );
603 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
605 $rows = array();
606 foreach ( $relations as $tag => $values ) {
607 if ( !strlen( $tag ) ) {
608 throw new MWException( "Got empty log search tag." );
611 if ( !is_array( $values ) ) {
612 $values = array( $values );
615 foreach ( $values as $value ) {
616 $rows[] = array(
617 'ls_field' => $tag,
618 'ls_value' => $value,
619 'ls_log_id' => $this->id
623 if ( count( $rows ) ) {
624 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
627 return $this->id;
631 * Get a RecentChanges object for the log entry
633 * @param int $newId
634 * @return RecentChange
635 * @since 1.23
637 public function getRecentChange( $newId = 0 ) {
638 $formatter = LogFormatter::newFromEntry( $this );
639 $context = RequestContext::newExtraneousContext( $this->getTarget() );
640 $formatter->setContext( $context );
642 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
643 $user = $this->getPerformer();
644 $ip = "";
645 if ( $user->isAnon() ) {
646 // "MediaWiki default" and friends may have
647 // no IP address in their name
648 if ( IP::isIPAddress( $user->getName() ) ) {
649 $ip = $user->getName();
653 return RecentChange::newLogEntry(
654 $this->getTimestamp(),
655 $logpage,
656 $user,
657 $formatter->getPlainActionText(),
658 $ip,
659 $this->getType(),
660 $this->getSubtype(),
661 $this->getTarget(),
662 $this->getComment(),
663 LogEntryBase::makeParamBlob( $this->getParameters() ),
664 $newId,
665 $formatter->getIRCActionComment(), // Used for IRC feeds
666 $this->getAssociatedRevId() // Used for e.g. moves and uploads
671 * Publish the log entry.
673 * @param int $newId Id of the log entry.
674 * @param string $to One of: rcandudp (default), rc, udp
675 * @return RecentChange|null
677 public function publish( $newId, $to = 'rcandudp' ) {
678 $log = new LogPage( $this->getType() );
679 if ( $log->isRestricted() ) {
680 return null;
683 $rc = $this->getRecentChange( $newId );
685 if ( $to === 'rc' || $to === 'rcandudp' ) {
686 $rc->save( 'pleasedontudp' );
689 if ( $to === 'udp' || $to === 'rcandudp' ) {
690 $rc->notifyRCFeeds();
693 // Log the autopatrol if an associated rev id was passed
694 if ( $this->getAssociatedRevId() > 0 &&
695 $rc->getAttribute( 'rc_patrolled' ) === 1 ) {
696 PatrolLog::record( $rc, true, $this->getPerformer() );
699 return $rc;
702 public function getType() {
703 return $this->type;
706 public function getSubtype() {
707 return $this->subtype;
710 public function getParameters() {
711 return $this->parameters;
715 * @return User
717 public function getPerformer() {
718 return $this->performer;
722 * @return Title
724 public function getTarget() {
725 return $this->target;
728 public function getTimestamp() {
729 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
731 return wfTimestamp( TS_MW, $ts );
734 public function getComment() {
735 return $this->comment;
739 * @since 1.27
740 * @return int
742 public function getAssociatedRevId() {
743 return $this->revId;
747 * @since 1.25
748 * @return bool
750 public function isLegacy() {
751 return $this->legacy;
754 public function getDeleted() {
755 return (int)$this->deleted;