LogEntry: Boring whitespace and comment changes, some documentation too
[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 = array( 'logging', 'user' );
172 $fields = array(
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 = array(
181 // IPs don't have an entry in user table
182 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
183 );
184
185 return array(
186 'tables' => $tables,
187 'fields' => $fields,
188 'conds' => array(),
189 'options' => array(),
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 bool Whether the parameters for this log entry are stored in new or old format. */
217 protected $legacy;
218
219 protected function __construct( $row ) {
220 $this->row = $row;
221 }
222
223 /**
224 * Returns the unique database id.
225 *
226 * @return int
227 */
228 public function getId() {
229 return (int)$this->row->log_id;
230 }
231
232 /**
233 * Returns whatever is stored in the database field.
234 *
235 * @return string
236 */
237 protected function getRawParameters() {
238 return $this->row->log_params;
239 }
240
241 public function isLegacy() {
242 // This does the check
243 $this->getParameters();
244 return $this->legacy;
245 }
246
247 public function getType() {
248 return $this->row->log_type;
249 }
250
251 public function getSubtype() {
252 return $this->row->log_action;
253 }
254
255 public function getParameters() {
256 if ( !isset( $this->params ) ) {
257 $blob = $this->getRawParameters();
258 MediaWiki\suppressWarnings();
259 $params = LogEntryBase::extractParams( $blob );
260 MediaWiki\restoreWarnings();
261 if ( $params !== false ) {
262 $this->params = $params;
263 $this->legacy = false;
264 } else {
265 $this->params = LogPage::extractParams( $blob );
266 $this->legacy = true;
267 }
268 }
269
270 return $this->params;
271 }
272
273 public function getPerformer() {
274 if ( !$this->performer ) {
275 $userId = (int)$this->row->log_user;
276 if ( $userId !== 0 ) {
277 // logged-in users
278 if ( isset( $this->row->user_name ) ) {
279 $this->performer = User::newFromRow( $this->row );
280 } else {
281 $this->performer = User::newFromId( $userId );
282 }
283 } else {
284 // IP users
285 $userText = $this->row->log_user_text;
286 $this->performer = User::newFromName( $userText, false );
287 }
288 }
289
290 return $this->performer;
291 }
292
293 public function getTarget() {
294 $namespace = $this->row->log_namespace;
295 $page = $this->row->log_title;
296 $title = Title::makeTitle( $namespace, $page );
297
298 return $title;
299 }
300
301 public function getTimestamp() {
302 return wfTimestamp( TS_MW, $this->row->log_timestamp );
303 }
304
305 public function getComment() {
306 return $this->row->log_comment;
307 }
308
309 public function getDeleted() {
310 return $this->row->log_deleted;
311 }
312 }
313
314 class RCDatabaseLogEntry extends DatabaseLogEntry {
315
316 public function getId() {
317 return $this->row->rc_logid;
318 }
319
320 protected function getRawParameters() {
321 return $this->row->rc_params;
322 }
323
324 public function getType() {
325 return $this->row->rc_log_type;
326 }
327
328 public function getSubtype() {
329 return $this->row->rc_log_action;
330 }
331
332 public function getPerformer() {
333 if ( !$this->performer ) {
334 $userId = (int)$this->row->rc_user;
335 if ( $userId !== 0 ) {
336 $this->performer = User::newFromId( $userId );
337 } else {
338 $userText = $this->row->rc_user_text;
339 // Might be an IP, don't validate the username
340 $this->performer = User::newFromName( $userText, false );
341 }
342 }
343
344 return $this->performer;
345 }
346
347 public function getTarget() {
348 $namespace = $this->row->rc_namespace;
349 $page = $this->row->rc_title;
350 $title = Title::makeTitle( $namespace, $page );
351
352 return $title;
353 }
354
355 public function getTimestamp() {
356 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
357 }
358
359 public function getComment() {
360 return $this->row->rc_comment;
361 }
362
363 public function getDeleted() {
364 return $this->row->rc_deleted;
365 }
366 }
367
368 /**
369 * Class for creating log entries manually, to inject them into the database.
370 *
371 * @since 1.19
372 */
373 class ManualLogEntry extends LogEntryBase {
374 /** @var string Type of log entry */
375 protected $type;
376
377 /** @var string Sub type of log entry */
378 protected $subtype;
379
380 /** @var array Parameters for log entry */
381 protected $parameters = array();
382
383 /** @var array */
384 protected $relations = array();
385
386 /** @var User Performer of the action for the log entry */
387 protected $performer;
388
389 /** @var Title Target title for the log entry */
390 protected $target;
391
392 /** @var string Timestamp of creation of the log entry */
393 protected $timestamp;
394
395 /** @var string Comment for the log entry */
396 protected $comment = '';
397
398 /** @var int A rev id associated to the log entry */
399 protected $revId = 0;
400
401 /** @var int Deletion state of the log entry */
402 protected $deleted;
403
404 /** @var int ID of the log entry */
405 protected $id;
406
407 /** @var bool Whether this is a legacy log entry */
408 protected $legacy = false;
409
410 /**
411 * Constructor.
412 *
413 * @since 1.19
414 * @param string $type
415 * @param string $subtype
416 */
417 public function __construct( $type, $subtype ) {
418 $this->type = $type;
419 $this->subtype = $subtype;
420 }
421
422 /**
423 * Set extra log parameters.
424 *
425 * You can pass params to the log action message by prefixing the keys with
426 * a number and optional type, using colons to separate the fields. The
427 * numbering should start with number 4, the first three parameters are
428 * hardcoded for every message.
429 *
430 * If you want to store stuff that should not be available in messages, don't
431 * prefix the array key with a number and don't use the colons.
432 *
433 * Example:
434 * $entry->setParameters(
435 * '4::color' => 'blue',
436 * '5:number:count' => 3000,
437 * 'animal' => 'dog'
438 * );
439 *
440 * @since 1.19
441 * @param array $parameters Associative array
442 */
443 public function setParameters( $parameters ) {
444 $this->parameters = $parameters;
445 }
446
447 /**
448 * Declare arbitrary tag/value relations to this log entry.
449 * These can be used to filter log entries later on.
450 *
451 * @param array $relations Map of (tag => (list of values|value))
452 * @since 1.22
453 */
454 public function setRelations( array $relations ) {
455 $this->relations = $relations;
456 }
457
458 /**
459 * Set the user that performed the action being logged.
460 *
461 * @since 1.19
462 * @param User $performer
463 */
464 public function setPerformer( User $performer ) {
465 $this->performer = $performer;
466 }
467
468 /**
469 * Set the title of the object changed.
470 *
471 * @since 1.19
472 * @param Title $target
473 */
474 public function setTarget( Title $target ) {
475 $this->target = $target;
476 }
477
478 /**
479 * Set the timestamp of when the logged action took place.
480 *
481 * @since 1.19
482 * @param string $timestamp
483 */
484 public function setTimestamp( $timestamp ) {
485 $this->timestamp = $timestamp;
486 }
487
488 /**
489 * Set a comment associated with the action being logged.
490 *
491 * @since 1.19
492 * @param string $comment
493 */
494 public function setComment( $comment ) {
495 $this->comment = $comment;
496 }
497
498 /**
499 * Set an associated revision id.
500 *
501 * For example, the ID of the revision that was inserted to mark a page move
502 * or protection, file upload, etc.
503 *
504 * @since 1.27
505 * @param int $revId
506 */
507 public function setAssociatedRevId( $revId ) {
508 $this->revId = $revId;
509 }
510
511 /**
512 * Set the 'legacy' flag
513 *
514 * @since 1.25
515 * @param bool $legacy
516 */
517 public function setLegacy( $legacy ) {
518 $this->legacy = $legacy;
519 }
520
521 /**
522 * Set the 'deleted' flag.
523 *
524 * @since 1.19
525 * @param int $deleted One of LogPage::DELETED_* bitfield constants
526 */
527 public function setDeleted( $deleted ) {
528 $this->deleted = $deleted;
529 }
530
531 /**
532 * Insert the entry into the `logging` table.
533 *
534 * @param IDatabase $dbw
535 * @return int ID of the log entry
536 * @throws MWException
537 */
538 public function insert( IDatabase $dbw = null ) {
539 global $wgContLang;
540
541 $dbw = $dbw ?: wfGetDB( DB_MASTER );
542 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
543
544 if ( $this->timestamp === null ) {
545 $this->timestamp = wfTimestampNow();
546 }
547
548 // Trim spaces on user supplied text
549 $comment = trim( $this->getComment() );
550
551 // Truncate for whole multibyte characters.
552 $comment = $wgContLang->truncate( $comment, 255 );
553
554 $data = array(
555 'log_id' => $id,
556 'log_type' => $this->getType(),
557 'log_action' => $this->getSubtype(),
558 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
559 'log_user' => $this->getPerformer()->getId(),
560 'log_user_text' => $this->getPerformer()->getName(),
561 'log_namespace' => $this->getTarget()->getNamespace(),
562 'log_title' => $this->getTarget()->getDBkey(),
563 'log_page' => $this->getTarget()->getArticleID(),
564 'log_comment' => $comment,
565 'log_params' => LogEntryBase::makeParamBlob( $this->getParameters() ),
566 );
567 if ( isset( $this->deleted ) ) {
568 $data['log_deleted'] = $this->deleted;
569 }
570
571 $dbw->insert( 'logging', $data, __METHOD__ );
572 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
573
574 $rows = array();
575 foreach ( $this->relations as $tag => $values ) {
576 if ( !strlen( $tag ) ) {
577 throw new MWException( "Got empty log search tag." );
578 }
579
580 if ( !is_array( $values ) ) {
581 $values = array( $values );
582 }
583
584 foreach ( $values as $value ) {
585 $rows[] = array(
586 'ls_field' => $tag,
587 'ls_value' => $value,
588 'ls_log_id' => $this->id
589 );
590 }
591 }
592 if ( count( $rows ) ) {
593 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
594 }
595
596 return $this->id;
597 }
598
599 /**
600 * Get a RecentChanges object for the log entry
601 *
602 * @param int $newId
603 * @return RecentChange
604 * @since 1.23
605 */
606 public function getRecentChange( $newId = 0 ) {
607 $formatter = LogFormatter::newFromEntry( $this );
608 $context = RequestContext::newExtraneousContext( $this->getTarget() );
609 $formatter->setContext( $context );
610
611 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
612 $user = $this->getPerformer();
613 $ip = "";
614 if ( $user->isAnon() ) {
615 // "MediaWiki default" and friends may have
616 // no IP address in their name
617 if ( IP::isIPAddress( $user->getName() ) ) {
618 $ip = $user->getName();
619 }
620 }
621
622 return RecentChange::newLogEntry(
623 $this->getTimestamp(),
624 $logpage,
625 $user,
626 $formatter->getPlainActionText(),
627 $ip,
628 $this->getType(),
629 $this->getSubtype(),
630 $this->getTarget(),
631 $this->getComment(),
632 LogEntryBase::makeParamBlob( $this->getParameters() ),
633 $newId,
634 $formatter->getIRCActionComment(), // Used for IRC feeds
635 $this->getAssociatedRevId() // Used for e.g. moves and uploads
636 );
637 }
638
639 /**
640 * Publish the log entry.
641 *
642 * @param int $newId Id of the log entry.
643 * @param string $to One of: rcandudp (default), rc, udp
644 */
645 public function publish( $newId, $to = 'rcandudp' ) {
646 $log = new LogPage( $this->getType() );
647 if ( $log->isRestricted() ) {
648 return;
649 }
650
651 $rc = $this->getRecentChange( $newId );
652
653 if ( $to === 'rc' || $to === 'rcandudp' ) {
654 $rc->save( 'pleasedontudp' );
655 }
656
657 if ( $to === 'udp' || $to === 'rcandudp' ) {
658 $rc->notifyRCFeeds();
659 }
660
661 // Log the autopatrol if an associated rev id was passed
662 if ( $this->getAssociatedRevId() > 0 &&
663 $rc->getAttribute( 'rc_patrolled' ) === 1 ) {
664 PatrolLog::record( $rc, true, $this->getPerformer() );
665 }
666 }
667
668 public function getType() {
669 return $this->type;
670 }
671
672 public function getSubtype() {
673 return $this->subtype;
674 }
675
676 public function getParameters() {
677 return $this->parameters;
678 }
679
680 /**
681 * @return User
682 */
683 public function getPerformer() {
684 return $this->performer;
685 }
686
687 /**
688 * @return Title
689 */
690 public function getTarget() {
691 return $this->target;
692 }
693
694 public function getTimestamp() {
695 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
696
697 return wfTimestamp( TS_MW, $ts );
698 }
699
700 public function getComment() {
701 return $this->comment;
702 }
703
704 /**
705 * @since 1.27
706 * @return int
707 */
708 public function getAssociatedRevId() {
709 return $this->revId;
710 }
711
712 /**
713 * @since 1.25
714 * @return bool
715 */
716 public function isLegacy() {
717 return $this->legacy;
718 }
719
720 public function getDeleted() {
721 return (int)$this->deleted;
722 }
723 }