c6f717b4e441faff04842f95c5429523143b362a
[lhc/web/wiklou.git] / includes / logging / LogEntry.php
1 <?php
2 /**
3 * Contain classes for dealing with individual log entries
4 *
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
9 *
10 * @file
11 * @author Niklas Laxström
12 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
13 * @since 1.19
14 */
15
16 /**
17 * Interface for log entries. Every log entry has these methods.
18 * @since 1.19
19 */
20 interface LogEntry {
21
22 /**
23 * The main log type.
24 * @return string
25 */
26 public function getType();
27
28 /**
29 * The log subtype.
30 * @return string
31 */
32 public function getSubtype();
33
34 /**
35 * The full logtype in format maintype/subtype.
36 * @return string
37 */
38 public function getFullType();
39
40 /**
41 * Get the extra parameters stored for this message.
42 * @return array
43 */
44 public function getParameters();
45
46 /**
47 * Get the user for performed this action.
48 * @return User
49 */
50 public function getPerformer();
51
52 /**
53 * Get the target page of this action.
54 * @return Title
55 */
56 public function getTarget();
57
58 /**
59 * Get the timestamp when the action was executed.
60 * @return string
61 */
62 public function getTimestamp();
63
64 /**
65 * Get the user provided comment.
66 * @return string
67 */
68 public function getComment();
69
70 /**
71 * Get the access restriction.
72 * @return string
73 */
74 public function getDeleted();
75
76 /**
77 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
78 * @return Boolean
79 */
80 public function isDeleted( $field );
81 }
82
83 /**
84 * Extends the LogEntryInterface with some basic functionality
85 * @since 1.19
86 */
87 abstract class LogEntryBase implements LogEntry {
88
89 public function getFullType() {
90 return $this->getType() . '/' . $this->getSubtype();
91 }
92
93 public function isDeleted( $field ) {
94 return ( $this->getDeleted() & $field ) === $field;
95 }
96
97 /**
98 * Whether the parameters for this log are stored in new or
99 * old format.
100 */
101 public function isLegacy() {
102 return false;
103 }
104
105 }
106
107 /**
108 * This class wraps around database result row.
109 * @since 1.19
110 */
111 class DatabaseLogEntry extends LogEntryBase {
112 // Static->
113
114 /**
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
118 * @return array
119 */
120 public static function getSelectQueryData() {
121 $tables = array( 'logging', 'user' );
122 $fields = array(
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',
128 );
129
130 $joins = array(
131 // IP's don't have an entry in user table
132 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
133 );
134
135 return array(
136 'tables' => $tables,
137 'fields' => $fields,
138 'conds' => array(),
139 'options' => array(),
140 'join_conds' => $joins,
141 );
142 }
143
144 /**
145 * Constructs new LogEntry from database result row.
146 * Supports rows from both logging and recentchanges table.
147 * @param $row
148 * @return DatabaseLogEntry
149 */
150 public static function newFromRow( $row ) {
151 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
152 return new RCDatabaseLogEntry( (object) $row );
153 } else {
154 return new self( $row );
155 }
156 }
157
158 // Non-static->
159
160 /// Database result row.
161 protected $row;
162
163 protected function __construct( $row ) {
164 $this->row = $row;
165 }
166
167 /**
168 * Returns the unique database id.
169 * @return int
170 */
171 public function getId() {
172 return (int)$this->row->log_id;
173 }
174
175 /**
176 * Returns whatever is stored in the database field.
177 * @return string
178 */
179 protected function getRawParameters() {
180 return $this->row->log_params;
181 }
182
183 // LogEntryBase->
184
185 public function isLegacy() {
186 // This does the check
187 $this->getParameters();
188 return $this->legacy;
189 }
190
191 // LogEntry->
192
193 public function getType() {
194 return $this->row->log_type;
195 }
196
197 public function getSubtype() {
198 return $this->row->log_action;
199 }
200
201 public function getParameters() {
202 if ( !isset( $this->params ) ) {
203 $blob = $this->getRawParameters();
204 wfSuppressWarnings();
205 $params = unserialize( $blob );
206 wfRestoreWarnings();
207 if ( $params !== false ) {
208 $this->params = $params;
209 $this->legacy = false;
210 } else {
211 $this->params = $blob === '' ? array() : explode( "\n", $blob );
212 $this->legacy = true;
213 }
214 }
215 return $this->params;
216 }
217
218 public function getPerformer() {
219 $userId = (int) $this->row->log_user;
220 if ( $userId !== 0 ) { // logged-in users
221 if ( isset( $this->row->user_name ) ) {
222 return User::newFromRow( $this->row );
223 } else {
224 return User::newFromId( $userId );
225 }
226 } else { // IP users
227 $userText = $this->row->log_user_text;
228 return User::newFromName( $userText, false );
229 }
230 }
231
232 public function getTarget() {
233 $namespace = $this->row->log_namespace;
234 $page = $this->row->log_title;
235 $title = Title::makeTitle( $namespace, $page );
236 return $title;
237 }
238
239 public function getTimestamp() {
240 return wfTimestamp( TS_MW, $this->row->log_timestamp );
241 }
242
243 public function getComment() {
244 return $this->row->log_comment;
245 }
246
247 public function getDeleted() {
248 return $this->row->log_deleted;
249 }
250
251 }
252
253 class RCDatabaseLogEntry extends DatabaseLogEntry {
254
255 public function getId() {
256 return $this->row->rc_logid;
257 }
258
259 protected function getRawParameters() {
260 return $this->row->rc_params;
261 }
262
263 // LogEntry->
264
265 public function getType() {
266 return $this->row->rc_log_type;
267 }
268
269 public function getSubtype() {
270 return $this->row->rc_log_action;
271 }
272
273 public function getPerformer() {
274 $userId = (int) $this->row->rc_user;
275 if ( $userId !== 0 ) {
276 return User::newFromId( $userId );
277 } else {
278 $userText = $this->row->rc_user_text;
279 // Might be an IP, don't validate the username
280 return User::newFromName( $userText, false );
281 }
282 }
283
284 public function getTarget() {
285 $namespace = $this->row->rc_namespace;
286 $page = $this->row->rc_title;
287 $title = Title::makeTitle( $namespace, $page );
288 return $title;
289 }
290
291 public function getTimestamp() {
292 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
293 }
294
295 public function getComment() {
296 return $this->row->rc_comment;
297 }
298
299 public function getDeleted() {
300 return $this->row->rc_deleted;
301 }
302
303 }
304
305 /**
306 * Class for creating log entries manually, for
307 * example to inject them into the database.
308 * @since 1.19
309 */
310 class ManualLogEntry extends LogEntryBase {
311 protected $type; ///!< @var string
312 protected $subtype; ///!< @var string
313 protected $parameters = array(); ///!< @var array
314 protected $performer; ///!< @var User
315 protected $target; ///!< @var Title
316 protected $timestamp; ///!< @var string
317 protected $comment = ''; ///!< @var string
318 protected $deleted; ///!< @var int
319
320 /**
321 * Constructor.
322 *
323 * @since 1.19
324 *
325 * @param string $type
326 * @param string $subtype
327 */
328 public function __construct( $type, $subtype ) {
329 $this->type = $type;
330 $this->subtype = $subtype;
331 }
332
333 /**
334 * Set extra log parameters.
335 * You can pass params to the log action message
336 * by prefixing the keys with a number and colon.
337 * The numbering should start with number 4, the
338 * first three parameters are hardcoded for every
339 * message. Example:
340 * $entry->setParameters(
341 * '4:color' => 'blue',
342 * 'animal' => 'dog'
343 * );
344 *
345 * @since 1.19
346 *
347 * @param $parameters Associative array
348 */
349 public function setParameters( $parameters ) {
350 $this->parameters = $parameters;
351 }
352
353 /**
354 * Set the user that performed the action being logged.
355 *
356 * @since 1.19
357 *
358 * @param User $performer
359 */
360 public function setPerformer( User $performer ) {
361 $this->performer = $performer;
362 }
363
364 /**
365 * Set the title of the object changed.
366 *
367 * @since 1.19
368 *
369 * @param Title $target
370 */
371 public function setTarget( Title $target ) {
372 $this->target = $target;
373 }
374
375 /**
376 * Set the timestamp of when the logged action took place.
377 *
378 * @since 1.19
379 *
380 * @param string $timestamp
381 */
382 public function setTimestamp( $timestamp ) {
383 $this->timestamp = $timestamp;
384 }
385
386 /**
387 * Set a comment associated with the action being logged.
388 *
389 * @since 1.19
390 *
391 * @param string $comment
392 */
393 public function setComment( $comment ) {
394 $this->comment = $comment;
395 }
396
397 /**
398 * TODO: document
399 *
400 * @since 1.19
401 *
402 * @param integer $deleted
403 */
404 public function setDeleted( $deleted ) {
405 $this->deleted = $deleted;
406 }
407
408 /**
409 * Inserts the entry into the logging table.
410 * @return int If of the log entry
411 */
412 public function insert() {
413 global $wgContLang;
414
415 $dbw = wfGetDB( DB_MASTER );
416 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
417
418 if ( $this->timestamp === null ) {
419 $this->timestamp = wfTimestampNow();
420 }
421
422 # Truncate for whole multibyte characters.
423 $comment = $wgContLang->truncate( $this->getComment(), 255 );
424
425 $data = array(
426 'log_id' => $id,
427 'log_type' => $this->getType(),
428 'log_action' => $this->getSubtype(),
429 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
430 'log_user' => $this->getPerformer()->getId(),
431 'log_user_text' => $this->getPerformer()->getName(),
432 'log_namespace' => $this->getTarget()->getNamespace(),
433 'log_title' => $this->getTarget()->getDBkey(),
434 'log_page' => $this->getTarget()->getArticleId(),
435 'log_comment' => $comment,
436 'log_params' => serialize( (array) $this->getParameters() ),
437 );
438 $dbw->insert( 'logging', $data, __METHOD__ );
439 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
440 return $this->id;
441 }
442
443 /**
444 * Publishes the log entry.
445 * @param $newId int id of the log entry.
446 * @param $to string: rcandudp (default), rc, udp
447 */
448 public function publish( $newId, $to = 'rcandudp' ) {
449 $log = new LogPage( $this->getType() );
450 if ( $log->isRestricted() ) {
451 return;
452 }
453
454 $formatter = LogFormatter::newFromEntry( $this );
455 $context = RequestContext::newExtraneousContext( $this->getTarget() );
456 $formatter->setContext( $context );
457
458 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
459 $user = $this->getPerformer();
460 $rc = RecentChange::newLogEntry(
461 $this->getTimestamp(),
462 $logpage,
463 $user,
464 $formatter->getPlainActionText(), // Used for IRC feeds
465 $user->isAnon() ? $user->getName() : '',
466 $this->getType(),
467 $this->getSubtype(),
468 $this->getTarget(),
469 $this->getComment(),
470 serialize( (array) $this->getParameters() ),
471 $newId
472 );
473
474 if ( $to === 'rc' || $to === 'rcandudp' ) {
475 $rc->save( 'pleasedontudp' );
476 }
477
478 if ( $to === 'udp' || $to === 'rcandudp' ) {
479 $rc->notifyRC2UDP();
480 }
481 }
482
483 // LogEntry->
484
485 public function getType() {
486 return $this->type;
487 }
488
489 public function getSubtype() {
490 return $this->subtype;
491 }
492
493 public function getParameters() {
494 return $this->parameters;
495 }
496
497 public function getPerformer() {
498 return $this->performer;
499 }
500
501 public function getTarget() {
502 return $this->target;
503 }
504
505 public function getTimestamp() {
506 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
507 return wfTimestamp( TS_MW, $ts );
508 }
509
510 public function getComment() {
511 return $this->comment;
512 }
513
514 public function getDeleted() {
515 return (int) $this->deleted;
516 }
517
518 }