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