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