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
11 * @author Niklas Laxström
12 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
17 * Interface for log entries. Every log entry has these methods.
26 public function getType();
32 public function getSubtype();
35 * The full logtype in format maintype/subtype.
38 public function getFullType();
41 * Get the extra parameters stored for this message.
44 public function getParameters();
47 * Get the user for performed this action.
50 public function getPerformer();
53 * Get the target page of this action.
56 public function getTarget();
59 * Get the timestamp when the action was executed.
62 public function getTimestamp();
65 * Get the user provided comment.
68 public function getComment();
71 * Get the access restriction.
74 public function getDeleted();
77 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
80 public function isDeleted( $field );
84 * Extends the LogEntryInterface with some basic functionality
87 abstract class LogEntryBase
implements LogEntry
{
89 public function getFullType() {
90 return $this->getType() . '/' . $this->getSubtype();
93 public function isDeleted( $field ) {
94 return ( $this->getDeleted() & $field ) === $field;
98 * Whether the parameters for this log are stored in new or
101 public function isLegacy() {
108 * This class wraps around database result row.
111 class DatabaseLogEntry
extends LogEntryBase
{
115 * Returns array of information that is needed for querying
116 * log entries. Array contains the following keys:
117 * tables, fields, conds, options and join_conds
120 public static function getSelectQueryData() {
121 $tables = array( 'logging', 'user' );
123 'log_id', 'log_type', 'log_action', 'log_timestamp',
124 'log_user', 'log_user_text',
125 'log_namespace', 'log_title', // unused log_page
126 'log_comment', 'log_params', 'log_deleted',
127 'user_id', 'user_name', 'user_editcount',
131 // IP's don't have an entry in user table
132 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
139 'options' => array(),
140 'join_conds' => $joins,
145 * Constructs new LogEntry from database result row.
146 * Supports rows from both logging and recentchanges table.
148 * @return DatabaseLogEntry
150 public static function newFromRow( $row ) {
151 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
152 return new RCDatabaseLogEntry( (object) $row );
154 return new self( $row );
160 /// Database result row.
163 protected function __construct( $row ) {
168 * Returns the unique database id.
171 public function getId() {
172 return (int)$this->row
->log_id
;
176 * Returns whatever is stored in the database field.
179 protected function getRawParameters() {
180 return $this->row
->log_params
;
185 public function isLegacy() {
186 // This does the check
187 $this->getParameters();
188 return $this->legacy
;
193 public function getType() {
194 return $this->row
->log_type
;
197 public function getSubtype() {
198 return $this->row
->log_action
;
201 public function getParameters() {
202 if ( !isset( $this->params
) ) {
203 $blob = $this->getRawParameters();
204 wfSuppressWarnings();
205 $params = unserialize( $blob );
207 if ( $params !== false ) {
208 $this->params
= $params;
209 $this->legacy
= false;
211 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
212 $this->legacy
= true;
215 return $this->params
;
218 public function getPerformer() {
219 $userId = (int) $this->row
->log_user
;
220 if ( $userId !== 0 ) {
221 return User
::newFromRow( $this->row
);
223 $userText = $this->row
->log_user_text
;
224 return User
::newFromName( $userText, false );
228 public function getTarget() {
229 $namespace = $this->row
->log_namespace
;
230 $page = $this->row
->log_title
;
231 $title = Title
::makeTitle( $namespace, $page );
235 public function getTimestamp() {
236 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
239 public function getComment() {
240 return $this->row
->log_comment
;
243 public function getDeleted() {
244 return $this->row
->log_deleted
;
249 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
251 public function getId() {
252 return $this->row
->rc_logid
;
255 protected function getRawParameters() {
256 return $this->row
->rc_params
;
261 public function getType() {
262 return $this->row
->rc_log_type
;
265 public function getSubtype() {
266 return $this->row
->rc_log_action
;
269 public function getPerformer() {
270 $userId = (int) $this->row
->rc_user
;
271 if ( $userId !== 0 ) {
272 return User
::newFromId( $userId );
274 $userText = $this->row
->rc_user_text
;
275 // Might be an IP, don't validate the username
276 return User
::newFromName( $userText, false );
280 public function getTarget() {
281 $namespace = $this->row
->rc_namespace
;
282 $page = $this->row
->rc_title
;
283 $title = Title
::makeTitle( $namespace, $page );
287 public function getTimestamp() {
288 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
291 public function getComment() {
292 return $this->row
->rc_comment
;
295 public function getDeleted() {
296 return $this->row
->rc_deleted
;
302 * Class for creating log entries manually, for
303 * example to inject them into the database.
306 class ManualLogEntry
extends LogEntryBase
{
307 protected $type; ///!< @var string
308 protected $subtype; ///!< @var string
309 protected $parameters = array(); ///!< @var array
310 protected $performer; ///!< @var User
311 protected $target; ///!< @var Title
312 protected $timestamp; ///!< @var string
313 protected $comment = ''; ///!< @var string
314 protected $deleted; ///!< @var int
316 public function __construct( $type, $subtype ) {
318 $this->subtype
= $subtype;
322 * Set extra log parameters.
323 * You can pass params to the log action message
324 * by prefixing the keys with a number and colon.
325 * The numbering should start with number 4, the
326 * first three parameters are hardcoded for every
328 * $entry->setParameters(
329 * '4:color' => 'blue',
332 * @param $parameters Associative array
334 public function setParameters( $parameters ) {
335 $this->parameters
= $parameters;
338 public function setPerformer( User
$performer ) {
339 $this->performer
= $performer;
342 public function setTarget( Title
$target ) {
343 $this->target
= $target;
346 public function setTimestamp( $timestamp ) {
347 $this->timestamp
= $timestamp;
350 public function setComment( $comment ) {
351 $this->comment
= $comment;
354 public function setDeleted( $deleted ) {
355 $this->deleted
= $deleted;
359 * Inserts the entry into the logging table.
360 * @return int If of the log entry
362 public function insert() {
363 $dbw = wfGetDB( DB_MASTER
);
364 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
366 if ( $this->timestamp
=== null ) {
367 $this->timestamp
= wfTimestampNow();
372 'log_type' => $this->getType(),
373 'log_action' => $this->getSubtype(),
374 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
375 'log_user' => $this->getPerformer()->getId(),
376 'log_user_text' => $this->getPerformer()->getName(),
377 'log_namespace' => $this->getTarget()->getNamespace(),
378 'log_title' => $this->getTarget()->getDBkey(),
379 'log_page' => $this->getTarget()->getArticleId(),
380 'log_comment' => $this->getComment(),
381 'log_params' => serialize( (array) $this->getParameters() ),
383 $dbw->insert( 'logging', $data, __METHOD__
);
384 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
389 * Publishes the log entry.
390 * @param $newId int id of the log entry.
391 * @param $to string: rcandudp (default), rc, udp
393 public function publish( $newId, $to = 'rcandudp' ) {
394 $log = new LogPage( $this->getType() );
395 if ( $log->isRestricted() ) {
399 $formatter = LogFormatter
::newFromEntry( $this );
400 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
401 $formatter->setContext( $context );
403 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
404 $user = $this->getPerformer();
405 $rc = RecentChange
::newLogEntry(
406 $this->getTimestamp(),
409 $formatter->getPlainActionText(), // Used for IRC feeds
410 $user->isAnon() ?
$user->getName() : '',
415 serialize( (array) $this->getParameters() ),
419 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
420 $rc->save( 'pleasedontudp' );
423 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
430 public function getType() {
434 public function getSubtype() {
435 return $this->subtype
;
438 public function getParameters() {
439 return $this->parameters
;
442 public function getPerformer() {
443 return $this->performer
;
446 public function getTarget() {
447 return $this->target
;
450 public function getTimestamp() {
451 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
452 return wfTimestamp( TS_MW
, $ts );
455 public function getComment() {
456 return $this->comment
;
459 public function getDeleted() {
460 return (int) $this->deleted
;