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 );
217 * Loads a LogEntry with the given id from database
220 * @param IDatabase $db
221 * @return DatabaseLogEntry|null
223 public static function newFromId( $id, IDatabase
$db ) {
224 $queryInfo = self
::getSelectQueryData();
225 $queryInfo['conds'] +
= [ 'log_id' => $id ];
226 $row = $db->selectRow(
227 $queryInfo['tables'],
228 $queryInfo['fields'],
231 $queryInfo['options'],
232 $queryInfo['join_conds']
237 return self
::newFromRow( $row );
240 /** @var stdClass Database result row. */
244 protected $performer;
246 /** @var array Parameters for log entry */
249 /** @var int A rev id associated to the log entry */
250 protected $revId = null;
252 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
255 protected function __construct( $row ) {
260 * Returns the unique database id.
264 public function getId() {
265 return (int)$this->row
->log_id
;
269 * Returns whatever is stored in the database field.
273 protected function getRawParameters() {
274 return $this->row
->log_params
;
277 public function isLegacy() {
278 // This extracts the property
279 $this->getParameters();
280 return $this->legacy
;
283 public function getType() {
284 return $this->row
->log_type
;
287 public function getSubtype() {
288 return $this->row
->log_action
;
291 public function getParameters() {
292 if ( !isset( $this->params
) ) {
293 $blob = $this->getRawParameters();
294 Wikimedia\
suppressWarnings();
295 $params = LogEntryBase
::extractParams( $blob );
296 Wikimedia\restoreWarnings
();
297 if ( $params !== false ) {
298 $this->params
= $params;
299 $this->legacy
= false;
301 $this->params
= LogPage
::extractParams( $blob );
302 $this->legacy
= true;
305 if ( isset( $this->params
['associated_rev_id'] ) ) {
306 $this->revId
= $this->params
['associated_rev_id'];
307 unset( $this->params
['associated_rev_id'] );
311 return $this->params
;
314 public function getAssociatedRevId() {
315 // This extracts the property
316 $this->getParameters();
320 public function getPerformer() {
321 if ( !$this->performer
) {
322 $actorId = isset( $this->row
->log_actor
) ?
(int)$this->row
->log_actor
: 0;
323 $userId = (int)$this->row
->log_user
;
324 if ( $userId !== 0 ||
$actorId !== 0 ) {
326 if ( isset( $this->row
->user_name
) ) {
327 $this->performer
= User
::newFromRow( $this->row
);
328 } elseif ( $actorId !== 0 ) {
329 $this->performer
= User
::newFromActorId( $actorId );
331 $this->performer
= User
::newFromId( $userId );
335 $userText = $this->row
->log_user_text
;
336 $this->performer
= User
::newFromName( $userText, false );
340 return $this->performer
;
343 public function getTarget() {
344 $namespace = $this->row
->log_namespace
;
345 $page = $this->row
->log_title
;
346 $title = Title
::makeTitle( $namespace, $page );
351 public function getTimestamp() {
352 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
355 public function getComment() {
356 return CommentStore
::getStore()->getComment( 'log_comment', $this->row
)->text
;
359 public function getDeleted() {
360 return $this->row
->log_deleted
;
364 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
366 public function getId() {
367 return $this->row
->rc_logid
;
370 protected function getRawParameters() {
371 return $this->row
->rc_params
;
374 public function getAssociatedRevId() {
375 return $this->row
->rc_this_oldid
;
378 public function getType() {
379 return $this->row
->rc_log_type
;
382 public function getSubtype() {
383 return $this->row
->rc_log_action
;
386 public function getPerformer() {
387 if ( !$this->performer
) {
388 $actorId = isset( $this->row
->rc_actor
) ?
(int)$this->row
->rc_actor
: 0;
389 $userId = (int)$this->row
->rc_user
;
390 if ( $actorId !== 0 ) {
391 $this->performer
= User
::newFromActorId( $actorId );
392 } elseif ( $userId !== 0 ) {
393 $this->performer
= User
::newFromId( $userId );
395 $userText = $this->row
->rc_user_text
;
396 // Might be an IP, don't validate the username
397 $this->performer
= User
::newFromName( $userText, false );
401 return $this->performer
;
404 public function getTarget() {
405 $namespace = $this->row
->rc_namespace
;
406 $page = $this->row
->rc_title
;
407 $title = Title
::makeTitle( $namespace, $page );
412 public function getTimestamp() {
413 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
416 public function getComment() {
417 return CommentStore
::getStore()
418 // Legacy because the row may have used RecentChange::selectFields()
419 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $this->row
)->text
;
422 public function getDeleted() {
423 return $this->row
->rc_deleted
;
428 * Class for creating log entries manually, to inject them into the database.
432 class ManualLogEntry
extends LogEntryBase
{
433 /** @var string Type of log entry */
436 /** @var string Sub type of log entry */
439 /** @var array Parameters for log entry */
440 protected $parameters = [];
443 protected $relations = [];
445 /** @var User Performer of the action for the log entry */
446 protected $performer;
448 /** @var Title Target title for the log entry */
451 /** @var string Timestamp of creation of the log entry */
452 protected $timestamp;
454 /** @var string Comment for the log entry */
455 protected $comment = '';
457 /** @var int A rev id associated to the log entry */
458 protected $revId = 0;
460 /** @var array Change tags add to the log entry */
461 protected $tags = null;
463 /** @var int Deletion state of the log entry */
466 /** @var int ID of the log entry */
469 /** @var bool Can this log entry be patrolled? */
470 protected $isPatrollable = false;
472 /** @var bool Whether this is a legacy log entry */
473 protected $legacy = false;
477 * @param string $type
478 * @param string $subtype
480 public function __construct( $type, $subtype ) {
482 $this->subtype
= $subtype;
486 * Set extra log parameters.
488 * You can pass params to the log action message by prefixing the keys with
489 * a number and optional type, using colons to separate the fields. The
490 * numbering should start with number 4, the first three parameters are
491 * hardcoded for every message.
493 * If you want to store stuff that should not be available in messages, don't
494 * prefix the array key with a number and don't use the colons.
497 * $entry->setParameters(
498 * '4::color' => 'blue',
499 * '5:number:count' => 3000,
504 * @param array $parameters Associative array
506 public function setParameters( $parameters ) {
507 $this->parameters
= $parameters;
511 * Declare arbitrary tag/value relations to this log entry.
512 * These can be used to filter log entries later on.
514 * @param array $relations Map of (tag => (list of values|value))
517 public function setRelations( array $relations ) {
518 $this->relations
= $relations;
522 * Set the user that performed the action being logged.
525 * @param User $performer
527 public function setPerformer( User
$performer ) {
528 $this->performer
= $performer;
532 * Set the title of the object changed.
535 * @param Title $target
537 public function setTarget( Title
$target ) {
538 $this->target
= $target;
542 * Set the timestamp of when the logged action took place.
545 * @param string $timestamp
547 public function setTimestamp( $timestamp ) {
548 $this->timestamp
= $timestamp;
552 * Set a comment associated with the action being logged.
555 * @param string $comment
557 public function setComment( $comment ) {
558 $this->comment
= $comment;
562 * Set an associated revision id.
564 * For example, the ID of the revision that was inserted to mark a page move
565 * or protection, file upload, etc.
570 public function setAssociatedRevId( $revId ) {
571 $this->revId
= $revId;
575 * Set change tags for the log entry.
578 * @param string|string[] $tags
580 public function setTags( $tags ) {
581 if ( is_string( $tags ) ) {
588 * Set whether this log entry should be made patrollable
589 * This shouldn't depend on config, only on whether there is full support
590 * in the software for patrolling this log entry.
594 * @param bool $patrollable
596 public function setIsPatrollable( $patrollable ) {
597 $this->isPatrollable
= (bool)$patrollable;
601 * Set the 'legacy' flag
604 * @param bool $legacy
606 public function setLegacy( $legacy ) {
607 $this->legacy
= $legacy;
611 * Set the 'deleted' flag.
614 * @param int $deleted One of LogPage::DELETED_* bitfield constants
616 public function setDeleted( $deleted ) {
617 $this->deleted
= $deleted;
621 * Insert the entry into the `logging` table.
623 * @param IDatabase $dbw
624 * @return int ID of the log entry
625 * @throws MWException
627 public function insert( IDatabase
$dbw = null ) {
628 global $wgActorTableSchemaMigrationStage;
630 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
632 if ( $this->timestamp
=== null ) {
633 $this->timestamp
= wfTimestampNow();
636 // Trim spaces on user supplied text
637 $comment = trim( $this->getComment() );
639 $params = $this->getParameters();
640 $relations = $this->relations
;
642 // Ensure actor relations are set
643 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
&&
644 empty( $relations['target_author_actor'] )
647 if ( !empty( $relations['target_author_id'] ) ) {
648 foreach ( $relations['target_author_id'] as $id ) {
649 $actorIds[] = User
::newFromId( $id )->getActorId( $dbw );
652 if ( !empty( $relations['target_author_ip'] ) ) {
653 foreach ( $relations['target_author_ip'] as $ip ) {
654 $actorIds[] = User
::newFromName( $ip, false )->getActorId( $dbw );
658 $relations['target_author_actor'] = $actorIds;
659 $params['authorActors'] = $actorIds;
662 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_NEW
) {
663 unset( $relations['target_author_id'], $relations['target_author_ip'] );
664 unset( $params['authorIds'], $params['authorIPs'] );
667 // Additional fields for which there's no space in the database table schema
668 $revId = $this->getAssociatedRevId();
670 $params['associated_rev_id'] = $revId;
671 $relations['associated_rev_id'] = $revId;
675 'log_type' => $this->getType(),
676 'log_action' => $this->getSubtype(),
677 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
678 'log_namespace' => $this->getTarget()->getNamespace(),
679 'log_title' => $this->getTarget()->getDBkey(),
680 'log_page' => $this->getTarget()->getArticleID(),
681 'log_params' => LogEntryBase
::makeParamBlob( $params ),
683 if ( isset( $this->deleted
) ) {
684 $data['log_deleted'] = $this->deleted
;
686 $data +
= CommentStore
::getStore()->insert( $dbw, 'log_comment', $comment );
687 $data +
= ActorMigration
::newMigration()
688 ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
690 $dbw->insert( 'logging', $data, __METHOD__
);
691 $this->id
= $dbw->insertId();
694 foreach ( $relations as $tag => $values ) {
695 if ( !strlen( $tag ) ) {
696 throw new MWException( "Got empty log search tag." );
699 if ( !is_array( $values ) ) {
700 $values = [ $values ];
703 foreach ( $values as $value ) {
706 'ls_value' => $value,
707 'ls_log_id' => $this->id
711 if ( count( $rows ) ) {
712 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
719 * Get a RecentChanges object for the log entry
722 * @return RecentChange
725 public function getRecentChange( $newId = 0 ) {
726 $formatter = LogFormatter
::newFromEntry( $this );
727 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
728 $formatter->setContext( $context );
730 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
731 $user = $this->getPerformer();
733 if ( $user->isAnon() ) {
734 // "MediaWiki default" and friends may have
735 // no IP address in their name
736 if ( IP
::isIPAddress( $user->getName() ) ) {
737 $ip = $user->getName();
741 return RecentChange
::newLogEntry(
742 $this->getTimestamp(),
745 $formatter->getPlainActionText(),
751 LogEntryBase
::makeParamBlob( $this->getParameters() ),
753 $formatter->getIRCActionComment(), // Used for IRC feeds
754 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
755 $this->getIsPatrollable()
760 * Publish the log entry.
762 * @param int $newId Id of the log entry.
763 * @param string $to One of: rcandudp (default), rc, udp
765 public function publish( $newId, $to = 'rcandudp' ) {
766 DeferredUpdates
::addCallableUpdate(
767 function () use ( $newId, $to ) {
768 $log = new LogPage( $this->getType() );
769 if ( !$log->isRestricted() ) {
770 $rc = $this->getRecentChange( $newId );
772 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
773 // save RC, passing tags so they are applied there
774 $tags = $this->getTags();
775 if ( is_null( $tags ) ) {
778 $rc->addTags( $tags );
779 $rc->save( 'pleasedontudp' );
782 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
783 $rc->notifyRCFeeds();
786 // Log the autopatrol if the log entry is patrollable
787 if ( $this->getIsPatrollable() &&
788 $rc->getAttribute( 'rc_patrolled' ) === 2
790 PatrolLog
::record( $rc, true, $this->getPerformer() );
794 DeferredUpdates
::POSTSEND
,
799 public function getType() {
803 public function getSubtype() {
804 return $this->subtype
;
807 public function getParameters() {
808 return $this->parameters
;
814 public function getPerformer() {
815 return $this->performer
;
821 public function getTarget() {
822 return $this->target
;
825 public function getTimestamp() {
826 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
828 return wfTimestamp( TS_MW
, $ts );
831 public function getComment() {
832 return $this->comment
;
839 public function getAssociatedRevId() {
847 public function getTags() {
852 * Whether this log entry is patrollable
857 public function getIsPatrollable() {
858 return $this->isPatrollable
;
865 public function isLegacy() {
866 return $this->legacy
;
869 public function getDeleted() {
870 return (int)$this->deleted
;