Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / logging / LogEntry.php
blob8427adbb35d4d4a7ecc8eef47ea778e46ebb5552
1 <?php
2 /**
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
25 * @file
26 * @author Niklas Laxström
27 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
28 * @since 1.19
31 /**
32 * Interface for log entries. Every log entry has these methods.
33 * @since 1.19
35 interface LogEntry {
36 /**
37 * The main log type.
38 * @return string
40 public function getType();
42 /**
43 * The log subtype.
44 * @return string
46 public function getSubtype();
48 /**
49 * The full logtype in format maintype/subtype.
50 * @return string
52 public function getFullType();
54 /**
55 * Get the extra parameters stored for this message.
56 * @return array
58 public function getParameters();
60 /**
61 * Get the user for performed this action.
62 * @return User
64 public function getPerformer();
66 /**
67 * Get the target page of this action.
68 * @return Title
70 public function getTarget();
72 /**
73 * Get the timestamp when the action was executed.
74 * @return string
76 public function getTimestamp();
78 /**
79 * Get the user provided comment.
80 * @return string
82 public function getComment();
84 /**
85 * Get the access restriction.
86 * @return string
88 public function getDeleted();
90 /**
91 * @param int $field One of LogPage::DELETED_* bitfield constants
92 * @return bool
94 public function isDeleted( $field );
97 /**
98 * Extends the LogEntryInterface with some basic functionality
99 * @since 1.19
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
112 * old format.
113 * @return bool
115 public function isLegacy() {
116 return false;
120 * Create a blob from a parameter array
122 * @param array $params
123 * @return string
124 * @since 1.26
126 public static function makeParamBlob( $params ) {
127 return serialize( (array)$params );
131 * Extract a parameter array from a blob
133 * @param string $blob
134 * @return array
135 * @since 1.26
137 public static function extractParams( $blob ) {
138 return unserialize( $blob );
143 * This class wraps around database result row.
144 * @since 1.19
146 class DatabaseLogEntry extends LogEntryBase {
147 // Static->
150 * Returns array of information that is needed for querying
151 * log entries. Array contains the following keys:
152 * tables, fields, conds, options and join_conds
153 * @return array
155 public static function getSelectQueryData() {
156 $tables = array( 'logging', 'user' );
157 $fields = array(
158 'log_id', 'log_type', 'log_action', 'log_timestamp',
159 'log_user', 'log_user_text',
160 'log_namespace', 'log_title', // unused log_page
161 'log_comment', 'log_params', 'log_deleted',
162 'user_id', 'user_name', 'user_editcount',
165 $joins = array(
166 // IP's don't have an entry in user table
167 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
170 return array(
171 'tables' => $tables,
172 'fields' => $fields,
173 'conds' => array(),
174 'options' => array(),
175 'join_conds' => $joins,
180 * Constructs new LogEntry from database result row.
181 * Supports rows from both logging and recentchanges table.
182 * @param stdClass|array $row
183 * @return DatabaseLogEntry
185 public static function newFromRow( $row ) {
186 $row = (object)$row;
187 if ( isset( $row->rc_logid ) ) {
188 return new RCDatabaseLogEntry( $row );
189 } else {
190 return new self( $row );
194 // Non-static->
196 /** @var stdClass Database result row. */
197 protected $row;
199 /** @var User */
200 protected $performer;
202 /** @var bool Whether the parameters for this log entry are stored in new
203 * or old format.
205 protected $legacy;
207 protected function __construct( $row ) {
208 $this->row = $row;
212 * Returns the unique database id.
213 * @return int
215 public function getId() {
216 return (int)$this->row->log_id;
220 * Returns whatever is stored in the database field.
221 * @return string
223 protected function getRawParameters() {
224 return $this->row->log_params;
227 // LogEntryBase->
229 public function isLegacy() {
230 // This does the check
231 $this->getParameters();
233 return $this->legacy;
236 // LogEntry->
238 public function getType() {
239 return $this->row->log_type;
242 public function getSubtype() {
243 return $this->row->log_action;
246 public function getParameters() {
247 if ( !isset( $this->params ) ) {
248 $blob = $this->getRawParameters();
249 MediaWiki\suppressWarnings();
250 $params = LogEntryBase::extractParams( $blob );
251 MediaWiki\restoreWarnings();
252 if ( $params !== false ) {
253 $this->params = $params;
254 $this->legacy = false;
255 } else {
256 $this->params = LogPage::extractParams( $blob );
257 $this->legacy = true;
261 return $this->params;
264 public function getPerformer() {
265 if ( !$this->performer ) {
266 $userId = (int)$this->row->log_user;
267 if ( $userId !== 0 ) { // logged-in users
268 if ( isset( $this->row->user_name ) ) {
269 $this->performer = User::newFromRow( $this->row );
270 } else {
271 $this->performer = User::newFromId( $userId );
273 } else { // IP users
274 $userText = $this->row->log_user_text;
275 $this->performer = User::newFromName( $userText, false );
279 return $this->performer;
282 public function getTarget() {
283 $namespace = $this->row->log_namespace;
284 $page = $this->row->log_title;
285 $title = Title::makeTitle( $namespace, $page );
287 return $title;
290 public function getTimestamp() {
291 return wfTimestamp( TS_MW, $this->row->log_timestamp );
294 public function getComment() {
295 return $this->row->log_comment;
298 public function getDeleted() {
299 return $this->row->log_deleted;
303 class RCDatabaseLogEntry extends DatabaseLogEntry {
305 public function getId() {
306 return $this->row->rc_logid;
309 protected function getRawParameters() {
310 return $this->row->rc_params;
313 // LogEntry->
315 public function getType() {
316 return $this->row->rc_log_type;
319 public function getSubtype() {
320 return $this->row->rc_log_action;
323 public function getPerformer() {
324 if ( !$this->performer ) {
325 $userId = (int)$this->row->rc_user;
326 if ( $userId !== 0 ) {
327 $this->performer = User::newFromId( $userId );
328 } else {
329 $userText = $this->row->rc_user_text;
330 // Might be an IP, don't validate the username
331 $this->performer = User::newFromName( $userText, false );
335 return $this->performer;
338 public function getTarget() {
339 $namespace = $this->row->rc_namespace;
340 $page = $this->row->rc_title;
341 $title = Title::makeTitle( $namespace, $page );
343 return $title;
346 public function getTimestamp() {
347 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
350 public function getComment() {
351 return $this->row->rc_comment;
354 public function getDeleted() {
355 return $this->row->rc_deleted;
360 * Class for creating log entries manually, for
361 * example to inject them into the database.
362 * @since 1.19
364 class ManualLogEntry extends LogEntryBase {
365 /** @var string Type of log entry */
366 protected $type;
368 /** @var string Sub type of log entry */
369 protected $subtype;
371 /** @var array Parameters for log entry */
372 protected $parameters = array();
374 /** @var array */
375 protected $relations = array();
377 /** @var User Performer of the action for the log entry */
378 protected $performer;
380 /** @var Title Target title for the log entry */
381 protected $target;
383 /** @var string Timestamp of creation of the log entry */
384 protected $timestamp;
386 /** @var string Comment for the log entry */
387 protected $comment = '';
389 /** @var int Deletion state of the log entry */
390 protected $deleted;
392 /** @var int ID of the log entry */
393 protected $id;
395 /** @var bool Whether this is a legacy log entry */
396 protected $legacy = false;
399 * Constructor.
401 * @since 1.19
403 * @param string $type
404 * @param string $subtype
406 public function __construct( $type, $subtype ) {
407 $this->type = $type;
408 $this->subtype = $subtype;
412 * Set extra log parameters.
414 * You can pass params to the log action message by prefixing the keys with
415 * a number and optional type, using colons to separate the fields. The
416 * numbering should start with number 4, the first three parameters are
417 * hardcoded for every message. Example:
418 * $entry->setParameters(
419 * '4::color' => 'blue',
420 * '5:number:count' => 3000,
421 * 'animal' => 'dog'
422 * );
424 * @since 1.19
426 * @param array $parameters Associative array
428 public function setParameters( $parameters ) {
429 $this->parameters = $parameters;
433 * Declare arbitrary tag/value relations to this log entry.
434 * These can be used to filter log entries later on.
436 * @param array $relations Map of (tag => (list of values|value))
437 * @since 1.22
439 public function setRelations( array $relations ) {
440 $this->relations = $relations;
444 * Set the user that performed the action being logged.
446 * @since 1.19
448 * @param User $performer
450 public function setPerformer( User $performer ) {
451 $this->performer = $performer;
455 * Set the title of the object changed.
457 * @since 1.19
459 * @param Title $target
461 public function setTarget( Title $target ) {
462 $this->target = $target;
466 * Set the timestamp of when the logged action took place.
468 * @since 1.19
470 * @param string $timestamp
472 public function setTimestamp( $timestamp ) {
473 $this->timestamp = $timestamp;
477 * Set a comment associated with the action being logged.
479 * @since 1.19
481 * @param string $comment
483 public function setComment( $comment ) {
484 $this->comment = $comment;
488 * Set the 'legacy' flag
490 * @since 1.25
491 * @param bool $legacy
493 public function setLegacy( $legacy ) {
494 $this->legacy = $legacy;
498 * TODO: document
500 * @since 1.19
502 * @param int $deleted
504 public function setDeleted( $deleted ) {
505 $this->deleted = $deleted;
509 * Inserts the entry into the logging table.
510 * @param IDatabase $dbw
511 * @return int ID of the log entry
512 * @throws MWException
514 public function insert( IDatabase $dbw = null ) {
515 global $wgContLang;
517 $dbw = $dbw ?: wfGetDB( DB_MASTER );
518 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
520 if ( $this->timestamp === null ) {
521 $this->timestamp = wfTimestampNow();
524 # Trim spaces on user supplied text
525 $comment = trim( $this->getComment() );
527 # Truncate for whole multibyte characters.
528 $comment = $wgContLang->truncate( $comment, 255 );
530 $data = array(
531 'log_id' => $id,
532 'log_type' => $this->getType(),
533 'log_action' => $this->getSubtype(),
534 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
535 'log_user' => $this->getPerformer()->getId(),
536 'log_user_text' => $this->getPerformer()->getName(),
537 'log_namespace' => $this->getTarget()->getNamespace(),
538 'log_title' => $this->getTarget()->getDBkey(),
539 'log_page' => $this->getTarget()->getArticleID(),
540 'log_comment' => $comment,
541 'log_params' => LogEntryBase::makeParamBlob( $this->getParameters() ),
543 if ( isset( $this->deleted ) ) {
544 $data['log_deleted'] = $this->deleted;
547 $dbw->insert( 'logging', $data, __METHOD__ );
548 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
550 $rows = array();
551 foreach ( $this->relations as $tag => $values ) {
552 if ( !strlen( $tag ) ) {
553 throw new MWException( "Got empty log search tag." );
556 if ( !is_array( $values ) ) {
557 $values = array( $values );
560 foreach ( $values as $value ) {
561 $rows[] = array(
562 'ls_field' => $tag,
563 'ls_value' => $value,
564 'ls_log_id' => $this->id
568 if ( count( $rows ) ) {
569 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
572 return $this->id;
576 * Get a RecentChanges object for the log entry
577 * @param int $newId
578 * @return RecentChange
579 * @since 1.23
581 public function getRecentChange( $newId = 0 ) {
582 $formatter = LogFormatter::newFromEntry( $this );
583 $context = RequestContext::newExtraneousContext( $this->getTarget() );
584 $formatter->setContext( $context );
586 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
587 $user = $this->getPerformer();
588 $ip = "";
589 if ( $user->isAnon() ) {
591 * "MediaWiki default" and friends may have
592 * no IP address in their name
594 if ( IP::isIPAddress( $user->getName() ) ) {
595 $ip = $user->getName();
599 return RecentChange::newLogEntry(
600 $this->getTimestamp(),
601 $logpage,
602 $user,
603 $formatter->getPlainActionText(),
604 $ip,
605 $this->getType(),
606 $this->getSubtype(),
607 $this->getTarget(),
608 $this->getComment(),
609 LogEntryBase::makeParamBlob( $this->getParameters() ),
610 $newId,
611 $formatter->getIRCActionComment() // Used for IRC feeds
616 * Publishes the log entry.
617 * @param int $newId Id of the log entry.
618 * @param string $to One of: rcandudp (default), rc, udp
620 public function publish( $newId, $to = 'rcandudp' ) {
621 $log = new LogPage( $this->getType() );
622 if ( $log->isRestricted() ) {
623 return;
626 $rc = $this->getRecentChange( $newId );
628 if ( $to === 'rc' || $to === 'rcandudp' ) {
629 $rc->save( 'pleasedontudp' );
632 if ( $to === 'udp' || $to === 'rcandudp' ) {
633 $rc->notifyRCFeeds();
637 // LogEntry->
639 public function getType() {
640 return $this->type;
643 public function getSubtype() {
644 return $this->subtype;
647 public function getParameters() {
648 return $this->parameters;
652 * @return User
654 public function getPerformer() {
655 return $this->performer;
659 * @return Title
661 public function getTarget() {
662 return $this->target;
665 public function getTimestamp() {
666 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
668 return wfTimestamp( TS_MW, $ts );
671 public function getComment() {
672 return $this->comment;
676 * @since 1.25
677 * @return bool
679 public function isLegacy() {
680 return $this->legacy;
683 public function getDeleted() {
684 return (int)$this->deleted;