Merge "Test to make sure numRows() calls don't show unrelated errors"
[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 /**
38 * The main log type.
39 * @return string
40 */
41 public function getType();
42
43 /**
44 * The log subtype.
45 * @return string
46 */
47 public function getSubtype();
48
49 /**
50 * The full logtype in format maintype/subtype.
51 * @return string
52 */
53 public function getFullType();
54
55 /**
56 * Get the extra parameters stored for this message.
57 * @return array
58 */
59 public function getParameters();
60
61 /**
62 * Get the user for performed this action.
63 * @return User
64 */
65 public function getPerformer();
66
67 /**
68 * Get the target page of this action.
69 * @return Title
70 */
71 public function getTarget();
72
73 /**
74 * Get the timestamp when the action was executed.
75 * @return string
76 */
77 public function getTimestamp();
78
79 /**
80 * Get the user provided comment.
81 * @return string
82 */
83 public function getComment();
84
85 /**
86 * Get the access restriction.
87 * @return string
88 */
89 public function getDeleted();
90
91 /**
92 * @param $field Integer: one of LogPage::DELETED_* bitfield constants
93 * @return Boolean
94 */
95 public function isDeleted( $field );
96 }
97
98 /**
99 * Extends the LogEntryInterface with some basic functionality
100 * @since 1.19
101 */
102 abstract class LogEntryBase implements LogEntry {
103
104 public function getFullType() {
105 return $this->getType() . '/' . $this->getSubtype();
106 }
107
108 public function isDeleted( $field ) {
109 return ( $this->getDeleted() & $field ) === $field;
110 }
111
112 /**
113 * Whether the parameters for this log are stored in new or
114 * old format.
115 * @return bool
116 */
117 public function isLegacy() {
118 return false;
119 }
120
121 }
122
123 /**
124 * This class wraps around database result row.
125 * @since 1.19
126 */
127 class DatabaseLogEntry extends LogEntryBase {
128 // Static->
129
130 /**
131 * Returns array of information that is needed for querying
132 * log entries. Array contains the following keys:
133 * tables, fields, conds, options and join_conds
134 * @return array
135 */
136 public static function getSelectQueryData() {
137 $tables = array( 'logging', 'user' );
138 $fields = array(
139 'log_id', 'log_type', 'log_action', 'log_timestamp',
140 'log_user', 'log_user_text',
141 'log_namespace', 'log_title', // unused log_page
142 'log_comment', 'log_params', 'log_deleted',
143 'user_id', 'user_name', 'user_editcount',
144 );
145
146 $joins = array(
147 // IP's don't have an entry in user table
148 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
149 );
150
151 return array(
152 'tables' => $tables,
153 'fields' => $fields,
154 'conds' => array(),
155 'options' => array(),
156 'join_conds' => $joins,
157 );
158 }
159
160 /**
161 * Constructs new LogEntry from database result row.
162 * Supports rows from both logging and recentchanges table.
163 * @param $row
164 * @return DatabaseLogEntry
165 */
166 public static function newFromRow( $row ) {
167 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
168 return new RCDatabaseLogEntry( (object) $row );
169 } else {
170 return new self( $row );
171 }
172 }
173
174 // Non-static->
175
176 /// Database result row.
177 protected $row;
178 protected $performer;
179
180 protected function __construct( $row ) {
181 $this->row = $row;
182 }
183
184 /**
185 * Returns the unique database id.
186 * @return int
187 */
188 public function getId() {
189 return (int)$this->row->log_id;
190 }
191
192 /**
193 * Returns whatever is stored in the database field.
194 * @return string
195 */
196 protected function getRawParameters() {
197 return $this->row->log_params;
198 }
199
200 // LogEntryBase->
201
202 public function isLegacy() {
203 // This does the check
204 $this->getParameters();
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 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 return $this->performer;
250 }
251
252 public function getTarget() {
253 $namespace = $this->row->log_namespace;
254 $page = $this->row->log_title;
255 $title = Title::makeTitle( $namespace, $page );
256 return $title;
257 }
258
259 public function getTimestamp() {
260 return wfTimestamp( TS_MW, $this->row->log_timestamp );
261 }
262
263 public function getComment() {
264 return $this->row->log_comment;
265 }
266
267 public function getDeleted() {
268 return $this->row->log_deleted;
269 }
270
271 }
272
273 class RCDatabaseLogEntry extends DatabaseLogEntry {
274
275 public function getId() {
276 return $this->row->rc_logid;
277 }
278
279 protected function getRawParameters() {
280 return $this->row->rc_params;
281 }
282
283 // LogEntry->
284
285 public function getType() {
286 return $this->row->rc_log_type;
287 }
288
289 public function getSubtype() {
290 return $this->row->rc_log_action;
291 }
292
293 public function getPerformer() {
294 if( !$this->performer ) {
295 $userId = (int) $this->row->rc_user;
296 if ( $userId !== 0 ) {
297 $this->performer = User::newFromId( $userId );
298 } else {
299 $userText = $this->row->rc_user_text;
300 // Might be an IP, don't validate the username
301 $this->performer = User::newFromName( $userText, false );
302 }
303 }
304 return $this->performer;
305 }
306
307 public function getTarget() {
308 $namespace = $this->row->rc_namespace;
309 $page = $this->row->rc_title;
310 $title = Title::makeTitle( $namespace, $page );
311 return $title;
312 }
313
314 public function getTimestamp() {
315 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
316 }
317
318 public function getComment() {
319 return $this->row->rc_comment;
320 }
321
322 public function getDeleted() {
323 return $this->row->rc_deleted;
324 }
325
326 }
327
328 /**
329 * Class for creating log entries manually, for
330 * example to inject them into the database.
331 * @since 1.19
332 */
333 class ManualLogEntry extends LogEntryBase {
334 protected $type; ///!< @var string
335 protected $subtype; ///!< @var string
336 protected $parameters = array(); ///!< @var array
337 protected $performer; ///!< @var User
338 protected $target; ///!< @var Title
339 protected $timestamp; ///!< @var string
340 protected $comment = ''; ///!< @var string
341 protected $deleted; ///!< @var int
342
343 /**
344 * Constructor.
345 *
346 * @since 1.19
347 *
348 * @param string $type
349 * @param string $subtype
350 */
351 public function __construct( $type, $subtype ) {
352 $this->type = $type;
353 $this->subtype = $subtype;
354 }
355
356 /**
357 * Set extra log parameters.
358 * You can pass params to the log action message
359 * by prefixing the keys with a number and colon.
360 * The numbering should start with number 4, the
361 * first three parameters are hardcoded for every
362 * message. Example:
363 * $entry->setParameters(
364 * '4:color' => 'blue',
365 * 'animal' => 'dog'
366 * );
367 *
368 * @since 1.19
369 *
370 * @param array $parameters Associative array
371 */
372 public function setParameters( $parameters ) {
373 $this->parameters = $parameters;
374 }
375
376 /**
377 * Set the user that performed the action being logged.
378 *
379 * @since 1.19
380 *
381 * @param User $performer
382 */
383 public function setPerformer( User $performer ) {
384 $this->performer = $performer;
385 }
386
387 /**
388 * Set the title of the object changed.
389 *
390 * @since 1.19
391 *
392 * @param Title $target
393 */
394 public function setTarget( Title $target ) {
395 $this->target = $target;
396 }
397
398 /**
399 * Set the timestamp of when the logged action took place.
400 *
401 * @since 1.19
402 *
403 * @param string $timestamp
404 */
405 public function setTimestamp( $timestamp ) {
406 $this->timestamp = $timestamp;
407 }
408
409 /**
410 * Set a comment associated with the action being logged.
411 *
412 * @since 1.19
413 *
414 * @param string $comment
415 */
416 public function setComment( $comment ) {
417 $this->comment = $comment;
418 }
419
420 /**
421 * TODO: document
422 *
423 * @since 1.19
424 *
425 * @param integer $deleted
426 */
427 public function setDeleted( $deleted ) {
428 $this->deleted = $deleted;
429 }
430
431 /**
432 * Inserts the entry into the logging table.
433 * @return int If of the log entry
434 */
435 public function insert() {
436 global $wgContLang;
437
438 $dbw = wfGetDB( DB_MASTER );
439 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
440
441 if ( $this->timestamp === null ) {
442 $this->timestamp = wfTimestampNow();
443 }
444
445 # Trim spaces on user supplied text
446 $comment = trim( $this->getComment() );
447
448 # Truncate for whole multibyte characters.
449 $comment = $wgContLang->truncate( $comment, 255 );
450
451 $data = array(
452 'log_id' => $id,
453 'log_type' => $this->getType(),
454 'log_action' => $this->getSubtype(),
455 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
456 'log_user' => $this->getPerformer()->getId(),
457 'log_user_text' => $this->getPerformer()->getName(),
458 'log_namespace' => $this->getTarget()->getNamespace(),
459 'log_title' => $this->getTarget()->getDBkey(),
460 'log_page' => $this->getTarget()->getArticleID(),
461 'log_comment' => $comment,
462 'log_params' => serialize( (array) $this->getParameters() ),
463 );
464 $dbw->insert( 'logging', $data, __METHOD__ );
465 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
466 return $this->id;
467 }
468
469 /**
470 * Publishes the log entry.
471 * @param int $newId id of the log entry.
472 * @param string $to rcandudp (default), rc, udp
473 */
474 public function publish( $newId, $to = 'rcandudp' ) {
475 $log = new LogPage( $this->getType() );
476 if ( $log->isRestricted() ) {
477 return;
478 }
479
480 $formatter = LogFormatter::newFromEntry( $this );
481 $context = RequestContext::newExtraneousContext( $this->getTarget() );
482 $formatter->setContext( $context );
483
484 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
485 $user = $this->getPerformer();
486 $ip = "";
487 if ( $user->isAnon() ) {
488 /*
489 * "MediaWiki default" and friends may have
490 * no IP address in their name
491 */
492 if ( IP::isIPAddress( $user->getName() ) ) {
493 $ip = $user->getName();
494 }
495 }
496 $rc = RecentChange::newLogEntry(
497 $this->getTimestamp(),
498 $logpage,
499 $user,
500 $formatter->getPlainActionText(),
501 $ip,
502 $this->getType(),
503 $this->getSubtype(),
504 $this->getTarget(),
505 $this->getComment(),
506 serialize( (array) $this->getParameters() ),
507 $newId,
508 $formatter->getIRCActionComment() // Used for IRC feeds
509 );
510
511 if ( $to === 'rc' || $to === 'rcandudp' ) {
512 $rc->save( 'pleasedontudp' );
513 }
514
515 if ( $to === 'udp' || $to === 'rcandudp' ) {
516 $rc->notifyRC2UDP();
517 }
518 }
519
520 // LogEntry->
521
522 public function getType() {
523 return $this->type;
524 }
525
526 public function getSubtype() {
527 return $this->subtype;
528 }
529
530 public function getParameters() {
531 return $this->parameters;
532 }
533
534 /**
535 * @return User
536 */
537 public function getPerformer() {
538 return $this->performer;
539 }
540
541 /**
542 * @return Title
543 */
544 public function getTarget() {
545 return $this->target;
546 }
547
548 public function getTimestamp() {
549 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
550 return wfTimestamp( TS_MW, $ts );
551 }
552
553 public function getComment() {
554 return $this->comment;
555 }
556
557 public function getDeleted() {
558 return (int) $this->deleted;
559 }
560
561 }