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',
133 // IP's don't have an entry in user table
134 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
141 'options' => array(),
142 'join_conds' => $joins,
147 * Constructs new LogEntry from database result row.
148 * Supports rows from both logging and recentchanges table.
150 * @return DatabaseLogEntry
152 public static function newFromRow( $row ) {
153 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
154 return new RCDatabaseLogEntry( (object) $row );
156 return new self( $row );
162 /// Database result row.
165 protected function __construct( $row ) {
170 * Returns the unique database id.
173 public function getId() {
174 return (int)$this->row
->log_id
;
178 * Returns whatever is stored in the database field.
181 protected function getRawParameters() {
182 return $this->row
->log_params
;
187 public function isLegacy() {
188 // This does the check
189 $this->getParameters();
190 return $this->legacy
;
195 public function getType() {
196 return $this->row
->log_type
;
199 public function getSubtype() {
200 return $this->row
->log_action
;
203 public function getParameters() {
204 if ( !isset( $this->params
) ) {
205 $blob = $this->getRawParameters();
206 wfSuppressWarnings();
207 $params = unserialize( $blob );
209 if ( $params !== false ) {
210 $this->params
= $params;
211 $this->legacy
= false;
213 $this->params
= $blob === '' ?
array() : explode( "\n", $blob );
214 $this->legacy
= true;
217 return $this->params
;
220 public function getPerformer() {
221 $userId = (int) $this->row
->log_user
;
222 if ( $userId !== 0 ) {
223 return User
::newFromRow( $this->row
);
225 $userText = $this->row
->log_user_text
;
226 return User
::newFromName( $userText, false );
230 public function getTarget() {
231 $namespace = $this->row
->log_namespace
;
232 $page = $this->row
->log_title
;
233 $title = Title
::makeTitle( $namespace, $page );
237 public function getTimestamp() {
238 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
241 public function getComment() {
242 return $this->row
->log_comment
;
245 public function getDeleted() {
246 return $this->row
->log_deleted
;
251 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
253 public function getId() {
254 return $this->row
->rc_logid
;
257 protected function getRawParameters() {
258 return $this->row
->rc_params
;
263 public function getType() {
264 return $this->row
->rc_log_type
;
267 public function getSubtype() {
268 return $this->row
->rc_log_action
;
271 public function getPerformer() {
272 $userId = (int) $this->row
->rc_user
;
273 if ( $userId !== 0 ) {
274 return User
::newFromId( $userId );
276 $userText = $this->row
->rc_user_text
;
277 // Might be an IP, don't validate the username
278 return User
::newFromName( $userText, false );
282 public function getTarget() {
283 $namespace = $this->row
->rc_namespace
;
284 $page = $this->row
->rc_title
;
285 $title = Title
::makeTitle( $namespace, $page );
289 public function getTimestamp() {
290 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
293 public function getComment() {
294 return $this->row
->rc_comment
;
297 public function getDeleted() {
298 return $this->row
->rc_deleted
;
304 * Class for creating log entries manually, for
305 * example to inject them into the database.
308 class ManualLogEntry
extends LogEntryBase
{
309 protected $type; ///!< @var string
310 protected $subtype; ///!< @var string
311 protected $parameters = array(); ///!< @var array
312 protected $performer; ///!< @var User
313 protected $target; ///!< @var Title
314 protected $timestamp; ///!< @var string
315 protected $comment = ''; ///!< @var string
316 protected $deleted; ///!< @var int
318 public function __construct( $type, $subtype ) {
320 $this->subtype
= $subtype;
324 * Set extra log parameters.
325 * You can pass params to the log action message
326 * by prefixing the keys with a number and colon.
327 * The numbering should start with number 4, the
328 * first three parameters are hardcoded for every
330 * $entry->setParameters(
331 * '4:color' => 'blue',
334 * @param $parameters Associative array
336 public function setParameters( $parameters ) {
337 $this->parameters
= $parameters;
340 public function setPerformer( User
$performer ) {
341 $this->performer
= $performer;
344 public function setTarget( Title
$target ) {
345 $this->target
= $target;
348 public function setTimestamp( $timestamp ) {
349 $this->timestamp
= $timestamp;
352 public function setComment( $comment ) {
353 $this->comment
= $comment;
356 public function setDeleted( $deleted ) {
357 $this->deleted
= $deleted;
361 * Inserts the entry into the logging table.
362 * @return int If of the log entry
364 public function insert() {
365 global $wgLogRestrictions;
367 $dbw = wfGetDB( DB_MASTER
);
368 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
370 if ( $this->timestamp
=== null ) {
371 $this->timestamp
= wfTimestampNow();
376 'log_type' => $this->getType(),
377 'log_action' => $this->getSubtype(),
378 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
379 'log_user' => $this->getPerformer()->getId(),
380 'log_user_text' => $this->getPerformer()->getName(),
381 'log_namespace' => $this->getTarget()->getNamespace(),
382 'log_title' => $this->getTarget()->getDBkey(),
383 'log_page' => $this->getTarget()->getArticleId(),
384 'log_comment' => $this->getComment(),
385 'log_params' => serialize( (array) $this->getParameters() ),
387 $dbw->insert( 'logging', $data, __METHOD__
);
388 $this->id
= !is_null( $id ) ?
$id : $dbw->insertId();
393 * Publishes the log entry.
394 * @param $newId int id of the log entry.
395 * @param $to string: rcandudp (default), rc, udp
397 public function publish( $newId, $to = 'rcandudp' ) {
398 $log = new LogPage( $this->getType() );
399 if ( $log->isRestricted() ) {
403 $formatter = LogFormatter
::newFromEntry( $this );
404 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
405 $formatter->setContext( $context );
407 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
408 $user = $this->getPerformer();
409 $rc = RecentChange
::newLogEntry(
410 $this->getTimestamp(),
413 $formatter->getPlainActionText(), // Used for IRC feeds
414 $user->isAnon() ?
$user->getName() : '',
419 serialize( (array) $this->getParameters() ),
423 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
427 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
434 public function getType() {
438 public function getSubtype() {
439 return $this->subtype
;
442 public function getParameters() {
443 return $this->parameters
;
446 public function getPerformer() {
447 return $this->performer
;
450 public function getTarget() {
451 return $this->target
;
454 public function getTimestamp() {
455 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
456 return wfTimestamp( TS_MW
, $ts );
459 public function getComment() {
460 return $this->comment
;
463 public function getDeleted() {
464 return (int) $this->deleted
;