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() {
121 * This class wraps around database result row.
124 class DatabaseLogEntry
extends LogEntryBase
{
128 * Returns array of information that is needed for querying
129 * log entries. Array contains the following keys:
130 * tables, fields, conds, options and join_conds
133 public static function getSelectQueryData() {
134 $tables = array( 'logging', 'user' );
136 'log_id', 'log_type', 'log_action', 'log_timestamp',
137 'log_user', 'log_user_text',
138 'log_namespace', 'log_title', // unused log_page
139 'log_comment', 'log_params', 'log_deleted',
140 'user_id', 'user_name', 'user_editcount',
144 // IP's don't have an entry in user table
145 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
152 'options' => array(),
153 'join_conds' => $joins,
158 * Constructs new LogEntry from database result row.
159 * Supports rows from both logging and recentchanges table.
160 * @param stdClass|array $row
161 * @return DatabaseLogEntry
163 public static function newFromRow( $row ) {
165 if ( isset( $row->rc_logid
) ) {
166 return new RCDatabaseLogEntry( $row );
168 return new self( $row );
174 /** @var stdClass Database result row. */
178 protected $performer;
180 /** @var bool Whether the parameters for this log entry are stored in new
185 protected function __construct( $row ) {
190 * Returns the unique database id.
193 public function getId() {
194 return (int)$this->row
->log_id
;
198 * Returns whatever is stored in the database field.
201 protected function getRawParameters() {
202 return $this->row
->log_params
;
207 public function isLegacy() {
208 // This does the check
209 $this->getParameters();
211 return $this->legacy
;
216 public function getType() {
217 return $this->row
->log_type
;
220 public function getSubtype() {
221 return $this->row
->log_action
;
224 public function getParameters() {
225 if ( !isset( $this->params
) ) {
226 $blob = $this->getRawParameters();
227 wfSuppressWarnings();
228 $params = unserialize( $blob );
230 if ( $params !== false ) {
231 $this->params
= $params;
232 $this->legacy
= false;
234 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
235 $this->legacy
= true;
239 return $this->params
;
242 public function getPerformer() {
243 if ( !$this->performer
) {
244 $userId = (int)$this->row
->log_user
;
245 if ( $userId !== 0 ) { // logged-in users
246 if ( isset( $this->row
->user_name
) ) {
247 $this->performer
= User
::newFromRow( $this->row
);
249 $this->performer
= User
::newFromId( $userId );
252 $userText = $this->row
->log_user_text
;
253 $this->performer
= User
::newFromName( $userText, false );
257 return $this->performer
;
260 public function getTarget() {
261 $namespace = $this->row
->log_namespace
;
262 $page = $this->row
->log_title
;
263 $title = Title
::makeTitle( $namespace, $page );
268 public function getTimestamp() {
269 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
272 public function getComment() {
273 return $this->row
->log_comment
;
276 public function getDeleted() {
277 return $this->row
->log_deleted
;
281 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
283 public function getId() {
284 return $this->row
->rc_logid
;
287 protected function getRawParameters() {
288 return $this->row
->rc_params
;
293 public function getType() {
294 return $this->row
->rc_log_type
;
297 public function getSubtype() {
298 return $this->row
->rc_log_action
;
301 public function getPerformer() {
302 if ( !$this->performer
) {
303 $userId = (int)$this->row
->rc_user
;
304 if ( $userId !== 0 ) {
305 $this->performer
= User
::newFromId( $userId );
307 $userText = $this->row
->rc_user_text
;
308 // Might be an IP, don't validate the username
309 $this->performer
= User
::newFromName( $userText, false );
313 return $this->performer
;
316 public function getTarget() {
317 $namespace = $this->row
->rc_namespace
;
318 $page = $this->row
->rc_title
;
319 $title = Title
::makeTitle( $namespace, $page );
324 public function getTimestamp() {
325 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
328 public function getComment() {
329 return $this->row
->rc_comment
;
332 public function getDeleted() {
333 return $this->row
->rc_deleted
;
338 * Class for creating log entries manually, for
339 * example to inject them into the database.
342 class ManualLogEntry
extends LogEntryBase
{
343 /** @var string Type of log entry */
346 /** @var string Sub type of log entry */
349 /** @var array Parameters for log entry */
350 protected $parameters = array();
353 protected $relations = array();
355 /** @var User Performer of the action for the log entry */
356 protected $performer;
358 /** @var Title Target title for the log entry */
361 /** @var string Timestamp of creation of the log entry */
362 protected $timestamp;
364 /** @var string Comment for the log entry */
365 protected $comment = '';
367 /** @var int Deletion state of the log entry */
370 /** @var int ID of the log entry */
378 * @param string $type
379 * @param string $subtype
381 public function __construct( $type, $subtype ) {
383 $this->subtype
= $subtype;
387 * Set extra log parameters.
389 * You can pass params to the log action message by prefixing the keys with
390 * a number and optional type, using colons to separate the fields. The
391 * numbering should start with number 4, the first three parameters are
392 * hardcoded for every message. Example:
393 * $entry->setParameters(
394 * '4::color' => 'blue',
395 * '5:number:count' => 3000,
401 * @param array $parameters Associative array
403 public function setParameters( $parameters ) {
404 $this->parameters
= $parameters;
408 * Declare arbitrary tag/value relations to this log entry.
409 * These can be used to filter log entries later on.
411 * @param array $relations Map of (tag => (list of values|value))
414 public function setRelations( array $relations ) {
415 $this->relations
= $relations;
419 * Set the user that performed the action being logged.
423 * @param User $performer
425 public function setPerformer( User
$performer ) {
426 $this->performer
= $performer;
430 * Set the title of the object changed.
434 * @param Title $target
436 public function setTarget( Title
$target ) {
437 $this->target
= $target;
441 * Set the timestamp of when the logged action took place.
445 * @param string $timestamp
447 public function setTimestamp( $timestamp ) {
448 $this->timestamp
= $timestamp;
452 * Set a comment associated with the action being logged.
456 * @param string $comment
458 public function setComment( $comment ) {
459 $this->comment
= $comment;
467 * @param int $deleted
469 public function setDeleted( $deleted ) {
470 $this->deleted
= $deleted;
474 * Inserts the entry into the logging table.
475 * @param IDatabase $dbw
476 * @return int ID of the log entry
477 * @throws MWException
479 public function insert( IDatabase
$dbw = null ) {
482 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
483 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
485 if ( $this->timestamp
=== null ) {
486 $this->timestamp
= wfTimestampNow();
489 # Trim spaces on user supplied text
490 $comment = trim( $this->getComment() );
492 # Truncate for whole multibyte characters.
493 $comment = $wgContLang->truncate( $comment, 255 );
497 'log_type' => $this->getType(),
498 'log_action' => $this->getSubtype(),
499 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
500 'log_user' => $this->getPerformer()->getId(),
501 'log_user_text' => $this->getPerformer()->getName(),
502 'log_namespace' => $this->getTarget()->getNamespace(),
503 'log_title' => $this->getTarget()->getDBkey(),
504 'log_page' => $this->getTarget()->getArticleID(),
505 'log_comment' => $comment,
506 'log_params' => serialize( (array)$this->getParameters() ),
508 if ( isset( $this->deleted
) ) {
509 $data['log_deleted'] = $this->deleted
;
512 $dbw->insert( 'logging', $data, __METHOD__
);
513 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
516 foreach ( $this->relations
as $tag => $values ) {
517 if ( !strlen( $tag ) ) {
518 throw new MWException( "Got empty log search tag." );
521 if ( !is_array( $values ) ) {
522 $values = array( $values );
525 foreach ( $values as $value ) {
528 'ls_value' => $value,
529 'ls_log_id' => $this->id
533 if ( count( $rows ) ) {
534 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
537 // Update any bloom filter cache
538 $member = $this->getTarget()->getNamespace() . ':' . $this->getTarget()->getDBkey();
539 BloomCache
::get( 'main' )->insert( wfWikiId(), 'TitleHasLogs', $member );
545 * Get a RecentChanges object for the log entry
547 * @return RecentChange
550 public function getRecentChange( $newId = 0 ) {
551 $formatter = LogFormatter
::newFromEntry( $this );
552 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
553 $formatter->setContext( $context );
555 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
556 $user = $this->getPerformer();
558 if ( $user->isAnon() ) {
560 * "MediaWiki default" and friends may have
561 * no IP address in their name
563 if ( IP
::isIPAddress( $user->getName() ) ) {
564 $ip = $user->getName();
568 return RecentChange
::newLogEntry(
569 $this->getTimestamp(),
572 $formatter->getPlainActionText(),
578 serialize( (array)$this->getParameters() ),
580 $formatter->getIRCActionComment() // Used for IRC feeds
585 * Publishes the log entry.
586 * @param int $newId Id of the log entry.
587 * @param string $to One of: rcandudp (default), rc, udp
589 public function publish( $newId, $to = 'rcandudp' ) {
590 $log = new LogPage( $this->getType() );
591 if ( $log->isRestricted() ) {
595 $rc = $this->getRecentChange( $newId );
597 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
598 $rc->save( 'pleasedontudp' );
601 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
602 $rc->notifyRCFeeds();
608 public function getType() {
612 public function getSubtype() {
613 return $this->subtype
;
616 public function getParameters() {
617 return $this->parameters
;
623 public function getPerformer() {
624 return $this->performer
;
630 public function getTarget() {
631 return $this->target
;
634 public function getTimestamp() {
635 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
637 return wfTimestamp( TS_MW
, $ts );
640 public function getComment() {
641 return $this->comment
;
644 public function getDeleted() {
645 return (int)$this->deleted
;