Merge "Remove unused messages 'edit-externally' and 'edit-externally-help'"
[mediawiki.git] / includes / logging / LogEntry.php
blobe15943fd1058c1ae4b97a0a86ee7ed4dedf254ec
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 $field Integer: one of LogPage::DELETED_* bitfield constants
92 * @return Boolean
94 public function isDeleted( $field );
97 /**
98 * Extends the LogEntryInterface with some basic functionality
99 * @since 1.19
101 abstract class LogEntryBase implements LogEntry {
103 public function getFullType() {
104 return $this->getType() . '/' . $this->getSubtype();
107 public function isDeleted( $field ) {
108 return ( $this->getDeleted() & $field ) === $field;
112 * Whether the parameters for this log are stored in new or
113 * old format.
114 * @return bool
116 public function isLegacy() {
117 return false;
122 * This class wraps around database result row.
123 * @since 1.19
125 class DatabaseLogEntry extends LogEntryBase {
126 // Static->
129 * Returns array of information that is needed for querying
130 * log entries. Array contains the following keys:
131 * tables, fields, conds, options and join_conds
132 * @return array
134 public static function getSelectQueryData() {
135 $tables = array( 'logging', 'user' );
136 $fields = array(
137 'log_id', 'log_type', 'log_action', 'log_timestamp',
138 'log_user', 'log_user_text',
139 'log_namespace', 'log_title', // unused log_page
140 'log_comment', 'log_params', 'log_deleted',
141 'user_id', 'user_name', 'user_editcount',
144 $joins = array(
145 // IP's don't have an entry in user table
146 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
149 return array(
150 'tables' => $tables,
151 'fields' => $fields,
152 'conds' => array(),
153 'options' => array(),
154 'join_conds' => $joins,
159 * Constructs new LogEntry from database result row.
160 * Supports rows from both logging and recentchanges table.
161 * @param $row
162 * @return DatabaseLogEntry
164 public static function newFromRow( $row ) {
165 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
166 return new RCDatabaseLogEntry( (object)$row );
167 } else {
168 return new self( $row );
172 // Non-static->
174 /// Database result row.
175 protected $row;
176 protected $performer;
178 protected function __construct( $row ) {
179 $this->row = $row;
183 * Returns the unique database id.
184 * @return int
186 public function getId() {
187 return (int)$this->row->log_id;
191 * Returns whatever is stored in the database field.
192 * @return string
194 protected function getRawParameters() {
195 return $this->row->log_params;
198 // LogEntryBase->
200 public function isLegacy() {
201 // This does the check
202 $this->getParameters();
204 return $this->legacy;
207 // LogEntry->
209 public function getType() {
210 return $this->row->log_type;
213 public function getSubtype() {
214 return $this->row->log_action;
217 public function getParameters() {
218 if ( !isset( $this->params ) ) {
219 $blob = $this->getRawParameters();
220 wfSuppressWarnings();
221 $params = unserialize( $blob );
222 wfRestoreWarnings();
223 if ( $params !== false ) {
224 $this->params = $params;
225 $this->legacy = false;
226 } else {
227 $this->params = $blob === '' ? array() : explode( "\n", $blob );
228 $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 );
241 } else {
242 $this->performer = User::newFromId( $userId );
244 } else { // IP users
245 $userText = $this->row->log_user_text;
246 $this->performer = User::newFromName( $userText, false );
250 return $this->performer;
253 public function getTarget() {
254 $namespace = $this->row->log_namespace;
255 $page = $this->row->log_title;
256 $title = Title::makeTitle( $namespace, $page );
258 return $title;
261 public function getTimestamp() {
262 return wfTimestamp( TS_MW, $this->row->log_timestamp );
265 public function getComment() {
266 return $this->row->log_comment;
269 public function getDeleted() {
270 return $this->row->log_deleted;
274 class RCDatabaseLogEntry extends DatabaseLogEntry {
276 public function getId() {
277 return $this->row->rc_logid;
280 protected function getRawParameters() {
281 return $this->row->rc_params;
284 // LogEntry->
286 public function getType() {
287 return $this->row->rc_log_type;
290 public function getSubtype() {
291 return $this->row->rc_log_action;
294 public function getPerformer() {
295 if ( !$this->performer ) {
296 $userId = (int)$this->row->rc_user;
297 if ( $userId !== 0 ) {
298 $this->performer = User::newFromId( $userId );
299 } else {
300 $userText = $this->row->rc_user_text;
301 // Might be an IP, don't validate the username
302 $this->performer = User::newFromName( $userText, false );
306 return $this->performer;
309 public function getTarget() {
310 $namespace = $this->row->rc_namespace;
311 $page = $this->row->rc_title;
312 $title = Title::makeTitle( $namespace, $page );
314 return $title;
317 public function getTimestamp() {
318 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
321 public function getComment() {
322 return $this->row->rc_comment;
325 public function getDeleted() {
326 return $this->row->rc_deleted;
331 * Class for creating log entries manually, for
332 * example to inject them into the database.
333 * @since 1.19
335 class ManualLogEntry extends LogEntryBase {
336 protected $type; ///!< @var string
337 protected $subtype; ///!< @var string
338 protected $parameters = array(); ///!< @var array
339 protected $relations = array(); ///!< @var array
340 protected $performer; ///!< @var User
341 protected $target; ///!< @var Title
342 protected $timestamp; ///!< @var string
343 protected $comment = ''; ///!< @var string
344 protected $deleted; ///!< @var int
347 * Constructor.
349 * @since 1.19
351 * @param string $type
352 * @param string $subtype
354 public function __construct( $type, $subtype ) {
355 $this->type = $type;
356 $this->subtype = $subtype;
360 * Set extra log parameters.
361 * You can pass params to the log action message
362 * by prefixing the keys with a number and colon.
363 * The numbering should start with number 4, the
364 * first three parameters are hardcoded for every
365 * message. Example:
366 * $entry->setParameters(
367 * '4:color' => 'blue',
368 * 'animal' => 'dog'
369 * );
371 * @since 1.19
373 * @param array $parameters Associative array
375 public function setParameters( $parameters ) {
376 $this->parameters = $parameters;
380 * Declare arbitrary tag/value relations to this log entry.
381 * These can be used to filter log entries later on.
383 * @param array Map of (tag => (list of values))
384 * @since 1.22
386 public function setRelations( array $relations ) {
387 $this->relations = $relations;
391 * Set the user that performed the action being logged.
393 * @since 1.19
395 * @param User $performer
397 public function setPerformer( User $performer ) {
398 $this->performer = $performer;
402 * Set the title of the object changed.
404 * @since 1.19
406 * @param Title $target
408 public function setTarget( Title $target ) {
409 $this->target = $target;
413 * Set the timestamp of when the logged action took place.
415 * @since 1.19
417 * @param string $timestamp
419 public function setTimestamp( $timestamp ) {
420 $this->timestamp = $timestamp;
424 * Set a comment associated with the action being logged.
426 * @since 1.19
428 * @param string $comment
430 public function setComment( $comment ) {
431 $this->comment = $comment;
435 * TODO: document
437 * @since 1.19
439 * @param integer $deleted
441 public function setDeleted( $deleted ) {
442 $this->deleted = $deleted;
446 * Inserts the entry into the logging table.
447 * @param IDatabase $dbw
448 * @return int If of the log entry
450 public function insert( IDatabase $dbw = null ) {
451 global $wgContLang;
453 $dbw = $dbw ? : wfGetDB( DB_MASTER );
454 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
456 if ( $this->timestamp === null ) {
457 $this->timestamp = wfTimestampNow();
460 # Trim spaces on user supplied text
461 $comment = trim( $this->getComment() );
463 # Truncate for whole multibyte characters.
464 $comment = $wgContLang->truncate( $comment, 255 );
466 $data = array(
467 'log_id' => $id,
468 'log_type' => $this->getType(),
469 'log_action' => $this->getSubtype(),
470 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
471 'log_user' => $this->getPerformer()->getId(),
472 'log_user_text' => $this->getPerformer()->getName(),
473 'log_namespace' => $this->getTarget()->getNamespace(),
474 'log_title' => $this->getTarget()->getDBkey(),
475 'log_page' => $this->getTarget()->getArticleID(),
476 'log_comment' => $comment,
477 'log_params' => serialize( (array)$this->getParameters() ),
479 $dbw->insert( 'logging', $data, __METHOD__ );
480 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
482 $rows = array();
483 foreach ( $this->relations as $tag => $values ) {
484 if ( !strlen( $tag ) ) {
485 throw new MWException( "Got empty log search tag." );
487 foreach ( $values as $value ) {
488 $rows[] = array(
489 'ls_field' => $tag,
490 'ls_value' => $value,
491 'ls_log_id' => $this->id
495 if ( count( $rows ) ) {
496 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
499 return $this->id;
503 * Get a RecentChanges object for the log entry
504 * @param int $newId
505 * @return RecentChange
506 * @since 1.23
508 public function getRecentChange( $newId = 0 ) {
509 $formatter = LogFormatter::newFromEntry( $this );
510 $context = RequestContext::newExtraneousContext( $this->getTarget() );
511 $formatter->setContext( $context );
513 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
514 $user = $this->getPerformer();
515 $ip = "";
516 if ( $user->isAnon() ) {
518 * "MediaWiki default" and friends may have
519 * no IP address in their name
521 if ( IP::isIPAddress( $user->getName() ) ) {
522 $ip = $user->getName();
526 return RecentChange::newLogEntry(
527 $this->getTimestamp(),
528 $logpage,
529 $user,
530 $formatter->getPlainActionText(),
531 $ip,
532 $this->getType(),
533 $this->getSubtype(),
534 $this->getTarget(),
535 $this->getComment(),
536 serialize( (array)$this->getParameters() ),
537 $newId,
538 $formatter->getIRCActionComment() // Used for IRC feeds
543 * Publishes the log entry.
544 * @param int $newId id of the log entry.
545 * @param string $to rcandudp (default), rc, udp
547 public function publish( $newId, $to = 'rcandudp' ) {
548 $log = new LogPage( $this->getType() );
549 if ( $log->isRestricted() ) {
550 return;
553 $rc = $this->getRecentChange( $newId );
555 if ( $to === 'rc' || $to === 'rcandudp' ) {
556 $rc->save( 'pleasedontudp' );
559 if ( $to === 'udp' || $to === 'rcandudp' ) {
560 $rc->notifyRCFeeds();
564 // LogEntry->
566 public function getType() {
567 return $this->type;
570 public function getSubtype() {
571 return $this->subtype;
574 public function getParameters() {
575 return $this->parameters;
579 * @return User
581 public function getPerformer() {
582 return $this->performer;
586 * @return Title
588 public function getTarget() {
589 return $this->target;
592 public function getTimestamp() {
593 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
595 return wfTimestamp( TS_MW, $ts );
598 public function getComment() {
599 return $this->comment;
602 public function getDeleted() {
603 return (int)$this->deleted;