Merge "Store page_id in logging table for deletions and make queryable"
[lhc/web/wiklou.git] / includes / changes / RecentChange.php
1 <?php
2 /**
3 * Utility class for creating and accessing recent change entries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Utility class for creating new RC entries
25 *
26 * mAttribs:
27 * rc_id id of the row in the recentchanges table
28 * rc_timestamp time the entry was made
29 * rc_namespace namespace #
30 * rc_title non-prefixed db key
31 * rc_type is new entry, used to determine whether updating is necessary
32 * rc_source string representation of change source
33 * rc_minor is minor
34 * rc_cur_id page_id of associated page entry
35 * rc_user user id who made the entry
36 * rc_user_text user name who made the entry
37 * rc_comment edit summary
38 * rc_this_oldid rev_id associated with this entry (or zero)
39 * rc_last_oldid rev_id associated with the entry before this one (or zero)
40 * rc_bot is bot, hidden
41 * rc_ip IP address of the user in dotted quad notation
42 * rc_new obsolete, use rc_type==RC_NEW
43 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
44 * rc_old_len integer byte length of the text before the edit
45 * rc_new_len the same after the edit
46 * rc_deleted partial deletion
47 * rc_logid the log_id value for this log entry (or zero)
48 * rc_log_type the log type (or null)
49 * rc_log_action the log action (or null)
50 * rc_params log params
51 *
52 * mExtra:
53 * prefixedDBkey prefixed db key, used by external app via msg queue
54 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
55 * oldSize text size before the change
56 * newSize text size after the change
57 * pageStatus status of the page: created, deleted, moved, restored, changed
58 *
59 * temporary: not stored in the database
60 * notificationtimestamp
61 * numberofWatchingusers
62 */
63 class RecentChange {
64 // Constants for the rc_source field. Extensions may also have
65 // their own source constants.
66 const SRC_EDIT = 'mw.edit';
67 const SRC_NEW = 'mw.new';
68 const SRC_LOG = 'mw.log';
69 const SRC_EXTERNAL = 'mw.external'; // obsolete
70
71 public $mAttribs = array();
72 public $mExtra = array();
73
74 /**
75 * @var Title
76 */
77 public $mTitle = false;
78
79 /**
80 * @var User
81 */
82 private $mPerformer = false;
83
84 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
85 public $notificationtimestamp;
86
87 /**
88 * @var int Line number of recent change. Default -1.
89 */
90 public $counter = -1;
91
92 # Factory methods
93
94 /**
95 * @param mixed $row
96 * @return RecentChange
97 */
98 public static function newFromRow( $row ) {
99 $rc = new RecentChange;
100 $rc->loadFromRow( $row );
101
102 return $rc;
103 }
104
105 /**
106 * Parsing text to RC_* constants
107 * @since 1.24
108 * @param string|array $type
109 * @throws MWException
110 * @return int|array RC_TYPE
111 */
112 public static function parseToRCType( $type ) {
113 if ( is_array( $type ) ) {
114 $retval = array();
115 foreach ( $type as $t ) {
116 $retval[] = RecentChange::parseToRCType( $t );
117 }
118
119 return $retval;
120 }
121
122 switch ( $type ) {
123 case 'edit':
124 return RC_EDIT;
125 case 'new':
126 return RC_NEW;
127 case 'log':
128 return RC_LOG;
129 case 'external':
130 return RC_EXTERNAL;
131 default:
132 throw new MWException( "Unknown type '$type'" );
133 }
134 }
135
136 /**
137 * Parsing RC_* constants to human-readable test
138 * @since 1.24
139 * @param int $rc_type
140 * @return string $type
141 */
142 public static function parseFromRCType( $rcType ) {
143 switch ( $rcType ) {
144 case RC_EDIT:
145 $type = 'edit';
146 break;
147 case RC_NEW:
148 $type = 'new';
149 break;
150 case RC_MOVE:
151 $type = 'move';
152 break;
153 case RC_LOG:
154 $type = 'log';
155 break;
156 case RC_EXTERNAL:
157 $type = 'external';
158 break;
159 case RC_MOVE_OVER_REDIRECT:
160 $type = 'move over redirect';
161 break;
162 default:
163 $type = "$rcType";
164 }
165
166 return $type;
167 }
168
169 /**
170 * No uses left in Gerrit on 2013-11-19.
171 * @deprecated since 1.22
172 * @param mixed $row
173 * @return RecentChange
174 */
175 public static function newFromCurRow( $row ) {
176 wfDeprecated( __METHOD__, '1.22' );
177 $rc = new RecentChange;
178 $rc->loadFromCurRow( $row );
179 $rc->notificationtimestamp = false;
180 $rc->numberofWatchingusers = false;
181
182 return $rc;
183 }
184
185 /**
186 * Obtain the recent change with a given rc_id value
187 *
188 * @param int $rcid rc_id value to retrieve
189 * @return RecentChange
190 */
191 public static function newFromId( $rcid ) {
192 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
193 }
194
195 /**
196 * Find the first recent change matching some specific conditions
197 *
198 * @param array $conds Array of conditions
199 * @param mixed $fname Override the method name in profiling/logs
200 * @param array $options Query options
201 * @return RecentChange
202 */
203 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
204 $dbr = wfGetDB( DB_SLAVE );
205 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
206 if ( $row !== false ) {
207 return self::newFromRow( $row );
208 } else {
209 return null;
210 }
211 }
212
213 /**
214 * Return the list of recentchanges fields that should be selected to create
215 * a new recentchanges object.
216 * @return array
217 */
218 public static function selectFields() {
219 return array(
220 'rc_id',
221 'rc_timestamp',
222 'rc_user',
223 'rc_user_text',
224 'rc_namespace',
225 'rc_title',
226 'rc_comment',
227 'rc_minor',
228 'rc_bot',
229 'rc_new',
230 'rc_cur_id',
231 'rc_this_oldid',
232 'rc_last_oldid',
233 'rc_type',
234 'rc_source',
235 'rc_patrolled',
236 'rc_ip',
237 'rc_old_len',
238 'rc_new_len',
239 'rc_deleted',
240 'rc_logid',
241 'rc_log_type',
242 'rc_log_action',
243 'rc_params',
244 );
245 }
246
247 # Accessors
248
249 /**
250 * @param array $attribs
251 */
252 public function setAttribs( $attribs ) {
253 $this->mAttribs = $attribs;
254 }
255
256 /**
257 * @param array $extra
258 */
259 public function setExtra( $extra ) {
260 $this->mExtra = $extra;
261 }
262
263 /**
264 * @return Title
265 */
266 public function &getTitle() {
267 if ( $this->mTitle === false ) {
268 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
269 }
270
271 return $this->mTitle;
272 }
273
274 /**
275 * Get the User object of the person who performed this change.
276 *
277 * @return User
278 */
279 public function getPerformer() {
280 if ( $this->mPerformer === false ) {
281 if ( $this->mAttribs['rc_user'] ) {
282 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] );
283 } else {
284 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
285 }
286 }
287
288 return $this->mPerformer;
289 }
290
291 /**
292 * Writes the data in this object to the database
293 * @param bool $noudp
294 */
295 public function save( $noudp = false ) {
296 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
297
298 $dbw = wfGetDB( DB_MASTER );
299 if ( !is_array( $this->mExtra ) ) {
300 $this->mExtra = array();
301 }
302
303 if ( !$wgPutIPinRC ) {
304 $this->mAttribs['rc_ip'] = '';
305 }
306
307 # If our database is strict about IP addresses, use NULL instead of an empty string
308 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
309 unset( $this->mAttribs['rc_ip'] );
310 }
311
312 # Trim spaces on user supplied text
313 $this->mAttribs['rc_comment'] = trim( $this->mAttribs['rc_comment'] );
314
315 # Make sure summary is truncated (whole multibyte characters)
316 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
317
318 # Fixup database timestamps
319 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
320 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
321
322 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
323 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id'] == 0 ) {
324 unset( $this->mAttribs['rc_cur_id'] );
325 }
326
327 # Insert new row
328 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
329
330 # Set the ID
331 $this->mAttribs['rc_id'] = $dbw->insertId();
332
333 # Notify extensions
334 wfRunHooks( 'RecentChange_save', array( &$this ) );
335
336 # Notify external application via UDP
337 if ( !$noudp ) {
338 $this->notifyRCFeeds();
339 }
340
341 # E-mail notifications
342 if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
343 $editor = $this->getPerformer();
344 $title = $this->getTitle();
345
346 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title, $this ) ) ) {
347 # @todo FIXME: This would be better as an extension hook
348 $enotif = new EmailNotification();
349 $enotif->notifyOnPageChange( $editor, $title,
350 $this->mAttribs['rc_timestamp'],
351 $this->mAttribs['rc_comment'],
352 $this->mAttribs['rc_minor'],
353 $this->mAttribs['rc_last_oldid'],
354 $this->mExtra['pageStatus'] );
355 }
356 }
357 }
358
359 /**
360 * @deprecated since 1.22, use notifyRCFeeds instead.
361 */
362 public function notifyRC2UDP() {
363 wfDeprecated( __METHOD__, '1.22' );
364 $this->notifyRCFeeds();
365 }
366
367 /**
368 * Send some text to UDP.
369 * @deprecated since 1.22
370 */
371 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
372 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
373
374 wfDeprecated( __METHOD__, '1.22' );
375
376 # Assume default for standard RC case
377 $address = $address ? $address : $wgRC2UDPAddress;
378 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix;
379 $port = $port ? $port : $wgRC2UDPPort;
380
381 $engine = new UDPRCFeedEngine();
382 $feed = array(
383 'uri' => "udp://$address:$port/$prefix",
384 'formatter' => 'IRCColourfulRCFeedFormatter',
385 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
386 );
387
388 $engine->send( $feed, $line );
389 }
390
391 /**
392 * Notify all the feeds about the change.
393 */
394 public function notifyRCFeeds() {
395 global $wgRCFeeds;
396
397 $performer = $this->getPerformer();
398
399 foreach ( $wgRCFeeds as $feed ) {
400 $feed += array(
401 'omit_bots' => false,
402 'omit_anon' => false,
403 'omit_user' => false,
404 'omit_minor' => false,
405 'omit_patrolled' => false,
406 );
407
408 if (
409 ( $feed['omit_bots'] && $this->mAttribs['rc_bot'] ) ||
410 ( $feed['omit_anon'] && $performer->isAnon() ) ||
411 ( $feed['omit_user'] && !$performer->isAnon() ) ||
412 ( $feed['omit_minor'] && $this->mAttribs['rc_minor'] ) ||
413 ( $feed['omit_patrolled'] && $this->mAttribs['rc_patrolled'] ) ||
414 $this->mAttribs['rc_type'] == RC_EXTERNAL
415 ) {
416 continue;
417 }
418
419 $engine = self::getEngine( $feed['uri'] );
420
421 if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
422 $actionComment = $this->mExtra['actionCommentIRC'];
423 } else {
424 $actionComment = null;
425 }
426
427 /** @var $formatter RCFeedFormatter */
428 $formatter = new $feed['formatter']();
429 $line = $formatter->getLine( $feed, $this, $actionComment );
430
431 $engine->send( $feed, $line );
432 }
433 }
434
435 /**
436 * Gets the stream engine object for a given URI from $wgRCEngines
437 *
438 * @param string $uri URI to get the engine object for
439 * @throws MWException
440 * @return RCFeedEngine The engine object
441 */
442 public static function getEngine( $uri ) {
443 global $wgRCEngines;
444
445 $scheme = parse_url( $uri, PHP_URL_SCHEME );
446 if ( !$scheme ) {
447 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
448 }
449
450 if ( !isset( $wgRCEngines[$scheme] ) ) {
451 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
452 }
453
454 return new $wgRCEngines[$scheme];
455 }
456
457 /**
458 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
459 */
460 public static function cleanupForIRC( $text ) {
461 wfDeprecated( __METHOD__, '1.22' );
462
463 return IRCColourfulRCFeedFormatter::cleanupForIRC( $text );
464 }
465
466 /**
467 * Mark a given change as patrolled
468 *
469 * @param RecentChange|int $change RecentChange or corresponding rc_id
470 * @param bool $auto For automatic patrol
471 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
472 */
473 public static function markPatrolled( $change, $auto = false ) {
474 global $wgUser;
475
476 $change = $change instanceof RecentChange
477 ? $change
478 : RecentChange::newFromId( $change );
479
480 if ( !$change instanceof RecentChange ) {
481 return null;
482 }
483
484 return $change->doMarkPatrolled( $wgUser, $auto );
485 }
486
487 /**
488 * Mark this RecentChange as patrolled
489 *
490 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
491 * 'markedaspatrollederror-noautopatrol' as errors
492 * @param User $user User object doing the action
493 * @param bool $auto For automatic patrol
494 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
495 */
496 public function doMarkPatrolled( User $user, $auto = false ) {
497 global $wgUseRCPatrol, $wgUseNPPatrol;
498 $errors = array();
499 // If recentchanges patrol is disabled, only new pages
500 // can be patrolled
501 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
502 $errors[] = array( 'rcpatroldisabled' );
503 }
504 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
505 $right = $auto ? 'autopatrol' : 'patrol';
506 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
507 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
508 $errors[] = array( 'hookaborted' );
509 }
510 // Users without the 'autopatrol' right can't patrol their
511 // own revisions
512 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
513 && !$user->isAllowed( 'autopatrol' )
514 ) {
515 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
516 }
517 if ( $errors ) {
518 return $errors;
519 }
520 // If the change was patrolled already, do nothing
521 if ( $this->getAttribute( 'rc_patrolled' ) ) {
522 return array();
523 }
524 // Actually set the 'patrolled' flag in RC
525 $this->reallyMarkPatrolled();
526 // Log this patrol event
527 PatrolLog::record( $this, $auto, $user );
528 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
529
530 return array();
531 }
532
533 /**
534 * Mark this RecentChange patrolled, without error checking
535 * @return int Number of affected rows
536 */
537 public function reallyMarkPatrolled() {
538 $dbw = wfGetDB( DB_MASTER );
539 $dbw->update(
540 'recentchanges',
541 array(
542 'rc_patrolled' => 1
543 ),
544 array(
545 'rc_id' => $this->getAttribute( 'rc_id' )
546 ),
547 __METHOD__
548 );
549 // Invalidate the page cache after the page has been patrolled
550 // to make sure that the Patrol link isn't visible any longer!
551 $this->getTitle()->invalidateCache();
552
553 return $dbw->affectedRows();
554 }
555
556 /**
557 * Makes an entry in the database corresponding to an edit
558 *
559 * @param string $timestamp
560 * @param Title $title
561 * @param bool $minor
562 * @param User $user
563 * @param string $comment
564 * @param int $oldId
565 * @param string $lastTimestamp
566 * @param bool $bot
567 * @param string $ip
568 * @param int $oldSize
569 * @param int $newSize
570 * @param int $newId
571 * @param int $patrol
572 * @return RecentChange
573 */
574 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
575 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
576 $rc = new RecentChange;
577 $rc->mTitle = $title;
578 $rc->mPerformer = $user;
579 $rc->mAttribs = array(
580 'rc_timestamp' => $timestamp,
581 'rc_namespace' => $title->getNamespace(),
582 'rc_title' => $title->getDBkey(),
583 'rc_type' => RC_EDIT,
584 'rc_source' => self::SRC_EDIT,
585 'rc_minor' => $minor ? 1 : 0,
586 'rc_cur_id' => $title->getArticleID(),
587 'rc_user' => $user->getId(),
588 'rc_user_text' => $user->getName(),
589 'rc_comment' => $comment,
590 'rc_this_oldid' => $newId,
591 'rc_last_oldid' => $oldId,
592 'rc_bot' => $bot ? 1 : 0,
593 'rc_ip' => self::checkIPAddress( $ip ),
594 'rc_patrolled' => intval( $patrol ),
595 'rc_new' => 0, # obsolete
596 'rc_old_len' => $oldSize,
597 'rc_new_len' => $newSize,
598 'rc_deleted' => 0,
599 'rc_logid' => 0,
600 'rc_log_type' => null,
601 'rc_log_action' => '',
602 'rc_params' => ''
603 );
604
605 $rc->mExtra = array(
606 'prefixedDBkey' => $title->getPrefixedDBkey(),
607 'lastTimestamp' => $lastTimestamp,
608 'oldSize' => $oldSize,
609 'newSize' => $newSize,
610 'pageStatus' => 'changed'
611 );
612 $rc->save();
613
614 return $rc;
615 }
616
617 /**
618 * Makes an entry in the database corresponding to page creation
619 * Note: the title object must be loaded with the new id using resetArticleID()
620 *
621 * @param string $timestamp
622 * @param Title $title
623 * @param bool $minor
624 * @param User $user
625 * @param string $comment
626 * @param bool $bot
627 * @param string $ip
628 * @param int $size
629 * @param int $newId
630 * @param int $patrol
631 * @return RecentChange
632 */
633 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
634 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
635 $rc = new RecentChange;
636 $rc->mTitle = $title;
637 $rc->mPerformer = $user;
638 $rc->mAttribs = array(
639 'rc_timestamp' => $timestamp,
640 'rc_namespace' => $title->getNamespace(),
641 'rc_title' => $title->getDBkey(),
642 'rc_type' => RC_NEW,
643 'rc_source' => self::SRC_NEW,
644 'rc_minor' => $minor ? 1 : 0,
645 'rc_cur_id' => $title->getArticleID(),
646 'rc_user' => $user->getId(),
647 'rc_user_text' => $user->getName(),
648 'rc_comment' => $comment,
649 'rc_this_oldid' => $newId,
650 'rc_last_oldid' => 0,
651 'rc_bot' => $bot ? 1 : 0,
652 'rc_ip' => self::checkIPAddress( $ip ),
653 'rc_patrolled' => intval( $patrol ),
654 'rc_new' => 1, # obsolete
655 'rc_old_len' => 0,
656 'rc_new_len' => $size,
657 'rc_deleted' => 0,
658 'rc_logid' => 0,
659 'rc_log_type' => null,
660 'rc_log_action' => '',
661 'rc_params' => ''
662 );
663
664 $rc->mExtra = array(
665 'prefixedDBkey' => $title->getPrefixedDBkey(),
666 'lastTimestamp' => 0,
667 'oldSize' => 0,
668 'newSize' => $size,
669 'pageStatus' => 'created'
670 );
671 $rc->save();
672
673 return $rc;
674 }
675
676 /**
677 * @param string $timestamp
678 * @param Title $title
679 * @param User $user
680 * @param string $actionComment
681 * @param string $ip
682 * @param string $type
683 * @param string $action
684 * @param Title $target
685 * @param string $logComment
686 * @param string $params
687 * @param int $newId
688 * @param string $actionCommentIRC
689 * @return bool
690 */
691 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
692 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
693 ) {
694 global $wgLogRestrictions;
695
696 # Don't add private logs to RC!
697 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
698 return false;
699 }
700 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
701 $target, $logComment, $params, $newId, $actionCommentIRC );
702 $rc->save();
703
704 return true;
705 }
706
707 /**
708 * @param string $timestamp
709 * @param Title $title
710 * @param User $user
711 * @param string $actionComment
712 * @param string $ip
713 * @param string $type
714 * @param string $action
715 * @param Title $target
716 * @param string $logComment
717 * @param string $params
718 * @param int $newId
719 * @param string $actionCommentIRC
720 * @return RecentChange
721 */
722 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
723 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
724 global $wgRequest;
725
726 ## Get pageStatus for email notification
727 switch ( $type . '-' . $action ) {
728 case 'delete-delete':
729 $pageStatus = 'deleted';
730 break;
731 case 'move-move':
732 case 'move-move_redir':
733 $pageStatus = 'moved';
734 break;
735 case 'delete-restore':
736 $pageStatus = 'restored';
737 break;
738 case 'upload-upload':
739 $pageStatus = 'created';
740 break;
741 case 'upload-overwrite':
742 default:
743 $pageStatus = 'changed';
744 break;
745 }
746
747 $rc = new RecentChange;
748 $rc->mTitle = $target;
749 $rc->mPerformer = $user;
750 $rc->mAttribs = array(
751 'rc_timestamp' => $timestamp,
752 'rc_namespace' => $target->getNamespace(),
753 'rc_title' => $target->getDBkey(),
754 'rc_type' => RC_LOG,
755 'rc_source' => self::SRC_LOG,
756 'rc_minor' => 0,
757 'rc_cur_id' => $target->getArticleID(),
758 'rc_user' => $user->getId(),
759 'rc_user_text' => $user->getName(),
760 'rc_comment' => $logComment,
761 'rc_this_oldid' => 0,
762 'rc_last_oldid' => 0,
763 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
764 'rc_ip' => self::checkIPAddress( $ip ),
765 'rc_patrolled' => 1,
766 'rc_new' => 0, # obsolete
767 'rc_old_len' => null,
768 'rc_new_len' => null,
769 'rc_deleted' => 0,
770 'rc_logid' => $newId,
771 'rc_log_type' => $type,
772 'rc_log_action' => $action,
773 'rc_params' => $params
774 );
775
776 $rc->mExtra = array(
777 'prefixedDBkey' => $title->getPrefixedDBkey(),
778 'lastTimestamp' => 0,
779 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
780 'pageStatus' => $pageStatus,
781 'actionCommentIRC' => $actionCommentIRC
782 );
783
784 return $rc;
785 }
786
787 /**
788 * Initialises the members of this object from a mysql row object
789 *
790 * @param mixed $row
791 */
792 public function loadFromRow( $row ) {
793 $this->mAttribs = get_object_vars( $row );
794 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
795 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
796 }
797
798 /**
799 * Makes a pseudo-RC entry from a cur row
800 *
801 * @deprecated since 1.22
802 * @param mixed $row
803 */
804 public function loadFromCurRow( $row ) {
805 wfDeprecated( __METHOD__, '1.22' );
806 $this->mAttribs = array(
807 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
808 'rc_user' => $row->rev_user,
809 'rc_user_text' => $row->rev_user_text,
810 'rc_namespace' => $row->page_namespace,
811 'rc_title' => $row->page_title,
812 'rc_comment' => $row->rev_comment,
813 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
814 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
815 'rc_source' => $row->page_is_new ? self::SRC_NEW : self::SRC_EDIT,
816 'rc_cur_id' => $row->page_id,
817 'rc_this_oldid' => $row->rev_id,
818 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
819 'rc_bot' => 0,
820 'rc_ip' => '',
821 'rc_id' => $row->rc_id,
822 'rc_patrolled' => $row->rc_patrolled,
823 'rc_new' => $row->page_is_new, # obsolete
824 'rc_old_len' => $row->rc_old_len,
825 'rc_new_len' => $row->rc_new_len,
826 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
827 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
828 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
829 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
830 'rc_deleted' => $row->rc_deleted // MUST be set
831 );
832 }
833
834 /**
835 * Get an attribute value
836 *
837 * @param string $name Attribute name
838 * @return mixed
839 */
840 public function getAttribute( $name ) {
841 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
842 }
843
844 /**
845 * @return array
846 */
847 public function getAttributes() {
848 return $this->mAttribs;
849 }
850
851 /**
852 * Gets the end part of the diff URL associated with this object
853 * Blank if no diff link should be displayed
854 * @param bool $forceCur
855 * @return string
856 */
857 public function diffLinkTrail( $forceCur ) {
858 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
859 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
860 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
861 if ( $forceCur ) {
862 $trail .= '&diff=0';
863 } else {
864 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
865 }
866 } else {
867 $trail = '';
868 }
869
870 return $trail;
871 }
872
873 /**
874 * Returns the change size (HTML).
875 * The lengths can be given optionally.
876 * @param int $old
877 * @param int $new
878 * @return string
879 */
880 public function getCharacterDifference( $old = 0, $new = 0 ) {
881 if ( $old === 0 ) {
882 $old = $this->mAttribs['rc_old_len'];
883 }
884 if ( $new === 0 ) {
885 $new = $this->mAttribs['rc_new_len'];
886 }
887 if ( $old === null || $new === null ) {
888 return '';
889 }
890
891 return ChangesList::showCharacterDifference( $old, $new );
892 }
893
894 /**
895 * Purge expired changes from the recentchanges table
896 * @since 1.22
897 */
898 public static function purgeExpiredChanges() {
899 if ( wfReadOnly() ) {
900 return;
901 }
902
903 $method = __METHOD__;
904 $dbw = wfGetDB( DB_MASTER );
905 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
906 global $wgRCMaxAge;
907
908 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
909 $dbw->delete(
910 'recentchanges',
911 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
912 $method
913 );
914 } );
915 }
916
917 private static function checkIPAddress( $ip ) {
918 global $wgRequest;
919 if ( $ip ) {
920 if ( !IP::isIPAddress( $ip ) ) {
921 throw new MWException( "Attempt to write \"" . $ip .
922 "\" as an IP address into recent changes" );
923 }
924 } else {
925 $ip = $wgRequest->getIP();
926 if ( !$ip ) {
927 $ip = '';
928 }
929 }
930
931 return $ip;
932 }
933
934 /**
935 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
936 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
937 * or rows which will be deleted soon shouldn't be included.
938 *
939 * @param mixed $timestamp MWTimestamp compatible timestamp
940 * @param int $tolerance Tolerance in seconds
941 * @return bool
942 */
943 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
944 global $wgRCMaxAge;
945
946 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
947 }
948 }