Merge "Omit 'external' changes from ChangesFeed"
[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 * 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.
14 *
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.
19 *
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
24 *
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
29 */
30
31 /**
32 * Interface for log entries. Every log entry has these methods.
33 * @since 1.19
34 */
35 interface LogEntry {
36 /**
37 * The main log type.
38 * @return string
39 */
40 public function getType();
41
42 /**
43 * The log subtype.
44 * @return string
45 */
46 public function getSubtype();
47
48 /**
49 * The full logtype in format maintype/subtype.
50 * @return string
51 */
52 public function getFullType();
53
54 /**
55 * Get the extra parameters stored for this message.
56 * @return array
57 */
58 public function getParameters();
59
60 /**
61 * Get the user for performed this action.
62 * @return User
63 */
64 public function getPerformer();
65
66 /**
67 * Get the target page of this action.
68 * @return Title
69 */
70 public function getTarget();
71
72 /**
73 * Get the timestamp when the action was executed.
74 * @return string
75 */
76 public function getTimestamp();
77
78 /**
79 * Get the user provided comment.
80 * @return string
81 */
82 public function getComment();
83
84 /**
85 * Get the access restriction.
86 * @return string
87 */
88 public function getDeleted();
89
90 /**
91 * @param int $field One of LogPage::DELETED_* bitfield constants
92 * @return bool
93 */
94 public function isDeleted( $field );
95 }
96
97 /**
98 * Extends the LogEntryInterface with some basic functionality
99 * @since 1.19
100 */
101 abstract class LogEntryBase implements LogEntry {
102 public function getFullType() {
103 return $this->getType() . '/' . $this->getSubtype();
104 }
105
106 public function isDeleted( $field ) {
107 return ( $this->getDeleted() & $field ) === $field;
108 }
109
110 /**
111 * Whether the parameters for this log are stored in new or
112 * old format.
113 * @return bool
114 */
115 public function isLegacy() {
116 return false;
117 }
118
119 /**
120 * Create a blob from a parameter array
121 *
122 * @param array $params
123 * @return string
124 * @since 1.26
125 */
126 public static function makeParamBlob( $params ) {
127 return serialize( (array)$params );
128 }
129
130 /**
131 * Extract a parameter array from a blob
132 *
133 * @param string $blob
134 * @return array
135 * @since 1.26
136 */
137 public static function extractParams( $blob ) {
138 return unserialize( $blob );
139 }
140 }
141
142 /**
143 * This class wraps around database result row.
144 * @since 1.19
145 */
146 class DatabaseLogEntry extends LogEntryBase {
147 // Static->
148
149 /**
150 * Returns array of information that is needed for querying
151 * log entries. Array contains the following keys:
152 * tables, fields, conds, options and join_conds
153 * @return array
154 */
155 public static function getSelectQueryData() {
156 $tables = array( 'logging', 'user' );
157 $fields = array(
158 'log_id', 'log_type', 'log_action', 'log_timestamp',
159 'log_user', 'log_user_text',
160 'log_namespace', 'log_title', // unused log_page
161 'log_comment', 'log_params', 'log_deleted',
162 'user_id', 'user_name', 'user_editcount',
163 );
164
165 $joins = array(
166 // IP's don't have an entry in user table
167 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
168 );
169
170 return array(
171 'tables' => $tables,
172 'fields' => $fields,
173 'conds' => array(),
174 'options' => array(),
175 'join_conds' => $joins,
176 );
177 }
178
179 /**
180 * Constructs new LogEntry from database result row.
181 * Supports rows from both logging and recentchanges table.
182 * @param stdClass|array $row
183 * @return DatabaseLogEntry
184 */
185 public static function newFromRow( $row ) {
186 $row = (object)$row;
187 if ( isset( $row->rc_logid ) ) {
188 return new RCDatabaseLogEntry( $row );
189 } else {
190 return new self( $row );
191 }
192 }
193
194 // Non-static->
195
196 /** @var stdClass Database result row. */
197 protected $row;
198
199 /** @var User */
200 protected $performer;
201
202 /** @var bool Whether the parameters for this log entry are stored in new
203 * or old format.
204 */
205 protected $legacy;
206
207 protected function __construct( $row ) {
208 $this->row = $row;
209 }
210
211 /**
212 * Returns the unique database id.
213 * @return int
214 */
215 public function getId() {
216 return (int)$this->row->log_id;
217 }
218
219 /**
220 * Returns whatever is stored in the database field.
221 * @return string
222 */
223 protected function getRawParameters() {
224 return $this->row->log_params;
225 }
226
227 // LogEntryBase->
228
229 public function isLegacy() {
230 // This does the check
231 $this->getParameters();
232
233 return $this->legacy;
234 }
235
236 // LogEntry->
237
238 public function getType() {
239 return $this->row->log_type;
240 }
241
242 public function getSubtype() {
243 return $this->row->log_action;
244 }
245
246 public function getParameters() {
247 if ( !isset( $this->params ) ) {
248 $blob = $this->getRawParameters();
249 MediaWiki\suppressWarnings();
250 $params = LogEntryBase::extractParams( $blob );
251 MediaWiki\restoreWarnings();
252 if ( $params !== false ) {
253 $this->params = $params;
254 $this->legacy = false;
255 } else {
256 $this->params = LogPage::extractParams( $blob );
257 $this->legacy = true;
258 }
259 }
260
261 return $this->params;
262 }
263
264 public function getPerformer() {
265 if ( !$this->performer ) {
266 $userId = (int)$this->row->log_user;
267 if ( $userId !== 0 ) { // logged-in users
268 if ( isset( $this->row->user_name ) ) {
269 $this->performer = User::newFromRow( $this->row );
270 } else {
271 $this->performer = User::newFromId( $userId );
272 }
273 } else { // IP users
274 $userText = $this->row->log_user_text;
275 $this->performer = User::newFromName( $userText, false );
276 }
277 }
278
279 return $this->performer;
280 }
281
282 public function getTarget() {
283 $namespace = $this->row->log_namespace;
284 $page = $this->row->log_title;
285 $title = Title::makeTitle( $namespace, $page );
286
287 return $title;
288 }
289
290 public function getTimestamp() {
291 return wfTimestamp( TS_MW, $this->row->log_timestamp );
292 }
293
294 public function getComment() {
295 return $this->row->log_comment;
296 }
297
298 public function getDeleted() {
299 return $this->row->log_deleted;
300 }
301 }
302
303 class RCDatabaseLogEntry extends DatabaseLogEntry {
304
305 public function getId() {
306 return $this->row->rc_logid;
307 }
308
309 protected function getRawParameters() {
310 return $this->row->rc_params;
311 }
312
313 // LogEntry->
314
315 public function getType() {
316 return $this->row->rc_log_type;
317 }
318
319 public function getSubtype() {
320 return $this->row->rc_log_action;
321 }
322
323 public function getPerformer() {
324 if ( !$this->performer ) {
325 $userId = (int)$this->row->rc_user;
326 if ( $userId !== 0 ) {
327 $this->performer = User::newFromId( $userId );
328 } else {
329 $userText = $this->row->rc_user_text;
330 // Might be an IP, don't validate the username
331 $this->performer = User::newFromName( $userText, false );
332 }
333 }
334
335 return $this->performer;
336 }
337
338 public function getTarget() {
339 $namespace = $this->row->rc_namespace;
340 $page = $this->row->rc_title;
341 $title = Title::makeTitle( $namespace, $page );
342
343 return $title;
344 }
345
346 public function getTimestamp() {
347 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
348 }
349
350 public function getComment() {
351 return $this->row->rc_comment;
352 }
353
354 public function getDeleted() {
355 return $this->row->rc_deleted;
356 }
357 }
358
359 /**
360 * Class for creating log entries manually, for
361 * example to inject them into the database.
362 * @since 1.19
363 */
364 class ManualLogEntry extends LogEntryBase {
365 /** @var string Type of log entry */
366 protected $type;
367
368 /** @var string Sub type of log entry */
369 protected $subtype;
370
371 /** @var array Parameters for log entry */
372 protected $parameters = array();
373
374 /** @var array */
375 protected $relations = array();
376
377 /** @var User Performer of the action for the log entry */
378 protected $performer;
379
380 /** @var Title Target title for the log entry */
381 protected $target;
382
383 /** @var string Timestamp of creation of the log entry */
384 protected $timestamp;
385
386 /** @var string Comment for the log entry */
387 protected $comment = '';
388
389 /** @var int Deletion state of the log entry */
390 protected $deleted;
391
392 /** @var int ID of the log entry */
393 protected $id;
394
395 /** @var bool Whether this is a legacy log entry */
396 protected $legacy = false;
397
398 /**
399 * Constructor.
400 *
401 * @since 1.19
402 *
403 * @param string $type
404 * @param string $subtype
405 */
406 public function __construct( $type, $subtype ) {
407 $this->type = $type;
408 $this->subtype = $subtype;
409 }
410
411 /**
412 * Set extra log parameters.
413 *
414 * You can pass params to the log action message by prefixing the keys with
415 * a number and optional type, using colons to separate the fields. The
416 * numbering should start with number 4, the first three parameters are
417 * hardcoded for every message. Example:
418 * $entry->setParameters(
419 * '4::color' => 'blue',
420 * '5:number:count' => 3000,
421 * 'animal' => 'dog'
422 * );
423 *
424 * @since 1.19
425 *
426 * @param array $parameters Associative array
427 */
428 public function setParameters( $parameters ) {
429 $this->parameters = $parameters;
430 }
431
432 /**
433 * Declare arbitrary tag/value relations to this log entry.
434 * These can be used to filter log entries later on.
435 *
436 * @param array $relations Map of (tag => (list of values|value))
437 * @since 1.22
438 */
439 public function setRelations( array $relations ) {
440 $this->relations = $relations;
441 }
442
443 /**
444 * Set the user that performed the action being logged.
445 *
446 * @since 1.19
447 *
448 * @param User $performer
449 */
450 public function setPerformer( User $performer ) {
451 $this->performer = $performer;
452 }
453
454 /**
455 * Set the title of the object changed.
456 *
457 * @since 1.19
458 *
459 * @param Title $target
460 */
461 public function setTarget( Title $target ) {
462 $this->target = $target;
463 }
464
465 /**
466 * Set the timestamp of when the logged action took place.
467 *
468 * @since 1.19
469 *
470 * @param string $timestamp
471 */
472 public function setTimestamp( $timestamp ) {
473 $this->timestamp = $timestamp;
474 }
475
476 /**
477 * Set a comment associated with the action being logged.
478 *
479 * @since 1.19
480 *
481 * @param string $comment
482 */
483 public function setComment( $comment ) {
484 $this->comment = $comment;
485 }
486
487 /**
488 * Set the 'legacy' flag
489 *
490 * @since 1.25
491 * @param bool $legacy
492 */
493 public function setLegacy( $legacy ) {
494 $this->legacy = $legacy;
495 }
496
497 /**
498 * TODO: document
499 *
500 * @since 1.19
501 *
502 * @param int $deleted
503 */
504 public function setDeleted( $deleted ) {
505 $this->deleted = $deleted;
506 }
507
508 /**
509 * Inserts the entry into the logging table.
510 * @param IDatabase $dbw
511 * @return int ID of the log entry
512 * @throws MWException
513 */
514 public function insert( IDatabase $dbw = null ) {
515 global $wgContLang;
516
517 $dbw = $dbw ?: wfGetDB( DB_MASTER );
518 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
519
520 if ( $this->timestamp === null ) {
521 $this->timestamp = wfTimestampNow();
522 }
523
524 # Trim spaces on user supplied text
525 $comment = trim( $this->getComment() );
526
527 # Truncate for whole multibyte characters.
528 $comment = $wgContLang->truncate( $comment, 255 );
529
530 $data = array(
531 'log_id' => $id,
532 'log_type' => $this->getType(),
533 'log_action' => $this->getSubtype(),
534 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
535 'log_user' => $this->getPerformer()->getId(),
536 'log_user_text' => $this->getPerformer()->getName(),
537 'log_namespace' => $this->getTarget()->getNamespace(),
538 'log_title' => $this->getTarget()->getDBkey(),
539 'log_page' => $this->getTarget()->getArticleID(),
540 'log_comment' => $comment,
541 'log_params' => LogEntryBase::makeParamBlob( $this->getParameters() ),
542 );
543 if ( isset( $this->deleted ) ) {
544 $data['log_deleted'] = $this->deleted;
545 }
546
547 $dbw->insert( 'logging', $data, __METHOD__ );
548 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
549
550 $rows = array();
551 foreach ( $this->relations as $tag => $values ) {
552 if ( !strlen( $tag ) ) {
553 throw new MWException( "Got empty log search tag." );
554 }
555
556 if ( !is_array( $values ) ) {
557 $values = array( $values );
558 }
559
560 foreach ( $values as $value ) {
561 $rows[] = array(
562 'ls_field' => $tag,
563 'ls_value' => $value,
564 'ls_log_id' => $this->id
565 );
566 }
567 }
568 if ( count( $rows ) ) {
569 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
570 }
571
572 return $this->id;
573 }
574
575 /**
576 * Get a RecentChanges object for the log entry
577 * @param int $newId
578 * @return RecentChange
579 * @since 1.23
580 */
581 public function getRecentChange( $newId = 0 ) {
582 $formatter = LogFormatter::newFromEntry( $this );
583 $context = RequestContext::newExtraneousContext( $this->getTarget() );
584 $formatter->setContext( $context );
585
586 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
587 $user = $this->getPerformer();
588 $ip = "";
589 if ( $user->isAnon() ) {
590 /*
591 * "MediaWiki default" and friends may have
592 * no IP address in their name
593 */
594 if ( IP::isIPAddress( $user->getName() ) ) {
595 $ip = $user->getName();
596 }
597 }
598
599 return RecentChange::newLogEntry(
600 $this->getTimestamp(),
601 $logpage,
602 $user,
603 $formatter->getPlainActionText(),
604 $ip,
605 $this->getType(),
606 $this->getSubtype(),
607 $this->getTarget(),
608 $this->getComment(),
609 LogEntryBase::makeParamBlob( $this->getParameters() ),
610 $newId,
611 $formatter->getIRCActionComment() // Used for IRC feeds
612 );
613 }
614
615 /**
616 * Publishes the log entry.
617 * @param int $newId Id of the log entry.
618 * @param string $to One of: rcandudp (default), rc, udp
619 */
620 public function publish( $newId, $to = 'rcandudp' ) {
621 $log = new LogPage( $this->getType() );
622 if ( $log->isRestricted() ) {
623 return;
624 }
625
626 $rc = $this->getRecentChange( $newId );
627
628 if ( $to === 'rc' || $to === 'rcandudp' ) {
629 $rc->save( 'pleasedontudp' );
630 }
631
632 if ( $to === 'udp' || $to === 'rcandudp' ) {
633 $rc->notifyRCFeeds();
634 }
635 }
636
637 // LogEntry->
638
639 public function getType() {
640 return $this->type;
641 }
642
643 public function getSubtype() {
644 return $this->subtype;
645 }
646
647 public function getParameters() {
648 return $this->parameters;
649 }
650
651 /**
652 * @return User
653 */
654 public function getPerformer() {
655 return $this->performer;
656 }
657
658 /**
659 * @return Title
660 */
661 public function getTarget() {
662 return $this->target;
663 }
664
665 public function getTimestamp() {
666 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
667
668 return wfTimestamp( TS_MW, $ts );
669 }
670
671 public function getComment() {
672 return $this->comment;
673 }
674
675 /**
676 * @since 1.25
677 * @return bool
678 */
679 public function isLegacy() {
680 return $this->legacy;
681 }
682
683 public function getDeleted() {
684 return (int)$this->deleted;
685 }
686 }