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 Deletion state of the log entry */
392 /** @var int ID of the log entry */
395 /** @var bool Whether this is a legacy log entry */
396 protected $legacy = false;
403 * @param string $type
404 * @param string $subtype
406 public function __construct( $type, $subtype ) {
408 $this->subtype
= $subtype;
412 * Set extra log parameters.
414 * You can pass params to the log action message by prefixing the keys with
415 * a number and optional type, using colons to separate the fields. The
416 * numbering should start with number 4, the first three parameters are
417 * hardcoded for every message. Example:
418 * $entry->setParameters(
419 * '4::color' => 'blue',
420 * '5:number:count' => 3000,
426 * @param array $parameters Associative array
428 public function setParameters( $parameters ) {
429 $this->parameters
= $parameters;
433 * Declare arbitrary tag/value relations to this log entry.
434 * These can be used to filter log entries later on.
436 * @param array $relations Map of (tag => (list of values|value))
439 public function setRelations( array $relations ) {
440 $this->relations
= $relations;
444 * Set the user that performed the action being logged.
448 * @param User $performer
450 public function setPerformer( User
$performer ) {
451 $this->performer
= $performer;
455 * Set the title of the object changed.
459 * @param Title $target
461 public function setTarget( Title
$target ) {
462 $this->target
= $target;
466 * Set the timestamp of when the logged action took place.
470 * @param string $timestamp
472 public function setTimestamp( $timestamp ) {
473 $this->timestamp
= $timestamp;
477 * Set a comment associated with the action being logged.
481 * @param string $comment
483 public function setComment( $comment ) {
484 $this->comment
= $comment;
488 * Set the 'legacy' flag
491 * @param bool $legacy
493 public function setLegacy( $legacy ) {
494 $this->legacy
= $legacy;
502 * @param int $deleted
504 public function setDeleted( $deleted ) {
505 $this->deleted
= $deleted;
509 * Inserts the entry into the logging table.
510 * @param IDatabase $dbw
511 * @return int ID of the log entry
512 * @throws MWException
514 public function insert( IDatabase
$dbw = null ) {
517 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
518 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
520 if ( $this->timestamp
=== null ) {
521 $this->timestamp
= wfTimestampNow();
524 # Trim spaces on user supplied text
525 $comment = trim( $this->getComment() );
527 # Truncate for whole multibyte characters.
528 $comment = $wgContLang->truncate( $comment, 255 );
532 'log_type' => $this->getType(),
533 'log_action' => $this->getSubtype(),
534 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
535 'log_user' => $this->getPerformer()->getId(),
536 'log_user_text' => $this->getPerformer()->getName(),
537 'log_namespace' => $this->getTarget()->getNamespace(),
538 'log_title' => $this->getTarget()->getDBkey(),
539 'log_page' => $this->getTarget()->getArticleID(),
540 'log_comment' => $comment,
541 'log_params' => LogEntryBase
::makeParamBlob( $this->getParameters() ),
543 if ( isset( $this->deleted
) ) {
544 $data['log_deleted'] = $this->deleted
;
547 $dbw->insert( 'logging', $data, __METHOD__
);
548 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
551 foreach ( $this->relations
as $tag => $values ) {
552 if ( !strlen( $tag ) ) {
553 throw new MWException( "Got empty log search tag." );
556 if ( !is_array( $values ) ) {
557 $values = array( $values );
560 foreach ( $values as $value ) {
563 'ls_value' => $value,
564 'ls_log_id' => $this->id
568 if ( count( $rows ) ) {
569 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
576 * Get a RecentChanges object for the log entry
578 * @return RecentChange
581 public function getRecentChange( $newId = 0 ) {
582 $formatter = LogFormatter
::newFromEntry( $this );
583 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
584 $formatter->setContext( $context );
586 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
587 $user = $this->getPerformer();
589 if ( $user->isAnon() ) {
591 * "MediaWiki default" and friends may have
592 * no IP address in their name
594 if ( IP
::isIPAddress( $user->getName() ) ) {
595 $ip = $user->getName();
599 return RecentChange
::newLogEntry(
600 $this->getTimestamp(),
603 $formatter->getPlainActionText(),
609 LogEntryBase
::makeParamBlob( $this->getParameters() ),
611 $formatter->getIRCActionComment() // Used for IRC feeds
616 * Publishes the log entry.
617 * @param int $newId Id of the log entry.
618 * @param string $to One of: rcandudp (default), rc, udp
620 public function publish( $newId, $to = 'rcandudp' ) {
621 $log = new LogPage( $this->getType() );
622 if ( $log->isRestricted() ) {
626 $rc = $this->getRecentChange( $newId );
628 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
629 $rc->save( 'pleasedontudp' );
632 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
633 $rc->notifyRCFeeds();
639 public function getType() {
643 public function getSubtype() {
644 return $this->subtype
;
647 public function getParameters() {
648 return $this->parameters
;
654 public function getPerformer() {
655 return $this->performer
;
661 public function getTarget() {
662 return $this->target
;
665 public function getTimestamp() {
666 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
668 return wfTimestamp( TS_MW
, $ts );
671 public function getComment() {
672 return $this->comment
;
679 public function isLegacy() {
680 return $this->legacy
;
683 public function getDeleted() {
684 return (int)$this->deleted
;