3 * Utility class for creating and accessing recent change entries.
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.
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.
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
22 use MediaWiki\ChangeTags\Taggable
;
23 use MediaWiki\MediaWikiServices
;
26 * Utility class for creating new RC entries
29 * rc_id id of the row in the recentchanges table
30 * rc_timestamp time the entry was made
31 * rc_namespace namespace #
32 * rc_title non-prefixed db key
33 * rc_type is new entry, used to determine whether updating is necessary
34 * rc_source string representation of change source
36 * rc_cur_id page_id of associated page entry
37 * rc_user user id who made the entry
38 * rc_user_text user name who made the entry
39 * rc_actor actor id who made the entry
40 * rc_comment edit summary
41 * rc_this_oldid rev_id associated with this entry (or zero)
42 * rc_last_oldid rev_id associated with the entry before this one (or zero)
43 * rc_bot is bot, hidden
44 * rc_ip IP address of the user in dotted quad notation
45 * rc_new obsolete, use rc_type==RC_NEW
46 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
47 * rc_old_len integer byte length of the text before the edit
48 * rc_new_len the same after the edit
49 * rc_deleted partial deletion
50 * rc_logid the log_id value for this log entry (or zero)
51 * rc_log_type the log type (or null)
52 * rc_log_action the log action (or null)
53 * rc_params log params
56 * prefixedDBkey prefixed db key, used by external app via msg queue
57 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
58 * oldSize text size before the change
59 * newSize text size after the change
60 * pageStatus status of the page: created, deleted, moved, restored, changed
62 * temporary: not stored in the database
63 * notificationtimestamp
64 * numberofWatchingusers
66 * @todo Deprecate access to mAttribs (direct or via getAttributes). Right now
67 * we're having to include both rc_comment and rc_comment_text/rc_comment_data
68 * so random crap works right.
70 class RecentChange
implements Taggable
{
71 // Constants for the rc_source field. Extensions may also have
72 // their own source constants.
73 const SRC_EDIT
= 'mw.edit';
74 const SRC_NEW
= 'mw.new';
75 const SRC_LOG
= 'mw.log';
76 const SRC_EXTERNAL
= 'mw.external'; // obsolete
77 const SRC_CATEGORIZE
= 'mw.categorize';
79 const PRC_UNPATROLLED
= 0;
80 const PRC_PATROLLED
= 1;
81 const PRC_AUTOPATROLLED
= 2;
84 * @var bool For save() - save to the database only, without any events.
86 const SEND_NONE
= true;
89 * @var bool For save() - do emit the change to RCFeeds (usually public).
91 const SEND_FEED
= false;
94 public $mAttribs = [];
100 public $mTitle = false;
105 private $mPerformer = false;
107 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
108 public $notificationtimestamp;
111 * @var int Line number of recent change. Default -1.
113 public $counter = -1;
116 * @var array List of tags to apply
121 * @var array Array of change types
123 private static $changeTypes = [
127 'external' => RC_EXTERNAL
,
128 'categorize' => RC_CATEGORIZE
,
135 * @return RecentChange
137 public static function newFromRow( $row ) {
138 $rc = new RecentChange
;
139 $rc->loadFromRow( $row );
145 * Parsing text to RC_* constants
147 * @param string|array $type
148 * @throws MWException
149 * @return int|array RC_TYPE
151 public static function parseToRCType( $type ) {
152 if ( is_array( $type ) ) {
154 foreach ( $type as $t ) {
155 $retval[] = self
::parseToRCType( $t );
161 if ( !array_key_exists( $type, self
::$changeTypes ) ) {
162 throw new MWException( "Unknown type '$type'" );
164 return self
::$changeTypes[$type];
168 * Parsing RC_* constants to human-readable test
171 * @return string $type
173 public static function parseFromRCType( $rcType ) {
174 return array_search( $rcType, self
::$changeTypes, true ) ?
: "$rcType";
178 * Get an array of all change types
184 public static function getChangeTypes() {
185 return array_keys( self
::$changeTypes );
189 * Obtain the recent change with a given rc_id value
191 * @param int $rcid The rc_id value to retrieve
192 * @return RecentChange|null
194 public static function newFromId( $rcid ) {
195 return self
::newFromConds( [ 'rc_id' => $rcid ], __METHOD__
);
199 * Find the first recent change matching some specific conditions
201 * @param array $conds Array of conditions
202 * @param mixed $fname Override the method name in profiling/logs
203 * @param int $dbType DB_* constant
205 * @return RecentChange|null
207 public static function newFromConds(
212 $db = wfGetDB( $dbType );
213 $rcQuery = self
::getQueryInfo();
214 $row = $db->selectRow(
215 $rcQuery['tables'], $rcQuery['fields'], $conds, $fname, [], $rcQuery['joins']
217 if ( $row !== false ) {
218 return self
::newFromRow( $row );
225 * Return the tables, fields, and join conditions to be selected to create
226 * a new recentchanges object.
228 * @return array With three keys:
229 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
230 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
231 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
233 public static function getQueryInfo() {
234 $commentQuery = CommentStore
::getStore()->getJoin( 'rc_comment' );
235 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
237 'tables' => [ 'recentchanges' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
260 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
261 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
268 * @param array $attribs
270 public function setAttribs( $attribs ) {
271 $this->mAttribs
= $attribs;
275 * @param array $extra
277 public function setExtra( $extra ) {
278 $this->mExtra
= $extra;
284 public function &getTitle() {
285 if ( $this->mTitle
=== false ) {
286 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
289 return $this->mTitle
;
293 * Get the User object of the person who performed this change.
297 public function getPerformer() {
298 if ( $this->mPerformer
=== false ) {
299 if ( !empty( $this->mAttribs
['rc_actor'] ) ) {
300 $this->mPerformer
= User
::newFromActorId( $this->mAttribs
['rc_actor'] );
301 } elseif ( !empty( $this->mAttribs
['rc_user'] ) ) {
302 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
303 } elseif ( !empty( $this->mAttribs
['rc_user_text'] ) ) {
304 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
306 throw new MWException( 'RecentChange object lacks rc_actor, rc_user, and rc_user_text' );
310 return $this->mPerformer
;
314 * Writes the data in this object to the database
316 * For compatibility reasons, the SEND_ constants internally reference a value
317 * that may seem negated from their purpose (none=true, feed=false). This is
318 * because the parameter used to be called "$noudp", defaulting to false.
320 * @param bool $send self::SEND_FEED or self::SEND_NONE
322 public function save( $send = self
::SEND_FEED
) {
323 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
325 $dbw = wfGetDB( DB_MASTER
);
326 if ( !is_array( $this->mExtra
) ) {
330 if ( !$wgPutIPinRC ) {
331 $this->mAttribs
['rc_ip'] = '';
334 # Strict mode fixups (not-NULL fields)
335 foreach ( [ 'minor', 'bot', 'new', 'patrolled', 'deleted' ] as $field ) {
336 $this->mAttribs
["rc_$field"] = (int)$this->mAttribs
["rc_$field"];
338 # ...more fixups (NULL fields)
339 foreach ( [ 'old_len', 'new_len' ] as $field ) {
340 $this->mAttribs
["rc_$field"] = isset( $this->mAttribs
["rc_$field"] )
341 ?
(int)$this->mAttribs
["rc_$field"]
345 # If our database is strict about IP addresses, use NULL instead of an empty string
346 $strictIPs = $dbw->getType() === 'postgres'; // legacy
347 if ( $strictIPs && $this->mAttribs
['rc_ip'] == '' ) {
348 unset( $this->mAttribs
['rc_ip'] );
351 # Trim spaces on user supplied text
352 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
354 # Fixup database timestamps
355 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
357 # # If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
358 if ( $this->mAttribs
['rc_cur_id'] == 0 ) {
359 unset( $this->mAttribs
['rc_cur_id'] );
362 $row = $this->mAttribs
;
364 # Convert mAttribs['rc_comment'] for CommentStore
365 $comment = $row['rc_comment'];
366 unset( $row['rc_comment'], $row['rc_comment_text'], $row['rc_comment_data'] );
367 $row +
= CommentStore
::getStore()->insert( $dbw, 'rc_comment', $comment );
369 # Convert mAttribs['rc_user'] etc for ActorMigration
370 $user = User
::newFromAnyId(
371 $row['rc_user'] ??
null,
372 $row['rc_user_text'] ??
null,
373 $row['rc_actor'] ??
null
375 unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
376 $row +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
378 # Don't reuse an existing rc_id for the new row, if one happens to be
379 # set for some reason.
380 unset( $row['rc_id'] );
383 $dbw->insert( 'recentchanges', $row, __METHOD__
);
386 $this->mAttribs
['rc_id'] = $dbw->insertId();
389 // Avoid PHP 7.1 warning from passing $this by reference
391 Hooks
::run( 'RecentChange_save', [ &$rc ] );
393 if ( count( $this->tags
) ) {
394 ChangeTags
::addTags( $this->tags
, $this->mAttribs
['rc_id'],
395 $this->mAttribs
['rc_this_oldid'], $this->mAttribs
['rc_logid'], null, $this );
398 if ( $send === self
::SEND_FEED
) {
399 // Emit the change to external applications via RCFeeds.
400 $this->notifyRCFeeds();
403 # E-mail notifications
404 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
405 $editor = $this->getPerformer();
406 $title = $this->getTitle();
408 // Never send an RC notification email about categorization changes
410 Hooks
::run( 'AbortEmailNotification', [ $editor, $title, $this ] ) &&
411 $this->mAttribs
['rc_type'] != RC_CATEGORIZE
413 // @FIXME: This would be better as an extension hook
414 // Send emails or email jobs once this row is safely committed
415 $dbw->onTransactionCommitOrIdle(
416 function () use ( $editor, $title ) {
417 $enotif = new EmailNotification();
418 $enotif->notifyOnPageChange(
421 $this->mAttribs
['rc_timestamp'],
422 $this->mAttribs
['rc_comment'],
423 $this->mAttribs
['rc_minor'],
424 $this->mAttribs
['rc_last_oldid'],
425 $this->mExtra
['pageStatus']
433 // Update the cached list of active users
434 if ( $this->mAttribs
['rc_user'] > 0 ) {
435 JobQueueGroup
::singleton()->lazyPush( RecentChangesUpdateJob
::newCacheUpdateJob() );
440 * Notify all the feeds about the change.
441 * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds
443 public function notifyRCFeeds( array $feeds = null ) {
445 if ( $feeds === null ) {
449 $performer = $this->getPerformer();
451 foreach ( $feeds as $params ) {
453 'omit_bots' => false,
454 'omit_anon' => false,
455 'omit_user' => false,
456 'omit_minor' => false,
457 'omit_patrolled' => false,
461 ( $params['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
462 ( $params['omit_anon'] && $performer->isAnon() ) ||
463 ( $params['omit_user'] && !$performer->isAnon() ) ||
464 ( $params['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
465 ( $params['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
466 $this->mAttribs
['rc_type'] == RC_EXTERNAL
471 $actionComment = $this->mExtra
['actionCommentIRC'] ??
null;
473 $feed = RCFeed
::factory( $params );
474 $feed->notify( $this, $actionComment );
480 * @deprecated since 1.29 Use RCFeed::factory() instead
481 * @param string $uri URI to get the engine object for
482 * @param array $params
483 * @return RCFeedEngine The engine object
484 * @throws MWException
486 public static function getEngine( $uri, $params = [] ) {
487 // TODO: Merge into RCFeed::factory().
489 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
491 throw new MWException( "Invalid RCFeed uri: '$uri'" );
493 if ( !isset( $wgRCEngines[$scheme] ) ) {
494 throw new MWException( "Unknown RCFeedEngine scheme: '$scheme'" );
496 if ( defined( 'MW_PHPUNIT_TEST' ) && is_object( $wgRCEngines[$scheme] ) ) {
497 return $wgRCEngines[$scheme];
499 return new $wgRCEngines[$scheme]( $params );
503 * Mark a given change as patrolled
505 * @param RecentChange|int $change RecentChange or corresponding rc_id
506 * @param bool $auto For automatic patrol
507 * @param string|string[]|null $tags Change tags to add to the patrol log entry
508 * ($user should be able to add the specified tags before this is called)
509 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
511 public static function markPatrolled( $change, $auto = false, $tags = null ) {
514 $change = $change instanceof RecentChange
516 : self
::newFromId( $change );
518 if ( !$change instanceof RecentChange
) {
522 return $change->doMarkPatrolled( $wgUser, $auto, $tags );
526 * Mark this RecentChange as patrolled
528 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
529 * 'markedaspatrollederror-noautopatrol' as errors
530 * @param User $user User object doing the action
531 * @param bool $auto For automatic patrol
532 * @param string|string[]|null $tags Change tags to add to the patrol log entry
533 * ($user should be able to add the specified tags before this is called)
534 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
536 public function doMarkPatrolled( User
$user, $auto = false, $tags = null ) {
537 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
539 // Fix up $tags so that the MarkPatrolled hook below always gets an array
540 if ( $tags === null ) {
542 } elseif ( is_string( $tags ) ) {
547 // If recentchanges patrol is disabled, only new pages or new file versions
548 // can be patrolled, provided the appropriate config variable is set
549 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) &&
550 ( !$wgUseFilePatrol ||
!( $this->getAttribute( 'rc_type' ) == RC_LOG
&&
551 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
552 $errors[] = [ 'rcpatroldisabled' ];
554 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
555 $right = $auto ?
'autopatrol' : 'patrol';
556 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
557 if ( !Hooks
::run( 'MarkPatrolled',
558 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto, &$tags ] )
560 $errors[] = [ 'hookaborted' ];
562 // Users without the 'autopatrol' right can't patrol their
564 if ( $user->getName() === $this->getAttribute( 'rc_user_text' ) &&
565 !MediaWikiServices
::getInstance()->getPermissionManager()
566 ->userHasRight( $user, 'autopatrol' )
568 $errors[] = [ 'markedaspatrollederror-noautopatrol' ];
573 // If the change was patrolled already, do nothing
574 if ( $this->getAttribute( 'rc_patrolled' ) ) {
577 // Actually set the 'patrolled' flag in RC
578 $this->reallyMarkPatrolled();
579 // Log this patrol event
580 PatrolLog
::record( $this, $auto, $user, $tags );
583 'MarkPatrolledComplete',
584 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ]
591 * Mark this RecentChange patrolled, without error checking
592 * @return int Number of affected rows
594 public function reallyMarkPatrolled() {
595 $dbw = wfGetDB( DB_MASTER
);
599 'rc_patrolled' => self
::PRC_PATROLLED
602 'rc_id' => $this->getAttribute( 'rc_id' )
606 // Invalidate the page cache after the page has been patrolled
607 // to make sure that the Patrol link isn't visible any longer!
608 $this->getTitle()->invalidateCache();
610 return $dbw->affectedRows();
614 * Makes an entry in the database corresponding to an edit
616 * @param string $timestamp
617 * @param Title $title
620 * @param string $comment
622 * @param string $lastTimestamp
625 * @param int $oldSize
626 * @param int $newSize
630 * @return RecentChange
632 public static function notifyEdit(
633 $timestamp, $title, $minor, $user, $comment, $oldId, $lastTimestamp,
634 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
637 $rc = new RecentChange
;
638 $rc->mTitle
= $title;
639 $rc->mPerformer
= $user;
641 'rc_timestamp' => $timestamp,
642 'rc_namespace' => $title->getNamespace(),
643 'rc_title' => $title->getDBkey(),
644 'rc_type' => RC_EDIT
,
645 'rc_source' => self
::SRC_EDIT
,
646 'rc_minor' => $minor ?
1 : 0,
647 'rc_cur_id' => $title->getArticleID(),
648 'rc_user' => $user->getId(),
649 'rc_user_text' => $user->getName(),
650 'rc_actor' => $user->getActorId(),
651 'rc_comment' => &$comment,
652 'rc_comment_text' => &$comment,
653 'rc_comment_data' => null,
654 'rc_this_oldid' => $newId,
655 'rc_last_oldid' => $oldId,
656 'rc_bot' => $bot ?
1 : 0,
657 'rc_ip' => self
::checkIPAddress( $ip ),
658 'rc_patrolled' => intval( $patrol ),
659 'rc_new' => 0, # obsolete
660 'rc_old_len' => $oldSize,
661 'rc_new_len' => $newSize,
664 'rc_log_type' => null,
665 'rc_log_action' => '',
670 'prefixedDBkey' => $title->getPrefixedDBkey(),
671 'lastTimestamp' => $lastTimestamp,
672 'oldSize' => $oldSize,
673 'newSize' => $newSize,
674 'pageStatus' => 'changed'
677 DeferredUpdates
::addCallableUpdate(
678 function () use ( $rc, $tags ) {
679 $rc->addTags( $tags );
682 DeferredUpdates
::POSTSEND
,
690 * Makes an entry in the database corresponding to page creation
691 * Note: the title object must be loaded with the new id using resetArticleID()
693 * @param string $timestamp
694 * @param Title $title
697 * @param string $comment
704 * @return RecentChange
706 public static function notifyNew(
707 $timestamp, $title, $minor, $user, $comment, $bot,
708 $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
710 $rc = new RecentChange
;
711 $rc->mTitle
= $title;
712 $rc->mPerformer
= $user;
714 'rc_timestamp' => $timestamp,
715 'rc_namespace' => $title->getNamespace(),
716 'rc_title' => $title->getDBkey(),
718 'rc_source' => self
::SRC_NEW
,
719 'rc_minor' => $minor ?
1 : 0,
720 'rc_cur_id' => $title->getArticleID(),
721 'rc_user' => $user->getId(),
722 'rc_user_text' => $user->getName(),
723 'rc_actor' => $user->getActorId(),
724 'rc_comment' => &$comment,
725 'rc_comment_text' => &$comment,
726 'rc_comment_data' => null,
727 'rc_this_oldid' => $newId,
728 'rc_last_oldid' => 0,
729 'rc_bot' => $bot ?
1 : 0,
730 'rc_ip' => self
::checkIPAddress( $ip ),
731 'rc_patrolled' => intval( $patrol ),
732 'rc_new' => 1, # obsolete
734 'rc_new_len' => $size,
737 'rc_log_type' => null,
738 'rc_log_action' => '',
743 'prefixedDBkey' => $title->getPrefixedDBkey(),
744 'lastTimestamp' => 0,
747 'pageStatus' => 'created'
750 DeferredUpdates
::addCallableUpdate(
751 function () use ( $rc, $tags ) {
752 $rc->addTags( $tags );
755 DeferredUpdates
::POSTSEND
,
763 * @param string $timestamp
764 * @param Title $title
766 * @param string $actionComment
768 * @param string $type
769 * @param string $action
770 * @param Title $target
771 * @param string $logComment
772 * @param string $params
774 * @param string $actionCommentIRC
777 public static function notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type,
778 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
780 global $wgLogRestrictions;
782 # Don't add private logs to RC!
783 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
786 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
787 $target, $logComment, $params, $newId, $actionCommentIRC );
794 * @param string $timestamp
795 * @param Title $title
797 * @param string $actionComment
799 * @param string $type
800 * @param string $action
801 * @param Title $target
802 * @param string $logComment
803 * @param string $params
805 * @param string $actionCommentIRC
806 * @param int $revId Id of associated revision, if any
807 * @param bool $isPatrollable Whether this log entry is patrollable
808 * @return RecentChange
810 public static function newLogEntry( $timestamp, $title, $user, $actionComment, $ip,
811 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
812 $revId = 0, $isPatrollable = false ) {
814 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
816 # # Get pageStatus for email notification
817 switch ( $type . '-' . $action ) {
818 case 'delete-delete':
819 case 'delete-delete_redir':
820 $pageStatus = 'deleted';
823 case 'move-move_redir':
824 $pageStatus = 'moved';
826 case 'delete-restore':
827 $pageStatus = 'restored';
829 case 'upload-upload':
830 $pageStatus = 'created';
832 case 'upload-overwrite':
834 $pageStatus = 'changed';
838 // Allow unpatrolled status for patrollable log entries
839 $canAutopatrol = $permissionManager->userHasRight( $user, 'autopatrol' );
840 $markPatrolled = $isPatrollable ?
$canAutopatrol : true;
842 $rc = new RecentChange
;
843 $rc->mTitle
= $target;
844 $rc->mPerformer
= $user;
846 'rc_timestamp' => $timestamp,
847 'rc_namespace' => $target->getNamespace(),
848 'rc_title' => $target->getDBkey(),
850 'rc_source' => self
::SRC_LOG
,
852 'rc_cur_id' => $target->getArticleID(),
853 'rc_user' => $user->getId(),
854 'rc_user_text' => $user->getName(),
855 'rc_actor' => $user->getActorId(),
856 'rc_comment' => &$logComment,
857 'rc_comment_text' => &$logComment,
858 'rc_comment_data' => null,
859 'rc_this_oldid' => $revId,
860 'rc_last_oldid' => 0,
861 'rc_bot' => $permissionManager->userHasRight( $user, 'bot' ) ?
862 (int)$wgRequest->getBool( 'bot', true ) : 0,
863 'rc_ip' => self
::checkIPAddress( $ip ),
864 'rc_patrolled' => $markPatrolled ? self
::PRC_AUTOPATROLLED
: self
::PRC_UNPATROLLED
,
865 'rc_new' => 0, # obsolete
866 'rc_old_len' => null,
867 'rc_new_len' => null,
869 'rc_logid' => $newId,
870 'rc_log_type' => $type,
871 'rc_log_action' => $action,
872 'rc_params' => $params
876 'prefixedDBkey' => $title->getPrefixedDBkey(),
877 'lastTimestamp' => 0,
878 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
879 'pageStatus' => $pageStatus,
880 'actionCommentIRC' => $actionCommentIRC
887 * Constructs a RecentChange object for the given categorization
888 * This does not call save() on the object and thus does not write to the db
892 * @param string $timestamp Timestamp of the recent change to occur
893 * @param Title $categoryTitle Title of the category a page is being added to or removed from
894 * @param User|null $user User object of the user that made the change
895 * @param string $comment Change summary
896 * @param Title $pageTitle Title of the page that is being added or removed
897 * @param int $oldRevId Parent revision ID of this change
898 * @param int $newRevId Revision ID of this change
899 * @param string $lastTimestamp Parent revision timestamp of this change
900 * @param bool $bot true, if the change was made by a bot
901 * @param string $ip IP address of the user, if the change was made anonymously
902 * @param int $deleted Indicates whether the change has been deleted
903 * @param bool|null $added true, if the category was added, false for removed
905 * @return RecentChange
907 public static function newForCategorization(
909 Title
$categoryTitle,
921 // Done in a backwards compatible way.
923 'hidden-cat' => WikiCategoryPage
::factory( $categoryTitle )->isHidden()
925 if ( $added !== null ) {
926 $params['added'] = $added;
929 $rc = new RecentChange
;
930 $rc->mTitle
= $categoryTitle;
931 $rc->mPerformer
= $user;
933 'rc_timestamp' => $timestamp,
934 'rc_namespace' => $categoryTitle->getNamespace(),
935 'rc_title' => $categoryTitle->getDBkey(),
936 'rc_type' => RC_CATEGORIZE
,
937 'rc_source' => self
::SRC_CATEGORIZE
,
939 'rc_cur_id' => $pageTitle->getArticleID(),
940 'rc_user' => $user ?
$user->getId() : 0,
941 'rc_user_text' => $user ?
$user->getName() : '',
942 'rc_actor' => $user ?
$user->getActorId() : null,
943 'rc_comment' => &$comment,
944 'rc_comment_text' => &$comment,
945 'rc_comment_data' => null,
946 'rc_this_oldid' => $newRevId,
947 'rc_last_oldid' => $oldRevId,
948 'rc_bot' => $bot ?
1 : 0,
949 'rc_ip' => self
::checkIPAddress( $ip ),
950 'rc_patrolled' => self
::PRC_AUTOPATROLLED
, // Always patrolled, just like log entries
951 'rc_new' => 0, # obsolete
952 'rc_old_len' => null,
953 'rc_new_len' => null,
954 'rc_deleted' => $deleted,
956 'rc_log_type' => null,
957 'rc_log_action' => '',
958 'rc_params' => serialize( $params )
962 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
963 'lastTimestamp' => $lastTimestamp,
966 'pageStatus' => 'changed'
973 * Get a parameter value
977 * @param string $name parameter name
980 public function getParam( $name ) {
981 $params = $this->parseParams();
982 return $params[$name] ??
null;
986 * Initialises the members of this object from a mysql row object
990 public function loadFromRow( $row ) {
991 $this->mAttribs
= get_object_vars( $row );
992 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
993 // rc_deleted MUST be set
994 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
;
996 if ( isset( $this->mAttribs
['rc_ip'] ) ) {
997 // Clean up CIDRs for Postgres per T164898. ("127.0.0.1" casts to "127.0.0.1/32")
998 $n = strpos( $this->mAttribs
['rc_ip'], '/' );
999 if ( $n !== false ) {
1000 $this->mAttribs
['rc_ip'] = substr( $this->mAttribs
['rc_ip'], 0, $n );
1004 $comment = CommentStore
::getStore()
1005 // Legacy because $row may have come from self::selectFields()
1006 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $row, true )
1008 $this->mAttribs
['rc_comment'] = &$comment;
1009 $this->mAttribs
['rc_comment_text'] = &$comment;
1010 $this->mAttribs
['rc_comment_data'] = null;
1012 $user = User
::newFromAnyId(
1013 $this->mAttribs
['rc_user'] ??
null,
1014 $this->mAttribs
['rc_user_text'] ??
null,
1015 $this->mAttribs
['rc_actor'] ??
null
1017 $this->mAttribs
['rc_user'] = $user->getId();
1018 $this->mAttribs
['rc_user_text'] = $user->getName();
1019 $this->mAttribs
['rc_actor'] = $user->getActorId();
1023 * Get an attribute value
1025 * @param string $name Attribute name
1028 public function getAttribute( $name ) {
1029 if ( $name === 'rc_comment' ) {
1030 return CommentStore
::getStore()
1031 ->getComment( 'rc_comment', $this->mAttribs
, true )->text
;
1034 if ( $name === 'rc_user' ||
$name === 'rc_user_text' ||
$name === 'rc_actor' ) {
1035 $user = User
::newFromAnyId(
1036 $this->mAttribs
['rc_user'] ??
null,
1037 $this->mAttribs
['rc_user_text'] ??
null,
1038 $this->mAttribs
['rc_actor'] ??
null
1040 if ( $name === 'rc_user' ) {
1041 return $user->getId();
1043 if ( $name === 'rc_user_text' ) {
1044 return $user->getName();
1046 if ( $name === 'rc_actor' ) {
1047 return $user->getActorId();
1051 return $this->mAttribs
[$name] ??
null;
1057 public function getAttributes() {
1058 return $this->mAttribs
;
1062 * Gets the end part of the diff URL associated with this object
1063 * Blank if no diff link should be displayed
1064 * @param bool $forceCur
1067 public function diffLinkTrail( $forceCur ) {
1068 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
1069 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
1070 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
1072 $trail .= '&diff=0';
1074 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
1084 * Returns the change size (HTML).
1085 * The lengths can be given optionally.
1090 public function getCharacterDifference( $old = 0, $new = 0 ) {
1092 $old = $this->mAttribs
['rc_old_len'];
1095 $new = $this->mAttribs
['rc_new_len'];
1097 if ( $old === null ||
$new === null ) {
1101 return ChangesList
::showCharacterDifference( $old, $new );
1104 private static function checkIPAddress( $ip ) {
1107 if ( !IP
::isIPAddress( $ip ) ) {
1108 throw new MWException( "Attempt to write \"" . $ip .
1109 "\" as an IP address into recent changes" );
1112 $ip = $wgRequest->getIP();
1122 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
1123 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
1124 * or rows which will be deleted soon shouldn't be included.
1126 * @param mixed $timestamp MWTimestamp compatible timestamp
1127 * @param int $tolerance Tolerance in seconds
1130 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
1133 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
1137 * Parses and returns the rc_params attribute
1141 * @return mixed|bool false on failed unserialization
1143 public function parseParams() {
1144 $rcParams = $this->getAttribute( 'rc_params' );
1146 Wikimedia\
suppressWarnings();
1147 $unserializedParams = unserialize( $rcParams );
1148 Wikimedia\restoreWarnings
();
1150 return $unserializedParams;
1154 * Tags to append to the recent change,
1155 * and associated revision/log
1159 * @param string|string[] $tags
1161 public function addTags( $tags ) {
1162 if ( is_string( $tags ) ) {
1163 $this->tags
[] = $tags;
1165 $this->tags
= array_merge( $tags, $this->tags
);