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
24 * Utility class for creating new RC entries
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
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_actor actor id who made the entry
38 * rc_comment edit summary
39 * rc_this_oldid rev_id associated with this entry (or zero)
40 * rc_last_oldid rev_id associated with the entry before this one (or zero)
41 * rc_bot is bot, hidden
42 * rc_ip IP address of the user in dotted quad notation
43 * rc_new obsolete, use rc_type==RC_NEW
44 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
45 * rc_old_len integer byte length of the text before the edit
46 * rc_new_len the same after the edit
47 * rc_deleted partial deletion
48 * rc_logid the log_id value for this log entry (or zero)
49 * rc_log_type the log type (or null)
50 * rc_log_action the log action (or null)
51 * rc_params log params
54 * prefixedDBkey prefixed db key, used by external app via msg queue
55 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
56 * oldSize text size before the change
57 * newSize text size after the change
58 * pageStatus status of the page: created, deleted, moved, restored, changed
60 * temporary: not stored in the database
61 * notificationtimestamp
62 * numberofWatchingusers
64 * @todo Deprecate access to mAttribs (direct or via getAttributes). Right now
65 * we're having to include both rc_comment and rc_comment_text/rc_comment_data
66 * so random crap works right.
69 // Constants for the rc_source field. Extensions may also have
70 // their own source constants.
71 const SRC_EDIT
= 'mw.edit';
72 const SRC_NEW
= 'mw.new';
73 const SRC_LOG
= 'mw.log';
74 const SRC_EXTERNAL
= 'mw.external'; // obsolete
75 const SRC_CATEGORIZE
= 'mw.categorize';
77 const PRC_UNPATROLLED
= 0;
78 const PRC_PATROLLED
= 1;
79 const PRC_AUTOPATROLLED
= 2;
82 * @var bool For save() - save to the database only, without any events.
84 const SEND_NONE
= true;
87 * @var bool For save() - do emit the change to RCFeeds (usually public).
89 const SEND_FEED
= false;
91 public $mAttribs = [];
97 public $mTitle = false;
102 private $mPerformer = false;
104 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
105 public $notificationtimestamp;
108 * @var int Line number of recent change. Default -1.
110 public $counter = -1;
113 * @var array List of tags to apply
118 * @var array Array of change types
120 private static $changeTypes = [
124 'external' => RC_EXTERNAL
,
125 'categorize' => RC_CATEGORIZE
,
132 * @return RecentChange
134 public static function newFromRow( $row ) {
135 $rc = new RecentChange
;
136 $rc->loadFromRow( $row );
142 * Parsing text to RC_* constants
144 * @param string|array $type
145 * @throws MWException
146 * @return int|array RC_TYPE
148 public static function parseToRCType( $type ) {
149 if ( is_array( $type ) ) {
151 foreach ( $type as $t ) {
152 $retval[] = self
::parseToRCType( $t );
158 if ( !array_key_exists( $type, self
::$changeTypes ) ) {
159 throw new MWException( "Unknown type '$type'" );
161 return self
::$changeTypes[$type];
165 * Parsing RC_* constants to human-readable test
168 * @return string $type
170 public static function parseFromRCType( $rcType ) {
171 return array_search( $rcType, self
::$changeTypes, true ) ?
: "$rcType";
175 * Get an array of all change types
181 public static function getChangeTypes() {
182 return array_keys( self
::$changeTypes );
186 * Obtain the recent change with a given rc_id value
188 * @param int $rcid The rc_id value to retrieve
189 * @return RecentChange|null
191 public static function newFromId( $rcid ) {
192 return self
::newFromConds( [ 'rc_id' => $rcid ], __METHOD__
);
196 * Find the first recent change matching some specific conditions
198 * @param array $conds Array of conditions
199 * @param mixed $fname Override the method name in profiling/logs
200 * @param int $dbType DB_* constant
202 * @return RecentChange|null
204 public static function newFromConds(
209 $db = wfGetDB( $dbType );
210 $rcQuery = self
::getQueryInfo();
211 $row = $db->selectRow(
212 $rcQuery['tables'], $rcQuery['fields'], $conds, $fname, [], $rcQuery['joins']
214 if ( $row !== false ) {
215 return self
::newFromRow( $row );
222 * Return the list of recentchanges fields that should be selected to create
223 * a new recentchanges object.
224 * @deprecated since 1.31, use self::getQueryInfo() instead.
227 public static function selectFields() {
228 global $wgActorTableSchemaMigrationStage;
230 wfDeprecated( __METHOD__
, '1.31' );
231 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
232 // If code is using this instead of self::getQueryInfo(), there's a
233 // decent chance it's going to try to directly access
234 // $row->rc_user or $row->rc_user_text and we can't give it
235 // useful values here once those aren't being used anymore.
236 throw new BadMethodCallException(
237 'Cannot use ' . __METHOD__
238 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
247 'rc_actor' => 'NULL',
267 ] + CommentStore
::getStore()->getFields( 'rc_comment' );
271 * Return the tables, fields, and join conditions to be selected to create
272 * a new recentchanges object.
274 * @return array With three keys:
275 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
276 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
277 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
279 public static function getQueryInfo() {
280 $commentQuery = CommentStore
::getStore()->getJoin( 'rc_comment' );
281 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
283 'tables' => [ 'recentchanges' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
306 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
307 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
314 * @param array $attribs
316 public function setAttribs( $attribs ) {
317 $this->mAttribs
= $attribs;
321 * @param array $extra
323 public function setExtra( $extra ) {
324 $this->mExtra
= $extra;
330 public function &getTitle() {
331 if ( $this->mTitle
=== false ) {
332 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
335 return $this->mTitle
;
339 * Get the User object of the person who performed this change.
343 public function getPerformer() {
344 if ( $this->mPerformer
=== false ) {
345 if ( !empty( $this->mAttribs
['rc_actor'] ) ) {
346 $this->mPerformer
= User
::newFromActorId( $this->mAttribs
['rc_actor'] );
347 } elseif ( !empty( $this->mAttribs
['rc_user'] ) ) {
348 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
349 } elseif ( !empty( $this->mAttribs
['rc_user_text'] ) ) {
350 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
352 throw new MWException( 'RecentChange object lacks rc_actor, rc_user, and rc_user_text' );
356 return $this->mPerformer
;
360 * Writes the data in this object to the database
362 * For compatibility reasons, the SEND_ constants internally reference a value
363 * that may seem negated from their purpose (none=true, feed=false). This is
364 * because the parameter used to be called "$noudp", defaulting to false.
366 * @param bool $send self::SEND_FEED or self::SEND_NONE
368 public function save( $send = self
::SEND_FEED
) {
369 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
371 if ( is_string( $send ) ) {
372 // Callers used to pass undocumented strings like 'noudp'
373 // or 'pleasedontudp' instead of self::SEND_NONE (true).
374 // @deprecated since 1.31 Use SEND_NONE instead.
375 $send = self
::SEND_NONE
;
378 $dbw = wfGetDB( DB_MASTER
);
379 if ( !is_array( $this->mExtra
) ) {
383 if ( !$wgPutIPinRC ) {
384 $this->mAttribs
['rc_ip'] = '';
387 # Strict mode fixups (not-NULL fields)
388 foreach ( [ 'minor', 'bot', 'new', 'patrolled', 'deleted' ] as $field ) {
389 $this->mAttribs
["rc_$field"] = (int)$this->mAttribs
["rc_$field"];
391 # ...more fixups (NULL fields)
392 foreach ( [ 'old_len', 'new_len' ] as $field ) {
393 $this->mAttribs
["rc_$field"] = isset( $this->mAttribs
["rc_$field"] )
394 ?
(int)$this->mAttribs
["rc_$field"]
398 # If our database is strict about IP addresses, use NULL instead of an empty string
399 $strictIPs = in_array( $dbw->getType(), [ 'oracle', 'postgres' ] ); // legacy
400 if ( $strictIPs && $this->mAttribs
['rc_ip'] == '' ) {
401 unset( $this->mAttribs
['rc_ip'] );
404 # Trim spaces on user supplied text
405 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
407 # Fixup database timestamps
408 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
410 # # If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
411 if ( $this->mAttribs
['rc_cur_id'] == 0 ) {
412 unset( $this->mAttribs
['rc_cur_id'] );
415 $row = $this->mAttribs
;
417 # Convert mAttribs['rc_comment'] for CommentStore
418 $comment = $row['rc_comment'];
419 unset( $row['rc_comment'], $row['rc_comment_text'], $row['rc_comment_data'] );
420 $row +
= CommentStore
::getStore()->insert( $dbw, 'rc_comment', $comment );
422 # Convert mAttribs['rc_user'] etc for ActorMigration
423 $user = User
::newFromAnyId(
424 $row['rc_user'] ??
null,
425 $row['rc_user_text'] ??
null,
426 $row['rc_actor'] ??
null
428 unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
429 $row +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
431 # Don't reuse an existing rc_id for the new row, if one happens to be
432 # set for some reason.
433 unset( $row['rc_id'] );
436 $dbw->insert( 'recentchanges', $row, __METHOD__
);
439 $this->mAttribs
['rc_id'] = $dbw->insertId();
442 // Avoid PHP 7.1 warning from passing $this by reference
444 Hooks
::run( 'RecentChange_save', [ &$rc ] );
446 if ( count( $this->tags
) ) {
447 ChangeTags
::addTags( $this->tags
, $this->mAttribs
['rc_id'],
448 $this->mAttribs
['rc_this_oldid'], $this->mAttribs
['rc_logid'], null, $this );
451 if ( $send === self
::SEND_FEED
) {
452 // Emit the change to external applications via RCFeeds.
453 $this->notifyRCFeeds();
456 # E-mail notifications
457 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
458 $editor = $this->getPerformer();
459 $title = $this->getTitle();
461 // Never send an RC notification email about categorization changes
463 Hooks
::run( 'AbortEmailNotification', [ $editor, $title, $this ] ) &&
464 $this->mAttribs
['rc_type'] != RC_CATEGORIZE
466 // @FIXME: This would be better as an extension hook
467 // Send emails or email jobs once this row is safely committed
468 $dbw->onTransactionCommitOrIdle(
469 function () use ( $editor, $title ) {
470 $enotif = new EmailNotification();
471 $enotif->notifyOnPageChange(
474 $this->mAttribs
['rc_timestamp'],
475 $this->mAttribs
['rc_comment'],
476 $this->mAttribs
['rc_minor'],
477 $this->mAttribs
['rc_last_oldid'],
478 $this->mExtra
['pageStatus']
486 // Update the cached list of active users
487 if ( $this->mAttribs
['rc_user'] > 0 ) {
488 JobQueueGroup
::singleton()->lazyPush( RecentChangesUpdateJob
::newCacheUpdateJob() );
493 * Notify all the feeds about the change.
494 * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds
496 public function notifyRCFeeds( array $feeds = null ) {
498 if ( $feeds === null ) {
502 $performer = $this->getPerformer();
504 foreach ( $feeds as $params ) {
506 'omit_bots' => false,
507 'omit_anon' => false,
508 'omit_user' => false,
509 'omit_minor' => false,
510 'omit_patrolled' => false,
514 ( $params['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
515 ( $params['omit_anon'] && $performer->isAnon() ) ||
516 ( $params['omit_user'] && !$performer->isAnon() ) ||
517 ( $params['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
518 ( $params['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
519 $this->mAttribs
['rc_type'] == RC_EXTERNAL
524 $actionComment = $this->mExtra
['actionCommentIRC'] ??
null;
526 $feed = RCFeed
::factory( $params );
527 $feed->notify( $this, $actionComment );
533 * @deprecated since 1.29 Use RCFeed::factory() instead
534 * @param string $uri URI to get the engine object for
535 * @param array $params
536 * @return RCFeedEngine The engine object
537 * @throws MWException
539 public static function getEngine( $uri, $params = [] ) {
540 // TODO: Merge into RCFeed::factory().
542 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
544 throw new MWException( "Invalid RCFeed uri: '$uri'" );
546 if ( !isset( $wgRCEngines[$scheme] ) ) {
547 throw new MWException( "Unknown RCFeedEngine scheme: '$scheme'" );
549 if ( defined( 'MW_PHPUNIT_TEST' ) && is_object( $wgRCEngines[$scheme] ) ) {
550 return $wgRCEngines[$scheme];
552 return new $wgRCEngines[$scheme]( $params );
556 * Mark a given change as patrolled
558 * @param RecentChange|int $change RecentChange or corresponding rc_id
559 * @param bool $auto For automatic patrol
560 * @param string|string[]|null $tags Change tags to add to the patrol log entry
561 * ($user should be able to add the specified tags before this is called)
562 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
564 public static function markPatrolled( $change, $auto = false, $tags = null ) {
567 $change = $change instanceof RecentChange
569 : self
::newFromId( $change );
571 if ( !$change instanceof RecentChange
) {
575 return $change->doMarkPatrolled( $wgUser, $auto, $tags );
579 * Mark this RecentChange as patrolled
581 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
582 * 'markedaspatrollederror-noautopatrol' as errors
583 * @param User $user User object doing the action
584 * @param bool $auto For automatic patrol
585 * @param string|string[]|null $tags Change tags to add to the patrol log entry
586 * ($user should be able to add the specified tags before this is called)
587 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
589 public function doMarkPatrolled( User
$user, $auto = false, $tags = null ) {
590 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
593 // If recentchanges patrol is disabled, only new pages or new file versions
594 // can be patrolled, provided the appropriate config variable is set
595 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) &&
596 ( !$wgUseFilePatrol ||
!( $this->getAttribute( 'rc_type' ) == RC_LOG
&&
597 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
598 $errors[] = [ 'rcpatroldisabled' ];
600 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
601 $right = $auto ?
'autopatrol' : 'patrol';
602 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
603 if ( !Hooks
::run( 'MarkPatrolled',
604 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ] )
606 $errors[] = [ 'hookaborted' ];
608 // Users without the 'autopatrol' right can't patrol their
610 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
611 && !$user->isAllowed( 'autopatrol' )
613 $errors[] = [ 'markedaspatrollederror-noautopatrol' ];
618 // If the change was patrolled already, do nothing
619 if ( $this->getAttribute( 'rc_patrolled' ) ) {
622 // Actually set the 'patrolled' flag in RC
623 $this->reallyMarkPatrolled();
624 // Log this patrol event
625 PatrolLog
::record( $this, $auto, $user, $tags );
628 'MarkPatrolledComplete',
629 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ]
636 * Mark this RecentChange patrolled, without error checking
637 * @return int Number of affected rows
639 public function reallyMarkPatrolled() {
640 $dbw = wfGetDB( DB_MASTER
);
644 'rc_patrolled' => self
::PRC_PATROLLED
647 'rc_id' => $this->getAttribute( 'rc_id' )
651 // Invalidate the page cache after the page has been patrolled
652 // to make sure that the Patrol link isn't visible any longer!
653 $this->getTitle()->invalidateCache();
655 return $dbw->affectedRows();
659 * Makes an entry in the database corresponding to an edit
661 * @param string $timestamp
662 * @param Title $title
665 * @param string $comment
667 * @param string $lastTimestamp
670 * @param int $oldSize
671 * @param int $newSize
675 * @return RecentChange
677 public static function notifyEdit(
678 $timestamp, $title, $minor, $user, $comment, $oldId, $lastTimestamp,
679 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
682 $rc = new RecentChange
;
683 $rc->mTitle
= $title;
684 $rc->mPerformer
= $user;
686 'rc_timestamp' => $timestamp,
687 'rc_namespace' => $title->getNamespace(),
688 'rc_title' => $title->getDBkey(),
689 'rc_type' => RC_EDIT
,
690 'rc_source' => self
::SRC_EDIT
,
691 'rc_minor' => $minor ?
1 : 0,
692 'rc_cur_id' => $title->getArticleID(),
693 'rc_user' => $user->getId(),
694 'rc_user_text' => $user->getName(),
695 'rc_actor' => $user->getActorId(),
696 'rc_comment' => &$comment,
697 'rc_comment_text' => &$comment,
698 'rc_comment_data' => null,
699 'rc_this_oldid' => $newId,
700 'rc_last_oldid' => $oldId,
701 'rc_bot' => $bot ?
1 : 0,
702 'rc_ip' => self
::checkIPAddress( $ip ),
703 'rc_patrolled' => intval( $patrol ),
704 'rc_new' => 0, # obsolete
705 'rc_old_len' => $oldSize,
706 'rc_new_len' => $newSize,
709 'rc_log_type' => null,
710 'rc_log_action' => '',
715 'prefixedDBkey' => $title->getPrefixedDBkey(),
716 'lastTimestamp' => $lastTimestamp,
717 'oldSize' => $oldSize,
718 'newSize' => $newSize,
719 'pageStatus' => 'changed'
722 DeferredUpdates
::addCallableUpdate(
723 function () use ( $rc, $tags ) {
724 $rc->addTags( $tags );
727 DeferredUpdates
::POSTSEND
,
735 * Makes an entry in the database corresponding to page creation
736 * Note: the title object must be loaded with the new id using resetArticleID()
738 * @param string $timestamp
739 * @param Title $title
742 * @param string $comment
749 * @return RecentChange
751 public static function notifyNew(
752 $timestamp, $title, $minor, $user, $comment, $bot,
753 $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
755 $rc = new RecentChange
;
756 $rc->mTitle
= $title;
757 $rc->mPerformer
= $user;
759 'rc_timestamp' => $timestamp,
760 'rc_namespace' => $title->getNamespace(),
761 'rc_title' => $title->getDBkey(),
763 'rc_source' => self
::SRC_NEW
,
764 'rc_minor' => $minor ?
1 : 0,
765 'rc_cur_id' => $title->getArticleID(),
766 'rc_user' => $user->getId(),
767 'rc_user_text' => $user->getName(),
768 'rc_actor' => $user->getActorId(),
769 'rc_comment' => &$comment,
770 'rc_comment_text' => &$comment,
771 'rc_comment_data' => null,
772 'rc_this_oldid' => $newId,
773 'rc_last_oldid' => 0,
774 'rc_bot' => $bot ?
1 : 0,
775 'rc_ip' => self
::checkIPAddress( $ip ),
776 'rc_patrolled' => intval( $patrol ),
777 'rc_new' => 1, # obsolete
779 'rc_new_len' => $size,
782 'rc_log_type' => null,
783 'rc_log_action' => '',
788 'prefixedDBkey' => $title->getPrefixedDBkey(),
789 'lastTimestamp' => 0,
792 'pageStatus' => 'created'
795 DeferredUpdates
::addCallableUpdate(
796 function () use ( $rc, $tags ) {
797 $rc->addTags( $tags );
800 DeferredUpdates
::POSTSEND
,
808 * @param string $timestamp
809 * @param Title $title
811 * @param string $actionComment
813 * @param string $type
814 * @param string $action
815 * @param Title $target
816 * @param string $logComment
817 * @param string $params
819 * @param string $actionCommentIRC
822 public static function notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type,
823 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
825 global $wgLogRestrictions;
827 # Don't add private logs to RC!
828 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
831 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
832 $target, $logComment, $params, $newId, $actionCommentIRC );
839 * @param string $timestamp
840 * @param Title $title
842 * @param string $actionComment
844 * @param string $type
845 * @param string $action
846 * @param Title $target
847 * @param string $logComment
848 * @param string $params
850 * @param string $actionCommentIRC
851 * @param int $revId Id of associated revision, if any
852 * @param bool $isPatrollable Whether this log entry is patrollable
853 * @return RecentChange
855 public static function newLogEntry( $timestamp, $title, $user, $actionComment, $ip,
856 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
857 $revId = 0, $isPatrollable = false ) {
860 # # Get pageStatus for email notification
861 switch ( $type . '-' . $action ) {
862 case 'delete-delete':
863 case 'delete-delete_redir':
864 $pageStatus = 'deleted';
867 case 'move-move_redir':
868 $pageStatus = 'moved';
870 case 'delete-restore':
871 $pageStatus = 'restored';
873 case 'upload-upload':
874 $pageStatus = 'created';
876 case 'upload-overwrite':
878 $pageStatus = 'changed';
882 // Allow unpatrolled status for patrollable log entries
883 $markPatrolled = $isPatrollable ?
$user->isAllowed( 'autopatrol' ) : true;
885 $rc = new RecentChange
;
886 $rc->mTitle
= $target;
887 $rc->mPerformer
= $user;
889 'rc_timestamp' => $timestamp,
890 'rc_namespace' => $target->getNamespace(),
891 'rc_title' => $target->getDBkey(),
893 'rc_source' => self
::SRC_LOG
,
895 'rc_cur_id' => $target->getArticleID(),
896 'rc_user' => $user->getId(),
897 'rc_user_text' => $user->getName(),
898 'rc_actor' => $user->getActorId(),
899 'rc_comment' => &$logComment,
900 'rc_comment_text' => &$logComment,
901 'rc_comment_data' => null,
902 'rc_this_oldid' => $revId,
903 'rc_last_oldid' => 0,
904 'rc_bot' => $user->isAllowed( 'bot' ) ?
(int)$wgRequest->getBool( 'bot', true ) : 0,
905 'rc_ip' => self
::checkIPAddress( $ip ),
906 'rc_patrolled' => $markPatrolled ? self
::PRC_AUTOPATROLLED
: self
::PRC_UNPATROLLED
,
907 'rc_new' => 0, # obsolete
908 'rc_old_len' => null,
909 'rc_new_len' => null,
911 'rc_logid' => $newId,
912 'rc_log_type' => $type,
913 'rc_log_action' => $action,
914 'rc_params' => $params
918 'prefixedDBkey' => $title->getPrefixedDBkey(),
919 'lastTimestamp' => 0,
920 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
921 'pageStatus' => $pageStatus,
922 'actionCommentIRC' => $actionCommentIRC
929 * Constructs a RecentChange object for the given categorization
930 * This does not call save() on the object and thus does not write to the db
934 * @param string $timestamp Timestamp of the recent change to occur
935 * @param Title $categoryTitle Title of the category a page is being added to or removed from
936 * @param User|null $user User object of the user that made the change
937 * @param string $comment Change summary
938 * @param Title $pageTitle Title of the page that is being added or removed
939 * @param int $oldRevId Parent revision ID of this change
940 * @param int $newRevId Revision ID of this change
941 * @param string $lastTimestamp Parent revision timestamp of this change
942 * @param bool $bot true, if the change was made by a bot
943 * @param string $ip IP address of the user, if the change was made anonymously
944 * @param int $deleted Indicates whether the change has been deleted
945 * @param bool|null $added true, if the category was added, false for removed
947 * @return RecentChange
949 public static function newForCategorization(
951 Title
$categoryTitle,
963 // Done in a backwards compatible way.
965 'hidden-cat' => WikiCategoryPage
::factory( $categoryTitle )->isHidden()
967 if ( $added !== null ) {
968 $params['added'] = $added;
971 $rc = new RecentChange
;
972 $rc->mTitle
= $categoryTitle;
973 $rc->mPerformer
= $user;
975 'rc_timestamp' => $timestamp,
976 'rc_namespace' => $categoryTitle->getNamespace(),
977 'rc_title' => $categoryTitle->getDBkey(),
978 'rc_type' => RC_CATEGORIZE
,
979 'rc_source' => self
::SRC_CATEGORIZE
,
981 'rc_cur_id' => $pageTitle->getArticleID(),
982 'rc_user' => $user ?
$user->getId() : 0,
983 'rc_user_text' => $user ?
$user->getName() : '',
984 'rc_actor' => $user ?
$user->getActorId() : null,
985 'rc_comment' => &$comment,
986 'rc_comment_text' => &$comment,
987 'rc_comment_data' => null,
988 'rc_this_oldid' => $newRevId,
989 'rc_last_oldid' => $oldRevId,
990 'rc_bot' => $bot ?
1 : 0,
991 'rc_ip' => self
::checkIPAddress( $ip ),
992 'rc_patrolled' => self
::PRC_AUTOPATROLLED
, // Always patrolled, just like log entries
993 'rc_new' => 0, # obsolete
994 'rc_old_len' => null,
995 'rc_new_len' => null,
996 'rc_deleted' => $deleted,
998 'rc_log_type' => null,
999 'rc_log_action' => '',
1000 'rc_params' => serialize( $params )
1004 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
1005 'lastTimestamp' => $lastTimestamp,
1008 'pageStatus' => 'changed'
1015 * Get a parameter value
1019 * @param string $name parameter name
1022 public function getParam( $name ) {
1023 $params = $this->parseParams();
1024 return $params[$name] ??
null;
1028 * Initialises the members of this object from a mysql row object
1032 public function loadFromRow( $row ) {
1033 $this->mAttribs
= get_object_vars( $row );
1034 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
1035 // rc_deleted MUST be set
1036 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
;
1038 if ( isset( $this->mAttribs
['rc_ip'] ) ) {
1039 // Clean up CIDRs for Postgres per T164898. ("127.0.0.1" casts to "127.0.0.1/32")
1040 $n = strpos( $this->mAttribs
['rc_ip'], '/' );
1041 if ( $n !== false ) {
1042 $this->mAttribs
['rc_ip'] = substr( $this->mAttribs
['rc_ip'], 0, $n );
1046 $comment = CommentStore
::getStore()
1047 // Legacy because $row may have come from self::selectFields()
1048 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $row, true )
1050 $this->mAttribs
['rc_comment'] = &$comment;
1051 $this->mAttribs
['rc_comment_text'] = &$comment;
1052 $this->mAttribs
['rc_comment_data'] = null;
1054 $user = User
::newFromAnyId(
1055 $this->mAttribs
['rc_user'] ??
null,
1056 $this->mAttribs
['rc_user_text'] ??
null,
1057 $this->mAttribs
['rc_actor'] ??
null
1059 $this->mAttribs
['rc_user'] = $user->getId();
1060 $this->mAttribs
['rc_user_text'] = $user->getName();
1061 $this->mAttribs
['rc_actor'] = $user->getActorId();
1065 * Get an attribute value
1067 * @param string $name Attribute name
1070 public function getAttribute( $name ) {
1071 if ( $name === 'rc_comment' ) {
1072 return CommentStore
::getStore()
1073 ->getComment( 'rc_comment', $this->mAttribs
, true )->text
;
1076 if ( $name === 'rc_user' ||
$name === 'rc_user_text' ||
$name === 'rc_actor' ) {
1077 $user = User
::newFromAnyId(
1078 $this->mAttribs
['rc_user'] ??
null,
1079 $this->mAttribs
['rc_user_text'] ??
null,
1080 $this->mAttribs
['rc_actor'] ??
null
1082 if ( $name === 'rc_user' ) {
1083 return $user->getId();
1085 if ( $name === 'rc_user_text' ) {
1086 return $user->getName();
1088 if ( $name === 'rc_actor' ) {
1089 return $user->getActorId();
1093 return $this->mAttribs
[$name] ??
null;
1099 public function getAttributes() {
1100 return $this->mAttribs
;
1104 * Gets the end part of the diff URL associated with this object
1105 * Blank if no diff link should be displayed
1106 * @param bool $forceCur
1109 public function diffLinkTrail( $forceCur ) {
1110 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
1111 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
1112 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
1114 $trail .= '&diff=0';
1116 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
1126 * Returns the change size (HTML).
1127 * The lengths can be given optionally.
1132 public function getCharacterDifference( $old = 0, $new = 0 ) {
1134 $old = $this->mAttribs
['rc_old_len'];
1137 $new = $this->mAttribs
['rc_new_len'];
1139 if ( $old === null ||
$new === null ) {
1143 return ChangesList
::showCharacterDifference( $old, $new );
1146 private static function checkIPAddress( $ip ) {
1149 if ( !IP
::isIPAddress( $ip ) ) {
1150 throw new MWException( "Attempt to write \"" . $ip .
1151 "\" as an IP address into recent changes" );
1154 $ip = $wgRequest->getIP();
1164 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
1165 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
1166 * or rows which will be deleted soon shouldn't be included.
1168 * @param mixed $timestamp MWTimestamp compatible timestamp
1169 * @param int $tolerance Tolerance in seconds
1172 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
1175 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
1179 * Parses and returns the rc_params attribute
1183 * @return mixed|bool false on failed unserialization
1185 public function parseParams() {
1186 $rcParams = $this->getAttribute( 'rc_params' );
1188 Wikimedia\
suppressWarnings();
1189 $unserializedParams = unserialize( $rcParams );
1190 Wikimedia\restoreWarnings
();
1192 return $unserializedParams;
1196 * Tags to append to the recent change,
1197 * and associated revision/log
1201 * @param string|array $tags
1203 public function addTags( $tags ) {
1204 if ( is_string( $tags ) ) {
1205 $this->tags
[] = $tags;
1207 $this->tags
= array_merge( $tags, $this->tags
);