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
::newKey( 'log_comment' )->getJoin();
175 $tables = [ 'logging', 'user' ] +
$commentQuery['tables'];
177 'log_id', 'log_type', 'log_action', 'log_timestamp',
178 'log_user', 'log_user_text',
179 'log_namespace', 'log_title', // unused log_page
180 'log_params', 'log_deleted',
181 'user_id', 'user_name', 'user_editcount',
182 ] +
$commentQuery['fields'];
185 // IPs don't have an entry in user table
186 'user' => [ 'LEFT JOIN', 'log_user=user_id' ],
187 ] +
$commentQuery['joins'];
194 'join_conds' => $joins,
199 * Constructs new LogEntry from database result row.
200 * Supports rows from both logging and recentchanges table.
202 * @param stdClass|array $row
203 * @return DatabaseLogEntry
205 public static function newFromRow( $row ) {
207 if ( isset( $row->rc_logid
) ) {
208 return new RCDatabaseLogEntry( $row );
210 return new self( $row );
214 /** @var stdClass Database result row. */
218 protected $performer;
220 /** @var array Parameters for log entry */
223 /** @var int A rev id associated to the log entry */
224 protected $revId = null;
226 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
229 protected function __construct( $row ) {
234 * Returns the unique database id.
238 public function getId() {
239 return (int)$this->row
->log_id
;
243 * Returns whatever is stored in the database field.
247 protected function getRawParameters() {
248 return $this->row
->log_params
;
251 public function isLegacy() {
252 // This extracts the property
253 $this->getParameters();
254 return $this->legacy
;
257 public function getType() {
258 return $this->row
->log_type
;
261 public function getSubtype() {
262 return $this->row
->log_action
;
265 public function getParameters() {
266 if ( !isset( $this->params
) ) {
267 $blob = $this->getRawParameters();
268 MediaWiki\
suppressWarnings();
269 $params = LogEntryBase
::extractParams( $blob );
270 MediaWiki\restoreWarnings
();
271 if ( $params !== false ) {
272 $this->params
= $params;
273 $this->legacy
= false;
275 $this->params
= LogPage
::extractParams( $blob );
276 $this->legacy
= true;
279 if ( isset( $this->params
['associated_rev_id'] ) ) {
280 $this->revId
= $this->params
['associated_rev_id'];
281 unset( $this->params
['associated_rev_id'] );
285 return $this->params
;
288 public function getAssociatedRevId() {
289 // This extracts the property
290 $this->getParameters();
294 public function getPerformer() {
295 if ( !$this->performer
) {
296 $userId = (int)$this->row
->log_user
;
297 if ( $userId !== 0 ) {
299 if ( isset( $this->row
->user_name
) ) {
300 $this->performer
= User
::newFromRow( $this->row
);
302 $this->performer
= User
::newFromId( $userId );
306 $userText = $this->row
->log_user_text
;
307 $this->performer
= User
::newFromName( $userText, false );
311 return $this->performer
;
314 public function getTarget() {
315 $namespace = $this->row
->log_namespace
;
316 $page = $this->row
->log_title
;
317 $title = Title
::makeTitle( $namespace, $page );
322 public function getTimestamp() {
323 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
326 public function getComment() {
327 return CommentStore
::newKey( 'log_comment' )->getComment( $this->row
)->text
;
330 public function getDeleted() {
331 return $this->row
->log_deleted
;
335 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
337 public function getId() {
338 return $this->row
->rc_logid
;
341 protected function getRawParameters() {
342 return $this->row
->rc_params
;
345 public function getAssociatedRevId() {
346 return $this->row
->rc_this_oldid
;
349 public function getType() {
350 return $this->row
->rc_log_type
;
353 public function getSubtype() {
354 return $this->row
->rc_log_action
;
357 public function getPerformer() {
358 if ( !$this->performer
) {
359 $userId = (int)$this->row
->rc_user
;
360 if ( $userId !== 0 ) {
361 $this->performer
= User
::newFromId( $userId );
363 $userText = $this->row
->rc_user_text
;
364 // Might be an IP, don't validate the username
365 $this->performer
= User
::newFromName( $userText, false );
369 return $this->performer
;
372 public function getTarget() {
373 $namespace = $this->row
->rc_namespace
;
374 $page = $this->row
->rc_title
;
375 $title = Title
::makeTitle( $namespace, $page );
380 public function getTimestamp() {
381 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
384 public function getComment() {
385 return CommentStore
::newKey( 'rc_comment' )
386 // Legacy because the row may have used RecentChange::selectFields()
387 ->getCommentLegacy( wfGetDB( DB_REPLICA
), $this->row
)->text
;
390 public function getDeleted() {
391 return $this->row
->rc_deleted
;
396 * Class for creating log entries manually, to inject them into the database.
400 class ManualLogEntry
extends LogEntryBase
{
401 /** @var string Type of log entry */
404 /** @var string Sub type of log entry */
407 /** @var array Parameters for log entry */
408 protected $parameters = [];
411 protected $relations = [];
413 /** @var User Performer of the action for the log entry */
414 protected $performer;
416 /** @var Title Target title for the log entry */
419 /** @var string Timestamp of creation of the log entry */
420 protected $timestamp;
422 /** @var string Comment for the log entry */
423 protected $comment = '';
425 /** @var int A rev id associated to the log entry */
426 protected $revId = 0;
428 /** @var array Change tags add to the log entry */
429 protected $tags = null;
431 /** @var int Deletion state of the log entry */
434 /** @var int ID of the log entry */
437 /** @var bool Can this log entry be patrolled? */
438 protected $isPatrollable = false;
440 /** @var bool Whether this is a legacy log entry */
441 protected $legacy = false;
445 * @param string $type
446 * @param string $subtype
448 public function __construct( $type, $subtype ) {
450 $this->subtype
= $subtype;
454 * Set extra log parameters.
456 * You can pass params to the log action message by prefixing the keys with
457 * a number and optional type, using colons to separate the fields. The
458 * numbering should start with number 4, the first three parameters are
459 * hardcoded for every message.
461 * If you want to store stuff that should not be available in messages, don't
462 * prefix the array key with a number and don't use the colons.
465 * $entry->setParameters(
466 * '4::color' => 'blue',
467 * '5:number:count' => 3000,
472 * @param array $parameters Associative array
474 public function setParameters( $parameters ) {
475 $this->parameters
= $parameters;
479 * Declare arbitrary tag/value relations to this log entry.
480 * These can be used to filter log entries later on.
482 * @param array $relations Map of (tag => (list of values|value))
485 public function setRelations( array $relations ) {
486 $this->relations
= $relations;
490 * Set the user that performed the action being logged.
493 * @param User $performer
495 public function setPerformer( User
$performer ) {
496 $this->performer
= $performer;
500 * Set the title of the object changed.
503 * @param Title $target
505 public function setTarget( Title
$target ) {
506 $this->target
= $target;
510 * Set the timestamp of when the logged action took place.
513 * @param string $timestamp
515 public function setTimestamp( $timestamp ) {
516 $this->timestamp
= $timestamp;
520 * Set a comment associated with the action being logged.
523 * @param string $comment
525 public function setComment( $comment ) {
526 $this->comment
= $comment;
530 * Set an associated revision id.
532 * For example, the ID of the revision that was inserted to mark a page move
533 * or protection, file upload, etc.
538 public function setAssociatedRevId( $revId ) {
539 $this->revId
= $revId;
543 * Set change tags for the log entry.
546 * @param string|string[] $tags
548 public function setTags( $tags ) {
549 if ( is_string( $tags ) ) {
556 * Set whether this log entry should be made patrollable
557 * This shouldn't depend on config, only on whether there is full support
558 * in the software for patrolling this log entry.
562 * @param bool $patrollable
564 public function setIsPatrollable( $patrollable ) {
565 $this->isPatrollable
= (bool)$patrollable;
569 * Set the 'legacy' flag
572 * @param bool $legacy
574 public function setLegacy( $legacy ) {
575 $this->legacy
= $legacy;
579 * Set the 'deleted' flag.
582 * @param int $deleted One of LogPage::DELETED_* bitfield constants
584 public function setDeleted( $deleted ) {
585 $this->deleted
= $deleted;
589 * Insert the entry into the `logging` table.
591 * @param IDatabase $dbw
592 * @return int ID of the log entry
593 * @throws MWException
595 public function insert( IDatabase
$dbw = null ) {
596 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
598 if ( $this->timestamp
=== null ) {
599 $this->timestamp
= wfTimestampNow();
602 // Trim spaces on user supplied text
603 $comment = trim( $this->getComment() );
605 $params = $this->getParameters();
606 $relations = $this->relations
;
608 // Additional fields for which there's no space in the database table schema
609 $revId = $this->getAssociatedRevId();
611 $params['associated_rev_id'] = $revId;
612 $relations['associated_rev_id'] = $revId;
616 'log_type' => $this->getType(),
617 'log_action' => $this->getSubtype(),
618 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
619 'log_user' => $this->getPerformer()->getId(),
620 'log_user_text' => $this->getPerformer()->getName(),
621 'log_namespace' => $this->getTarget()->getNamespace(),
622 'log_title' => $this->getTarget()->getDBkey(),
623 'log_page' => $this->getTarget()->getArticleID(),
624 'log_params' => LogEntryBase
::makeParamBlob( $params ),
626 if ( isset( $this->deleted
) ) {
627 $data['log_deleted'] = $this->deleted
;
629 $data +
= CommentStore
::newKey( 'log_comment' )->insert( $dbw, $comment );
631 $dbw->insert( 'logging', $data, __METHOD__
);
632 $this->id
= $dbw->insertId();
635 foreach ( $relations as $tag => $values ) {
636 if ( !strlen( $tag ) ) {
637 throw new MWException( "Got empty log search tag." );
640 if ( !is_array( $values ) ) {
641 $values = [ $values ];
644 foreach ( $values as $value ) {
647 'ls_value' => $value,
648 'ls_log_id' => $this->id
652 if ( count( $rows ) ) {
653 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
660 * Get a RecentChanges object for the log entry
663 * @return RecentChange
666 public function getRecentChange( $newId = 0 ) {
667 $formatter = LogFormatter
::newFromEntry( $this );
668 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
669 $formatter->setContext( $context );
671 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
672 $user = $this->getPerformer();
674 if ( $user->isAnon() ) {
675 // "MediaWiki default" and friends may have
676 // no IP address in their name
677 if ( IP
::isIPAddress( $user->getName() ) ) {
678 $ip = $user->getName();
682 return RecentChange
::newLogEntry(
683 $this->getTimestamp(),
686 $formatter->getPlainActionText(),
692 LogEntryBase
::makeParamBlob( $this->getParameters() ),
694 $formatter->getIRCActionComment(), // Used for IRC feeds
695 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
696 $this->getIsPatrollable()
701 * Publish the log entry.
703 * @param int $newId Id of the log entry.
704 * @param string $to One of: rcandudp (default), rc, udp
706 public function publish( $newId, $to = 'rcandudp' ) {
707 DeferredUpdates
::addCallableUpdate(
708 function () use ( $newId, $to ) {
709 $log = new LogPage( $this->getType() );
710 if ( !$log->isRestricted() ) {
711 $rc = $this->getRecentChange( $newId );
713 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
714 // save RC, passing tags so they are applied there
715 $tags = $this->getTags();
716 if ( is_null( $tags ) ) {
719 $rc->addTags( $tags );
720 $rc->save( 'pleasedontudp' );
723 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
724 $rc->notifyRCFeeds();
727 // Log the autopatrol if the log entry is patrollable
728 if ( $this->getIsPatrollable() &&
729 $rc->getAttribute( 'rc_patrolled' ) === 1
731 PatrolLog
::record( $rc, true, $this->getPerformer() );
735 DeferredUpdates
::POSTSEND
,
740 public function getType() {
744 public function getSubtype() {
745 return $this->subtype
;
748 public function getParameters() {
749 return $this->parameters
;
755 public function getPerformer() {
756 return $this->performer
;
762 public function getTarget() {
763 return $this->target
;
766 public function getTimestamp() {
767 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
769 return wfTimestamp( TS_MW
, $ts );
772 public function getComment() {
773 return $this->comment
;
780 public function getAssociatedRevId() {
788 public function getTags() {
793 * Whether this log entry is patrollable
798 public function getIsPatrollable() {
799 return $this->isPatrollable
;
806 public function isLegacy() {
807 return $this->legacy
;
810 public function getDeleted() {
811 return (int)$this->deleted
;