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
{
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.
161 * @return DatabaseLogEntry
163 public static function newFromRow( $row ) {
164 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
165 return new RCDatabaseLogEntry( (object)$row );
167 return new self( $row );
173 /** @var stdClass Database result row. */
177 protected $performer;
179 protected function __construct( $row ) {
184 * Returns the unique database id.
187 public function getId() {
188 return (int)$this->row
->log_id
;
192 * Returns whatever is stored in the database field.
195 protected function getRawParameters() {
196 return $this->row
->log_params
;
201 public function isLegacy() {
202 // This does the check
203 $this->getParameters();
205 return $this->legacy
;
210 public function getType() {
211 return $this->row
->log_type
;
214 public function getSubtype() {
215 return $this->row
->log_action
;
218 public function getParameters() {
219 if ( !isset( $this->params
) ) {
220 $blob = $this->getRawParameters();
221 wfSuppressWarnings();
222 $params = unserialize( $blob );
224 if ( $params !== false ) {
225 $this->params
= $params;
226 $this->legacy
= false;
228 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
229 $this->legacy
= true;
233 return $this->params
;
236 public function getPerformer() {
237 if ( !$this->performer
) {
238 $userId = (int)$this->row
->log_user
;
239 if ( $userId !== 0 ) { // logged-in users
240 if ( isset( $this->row
->user_name
) ) {
241 $this->performer
= User
::newFromRow( $this->row
);
243 $this->performer
= User
::newFromId( $userId );
246 $userText = $this->row
->log_user_text
;
247 $this->performer
= User
::newFromName( $userText, false );
251 return $this->performer
;
254 public function getTarget() {
255 $namespace = $this->row
->log_namespace
;
256 $page = $this->row
->log_title
;
257 $title = Title
::makeTitle( $namespace, $page );
262 public function getTimestamp() {
263 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
266 public function getComment() {
267 return $this->row
->log_comment
;
270 public function getDeleted() {
271 return $this->row
->log_deleted
;
275 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
277 public function getId() {
278 return $this->row
->rc_logid
;
281 protected function getRawParameters() {
282 return $this->row
->rc_params
;
287 public function getType() {
288 return $this->row
->rc_log_type
;
291 public function getSubtype() {
292 return $this->row
->rc_log_action
;
295 public function getPerformer() {
296 if ( !$this->performer
) {
297 $userId = (int)$this->row
->rc_user
;
298 if ( $userId !== 0 ) {
299 $this->performer
= User
::newFromId( $userId );
301 $userText = $this->row
->rc_user_text
;
302 // Might be an IP, don't validate the username
303 $this->performer
= User
::newFromName( $userText, false );
307 return $this->performer
;
310 public function getTarget() {
311 $namespace = $this->row
->rc_namespace
;
312 $page = $this->row
->rc_title
;
313 $title = Title
::makeTitle( $namespace, $page );
318 public function getTimestamp() {
319 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
322 public function getComment() {
323 return $this->row
->rc_comment
;
326 public function getDeleted() {
327 return $this->row
->rc_deleted
;
332 * Class for creating log entries manually, for
333 * example to inject them into the database.
336 class ManualLogEntry
extends LogEntryBase
{
337 /** @var string Type of log entry */
340 /** @var string Sub type of log entry */
343 /** @var array Parameters for log entry */
344 protected $parameters = array();
347 protected $relations = array();
349 /** @var User Performer of the action for the log entry */
350 protected $performer;
352 /** @var Title Target title for the log entry */
355 /** @var string Timestamp of creation of the log entry */
356 protected $timestamp;
358 /** @var string Comment for the log entry */
359 protected $comment = '';
361 /** @var int Deletion state of the log entry */
369 * @param string $type
370 * @param string $subtype
372 public function __construct( $type, $subtype ) {
374 $this->subtype
= $subtype;
378 * Set extra log parameters.
379 * You can pass params to the log action message
380 * by prefixing the keys with a number and colon.
381 * The numbering should start with number 4, the
382 * first three parameters are hardcoded for every
384 * $entry->setParameters(
385 * '4:color' => 'blue',
391 * @param array $parameters Associative array
393 public function setParameters( $parameters ) {
394 $this->parameters
= $parameters;
398 * Declare arbitrary tag/value relations to this log entry.
399 * These can be used to filter log entries later on.
401 * @param array $relations Map of (tag => (list of values))
404 public function setRelations( array $relations ) {
405 $this->relations
= $relations;
409 * Set the user that performed the action being logged.
413 * @param User $performer
415 public function setPerformer( User
$performer ) {
416 $this->performer
= $performer;
420 * Set the title of the object changed.
424 * @param Title $target
426 public function setTarget( Title
$target ) {
427 $this->target
= $target;
431 * Set the timestamp of when the logged action took place.
435 * @param string $timestamp
437 public function setTimestamp( $timestamp ) {
438 $this->timestamp
= $timestamp;
442 * Set a comment associated with the action being logged.
446 * @param string $comment
448 public function setComment( $comment ) {
449 $this->comment
= $comment;
457 * @param integer $deleted
459 public function setDeleted( $deleted ) {
460 $this->deleted
= $deleted;
464 * Inserts the entry into the logging table.
465 * @param IDatabase $dbw
466 * @return int ID of the log entry
467 * @throws MWException
469 public function insert( IDatabase
$dbw = null ) {
472 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
473 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
475 if ( $this->timestamp
=== null ) {
476 $this->timestamp
= wfTimestampNow();
479 # Trim spaces on user supplied text
480 $comment = trim( $this->getComment() );
482 # Truncate for whole multibyte characters.
483 $comment = $wgContLang->truncate( $comment, 255 );
487 'log_type' => $this->getType(),
488 'log_action' => $this->getSubtype(),
489 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
490 'log_user' => $this->getPerformer()->getId(),
491 'log_user_text' => $this->getPerformer()->getName(),
492 'log_namespace' => $this->getTarget()->getNamespace(),
493 'log_title' => $this->getTarget()->getDBkey(),
494 'log_page' => $this->getTarget()->getArticleID(),
495 'log_comment' => $comment,
496 'log_params' => serialize( (array)$this->getParameters() ),
498 $dbw->insert( 'logging', $data, __METHOD__
);
499 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
502 foreach ( $this->relations
as $tag => $values ) {
503 if ( !strlen( $tag ) ) {
504 throw new MWException( "Got empty log search tag." );
506 foreach ( $values as $value ) {
509 'ls_value' => $value,
510 'ls_log_id' => $this->id
514 if ( count( $rows ) ) {
515 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
522 * Get a RecentChanges object for the log entry
524 * @return RecentChange
527 public function getRecentChange( $newId = 0 ) {
528 $formatter = LogFormatter
::newFromEntry( $this );
529 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
530 $formatter->setContext( $context );
532 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
533 $user = $this->getPerformer();
535 if ( $user->isAnon() ) {
537 * "MediaWiki default" and friends may have
538 * no IP address in their name
540 if ( IP
::isIPAddress( $user->getName() ) ) {
541 $ip = $user->getName();
545 return RecentChange
::newLogEntry(
546 $this->getTimestamp(),
549 $formatter->getPlainActionText(),
555 serialize( (array)$this->getParameters() ),
557 $formatter->getIRCActionComment() // Used for IRC feeds
562 * Publishes the log entry.
563 * @param int $newId id of the log entry.
564 * @param string $to rcandudp (default), rc, udp
566 public function publish( $newId, $to = 'rcandudp' ) {
567 $log = new LogPage( $this->getType() );
568 if ( $log->isRestricted() ) {
572 $rc = $this->getRecentChange( $newId );
574 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
575 $rc->save( 'pleasedontudp' );
578 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
579 $rc->notifyRCFeeds();
585 public function getType() {
589 public function getSubtype() {
590 return $this->subtype
;
593 public function getParameters() {
594 return $this->parameters
;
600 public function getPerformer() {
601 return $this->performer
;
607 public function getTarget() {
608 return $this->target
;
611 public function getTimestamp() {
612 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
614 return wfTimestamp( TS_MW
, $ts );
617 public function getComment() {
618 return $this->comment
;
621 public function getDeleted() {
622 return (int)$this->deleted
;