f279ee5349249827d191e0743c7924289e0d18dc
[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 */
101 public function isLegacy() {
102 return false;
103 }
104
105 }
106
107 /**
108 * This class wraps around database result row.
109 * @since 1.19
110 */
111 class DatabaseLogEntry extends LogEntryBase {
112 // Static->
113
114 /**
115 * Returns array of information that is needed for querying
116 * log entries. Array contains the following keys:
117 * tables, fields, conds, options and join_conds
118 * @return array
119 */
120 public static function getSelectQueryData() {
121 $tables = array( 'logging', 'user' );
122 $fields = array(
123 'log_id', 'log_type', 'log_action', 'log_timestamp',
124 'log_user', 'log_user_text',
125 'log_namespace', 'log_title', // unused log_page
126 'log_comment', 'log_params', 'log_deleted',
127 'user_id', 'user_name', 'user_editcount',
128 );
129
130 $conds = array();
131
132 $joins = array(
133 // IP's don't have an entry in user table
134 'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
135 );
136
137 return array(
138 'tables' => $tables,
139 'fields' => $fields,
140 'conds' => array(),
141 'options' => array(),
142 'join_conds' => $joins,
143 );
144 }
145
146 /**
147 * Constructs new LogEntry from database result row.
148 * Supports rows from both logging and recentchanges table.
149 * @param $row
150 * @return DatabaseLogEntry
151 */
152 public static function newFromRow( $row ) {
153 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
154 return new RCDatabaseLogEntry( (object) $row );
155 } else {
156 return new self( $row );
157 }
158 }
159
160 // Non-static->
161
162 /// Database result row.
163 protected $row;
164
165 protected function __construct( $row ) {
166 $this->row = $row;
167 }
168
169 /**
170 * Returns the unique database id.
171 * @return int
172 */
173 public function getId() {
174 return (int)$this->row->log_id;
175 }
176
177 /**
178 * Returns whatever is stored in the database field.
179 * @return string
180 */
181 protected function getRawParameters() {
182 return $this->row->log_params;
183 }
184
185 // LogEntryBase->
186
187 public function isLegacy() {
188 // This does the check
189 $this->getParameters();
190 return $this->legacy;
191 }
192
193 // LogEntry->
194
195 public function getType() {
196 return $this->row->log_type;
197 }
198
199 public function getSubtype() {
200 return $this->row->log_action;
201 }
202
203 public function getParameters() {
204 if ( !isset( $this->params ) ) {
205 $blob = $this->getRawParameters();
206 wfSuppressWarnings();
207 $params = unserialize( $blob );
208 wfRestoreWarnings();
209 if ( $params !== false ) {
210 $this->params = $params;
211 $this->legacy = false;
212 } else {
213 $params = FormatJson::decode( $blob, true /* array */ );
214 if ( $params !== null ) {
215 $this->params = $params;
216 $this->legacy = false;
217 } else {
218 $this->params = explode( "\n", $blob );
219 $this->legacy = true;
220 }
221 }
222 }
223 return $this->params;
224 }
225
226 public function getPerformer() {
227 $userId = (int) $this->row->log_user;
228 if ( $userId !== 0 ) {
229 return User::newFromRow( $this->row );
230 } else {
231 $userText = $this->row->log_user_text;
232 return User::newFromName( $userText, false );
233 }
234 }
235
236 public function getTarget() {
237 $namespace = $this->row->log_namespace;
238 $page = $this->row->log_title;
239 $title = Title::makeTitle( $namespace, $page );
240 return $title;
241 }
242
243 public function getTimestamp() {
244 return wfTimestamp( TS_MW, $this->row->log_timestamp );
245 }
246
247 public function getComment() {
248 return $this->row->log_comment;
249 }
250
251 public function getDeleted() {
252 return $this->row->log_deleted;
253 }
254
255 }
256
257 class RCDatabaseLogEntry extends DatabaseLogEntry {
258
259 public function getId() {
260 return $this->row->rc_logid;
261 }
262
263 protected function getRawParameters() {
264 return $this->row->rc_params;
265 }
266
267 // LogEntry->
268
269 public function getType() {
270 return $this->row->rc_log_type;
271 }
272
273 public function getSubtype() {
274 return $this->row->rc_log_action;
275 }
276
277 public function getPerformer() {
278 $userId = (int) $this->row->rc_user;
279 if ( $userId !== 0 ) {
280 return User::newFromId( $userId );
281 } else {
282 $userText = $this->row->rc_user_text;
283 // Might be an IP, don't validate the username
284 return User::newFromName( $userText, false );
285 }
286 }
287
288 public function getTarget() {
289 $namespace = $this->row->rc_namespace;
290 $page = $this->row->rc_title;
291 $title = Title::makeTitle( $namespace, $page );
292 return $title;
293 }
294
295 public function getTimestamp() {
296 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
297 }
298
299 public function getComment() {
300 return $this->row->rc_comment;
301 }
302
303 public function getDeleted() {
304 return $this->row->rc_deleted;
305 }
306
307 }
308
309 /**
310 * Class for creating log entries manually, for
311 * example to inject them into the database.
312 * @since 1.19
313 */
314 class ManualLogEntry extends LogEntryBase {
315 protected $type; ///!< @var string
316 protected $subtype; ///!< @var string
317 protected $parameters = array(); ///!< @var array
318 protected $performer; ///!< @var User
319 protected $target; ///!< @var Title
320 protected $timestamp; ///!< @var string
321 protected $comment; ///!< @var string
322 protected $deleted; ///!< @var int
323
324 public function __construct( $type, $subtype ) {
325 $this->type = $type;
326 $this->subtype = $subtype;
327 }
328
329 /**
330 * Set extra log parameters.
331 * You can pass params to the log action message
332 * by prefixing the keys with a number and colon.
333 * The numbering should start with number 4, the
334 * first three parameters are hardcoded for every
335 * message. Example:
336 * $entry->setParameters(
337 * '4:color' => 'blue',
338 * 'animal' => 'dog'
339 * );
340 * @param $parameters Associative array
341 */
342 public function setParameters( $parameters ) {
343 $this->parameters = $parameters;
344 }
345
346 public function setPerformer( User $performer ) {
347 $this->performer = $performer;
348 }
349
350 public function setTarget( Title $target ) {
351 $this->target = $target;
352 }
353
354 public function setTimestamp( $timestamp ) {
355 $this->timestamp = $timestamp;
356 }
357
358 public function setComment( $comment ) {
359 $this->comment = $comment;
360 }
361
362 public function setDeleted( $deleted ) {
363 $this->deleted = $deleted;
364 }
365
366 /**
367 * Inserts the entry into the logging table.
368 * @return int If of the log entry
369 */
370 public function insert() {
371 global $wgLogRestrictions;
372
373 $dbw = wfGetDB( DB_MASTER );
374 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
375
376 if ( $this->timestamp === null ) {
377 $this->timestamp = wfTimestampNow();
378 }
379
380 $data = array(
381 'log_id' => $id,
382 'log_type' => $this->getType(),
383 'log_action' => $this->getSubtype(),
384 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
385 'log_user' => $this->getPerformer()->getId(),
386 'log_user_text' => $this->getPerformer()->getName(),
387 'log_namespace' => $this->getTarget()->getNamespace(),
388 'log_title' => $this->getTarget()->getDBkey(),
389 'log_page' => $this->getTarget()->getArticleId(),
390 'log_comment' => $this->getComment(),
391 'log_params' => serialize( (array) $this->getParameters() ),
392 );
393 $dbw->insert( 'logging', $data, __METHOD__ );
394 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
395 return $this->id;
396 }
397
398 /**
399 * Publishes the log entry.
400 * @param $newId int id of the log entry.
401 * @param $to string: rcandudp (default), rc, udp
402 */
403 public function publish( $newId, $to = 'rcandudp' ) {
404 $log = new LogPage( $this->getType() );
405 if ( $log->isRestricted() ) {
406 return;
407 }
408
409 $formatter = LogFormatter::newFromEntry( $this );
410 $context = RequestContext::newExtraneousContext( $this->getTarget() );
411 $formatter->setContext( $context );
412
413 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
414 $user = $this->getPerformer();
415 $rc = RecentChange::newLogEntry(
416 $this->getTimestamp(),
417 $logpage,
418 $user,
419 $formatter->getPlainActionText(), // Used for IRC feeds
420 $user->isAnon() ? $user->getName() : '',
421 $this->getType(),
422 $this->getSubtype(),
423 $this->getTarget(),
424 $this->getComment(),
425 serialize( (array) $this->getParameters() ),
426 $newId
427 );
428
429 if ( $to === 'rc' || $to === 'rcandudp' ) {
430 $rc->save();
431 }
432
433 if ( $to === 'udp' || $to === 'rcandudp' ) {
434 $rc->notifyRC2UDP();
435 }
436 }
437
438 // LogEntry->
439
440 public function getType() {
441 return $this->type;
442 }
443
444 public function getSubtype() {
445 return $this->subtype;
446 }
447
448 public function getParameters() {
449 return $this->parameters;
450 }
451
452 public function getPerformer() {
453 return $this->performer;
454 }
455
456 public function getTarget() {
457 return $this->target;
458 }
459
460 public function getTimestamp() {
461 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
462 return wfTimestamp( TS_MW, $ts );
463 }
464
465 public function getComment() {
466 return $this->comment;
467 }
468
469 public function getDeleted() {
470 return (int) $this->deleted;
471 }
472
473 }