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