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
11 * @author Niklas Laxström
12 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
17 * Interface for log entries. Every log entry has these methods.
26 public function getType();
32 public function getSubtype();
35 * The full logtype in format maintype/subtype.
38 public function getFullType();
41 * Get the extra parameters stored for this message.
44 public function getParameters();
47 * Get the user for performed this action.
50 public function getPerformer();
53 * Get the target page of this action.
56 public function getTarget();
59 * Get the timestamp when the action was executed.
62 public function getTimestamp();
65 * Get the user provided comment.
68 public function getComment();
71 * Get the access restriction.
74 public function getDeleted();
77 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
80 public function isDeleted( $field );
84 * Extends the LogEntryInterface with some basic functionality
87 abstract class LogEntryBase
implements LogEntry
{
89 public function getFullType() {
90 return $this->getType() . '/' . $this->getSubtype();
93 public function isDeleted( $field ) {
94 return ( $this->getDeleted() & $field ) === $field;
98 * Whether the parameters for this log are stored in new or
102 public function isLegacy() {
109 * This class wraps around database result row.
112 class DatabaseLogEntry
extends LogEntryBase
{
116 * Returns array of information that is needed for querying
117 * log entries. Array contains the following keys:
118 * tables, fields, conds, options and join_conds
121 public static function getSelectQueryData() {
122 $tables = array( 'logging', 'user' );
124 'log_id', 'log_type', 'log_action', 'log_timestamp',
125 'log_user', 'log_user_text',
126 'log_namespace', 'log_title', // unused log_page
127 'log_comment', 'log_params', 'log_deleted',
128 'user_id', 'user_name', 'user_editcount',
132 // IP's don't have an entry in user table
133 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
140 'options' => array(),
141 'join_conds' => $joins,
146 * Constructs new LogEntry from database result row.
147 * Supports rows from both logging and recentchanges table.
149 * @return DatabaseLogEntry
151 public static function newFromRow( $row ) {
152 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
153 return new RCDatabaseLogEntry( (object) $row );
155 return new self( $row );
161 /// Database result row.
164 protected function __construct( $row ) {
169 * Returns the unique database id.
172 public function getId() {
173 return (int)$this->row
->log_id
;
177 * Returns whatever is stored in the database field.
180 protected function getRawParameters() {
181 return $this->row
->log_params
;
186 public function isLegacy() {
187 // This does the check
188 $this->getParameters();
189 return $this->legacy
;
194 public function getType() {
195 return $this->row
->log_type
;
198 public function getSubtype() {
199 return $this->row
->log_action
;
202 public function getParameters() {
203 if ( !isset( $this->params
) ) {
204 $blob = $this->getRawParameters();
205 wfSuppressWarnings();
206 $params = unserialize( $blob );
208 if ( $params !== false ) {
209 $this->params
= $params;
210 $this->legacy
= false;
212 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
213 $this->legacy
= true;
216 return $this->params
;
219 public function getPerformer() {
220 $userId = (int) $this->row
->log_user
;
221 if ( $userId !== 0 ) { // logged-in users
222 if ( isset( $this->row
->user_name
) ) {
223 return User
::newFromRow( $this->row
);
225 return User
::newFromId( $userId );
228 $userText = $this->row
->log_user_text
;
229 return User
::newFromName( $userText, false );
233 public function getTarget() {
234 $namespace = $this->row
->log_namespace
;
235 $page = $this->row
->log_title
;
236 $title = Title
::makeTitle( $namespace, $page );
240 public function getTimestamp() {
241 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
244 public function getComment() {
245 return $this->row
->log_comment
;
248 public function getDeleted() {
249 return $this->row
->log_deleted
;
254 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
256 public function getId() {
257 return $this->row
->rc_logid
;
260 protected function getRawParameters() {
261 return $this->row
->rc_params
;
266 public function getType() {
267 return $this->row
->rc_log_type
;
270 public function getSubtype() {
271 return $this->row
->rc_log_action
;
274 public function getPerformer() {
275 $userId = (int) $this->row
->rc_user
;
276 if ( $userId !== 0 ) {
277 return User
::newFromId( $userId );
279 $userText = $this->row
->rc_user_text
;
280 // Might be an IP, don't validate the username
281 return User
::newFromName( $userText, false );
285 public function getTarget() {
286 $namespace = $this->row
->rc_namespace
;
287 $page = $this->row
->rc_title
;
288 $title = Title
::makeTitle( $namespace, $page );
292 public function getTimestamp() {
293 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
296 public function getComment() {
297 return $this->row
->rc_comment
;
300 public function getDeleted() {
301 return $this->row
->rc_deleted
;
307 * Class for creating log entries manually, for
308 * example to inject them into the database.
311 class ManualLogEntry
extends LogEntryBase
{
312 protected $type; ///!< @var string
313 protected $subtype; ///!< @var string
314 protected $parameters = array(); ///!< @var array
315 protected $performer; ///!< @var User
316 protected $target; ///!< @var Title
317 protected $timestamp; ///!< @var string
318 protected $comment = ''; ///!< @var string
319 protected $deleted; ///!< @var int
326 * @param string $type
327 * @param string $subtype
329 public function __construct( $type, $subtype ) {
331 $this->subtype
= $subtype;
335 * Set extra log parameters.
336 * You can pass params to the log action message
337 * by prefixing the keys with a number and colon.
338 * The numbering should start with number 4, the
339 * first three parameters are hardcoded for every
341 * $entry->setParameters(
342 * '4:color' => 'blue',
348 * @param $parameters array Associative array
350 public function setParameters( $parameters ) {
351 $this->parameters
= $parameters;
355 * Set the user that performed the action being logged.
359 * @param User $performer
361 public function setPerformer( User
$performer ) {
362 $this->performer
= $performer;
366 * Set the title of the object changed.
370 * @param Title $target
372 public function setTarget( Title
$target ) {
373 $this->target
= $target;
377 * Set the timestamp of when the logged action took place.
381 * @param string $timestamp
383 public function setTimestamp( $timestamp ) {
384 $this->timestamp
= $timestamp;
388 * Set a comment associated with the action being logged.
392 * @param string $comment
394 public function setComment( $comment ) {
395 $this->comment
= $comment;
403 * @param integer $deleted
405 public function setDeleted( $deleted ) {
406 $this->deleted
= $deleted;
410 * Inserts the entry into the logging table.
411 * @return int If of the log entry
413 public function insert() {
416 $dbw = wfGetDB( DB_MASTER
);
417 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
419 if ( $this->timestamp
=== null ) {
420 $this->timestamp
= wfTimestampNow();
423 # Truncate for whole multibyte characters.
424 $comment = $wgContLang->truncate( $this->getComment(), 255 );
428 'log_type' => $this->getType(),
429 'log_action' => $this->getSubtype(),
430 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
431 'log_user' => $this->getPerformer()->getId(),
432 'log_user_text' => $this->getPerformer()->getName(),
433 'log_namespace' => $this->getTarget()->getNamespace(),
434 'log_title' => $this->getTarget()->getDBkey(),
435 'log_page' => $this->getTarget()->getArticleID(),
436 'log_comment' => $comment,
437 'log_params' => serialize( (array) $this->getParameters() ),
439 $dbw->insert( 'logging', $data, __METHOD__
);
440 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
445 * Publishes the log entry.
446 * @param $newId int id of the log entry.
447 * @param $to string: rcandudp (default), rc, udp
449 public function publish( $newId, $to = 'rcandudp' ) {
450 $log = new LogPage( $this->getType() );
451 if ( $log->isRestricted() ) {
455 $formatter = LogFormatter
::newFromEntry( $this );
456 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
457 $formatter->setContext( $context );
459 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
460 $user = $this->getPerformer();
462 if ( $user->isAnon() ) {
464 * "MediaWiki default" and friends may have
465 * no IP address in their name
467 if ( IP
::isIPAddress( $user->getName() ) ) {
468 $ip = $user->getName();
471 $rc = RecentChange
::newLogEntry(
472 $this->getTimestamp(),
475 $formatter->getPlainActionText(),
481 serialize( (array) $this->getParameters() ),
483 $formatter->getIRCActionComment() // Used for IRC feeds
486 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
487 $rc->save( 'pleasedontudp' );
490 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
497 public function getType() {
501 public function getSubtype() {
502 return $this->subtype
;
505 public function getParameters() {
506 return $this->parameters
;
512 public function getPerformer() {
513 return $this->performer
;
519 public function getTarget() {
520 return $this->target
;
523 public function getTimestamp() {
524 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
525 return wfTimestamp( TS_MW
, $ts );
528 public function getComment() {
529 return $this->comment
;
532 public function getDeleted() {
533 return (int) $this->deleted
;