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 $field Integer: 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
{
103 public function getFullType() {
104 return $this->getType() . '/' . $this->getSubtype();
107 public function isDeleted( $field ) {
108 return ( $this->getDeleted() & $field ) === $field;
112 * Whether the parameters for this log are stored in new or
116 public function isLegacy() {
122 * This class wraps around database result row.
125 class DatabaseLogEntry
extends LogEntryBase
{
129 * Returns array of information that is needed for querying
130 * log entries. Array contains the following keys:
131 * tables, fields, conds, options and join_conds
134 public static function getSelectQueryData() {
135 $tables = array( 'logging', 'user' );
137 'log_id', 'log_type', 'log_action', 'log_timestamp',
138 'log_user', 'log_user_text',
139 'log_namespace', 'log_title', // unused log_page
140 'log_comment', 'log_params', 'log_deleted',
141 'user_id', 'user_name', 'user_editcount',
145 // IP's don't have an entry in user table
146 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
153 'options' => array(),
154 'join_conds' => $joins,
159 * Constructs new LogEntry from database result row.
160 * Supports rows from both logging and recentchanges table.
162 * @return DatabaseLogEntry
164 public static function newFromRow( $row ) {
165 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
166 return new RCDatabaseLogEntry( (object)$row );
168 return new self( $row );
174 /// Database result row.
176 protected $performer;
178 protected function __construct( $row ) {
183 * Returns the unique database id.
186 public function getId() {
187 return (int)$this->row
->log_id
;
191 * Returns whatever is stored in the database field.
194 protected function getRawParameters() {
195 return $this->row
->log_params
;
200 public function isLegacy() {
201 // This does the check
202 $this->getParameters();
204 return $this->legacy
;
209 public function getType() {
210 return $this->row
->log_type
;
213 public function getSubtype() {
214 return $this->row
->log_action
;
217 public function getParameters() {
218 if ( !isset( $this->params
) ) {
219 $blob = $this->getRawParameters();
220 wfSuppressWarnings();
221 $params = unserialize( $blob );
223 if ( $params !== false ) {
224 $this->params
= $params;
225 $this->legacy
= false;
227 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
228 $this->legacy
= true;
232 return $this->params
;
235 public function getPerformer() {
236 if ( !$this->performer
) {
237 $userId = (int)$this->row
->log_user
;
238 if ( $userId !== 0 ) { // logged-in users
239 if ( isset( $this->row
->user_name
) ) {
240 $this->performer
= User
::newFromRow( $this->row
);
242 $this->performer
= User
::newFromId( $userId );
245 $userText = $this->row
->log_user_text
;
246 $this->performer
= User
::newFromName( $userText, false );
250 return $this->performer
;
253 public function getTarget() {
254 $namespace = $this->row
->log_namespace
;
255 $page = $this->row
->log_title
;
256 $title = Title
::makeTitle( $namespace, $page );
261 public function getTimestamp() {
262 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
265 public function getComment() {
266 return $this->row
->log_comment
;
269 public function getDeleted() {
270 return $this->row
->log_deleted
;
274 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
276 public function getId() {
277 return $this->row
->rc_logid
;
280 protected function getRawParameters() {
281 return $this->row
->rc_params
;
286 public function getType() {
287 return $this->row
->rc_log_type
;
290 public function getSubtype() {
291 return $this->row
->rc_log_action
;
294 public function getPerformer() {
295 if ( !$this->performer
) {
296 $userId = (int)$this->row
->rc_user
;
297 if ( $userId !== 0 ) {
298 $this->performer
= User
::newFromId( $userId );
300 $userText = $this->row
->rc_user_text
;
301 // Might be an IP, don't validate the username
302 $this->performer
= User
::newFromName( $userText, false );
306 return $this->performer
;
309 public function getTarget() {
310 $namespace = $this->row
->rc_namespace
;
311 $page = $this->row
->rc_title
;
312 $title = Title
::makeTitle( $namespace, $page );
317 public function getTimestamp() {
318 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
321 public function getComment() {
322 return $this->row
->rc_comment
;
325 public function getDeleted() {
326 return $this->row
->rc_deleted
;
331 * Class for creating log entries manually, for
332 * example to inject them into the database.
335 class ManualLogEntry
extends LogEntryBase
{
336 protected $type; ///!< @var string
337 protected $subtype; ///!< @var string
338 protected $parameters = array(); ///!< @var array
339 protected $relations = array(); ///!< @var array
340 protected $performer; ///!< @var User
341 protected $target; ///!< @var Title
342 protected $timestamp; ///!< @var string
343 protected $comment = ''; ///!< @var string
344 protected $deleted; ///!< @var int
351 * @param string $type
352 * @param string $subtype
354 public function __construct( $type, $subtype ) {
356 $this->subtype
= $subtype;
360 * Set extra log parameters.
361 * You can pass params to the log action message
362 * by prefixing the keys with a number and colon.
363 * The numbering should start with number 4, the
364 * first three parameters are hardcoded for every
366 * $entry->setParameters(
367 * '4:color' => 'blue',
373 * @param array $parameters Associative array
375 public function setParameters( $parameters ) {
376 $this->parameters
= $parameters;
380 * Declare arbitrary tag/value relations to this log entry.
381 * These can be used to filter log entries later on.
383 * @param array Map of (tag => (list of values))
386 public function setRelations( array $relations ) {
387 $this->relations
= $relations;
391 * Set the user that performed the action being logged.
395 * @param User $performer
397 public function setPerformer( User
$performer ) {
398 $this->performer
= $performer;
402 * Set the title of the object changed.
406 * @param Title $target
408 public function setTarget( Title
$target ) {
409 $this->target
= $target;
413 * Set the timestamp of when the logged action took place.
417 * @param string $timestamp
419 public function setTimestamp( $timestamp ) {
420 $this->timestamp
= $timestamp;
424 * Set a comment associated with the action being logged.
428 * @param string $comment
430 public function setComment( $comment ) {
431 $this->comment
= $comment;
439 * @param integer $deleted
441 public function setDeleted( $deleted ) {
442 $this->deleted
= $deleted;
446 * Inserts the entry into the logging table.
447 * @param IDatabase $dbw
448 * @return int If of the log entry
450 public function insert( IDatabase
$dbw = null ) {
453 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
454 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
456 if ( $this->timestamp
=== null ) {
457 $this->timestamp
= wfTimestampNow();
460 # Trim spaces on user supplied text
461 $comment = trim( $this->getComment() );
463 # Truncate for whole multibyte characters.
464 $comment = $wgContLang->truncate( $comment, 255 );
468 'log_type' => $this->getType(),
469 'log_action' => $this->getSubtype(),
470 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
471 'log_user' => $this->getPerformer()->getId(),
472 'log_user_text' => $this->getPerformer()->getName(),
473 'log_namespace' => $this->getTarget()->getNamespace(),
474 'log_title' => $this->getTarget()->getDBkey(),
475 'log_page' => $this->getTarget()->getArticleID(),
476 'log_comment' => $comment,
477 'log_params' => serialize( (array)$this->getParameters() ),
479 $dbw->insert( 'logging', $data, __METHOD__
);
480 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
483 foreach ( $this->relations
as $tag => $values ) {
484 if ( !strlen( $tag ) ) {
485 throw new MWException( "Got empty log search tag." );
487 foreach ( $values as $value ) {
490 'ls_value' => $value,
491 'ls_log_id' => $this->id
495 if ( count( $rows ) ) {
496 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
503 * Get a RecentChanges object for the log entry
505 * @return RecentChange
508 public function getRecentChange( $newId = 0 ) {
509 $formatter = LogFormatter
::newFromEntry( $this );
510 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
511 $formatter->setContext( $context );
513 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
514 $user = $this->getPerformer();
516 if ( $user->isAnon() ) {
518 * "MediaWiki default" and friends may have
519 * no IP address in their name
521 if ( IP
::isIPAddress( $user->getName() ) ) {
522 $ip = $user->getName();
526 return RecentChange
::newLogEntry(
527 $this->getTimestamp(),
530 $formatter->getPlainActionText(),
536 serialize( (array)$this->getParameters() ),
538 $formatter->getIRCActionComment() // Used for IRC feeds
543 * Publishes the log entry.
544 * @param int $newId id of the log entry.
545 * @param string $to rcandudp (default), rc, udp
547 public function publish( $newId, $to = 'rcandudp' ) {
548 $log = new LogPage( $this->getType() );
549 if ( $log->isRestricted() ) {
553 $rc = $this->getRecentChange( $newId );
555 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
556 $rc->save( 'pleasedontudp' );
559 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
560 $rc->notifyRCFeeds();
566 public function getType() {
570 public function getSubtype() {
571 return $this->subtype
;
574 public function getParameters() {
575 return $this->parameters
;
581 public function getPerformer() {
582 return $this->performer
;
588 public function getTarget() {
589 return $this->target
;
592 public function getTimestamp() {
593 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
595 return wfTimestamp( TS_MW
, $ts );
598 public function getComment() {
599 return $this->comment
;
602 public function getDeleted() {
603 return (int)$this->deleted
;