Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / logging / LogEntry.php
blob16b72ea63c36c287a603d96e726de155e87141b6
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 {
37 /**
38 * The main log type.
39 * @return string
41 public function getType();
43 /**
44 * The log subtype.
45 * @return string
47 public function getSubtype();
49 /**
50 * The full logtype in format maintype/subtype.
51 * @return string
53 public function getFullType();
55 /**
56 * Get the extra parameters stored for this message.
57 * @return array
59 public function getParameters();
61 /**
62 * Get the user for performed this action.
63 * @return User
65 public function getPerformer();
67 /**
68 * Get the target page of this action.
69 * @return Title
71 public function getTarget();
73 /**
74 * Get the timestamp when the action was executed.
75 * @return string
77 public function getTimestamp();
79 /**
80 * Get the user provided comment.
81 * @return string
83 public function getComment();
85 /**
86 * Get the access restriction.
87 * @return string
89 public function getDeleted();
91 /**
92 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
93 * @return Boolean
95 public function isDeleted( $field );
98 /**
99 * Extends the LogEntryInterface with some basic functionality
100 * @since 1.19
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
114 * old format.
115 * @return bool
117 public function isLegacy() {
118 return false;
124 * This class wraps around database result row.
125 * @since 1.19
127 class DatabaseLogEntry extends LogEntryBase {
128 // Static->
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
134 * @return array
136 public static function getSelectQueryData() {
137 $tables = array( 'logging', 'user' );
138 $fields = array(
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',
146 $joins = array(
147 // IP's don't have an entry in user table
148 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
151 return array(
152 'tables' => $tables,
153 'fields' => $fields,
154 'conds' => array(),
155 'options' => array(),
156 'join_conds' => $joins,
161 * Constructs new LogEntry from database result row.
162 * Supports rows from both logging and recentchanges table.
163 * @param $row
164 * @return DatabaseLogEntry
166 public static function newFromRow( $row ) {
167 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
168 return new RCDatabaseLogEntry( (object) $row );
169 } else {
170 return new self( $row );
174 // Non-static->
176 /// Database result row.
177 protected $row;
178 protected $performer;
180 protected function __construct( $row ) {
181 $this->row = $row;
185 * Returns the unique database id.
186 * @return int
188 public function getId() {
189 return (int)$this->row->log_id;
193 * Returns whatever is stored in the database field.
194 * @return string
196 protected function getRawParameters() {
197 return $this->row->log_params;
200 // LogEntryBase->
202 public function isLegacy() {
203 // This does the check
204 $this->getParameters();
205 return $this->legacy;
208 // LogEntry->
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 );
223 wfRestoreWarnings();
224 if ( $params !== false ) {
225 $this->params = $params;
226 $this->legacy = false;
227 } else {
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 );
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 );
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 );
256 return $title;
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;
283 // LogEntry->
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 );
298 } else {
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 );
311 return $title;
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.
331 * @since 1.19
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
345 * Constructor.
347 * @since 1.19
349 * @param string $type
350 * @param string $subtype
352 public function __construct( $type, $subtype ) {
353 $this->type = $type;
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
363 * message. Example:
364 * $entry->setParameters(
365 * '4:color' => 'blue',
366 * 'animal' => 'dog'
367 * );
369 * @since 1.19
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))
382 * @since 1.22
384 public function setRelations( array $relations ) {
385 $this->relations = $relations;
389 * Set the user that performed the action being logged.
391 * @since 1.19
393 * @param User $performer
395 public function setPerformer( User $performer ) {
396 $this->performer = $performer;
400 * Set the title of the object changed.
402 * @since 1.19
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.
413 * @since 1.19
415 * @param string $timestamp
417 public function setTimestamp( $timestamp ) {
418 $this->timestamp = $timestamp;
422 * Set a comment associated with the action being logged.
424 * @since 1.19
426 * @param string $comment
428 public function setComment( $comment ) {
429 $this->comment = $comment;
433 * TODO: document
435 * @since 1.19
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 ) {
449 global $wgContLang;
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 );
464 $data = array(
465 'log_id' => $id,
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();
480 $rows = array();
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 ) {
486 $rows[] = array(
487 'ls_field' => $tag,
488 'ls_value' => $value,
489 'ls_log_id' => $this->id
493 if ( count( $rows ) ) {
494 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
497 return $this->id;
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() ) {
508 return;
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();
517 $ip = "";
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(),
529 $logpage,
530 $user,
531 $formatter->getPlainActionText(),
532 $ip,
533 $this->getType(),
534 $this->getSubtype(),
535 $this->getTarget(),
536 $this->getComment(),
537 serialize( (array) $this->getParameters() ),
538 $newId,
539 $formatter->getIRCActionComment() // Used for IRC feeds
542 if ( $to === 'rc' || $to === 'rcandudp' ) {
543 $rc->save( 'pleasedontudp' );
546 if ( $to === 'udp' || $to === 'rcandudp' ) {
547 $rc->notifyRC2UDP();
551 // LogEntry->
553 public function getType() {
554 return $this->type;
557 public function getSubtype() {
558 return $this->subtype;
561 public function getParameters() {
562 return $this->parameters;
566 * @return User
568 public function getPerformer() {
569 return $this->performer;
573 * @return Title
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;