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
32 * Interface for log entries. Every log entry has these methods.
40 public function getType();
46 public function getSubtype();
49 * The full logtype in format maintype/subtype.
52 public function getFullType();
55 * Get the extra parameters stored for this message.
58 public function getParameters();
61 * Get the user for performed this action.
64 public function getPerformer();
67 * Get the target page of this action.
70 public function getTarget();
73 * Get the timestamp when the action was executed.
76 public function getTimestamp();
79 * Get the user provided comment.
82 public function getComment();
85 * Get the access restriction.
88 public function getDeleted();
91 * @param int $field One of LogPage::DELETED_* bitfield constants
94 public function isDeleted( $field );
98 * Extends the LogEntryInterface with some basic functionality
101 abstract class LogEntryBase
implements LogEntry
{
102 public function getFullType() {
103 return $this->getType() . '/' . $this->getSubtype();
106 public function isDeleted( $field ) {
107 return ( $this->getDeleted() & $field ) === $field;
111 * Whether the parameters for this log are stored in new or
115 public function isLegacy() {
120 * Create a blob from a parameter array
122 * @param array $params
126 public static function makeParamBlob( $params ) {
127 return serialize( (array)$params );
131 * Extract a parameter array from a blob
133 * @param string $blob
137 public static function extractParams( $blob ) {
138 return unserialize( $blob );
143 * This class wraps around database result row.
146 class DatabaseLogEntry
extends LogEntryBase
{
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
155 public static function getSelectQueryData() {
156 $tables = array( 'logging', 'user' );
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',
166 // IP's don't have an entry in user table
167 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
174 'options' => array(),
175 'join_conds' => $joins,
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
185 public static function newFromRow( $row ) {
187 if ( isset( $row->rc_logid
) ) {
188 return new RCDatabaseLogEntry( $row );
190 return new self( $row );
196 /** @var stdClass Database result row. */
200 protected $performer;
202 /** @var bool Whether the parameters for this log entry are stored in new
207 protected function __construct( $row ) {
212 * Returns the unique database id.
215 public function getId() {
216 return (int)$this->row
->log_id
;
220 * Returns whatever is stored in the database field.
223 protected function getRawParameters() {
224 return $this->row
->log_params
;
229 public function isLegacy() {
230 // This does the check
231 $this->getParameters();
233 return $this->legacy
;
238 public function getType() {
239 return $this->row
->log_type
;
242 public function getSubtype() {
243 return $this->row
->log_action
;
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;
256 $this->params
= LogPage
::extractParams( $blob );
257 $this->legacy
= true;
261 return $this->params
;
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
);
271 $this->performer
= User
::newFromId( $userId );
274 $userText = $this->row
->log_user_text
;
275 $this->performer
= User
::newFromName( $userText, false );
279 return $this->performer
;
282 public function getTarget() {
283 $namespace = $this->row
->log_namespace
;
284 $page = $this->row
->log_title
;
285 $title = Title
::makeTitle( $namespace, $page );
290 public function getTimestamp() {
291 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
294 public function getComment() {
295 return $this->row
->log_comment
;
298 public function getDeleted() {
299 return $this->row
->log_deleted
;
303 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
305 public function getId() {
306 return $this->row
->rc_logid
;
309 protected function getRawParameters() {
310 return $this->row
->rc_params
;
315 public function getType() {
316 return $this->row
->rc_log_type
;
319 public function getSubtype() {
320 return $this->row
->rc_log_action
;
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 );
329 $userText = $this->row
->rc_user_text
;
330 // Might be an IP, don't validate the username
331 $this->performer
= User
::newFromName( $userText, false );
335 return $this->performer
;
338 public function getTarget() {
339 $namespace = $this->row
->rc_namespace
;
340 $page = $this->row
->rc_title
;
341 $title = Title
::makeTitle( $namespace, $page );
346 public function getTimestamp() {
347 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
350 public function getComment() {
351 return $this->row
->rc_comment
;
354 public function getDeleted() {
355 return $this->row
->rc_deleted
;
360 * Class for creating log entries manually, for
361 * example to inject them into the database.
364 class ManualLogEntry
extends LogEntryBase
{
365 /** @var string Type of log entry */
368 /** @var string Sub type of log entry */
371 /** @var array Parameters for log entry */
372 protected $parameters = array();
375 protected $relations = array();
377 /** @var User Performer of the action for the log entry */
378 protected $performer;
380 /** @var Title Target title for the log entry */
383 /** @var string Timestamp of creation of the log entry */
384 protected $timestamp;
386 /** @var string Comment for the log entry */
387 protected $comment = '';
389 /** @var int A rev id associated to the log entry */
390 protected $revId = 0;
392 /** @var int Deletion state of the log entry */
395 /** @var int ID of the log entry */
398 /** @var bool Whether this is a legacy log entry */
399 protected $legacy = false;
406 * @param string $type
407 * @param string $subtype
409 public function __construct( $type, $subtype ) {
411 $this->subtype
= $subtype;
415 * Set extra log parameters.
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,
429 * @param array $parameters Associative array
431 public function setParameters( $parameters ) {
432 $this->parameters
= $parameters;
436 * Declare arbitrary tag/value relations to this log entry.
437 * These can be used to filter log entries later on.
439 * @param array $relations Map of (tag => (list of values|value))
442 public function setRelations( array $relations ) {
443 $this->relations
= $relations;
447 * Set the user that performed the action being logged.
451 * @param User $performer
453 public function setPerformer( User
$performer ) {
454 $this->performer
= $performer;
458 * Set the title of the object changed.
462 * @param Title $target
464 public function setTarget( Title
$target ) {
465 $this->target
= $target;
469 * Set the timestamp of when the logged action took place.
473 * @param string $timestamp
475 public function setTimestamp( $timestamp ) {
476 $this->timestamp
= $timestamp;
480 * Set a comment associated with the action being logged.
484 * @param string $comment
486 public function setComment( $comment ) {
487 $this->comment
= $comment;
491 * Set an associated revision id.
497 public function setAssociatedRevId( $revId ) {
498 $this->revId
= $revId;
502 * Set the 'legacy' flag
505 * @param bool $legacy
507 public function setLegacy( $legacy ) {
508 $this->legacy
= $legacy;
516 * @param int $deleted
518 public function setDeleted( $deleted ) {
519 $this->deleted
= $deleted;
523 * Inserts the entry into the logging table.
524 * @param IDatabase $dbw
525 * @return int ID of the log entry
526 * @throws MWException
528 public function insert( IDatabase
$dbw = null ) {
531 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
532 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
534 if ( $this->timestamp
=== null ) {
535 $this->timestamp
= wfTimestampNow();
538 # Trim spaces on user supplied text
539 $comment = trim( $this->getComment() );
541 # Truncate for whole multibyte characters.
542 $comment = $wgContLang->truncate( $comment, 255 );
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() ),
557 if ( isset( $this->deleted
) ) {
558 $data['log_deleted'] = $this->deleted
;
561 $dbw->insert( 'logging', $data, __METHOD__
);
562 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
565 foreach ( $this->relations
as $tag => $values ) {
566 if ( !strlen( $tag ) ) {
567 throw new MWException( "Got empty log search tag." );
570 if ( !is_array( $values ) ) {
571 $values = array( $values );
574 foreach ( $values as $value ) {
577 'ls_value' => $value,
578 'ls_log_id' => $this->id
582 if ( count( $rows ) ) {
583 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
590 * Get a RecentChanges object for the log entry
592 * @return RecentChange
595 public function getRecentChange( $newId = 0 ) {
596 $formatter = LogFormatter
::newFromEntry( $this );
597 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
598 $formatter->setContext( $context );
600 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
601 $user = $this->getPerformer();
603 if ( $user->isAnon() ) {
605 * "MediaWiki default" and friends may have
606 * no IP address in their name
608 if ( IP
::isIPAddress( $user->getName() ) ) {
609 $ip = $user->getName();
613 return RecentChange
::newLogEntry(
614 $this->getTimestamp(),
617 $formatter->getPlainActionText(),
623 LogEntryBase
::makeParamBlob( $this->getParameters() ),
625 $formatter->getIRCActionComment(), // Used for IRC feeds
626 $this->getAssociatedRevId() // Used for e.g. moves and uploads
631 * Publishes the log entry.
632 * @param int $newId Id of the log entry.
633 * @param string $to One of: rcandudp (default), rc, udp
635 public function publish( $newId, $to = 'rcandudp' ) {
636 $log = new LogPage( $this->getType() );
637 if ( $log->isRestricted() ) {
641 $rc = $this->getRecentChange( $newId );
643 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
644 $rc->save( 'pleasedontudp' );
647 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
648 $rc->notifyRCFeeds();
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() );
660 public function getType() {
664 public function getSubtype() {
665 return $this->subtype
;
668 public function getParameters() {
669 return $this->parameters
;
675 public function getPerformer() {
676 return $this->performer
;
682 public function getTarget() {
683 return $this->target
;
686 public function getTimestamp() {
687 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
689 return wfTimestamp( TS_MW
, $ts );
692 public function getComment() {
693 return $this->comment
;
700 public function getAssociatedRevId() {
708 public function isLegacy() {
709 return $this->legacy
;
712 public function getDeleted() {
713 return (int)$this->deleted
;