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.
41 public function getType();
47 public function getSubtype();
50 * The full logtype in format maintype/subtype.
53 public function getFullType();
56 * Get the extra parameters stored for this message.
59 public function getParameters();
62 * Get the user for performed this action.
65 public function getPerformer();
68 * Get the target page of this action.
71 public function getTarget();
74 * Get the timestamp when the action was executed.
77 public function getTimestamp();
80 * Get the user provided comment.
83 public function getComment();
86 * Get the access restriction.
89 public function getDeleted();
92 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
95 public function isDeleted( $field );
99 * Extends the LogEntryInterface with some basic functionality
102 abstract class LogEntryBase
implements LogEntry
{
104 public function getFullType() {
105 return $this->getType() . '/' . $this->getSubtype();
108 public function isDeleted( $field ) {
109 return ( $this->getDeleted() & $field ) === $field;
113 * Whether the parameters for this log are stored in new or
117 public function isLegacy() {
124 * This class wraps around database result row.
127 class DatabaseLogEntry
extends LogEntryBase
{
131 * Returns array of information that is needed for querying
132 * log entries. Array contains the following keys:
133 * tables, fields, conds, options and join_conds
136 public static function getSelectQueryData() {
137 $tables = array( 'logging', 'user' );
139 'log_id', 'log_type', 'log_action', 'log_timestamp',
140 'log_user', 'log_user_text',
141 'log_namespace', 'log_title', // unused log_page
142 'log_comment', 'log_params', 'log_deleted',
143 'user_id', 'user_name', 'user_editcount',
147 // IP's don't have an entry in user table
148 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
155 'options' => array(),
156 'join_conds' => $joins,
161 * Constructs new LogEntry from database result row.
162 * Supports rows from both logging and recentchanges table.
164 * @return DatabaseLogEntry
166 public static function newFromRow( $row ) {
167 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
168 return new RCDatabaseLogEntry( (object) $row );
170 return new self( $row );
176 /// Database result row.
178 protected $performer;
180 protected function __construct( $row ) {
185 * Returns the unique database id.
188 public function getId() {
189 return (int)$this->row
->log_id
;
193 * Returns whatever is stored in the database field.
196 protected function getRawParameters() {
197 return $this->row
->log_params
;
202 public function isLegacy() {
203 // This does the check
204 $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;
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 );
249 return $this->performer
;
252 public function getTarget() {
253 $namespace = $this->row
->log_namespace
;
254 $page = $this->row
->log_title
;
255 $title = Title
::makeTitle( $namespace, $page );
259 public function getTimestamp() {
260 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
263 public function getComment() {
264 return $this->row
->log_comment
;
267 public function getDeleted() {
268 return $this->row
->log_deleted
;
273 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
275 public function getId() {
276 return $this->row
->rc_logid
;
279 protected function getRawParameters() {
280 return $this->row
->rc_params
;
285 public function getType() {
286 return $this->row
->rc_log_type
;
289 public function getSubtype() {
290 return $this->row
->rc_log_action
;
293 public function getPerformer() {
294 if ( !$this->performer
) {
295 $userId = (int) $this->row
->rc_user
;
296 if ( $userId !== 0 ) {
297 $this->performer
= User
::newFromId( $userId );
299 $userText = $this->row
->rc_user_text
;
300 // Might be an IP, don't validate the username
301 $this->performer
= User
::newFromName( $userText, false );
304 return $this->performer
;
307 public function getTarget() {
308 $namespace = $this->row
->rc_namespace
;
309 $page = $this->row
->rc_title
;
310 $title = Title
::makeTitle( $namespace, $page );
314 public function getTimestamp() {
315 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
318 public function getComment() {
319 return $this->row
->rc_comment
;
322 public function getDeleted() {
323 return $this->row
->rc_deleted
;
329 * Class for creating log entries manually, for
330 * example to inject them into the database.
333 class ManualLogEntry
extends LogEntryBase
{
334 protected $type; ///!< @var string
335 protected $subtype; ///!< @var string
336 protected $parameters = array(); ///!< @var array
337 protected $relations = array(); ///!< @var array
338 protected $performer; ///!< @var User
339 protected $target; ///!< @var Title
340 protected $timestamp; ///!< @var string
341 protected $comment = ''; ///!< @var string
342 protected $deleted; ///!< @var int
349 * @param string $type
350 * @param string $subtype
352 public function __construct( $type, $subtype ) {
354 $this->subtype
= $subtype;
358 * Set extra log parameters.
359 * You can pass params to the log action message
360 * by prefixing the keys with a number and colon.
361 * The numbering should start with number 4, the
362 * first three parameters are hardcoded for every
364 * $entry->setParameters(
365 * '4:color' => 'blue',
371 * @param array $parameters Associative array
373 public function setParameters( $parameters ) {
374 $this->parameters
= $parameters;
378 * Declare arbitrary tag/value relations to this log entry.
379 * These can be used to filter log entries later on.
381 * @param array Map of (tag => (list of values))
384 public function setRelations( array $relations ) {
385 $this->relations
= $relations;
389 * Set the user that performed the action being logged.
393 * @param User $performer
395 public function setPerformer( User
$performer ) {
396 $this->performer
= $performer;
400 * Set the title of the object changed.
404 * @param Title $target
406 public function setTarget( Title
$target ) {
407 $this->target
= $target;
411 * Set the timestamp of when the logged action took place.
415 * @param string $timestamp
417 public function setTimestamp( $timestamp ) {
418 $this->timestamp
= $timestamp;
422 * Set a comment associated with the action being logged.
426 * @param string $comment
428 public function setComment( $comment ) {
429 $this->comment
= $comment;
437 * @param integer $deleted
439 public function setDeleted( $deleted ) {
440 $this->deleted
= $deleted;
444 * Inserts the entry into the logging table.
445 * @param IDatabase $dbw
446 * @return int If of the log entry
448 public function insert( IDatabase
$dbw = null ) {
451 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
452 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
454 if ( $this->timestamp
=== null ) {
455 $this->timestamp
= wfTimestampNow();
458 # Trim spaces on user supplied text
459 $comment = trim( $this->getComment() );
461 # Truncate for whole multibyte characters.
462 $comment = $wgContLang->truncate( $comment, 255 );
466 'log_type' => $this->getType(),
467 'log_action' => $this->getSubtype(),
468 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
469 'log_user' => $this->getPerformer()->getId(),
470 'log_user_text' => $this->getPerformer()->getName(),
471 'log_namespace' => $this->getTarget()->getNamespace(),
472 'log_title' => $this->getTarget()->getDBkey(),
473 'log_page' => $this->getTarget()->getArticleID(),
474 'log_comment' => $comment,
475 'log_params' => serialize( (array) $this->getParameters() ),
477 $dbw->insert( 'logging', $data, __METHOD__
);
478 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
481 foreach ( $this->relations
as $tag => $values ) {
482 if ( !strlen( $tag ) ) {
483 throw new MWException( "Got empty log search tag." );
485 foreach ( $values as $value ) {
488 'ls_value' => $value,
489 'ls_log_id' => $this->id
493 if ( count( $rows ) ) {
494 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
501 * Publishes the log entry.
502 * @param int $newId id of the log entry.
503 * @param string $to rcandudp (default), rc, udp
505 public function publish( $newId, $to = 'rcandudp' ) {
506 $log = new LogPage( $this->getType() );
507 if ( $log->isRestricted() ) {
511 $formatter = LogFormatter
::newFromEntry( $this );
512 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
513 $formatter->setContext( $context );
515 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
516 $user = $this->getPerformer();
518 if ( $user->isAnon() ) {
520 * "MediaWiki default" and friends may have
521 * no IP address in their name
523 if ( IP
::isIPAddress( $user->getName() ) ) {
524 $ip = $user->getName();
527 $rc = RecentChange
::newLogEntry(
528 $this->getTimestamp(),
531 $formatter->getPlainActionText(),
537 serialize( (array) $this->getParameters() ),
539 $formatter->getIRCActionComment() // Used for IRC feeds
542 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
543 $rc->save( 'pleasedontudp' );
546 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
553 public function getType() {
557 public function getSubtype() {
558 return $this->subtype
;
561 public function getParameters() {
562 return $this->parameters
;
568 public function getPerformer() {
569 return $this->performer
;
575 public function getTarget() {
576 return $this->target
;
579 public function getTimestamp() {
580 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
581 return wfTimestamp( TS_MW
, $ts );
584 public function getComment() {
585 return $this->comment
;
588 public function getDeleted() {
589 return (int) $this->deleted
;