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 /** @var bool Whether the parameters for this log entry are stored in new
184 protected function __construct( $row ) {
189 * Returns the unique database id.
192 public function getId() {
193 return (int)$this->row
->log_id
;
197 * Returns whatever is stored in the database field.
200 protected function getRawParameters() {
201 return $this->row
->log_params
;
206 public function isLegacy() {
207 // This does the check
208 $this->getParameters();
210 return $this->legacy
;
215 public function getType() {
216 return $this->row
->log_type
;
219 public function getSubtype() {
220 return $this->row
->log_action
;
223 public function getParameters() {
224 if ( !isset( $this->params
) ) {
225 $blob = $this->getRawParameters();
226 wfSuppressWarnings();
227 $params = unserialize( $blob );
229 if ( $params !== false ) {
230 $this->params
= $params;
231 $this->legacy
= false;
233 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
234 $this->legacy
= true;
238 return $this->params
;
241 public function getPerformer() {
242 if ( !$this->performer
) {
243 $userId = (int)$this->row
->log_user
;
244 if ( $userId !== 0 ) { // logged-in users
245 if ( isset( $this->row
->user_name
) ) {
246 $this->performer
= User
::newFromRow( $this->row
);
248 $this->performer
= User
::newFromId( $userId );
251 $userText = $this->row
->log_user_text
;
252 $this->performer
= User
::newFromName( $userText, false );
256 return $this->performer
;
259 public function getTarget() {
260 $namespace = $this->row
->log_namespace
;
261 $page = $this->row
->log_title
;
262 $title = Title
::makeTitle( $namespace, $page );
267 public function getTimestamp() {
268 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
271 public function getComment() {
272 return $this->row
->log_comment
;
275 public function getDeleted() {
276 return $this->row
->log_deleted
;
280 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
282 public function getId() {
283 return $this->row
->rc_logid
;
286 protected function getRawParameters() {
287 return $this->row
->rc_params
;
292 public function getType() {
293 return $this->row
->rc_log_type
;
296 public function getSubtype() {
297 return $this->row
->rc_log_action
;
300 public function getPerformer() {
301 if ( !$this->performer
) {
302 $userId = (int)$this->row
->rc_user
;
303 if ( $userId !== 0 ) {
304 $this->performer
= User
::newFromId( $userId );
306 $userText = $this->row
->rc_user_text
;
307 // Might be an IP, don't validate the username
308 $this->performer
= User
::newFromName( $userText, false );
312 return $this->performer
;
315 public function getTarget() {
316 $namespace = $this->row
->rc_namespace
;
317 $page = $this->row
->rc_title
;
318 $title = Title
::makeTitle( $namespace, $page );
323 public function getTimestamp() {
324 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
327 public function getComment() {
328 return $this->row
->rc_comment
;
331 public function getDeleted() {
332 return $this->row
->rc_deleted
;
337 * Class for creating log entries manually, for
338 * example to inject them into the database.
341 class ManualLogEntry
extends LogEntryBase
{
342 /** @var string Type of log entry */
345 /** @var string Sub type of log entry */
348 /** @var array Parameters for log entry */
349 protected $parameters = array();
352 protected $relations = array();
354 /** @var User Performer of the action for the log entry */
355 protected $performer;
357 /** @var Title Target title for the log entry */
360 /** @var string Timestamp of creation of the log entry */
361 protected $timestamp;
363 /** @var string Comment for the log entry */
364 protected $comment = '';
366 /** @var int Deletion state of the log entry */
369 /** @var int ID of the log entry */
377 * @param string $type
378 * @param string $subtype
380 public function __construct( $type, $subtype ) {
382 $this->subtype
= $subtype;
386 * Set extra log parameters.
387 * You can pass params to the log action message
388 * by prefixing the keys with a number and colon.
389 * The numbering should start with number 4, the
390 * first three parameters are hardcoded for every
392 * $entry->setParameters(
393 * '4:color' => 'blue',
399 * @param array $parameters Associative array
401 public function setParameters( $parameters ) {
402 $this->parameters
= $parameters;
406 * Declare arbitrary tag/value relations to this log entry.
407 * These can be used to filter log entries later on.
409 * @param array $relations Map of (tag => (list of values))
412 public function setRelations( array $relations ) {
413 $this->relations
= $relations;
417 * Set the user that performed the action being logged.
421 * @param User $performer
423 public function setPerformer( User
$performer ) {
424 $this->performer
= $performer;
428 * Set the title of the object changed.
432 * @param Title $target
434 public function setTarget( Title
$target ) {
435 $this->target
= $target;
439 * Set the timestamp of when the logged action took place.
443 * @param string $timestamp
445 public function setTimestamp( $timestamp ) {
446 $this->timestamp
= $timestamp;
450 * Set a comment associated with the action being logged.
454 * @param string $comment
456 public function setComment( $comment ) {
457 $this->comment
= $comment;
465 * @param integer $deleted
467 public function setDeleted( $deleted ) {
468 $this->deleted
= $deleted;
472 * Inserts the entry into the logging table.
473 * @param IDatabase $dbw
474 * @return int ID of the log entry
475 * @throws MWException
477 public function insert( IDatabase
$dbw = null ) {
480 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
481 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
483 if ( $this->timestamp
=== null ) {
484 $this->timestamp
= wfTimestampNow();
487 # Trim spaces on user supplied text
488 $comment = trim( $this->getComment() );
490 # Truncate for whole multibyte characters.
491 $comment = $wgContLang->truncate( $comment, 255 );
495 'log_type' => $this->getType(),
496 'log_action' => $this->getSubtype(),
497 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
498 'log_user' => $this->getPerformer()->getId(),
499 'log_user_text' => $this->getPerformer()->getName(),
500 'log_namespace' => $this->getTarget()->getNamespace(),
501 'log_title' => $this->getTarget()->getDBkey(),
502 'log_page' => $this->getTarget()->getArticleID(),
503 'log_comment' => $comment,
504 'log_params' => serialize( (array)$this->getParameters() ),
506 $dbw->insert( 'logging', $data, __METHOD__
);
507 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
510 foreach ( $this->relations
as $tag => $values ) {
511 if ( !strlen( $tag ) ) {
512 throw new MWException( "Got empty log search tag." );
514 foreach ( $values as $value ) {
517 'ls_value' => $value,
518 'ls_log_id' => $this->id
522 if ( count( $rows ) ) {
523 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
530 * Get a RecentChanges object for the log entry
532 * @return RecentChange
535 public function getRecentChange( $newId = 0 ) {
536 $formatter = LogFormatter
::newFromEntry( $this );
537 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
538 $formatter->setContext( $context );
540 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
541 $user = $this->getPerformer();
543 if ( $user->isAnon() ) {
545 * "MediaWiki default" and friends may have
546 * no IP address in their name
548 if ( IP
::isIPAddress( $user->getName() ) ) {
549 $ip = $user->getName();
553 return RecentChange
::newLogEntry(
554 $this->getTimestamp(),
557 $formatter->getPlainActionText(),
563 serialize( (array)$this->getParameters() ),
565 $formatter->getIRCActionComment() // Used for IRC feeds
570 * Publishes the log entry.
571 * @param int $newId id of the log entry.
572 * @param string $to rcandudp (default), rc, udp
574 public function publish( $newId, $to = 'rcandudp' ) {
575 $log = new LogPage( $this->getType() );
576 if ( $log->isRestricted() ) {
580 $rc = $this->getRecentChange( $newId );
582 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
583 $rc->save( 'pleasedontudp' );
586 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
587 $rc->notifyRCFeeds();
593 public function getType() {
597 public function getSubtype() {
598 return $this->subtype
;
601 public function getParameters() {
602 return $this->parameters
;
608 public function getPerformer() {
609 return $this->performer
;
615 public function getTarget() {
616 return $this->target
;
619 public function getTimestamp() {
620 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
622 return wfTimestamp( TS_MW
, $ts );
625 public function getComment() {
626 return $this->comment
;
629 public function getDeleted() {
630 return (int)$this->deleted
;