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.
43 public function getType();
50 public function getSubtype();
53 * The full logtype in format maintype/subtype.
57 public function getFullType();
60 * Get the extra parameters stored for this message.
64 public function getParameters();
67 * Get the user for performed this action.
71 public function getPerformer();
74 * Get the target page of this action.
78 public function getTarget();
81 * Get the timestamp when the action was executed.
85 public function getTimestamp();
88 * Get the user provided comment.
92 public function getComment();
95 * Get the access restriction.
99 public function getDeleted();
102 * @param int $field One of LogPage::DELETED_* bitfield constants
105 public function isDeleted( $field );
109 * Extends the LogEntryInterface with some basic functionality
113 abstract class LogEntryBase
implements LogEntry
{
115 public function getFullType() {
116 return $this->getType() . '/' . $this->getSubtype();
119 public function isDeleted( $field ) {
120 return ( $this->getDeleted() & $field ) === $field;
124 * Whether the parameters for this log are stored in new or
129 public function isLegacy() {
134 * Create a blob from a parameter array
137 * @param array $params
140 public static function makeParamBlob( $params ) {
141 return serialize( (array)$params );
145 * Extract a parameter array from a blob
148 * @param string $blob
151 public static function extractParams( $blob ) {
152 return unserialize( $blob );
157 * This class wraps around database result row.
161 class DatabaseLogEntry
extends LogEntryBase
{
164 * Returns array of information that is needed for querying
165 * log entries. Array contains the following keys:
166 * tables, fields, conds, options and join_conds
170 public static function getSelectQueryData() {
171 $tables = array( 'logging', 'user' );
173 'log_id', 'log_type', 'log_action', 'log_timestamp',
174 'log_user', 'log_user_text',
175 'log_namespace', 'log_title', // unused log_page
176 'log_comment', 'log_params', 'log_deleted',
177 'user_id', 'user_name', 'user_editcount',
181 // IPs don't have an entry in user table
182 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
189 'options' => array(),
190 'join_conds' => $joins,
195 * Constructs new LogEntry from database result row.
196 * Supports rows from both logging and recentchanges table.
198 * @param stdClass|array $row
199 * @return DatabaseLogEntry
201 public static function newFromRow( $row ) {
203 if ( isset( $row->rc_logid
) ) {
204 return new RCDatabaseLogEntry( $row );
206 return new self( $row );
210 /** @var stdClass Database result row. */
214 protected $performer;
216 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
219 protected function __construct( $row ) {
224 * Returns the unique database id.
228 public function getId() {
229 return (int)$this->row
->log_id
;
233 * Returns whatever is stored in the database field.
237 protected function getRawParameters() {
238 return $this->row
->log_params
;
241 public function isLegacy() {
242 // This does the check
243 $this->getParameters();
244 return $this->legacy
;
247 public function getType() {
248 return $this->row
->log_type
;
251 public function getSubtype() {
252 return $this->row
->log_action
;
255 public function getParameters() {
256 if ( !isset( $this->params
) ) {
257 $blob = $this->getRawParameters();
258 MediaWiki\
suppressWarnings();
259 $params = LogEntryBase
::extractParams( $blob );
260 MediaWiki\restoreWarnings
();
261 if ( $params !== false ) {
262 $this->params
= $params;
263 $this->legacy
= false;
265 $this->params
= LogPage
::extractParams( $blob );
266 $this->legacy
= true;
270 return $this->params
;
273 public function getPerformer() {
274 if ( !$this->performer
) {
275 $userId = (int)$this->row
->log_user
;
276 if ( $userId !== 0 ) {
278 if ( isset( $this->row
->user_name
) ) {
279 $this->performer
= User
::newFromRow( $this->row
);
281 $this->performer
= User
::newFromId( $userId );
285 $userText = $this->row
->log_user_text
;
286 $this->performer
= User
::newFromName( $userText, false );
290 return $this->performer
;
293 public function getTarget() {
294 $namespace = $this->row
->log_namespace
;
295 $page = $this->row
->log_title
;
296 $title = Title
::makeTitle( $namespace, $page );
301 public function getTimestamp() {
302 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
305 public function getComment() {
306 return $this->row
->log_comment
;
309 public function getDeleted() {
310 return $this->row
->log_deleted
;
314 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
316 public function getId() {
317 return $this->row
->rc_logid
;
320 protected function getRawParameters() {
321 return $this->row
->rc_params
;
324 public function getType() {
325 return $this->row
->rc_log_type
;
328 public function getSubtype() {
329 return $this->row
->rc_log_action
;
332 public function getPerformer() {
333 if ( !$this->performer
) {
334 $userId = (int)$this->row
->rc_user
;
335 if ( $userId !== 0 ) {
336 $this->performer
= User
::newFromId( $userId );
338 $userText = $this->row
->rc_user_text
;
339 // Might be an IP, don't validate the username
340 $this->performer
= User
::newFromName( $userText, false );
344 return $this->performer
;
347 public function getTarget() {
348 $namespace = $this->row
->rc_namespace
;
349 $page = $this->row
->rc_title
;
350 $title = Title
::makeTitle( $namespace, $page );
355 public function getTimestamp() {
356 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
359 public function getComment() {
360 return $this->row
->rc_comment
;
363 public function getDeleted() {
364 return $this->row
->rc_deleted
;
369 * Class for creating log entries manually, to inject them into the database.
373 class ManualLogEntry
extends LogEntryBase
{
374 /** @var string Type of log entry */
377 /** @var string Sub type of log entry */
380 /** @var array Parameters for log entry */
381 protected $parameters = array();
384 protected $relations = array();
386 /** @var User Performer of the action for the log entry */
387 protected $performer;
389 /** @var Title Target title for the log entry */
392 /** @var string Timestamp of creation of the log entry */
393 protected $timestamp;
395 /** @var string Comment for the log entry */
396 protected $comment = '';
398 /** @var int A rev id associated to the log entry */
399 protected $revId = 0;
401 /** @var int Deletion state of the log entry */
404 /** @var int ID of the log entry */
407 /** @var bool Whether this is a legacy log entry */
408 protected $legacy = false;
414 * @param string $type
415 * @param string $subtype
417 public function __construct( $type, $subtype ) {
419 $this->subtype
= $subtype;
423 * Set extra log parameters.
425 * You can pass params to the log action message by prefixing the keys with
426 * a number and optional type, using colons to separate the fields. The
427 * numbering should start with number 4, the first three parameters are
428 * hardcoded for every message.
430 * If you want to store stuff that should not be available in messages, don't
431 * prefix the array key with a number and don't use the colons.
434 * $entry->setParameters(
435 * '4::color' => 'blue',
436 * '5:number:count' => 3000,
441 * @param array $parameters Associative array
443 public function setParameters( $parameters ) {
444 $this->parameters
= $parameters;
448 * Declare arbitrary tag/value relations to this log entry.
449 * These can be used to filter log entries later on.
451 * @param array $relations Map of (tag => (list of values|value))
454 public function setRelations( array $relations ) {
455 $this->relations
= $relations;
459 * Set the user that performed the action being logged.
462 * @param User $performer
464 public function setPerformer( User
$performer ) {
465 $this->performer
= $performer;
469 * Set the title of the object changed.
472 * @param Title $target
474 public function setTarget( Title
$target ) {
475 $this->target
= $target;
479 * Set the timestamp of when the logged action took place.
482 * @param string $timestamp
484 public function setTimestamp( $timestamp ) {
485 $this->timestamp
= $timestamp;
489 * Set a comment associated with the action being logged.
492 * @param string $comment
494 public function setComment( $comment ) {
495 $this->comment
= $comment;
499 * Set an associated revision id.
501 * For example, the ID of the revision that was inserted to mark a page move
502 * or protection, file upload, etc.
507 public function setAssociatedRevId( $revId ) {
508 $this->revId
= $revId;
512 * Set the 'legacy' flag
515 * @param bool $legacy
517 public function setLegacy( $legacy ) {
518 $this->legacy
= $legacy;
522 * Set the 'deleted' flag.
525 * @param int $deleted One of LogPage::DELETED_* bitfield constants
527 public function setDeleted( $deleted ) {
528 $this->deleted
= $deleted;
532 * Insert the entry into the `logging` table.
534 * @param IDatabase $dbw
535 * @return int ID of the log entry
536 * @throws MWException
538 public function insert( IDatabase
$dbw = null ) {
541 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
542 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
544 if ( $this->timestamp
=== null ) {
545 $this->timestamp
= wfTimestampNow();
548 // Trim spaces on user supplied text
549 $comment = trim( $this->getComment() );
551 // Truncate for whole multibyte characters.
552 $comment = $wgContLang->truncate( $comment, 255 );
556 'log_type' => $this->getType(),
557 'log_action' => $this->getSubtype(),
558 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
559 'log_user' => $this->getPerformer()->getId(),
560 'log_user_text' => $this->getPerformer()->getName(),
561 'log_namespace' => $this->getTarget()->getNamespace(),
562 'log_title' => $this->getTarget()->getDBkey(),
563 'log_page' => $this->getTarget()->getArticleID(),
564 'log_comment' => $comment,
565 'log_params' => LogEntryBase
::makeParamBlob( $this->getParameters() ),
567 if ( isset( $this->deleted
) ) {
568 $data['log_deleted'] = $this->deleted
;
571 $dbw->insert( 'logging', $data, __METHOD__
);
572 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
575 foreach ( $this->relations
as $tag => $values ) {
576 if ( !strlen( $tag ) ) {
577 throw new MWException( "Got empty log search tag." );
580 if ( !is_array( $values ) ) {
581 $values = array( $values );
584 foreach ( $values as $value ) {
587 'ls_value' => $value,
588 'ls_log_id' => $this->id
592 if ( count( $rows ) ) {
593 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
600 * Get a RecentChanges object for the log entry
603 * @return RecentChange
606 public function getRecentChange( $newId = 0 ) {
607 $formatter = LogFormatter
::newFromEntry( $this );
608 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
609 $formatter->setContext( $context );
611 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
612 $user = $this->getPerformer();
614 if ( $user->isAnon() ) {
615 // "MediaWiki default" and friends may have
616 // no IP address in their name
617 if ( IP
::isIPAddress( $user->getName() ) ) {
618 $ip = $user->getName();
622 return RecentChange
::newLogEntry(
623 $this->getTimestamp(),
626 $formatter->getPlainActionText(),
632 LogEntryBase
::makeParamBlob( $this->getParameters() ),
634 $formatter->getIRCActionComment(), // Used for IRC feeds
635 $this->getAssociatedRevId() // Used for e.g. moves and uploads
640 * Publish the log entry.
642 * @param int $newId Id of the log entry.
643 * @param string $to One of: rcandudp (default), rc, udp
645 public function publish( $newId, $to = 'rcandudp' ) {
646 $log = new LogPage( $this->getType() );
647 if ( $log->isRestricted() ) {
651 $rc = $this->getRecentChange( $newId );
653 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
654 $rc->save( 'pleasedontudp' );
657 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
658 $rc->notifyRCFeeds();
661 // Log the autopatrol if an associated rev id was passed
662 if ( $this->getAssociatedRevId() > 0 &&
663 $rc->getAttribute( 'rc_patrolled' ) === 1 ) {
664 PatrolLog
::record( $rc, true, $this->getPerformer() );
668 public function getType() {
672 public function getSubtype() {
673 return $this->subtype
;
676 public function getParameters() {
677 return $this->parameters
;
683 public function getPerformer() {
684 return $this->performer
;
690 public function getTarget() {
691 return $this->target
;
694 public function getTimestamp() {
695 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
697 return wfTimestamp( TS_MW
, $ts );
700 public function getComment() {
701 return $this->comment
;
708 public function getAssociatedRevId() {
716 public function isLegacy() {
717 return $this->legacy
;
720 public function getDeleted() {
721 return (int)$this->deleted
;