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
26 * @author Niklas Laxström
27 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
31 use Wikimedia\Rdbms\IDatabase
;
34 * Interface for log entries. Every log entry has these methods.
45 public function getType();
52 public function getSubtype();
55 * The full logtype in format maintype/subtype.
59 public function getFullType();
62 * Get the extra parameters stored for this message.
66 public function getParameters();
69 * Get the user for performed this action.
73 public function getPerformer();
76 * Get the target page of this action.
80 public function getTarget();
83 * Get the timestamp when the action was executed.
87 public function getTimestamp();
90 * Get the user provided comment.
94 public function getComment();
97 * Get the access restriction.
101 public function getDeleted();
104 * @param int $field One of LogPage::DELETED_* bitfield constants
107 public function isDeleted( $field );
111 * Extends the LogEntryInterface with some basic functionality
115 abstract class LogEntryBase
implements LogEntry
{
117 public function getFullType() {
118 return $this->getType() . '/' . $this->getSubtype();
121 public function isDeleted( $field ) {
122 return ( $this->getDeleted() & $field ) === $field;
126 * Whether the parameters for this log are stored in new or
131 public function isLegacy() {
136 * Create a blob from a parameter array
139 * @param array $params
142 public static function makeParamBlob( $params ) {
143 return serialize( (array)$params );
147 * Extract a parameter array from a blob
150 * @param string $blob
153 public static function extractParams( $blob ) {
154 return unserialize( $blob );
159 * This class wraps around database result row.
163 class DatabaseLogEntry
extends LogEntryBase
{
166 * Returns array of information that is needed for querying
167 * log entries. Array contains the following keys:
168 * tables, fields, conds, options and join_conds
172 public static function getSelectQueryData() {
173 $commentQuery = CommentStore
::getStore()->getJoin( 'log_comment' );
174 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
176 $tables = array_merge(
177 [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
180 'log_id', 'log_type', 'log_action', 'log_timestamp',
181 'log_namespace', 'log_title', // unused log_page
182 'log_params', 'log_deleted',
183 'user_id', 'user_name', 'user_editcount',
184 ] +
$commentQuery['fields'] +
$actorQuery['fields'];
187 // IPs don't have an entry in user table
188 'user' => [ 'LEFT JOIN', 'user_id=' . $actorQuery['fields']['log_user'] ],
189 ] +
$commentQuery['joins'] +
$actorQuery['joins'];
196 'join_conds' => $joins,
201 * Constructs new LogEntry from database result row.
202 * Supports rows from both logging and recentchanges table.
204 * @param stdClass|array $row
205 * @return DatabaseLogEntry
207 public static function newFromRow( $row ) {
209 if ( isset( $row->rc_logid
) ) {
210 return new RCDatabaseLogEntry( $row );
212 return new self( $row );
216 /** @var stdClass Database result row. */
220 protected $performer;
222 /** @var array Parameters for log entry */
225 /** @var int A rev id associated to the log entry */
226 protected $revId = null;
228 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
231 protected function __construct( $row ) {
236 * Returns the unique database id.
240 public function getId() {
241 return (int)$this->row
->log_id
;
245 * Returns whatever is stored in the database field.
249 protected function getRawParameters() {
250 return $this->row
->log_params
;
253 public function isLegacy() {
254 // This extracts the property
255 $this->getParameters();
256 return $this->legacy
;
259 public function getType() {
260 return $this->row
->log_type
;
263 public function getSubtype() {
264 return $this->row
->log_action
;
267 public function getParameters() {
268 if ( !isset( $this->params
) ) {
269 $blob = $this->getRawParameters();
270 Wikimedia\
suppressWarnings();
271 $params = LogEntryBase
::extractParams( $blob );
272 Wikimedia\restoreWarnings
();
273 if ( $params !== false ) {
274 $this->params
= $params;
275 $this->legacy
= false;
277 $this->params
= LogPage
::extractParams( $blob );
278 $this->legacy
= true;
281 if ( isset( $this->params
['associated_rev_id'] ) ) {
282 $this->revId
= $this->params
['associated_rev_id'];
283 unset( $this->params
['associated_rev_id'] );
287 return $this->params
;
290 public function getAssociatedRevId() {
291 // This extracts the property
292 $this->getParameters();
296 public function getPerformer() {
297 if ( !$this->performer
) {
298 $actorId = isset( $this->row
->log_actor
) ?
(int)$this->row
->log_actor
: 0;
299 $userId = (int)$this->row
->log_user
;
300 if ( $userId !== 0 ||
$actorId !== 0 ) {
302 if ( isset( $this->row
->user_name
) ) {
303 $this->performer
= User
::newFromRow( $this->row
);
304 } elseif ( $actorId !== 0 ) {
305 $this->performer
= User
::newFromActorId( $actorId );
307 $this->performer
= User
::newFromId( $userId );
311 $userText = $this->row
->log_user_text
;
312 $this->performer
= User
::newFromName( $userText, false );
316 return $this->performer
;
319 public function getTarget() {
320 $namespace = $this->row
->log_namespace
;
321 $page = $this->row
->log_title
;
322 $title = Title
::makeTitle( $namespace, $page );
327 public function getTimestamp() {
328 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
331 public function getComment() {
332 return CommentStore
::getStore()->getComment( 'log_comment', $this->row
)->text
;
335 public function getDeleted() {
336 return $this->row
->log_deleted
;
340 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
342 public function getId() {
343 return $this->row
->rc_logid
;
346 protected function getRawParameters() {
347 return $this->row
->rc_params
;
350 public function getAssociatedRevId() {
351 return $this->row
->rc_this_oldid
;
354 public function getType() {
355 return $this->row
->rc_log_type
;
358 public function getSubtype() {
359 return $this->row
->rc_log_action
;
362 public function getPerformer() {
363 if ( !$this->performer
) {
364 $actorId = isset( $this->row
->rc_actor
) ?
(int)$this->row
->rc_actor
: 0;
365 $userId = (int)$this->row
->rc_user
;
366 if ( $actorId !== 0 ) {
367 $this->performer
= User
::newFromActorId( $actorId );
368 } elseif ( $userId !== 0 ) {
369 $this->performer
= User
::newFromId( $userId );
371 $userText = $this->row
->rc_user_text
;
372 // Might be an IP, don't validate the username
373 $this->performer
= User
::newFromName( $userText, false );
377 return $this->performer
;
380 public function getTarget() {
381 $namespace = $this->row
->rc_namespace
;
382 $page = $this->row
->rc_title
;
383 $title = Title
::makeTitle( $namespace, $page );
388 public function getTimestamp() {
389 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
392 public function getComment() {
393 return CommentStore
::getStore()
394 // Legacy because the row may have used RecentChange::selectFields()
395 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $this->row
)->text
;
398 public function getDeleted() {
399 return $this->row
->rc_deleted
;
404 * Class for creating log entries manually, to inject them into the database.
408 class ManualLogEntry
extends LogEntryBase
{
409 /** @var string Type of log entry */
412 /** @var string Sub type of log entry */
415 /** @var array Parameters for log entry */
416 protected $parameters = [];
419 protected $relations = [];
421 /** @var User Performer of the action for the log entry */
422 protected $performer;
424 /** @var Title Target title for the log entry */
427 /** @var string Timestamp of creation of the log entry */
428 protected $timestamp;
430 /** @var string Comment for the log entry */
431 protected $comment = '';
433 /** @var int A rev id associated to the log entry */
434 protected $revId = 0;
436 /** @var array Change tags add to the log entry */
437 protected $tags = null;
439 /** @var int Deletion state of the log entry */
442 /** @var int ID of the log entry */
445 /** @var bool Can this log entry be patrolled? */
446 protected $isPatrollable = false;
448 /** @var bool Whether this is a legacy log entry */
449 protected $legacy = false;
453 * @param string $type
454 * @param string $subtype
456 public function __construct( $type, $subtype ) {
458 $this->subtype
= $subtype;
462 * Set extra log parameters.
464 * You can pass params to the log action message by prefixing the keys with
465 * a number and optional type, using colons to separate the fields. The
466 * numbering should start with number 4, the first three parameters are
467 * hardcoded for every message.
469 * If you want to store stuff that should not be available in messages, don't
470 * prefix the array key with a number and don't use the colons.
473 * $entry->setParameters(
474 * '4::color' => 'blue',
475 * '5:number:count' => 3000,
480 * @param array $parameters Associative array
482 public function setParameters( $parameters ) {
483 $this->parameters
= $parameters;
487 * Declare arbitrary tag/value relations to this log entry.
488 * These can be used to filter log entries later on.
490 * @param array $relations Map of (tag => (list of values|value))
493 public function setRelations( array $relations ) {
494 $this->relations
= $relations;
498 * Set the user that performed the action being logged.
501 * @param User $performer
503 public function setPerformer( User
$performer ) {
504 $this->performer
= $performer;
508 * Set the title of the object changed.
511 * @param Title $target
513 public function setTarget( Title
$target ) {
514 $this->target
= $target;
518 * Set the timestamp of when the logged action took place.
521 * @param string $timestamp
523 public function setTimestamp( $timestamp ) {
524 $this->timestamp
= $timestamp;
528 * Set a comment associated with the action being logged.
531 * @param string $comment
533 public function setComment( $comment ) {
534 $this->comment
= $comment;
538 * Set an associated revision id.
540 * For example, the ID of the revision that was inserted to mark a page move
541 * or protection, file upload, etc.
546 public function setAssociatedRevId( $revId ) {
547 $this->revId
= $revId;
551 * Set change tags for the log entry.
554 * @param string|string[] $tags
556 public function setTags( $tags ) {
557 if ( is_string( $tags ) ) {
564 * Set whether this log entry should be made patrollable
565 * This shouldn't depend on config, only on whether there is full support
566 * in the software for patrolling this log entry.
570 * @param bool $patrollable
572 public function setIsPatrollable( $patrollable ) {
573 $this->isPatrollable
= (bool)$patrollable;
577 * Set the 'legacy' flag
580 * @param bool $legacy
582 public function setLegacy( $legacy ) {
583 $this->legacy
= $legacy;
587 * Set the 'deleted' flag.
590 * @param int $deleted One of LogPage::DELETED_* bitfield constants
592 public function setDeleted( $deleted ) {
593 $this->deleted
= $deleted;
597 * Insert the entry into the `logging` table.
599 * @param IDatabase $dbw
600 * @return int ID of the log entry
601 * @throws MWException
603 public function insert( IDatabase
$dbw = null ) {
604 global $wgActorTableSchemaMigrationStage;
606 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
608 if ( $this->timestamp
=== null ) {
609 $this->timestamp
= wfTimestampNow();
612 // Trim spaces on user supplied text
613 $comment = trim( $this->getComment() );
615 $params = $this->getParameters();
616 $relations = $this->relations
;
618 // Ensure actor relations are set
619 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
&&
620 empty( $relations['target_author_actor'] )
623 if ( !empty( $relations['target_author_id'] ) ) {
624 foreach ( $relations['target_author_id'] as $id ) {
625 $actorIds[] = User
::newFromId( $id )->getActorId( $dbw );
628 if ( !empty( $relations['target_author_ip'] ) ) {
629 foreach ( $relations['target_author_ip'] as $ip ) {
630 $actorIds[] = User
::newFromName( $ip, false )->getActorId( $dbw );
634 $relations['target_author_actor'] = $actorIds;
635 $params['authorActors'] = $actorIds;
638 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_NEW
) {
639 unset( $relations['target_author_id'], $relations['target_author_ip'] );
640 unset( $params['authorIds'], $params['authorIPs'] );
643 // Additional fields for which there's no space in the database table schema
644 $revId = $this->getAssociatedRevId();
646 $params['associated_rev_id'] = $revId;
647 $relations['associated_rev_id'] = $revId;
651 'log_type' => $this->getType(),
652 'log_action' => $this->getSubtype(),
653 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
654 'log_namespace' => $this->getTarget()->getNamespace(),
655 'log_title' => $this->getTarget()->getDBkey(),
656 'log_page' => $this->getTarget()->getArticleID(),
657 'log_params' => LogEntryBase
::makeParamBlob( $params ),
659 if ( isset( $this->deleted
) ) {
660 $data['log_deleted'] = $this->deleted
;
662 $data +
= CommentStore
::getStore()->insert( $dbw, 'log_comment', $comment );
663 $data +
= ActorMigration
::newMigration()
664 ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
666 $dbw->insert( 'logging', $data, __METHOD__
);
667 $this->id
= $dbw->insertId();
670 foreach ( $relations as $tag => $values ) {
671 if ( !strlen( $tag ) ) {
672 throw new MWException( "Got empty log search tag." );
675 if ( !is_array( $values ) ) {
676 $values = [ $values ];
679 foreach ( $values as $value ) {
682 'ls_value' => $value,
683 'ls_log_id' => $this->id
687 if ( count( $rows ) ) {
688 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
695 * Get a RecentChanges object for the log entry
698 * @return RecentChange
701 public function getRecentChange( $newId = 0 ) {
702 $formatter = LogFormatter
::newFromEntry( $this );
703 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
704 $formatter->setContext( $context );
706 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
707 $user = $this->getPerformer();
709 if ( $user->isAnon() ) {
710 // "MediaWiki default" and friends may have
711 // no IP address in their name
712 if ( IP
::isIPAddress( $user->getName() ) ) {
713 $ip = $user->getName();
717 return RecentChange
::newLogEntry(
718 $this->getTimestamp(),
721 $formatter->getPlainActionText(),
727 LogEntryBase
::makeParamBlob( $this->getParameters() ),
729 $formatter->getIRCActionComment(), // Used for IRC feeds
730 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
731 $this->getIsPatrollable()
736 * Publish the log entry.
738 * @param int $newId Id of the log entry.
739 * @param string $to One of: rcandudp (default), rc, udp
741 public function publish( $newId, $to = 'rcandudp' ) {
742 DeferredUpdates
::addCallableUpdate(
743 function () use ( $newId, $to ) {
744 $log = new LogPage( $this->getType() );
745 if ( !$log->isRestricted() ) {
746 $rc = $this->getRecentChange( $newId );
748 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
749 // save RC, passing tags so they are applied there
750 $tags = $this->getTags();
751 if ( is_null( $tags ) ) {
754 $rc->addTags( $tags );
755 $rc->save( 'pleasedontudp' );
758 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
759 $rc->notifyRCFeeds();
762 // Log the autopatrol if the log entry is patrollable
763 if ( $this->getIsPatrollable() &&
764 $rc->getAttribute( 'rc_patrolled' ) === 1
766 PatrolLog
::record( $rc, true, $this->getPerformer() );
770 DeferredUpdates
::POSTSEND
,
775 public function getType() {
779 public function getSubtype() {
780 return $this->subtype
;
783 public function getParameters() {
784 return $this->parameters
;
790 public function getPerformer() {
791 return $this->performer
;
797 public function getTarget() {
798 return $this->target
;
801 public function getTimestamp() {
802 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
804 return wfTimestamp( TS_MW
, $ts );
807 public function getComment() {
808 return $this->comment
;
815 public function getAssociatedRevId() {
823 public function getTags() {
828 * Whether this log entry is patrollable
833 public function getIsPatrollable() {
834 return $this->isPatrollable
;
841 public function isLegacy() {
842 return $this->legacy
;
845 public function getDeleted() {
846 return (int)$this->deleted
;