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