Add a way for packagers to override some installation details
[mediawiki.git] / includes / logging / LogEntry.php
blob37560d8055b6e9e4d5d783e49222a22a10bd44d2
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;
179 protected function __construct( $row ) {
180 $this->row = $row;
184 * Returns the unique database id.
185 * @return int
187 public function getId() {
188 return (int)$this->row->log_id;
192 * Returns whatever is stored in the database field.
193 * @return string
195 protected function getRawParameters() {
196 return $this->row->log_params;
199 // LogEntryBase->
201 public function isLegacy() {
202 // This does the check
203 $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;
231 return $this->params;
234 public function getPerformer() {
235 $userId = (int) $this->row->log_user;
236 if ( $userId !== 0 ) { // logged-in users
237 if ( isset( $this->row->user_name ) ) {
238 return User::newFromRow( $this->row );
239 } else {
240 return User::newFromId( $userId );
242 } else { // IP users
243 $userText = $this->row->log_user_text;
244 return User::newFromName( $userText, false );
248 public function getTarget() {
249 $namespace = $this->row->log_namespace;
250 $page = $this->row->log_title;
251 $title = Title::makeTitle( $namespace, $page );
252 return $title;
255 public function getTimestamp() {
256 return wfTimestamp( TS_MW, $this->row->log_timestamp );
259 public function getComment() {
260 return $this->row->log_comment;
263 public function getDeleted() {
264 return $this->row->log_deleted;
269 class RCDatabaseLogEntry extends DatabaseLogEntry {
271 public function getId() {
272 return $this->row->rc_logid;
275 protected function getRawParameters() {
276 return $this->row->rc_params;
279 // LogEntry->
281 public function getType() {
282 return $this->row->rc_log_type;
285 public function getSubtype() {
286 return $this->row->rc_log_action;
289 public function getPerformer() {
290 $userId = (int) $this->row->rc_user;
291 if ( $userId !== 0 ) {
292 return User::newFromId( $userId );
293 } else {
294 $userText = $this->row->rc_user_text;
295 // Might be an IP, don't validate the username
296 return User::newFromName( $userText, false );
300 public function getTarget() {
301 $namespace = $this->row->rc_namespace;
302 $page = $this->row->rc_title;
303 $title = Title::makeTitle( $namespace, $page );
304 return $title;
307 public function getTimestamp() {
308 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
311 public function getComment() {
312 return $this->row->rc_comment;
315 public function getDeleted() {
316 return $this->row->rc_deleted;
322 * Class for creating log entries manually, for
323 * example to inject them into the database.
324 * @since 1.19
326 class ManualLogEntry extends LogEntryBase {
327 protected $type; ///!< @var string
328 protected $subtype; ///!< @var string
329 protected $parameters = array(); ///!< @var array
330 protected $performer; ///!< @var User
331 protected $target; ///!< @var Title
332 protected $timestamp; ///!< @var string
333 protected $comment = ''; ///!< @var string
334 protected $deleted; ///!< @var int
337 * Constructor.
339 * @since 1.19
341 * @param string $type
342 * @param string $subtype
344 public function __construct( $type, $subtype ) {
345 $this->type = $type;
346 $this->subtype = $subtype;
350 * Set extra log parameters.
351 * You can pass params to the log action message
352 * by prefixing the keys with a number and colon.
353 * The numbering should start with number 4, the
354 * first three parameters are hardcoded for every
355 * message. Example:
356 * $entry->setParameters(
357 * '4:color' => 'blue',
358 * 'animal' => 'dog'
359 * );
361 * @since 1.19
363 * @param $parameters array Associative array
365 public function setParameters( $parameters ) {
366 $this->parameters = $parameters;
370 * Set the user that performed the action being logged.
372 * @since 1.19
374 * @param User $performer
376 public function setPerformer( User $performer ) {
377 $this->performer = $performer;
381 * Set the title of the object changed.
383 * @since 1.19
385 * @param Title $target
387 public function setTarget( Title $target ) {
388 $this->target = $target;
392 * Set the timestamp of when the logged action took place.
394 * @since 1.19
396 * @param string $timestamp
398 public function setTimestamp( $timestamp ) {
399 $this->timestamp = $timestamp;
403 * Set a comment associated with the action being logged.
405 * @since 1.19
407 * @param string $comment
409 public function setComment( $comment ) {
410 $this->comment = $comment;
414 * TODO: document
416 * @since 1.19
418 * @param integer $deleted
420 public function setDeleted( $deleted ) {
421 $this->deleted = $deleted;
425 * Inserts the entry into the logging table.
426 * @return int If of the log entry
428 public function insert() {
429 global $wgContLang;
431 $dbw = wfGetDB( DB_MASTER );
432 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
434 if ( $this->timestamp === null ) {
435 $this->timestamp = wfTimestampNow();
438 # Truncate for whole multibyte characters.
439 $comment = $wgContLang->truncate( $this->getComment(), 255 );
441 $data = array(
442 'log_id' => $id,
443 'log_type' => $this->getType(),
444 'log_action' => $this->getSubtype(),
445 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
446 'log_user' => $this->getPerformer()->getId(),
447 'log_user_text' => $this->getPerformer()->getName(),
448 'log_namespace' => $this->getTarget()->getNamespace(),
449 'log_title' => $this->getTarget()->getDBkey(),
450 'log_page' => $this->getTarget()->getArticleID(),
451 'log_comment' => $comment,
452 'log_params' => serialize( (array) $this->getParameters() ),
454 $dbw->insert( 'logging', $data, __METHOD__ );
455 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
456 return $this->id;
460 * Publishes the log entry.
461 * @param $newId int id of the log entry.
462 * @param $to string: rcandudp (default), rc, udp
464 public function publish( $newId, $to = 'rcandudp' ) {
465 $log = new LogPage( $this->getType() );
466 if ( $log->isRestricted() ) {
467 return;
470 $formatter = LogFormatter::newFromEntry( $this );
471 $context = RequestContext::newExtraneousContext( $this->getTarget() );
472 $formatter->setContext( $context );
474 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
475 $user = $this->getPerformer();
476 $ip = "";
477 if ( $user->isAnon() ) {
479 * "MediaWiki default" and friends may have
480 * no IP address in their name
482 if ( IP::isIPAddress( $user->getName() ) ) {
483 $ip = $user->getName();
486 $rc = RecentChange::newLogEntry(
487 $this->getTimestamp(),
488 $logpage,
489 $user,
490 $formatter->getPlainActionText(),
491 $ip,
492 $this->getType(),
493 $this->getSubtype(),
494 $this->getTarget(),
495 $this->getComment(),
496 serialize( (array) $this->getParameters() ),
497 $newId,
498 $formatter->getIRCActionComment() // Used for IRC feeds
501 if ( $to === 'rc' || $to === 'rcandudp' ) {
502 $rc->save( 'pleasedontudp' );
505 if ( $to === 'udp' || $to === 'rcandudp' ) {
506 $rc->notifyRC2UDP();
510 // LogEntry->
512 public function getType() {
513 return $this->type;
516 public function getSubtype() {
517 return $this->subtype;
520 public function getParameters() {
521 return $this->parameters;
525 * @return User
527 public function getPerformer() {
528 return $this->performer;
532 * @return Title
534 public function getTarget() {
535 return $this->target;
538 public function getTimestamp() {
539 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
540 return wfTimestamp( TS_MW, $ts );
543 public function getComment() {
544 return $this->comment;
547 public function getDeleted() {
548 return (int) $this->deleted;