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