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 > MIGRATION_WRITE_BOTH
) {
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 written anymore.
236 throw new BadMethodCallException(
237 'Cannot use ' . __METHOD__
. ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
246 'rc_actor' => 'NULL',
266 ] + CommentStore
::getStore()->getFields( 'rc_comment' );
270 * Return the tables, fields, and join conditions to be selected to create
271 * a new recentchanges object.
273 * @return array With three keys:
274 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
275 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
276 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
278 public static function getQueryInfo() {
279 $commentQuery = CommentStore
::getStore()->getJoin( 'rc_comment' );
280 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
282 'tables' => [ 'recentchanges' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
305 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
306 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
313 * @param array $attribs
315 public function setAttribs( $attribs ) {
316 $this->mAttribs
= $attribs;
320 * @param array $extra
322 public function setExtra( $extra ) {
323 $this->mExtra
= $extra;
329 public function &getTitle() {
330 if ( $this->mTitle
=== false ) {
331 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
334 return $this->mTitle
;
338 * Get the User object of the person who performed this change.
342 public function getPerformer() {
343 if ( $this->mPerformer
=== false ) {
344 if ( !empty( $this->mAttribs
['rc_actor'] ) ) {
345 $this->mPerformer
= User
::newFromActorId( $this->mAttribs
['rc_actor'] );
346 } elseif ( !empty( $this->mAttribs
['rc_user'] ) ) {
347 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
348 } elseif ( !empty( $this->mAttribs
['rc_user_text'] ) ) {
349 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
351 throw new MWException( 'RecentChange object lacks rc_actor, rc_user, and rc_user_text' );
355 return $this->mPerformer
;
359 * Writes the data in this object to the database
361 * For compatibility reasons, the SEND_ constants internally reference a value
362 * that may seem negated from their purpose (none=true, feed=false). This is
363 * because the parameter used to be called "$noudp", defaulting to false.
365 * @param bool $send self::SEND_FEED or self::SEND_NONE
367 public function save( $send = self
::SEND_FEED
) {
368 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
370 if ( is_string( $send ) ) {
371 // Callers used to pass undocumented strings like 'noudp'
372 // or 'pleasedontudp' instead of self::SEND_NONE (true).
373 // @deprecated since 1.31 Use SEND_NONE instead.
374 $send = self
::SEND_NONE
;
377 $dbw = wfGetDB( DB_MASTER
);
378 if ( !is_array( $this->mExtra
) ) {
382 if ( !$wgPutIPinRC ) {
383 $this->mAttribs
['rc_ip'] = '';
386 # Strict mode fixups (not-NULL fields)
387 foreach ( [ 'minor', 'bot', 'new', 'patrolled', 'deleted' ] as $field ) {
388 $this->mAttribs
["rc_$field"] = (int)$this->mAttribs
["rc_$field"];
390 # ...more fixups (NULL fields)
391 foreach ( [ 'old_len', 'new_len' ] as $field ) {
392 $this->mAttribs
["rc_$field"] = isset( $this->mAttribs
["rc_$field"] )
393 ?
(int)$this->mAttribs
["rc_$field"]
397 # If our database is strict about IP addresses, use NULL instead of an empty string
398 $strictIPs = in_array( $dbw->getType(), [ 'oracle', 'postgres' ] ); // legacy
399 if ( $strictIPs && $this->mAttribs
['rc_ip'] == '' ) {
400 unset( $this->mAttribs
['rc_ip'] );
403 # Trim spaces on user supplied text
404 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
406 # Fixup database timestamps
407 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
409 # # If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
410 if ( $this->mAttribs
['rc_cur_id'] == 0 ) {
411 unset( $this->mAttribs
['rc_cur_id'] );
414 $row = $this->mAttribs
;
416 # Convert mAttribs['rc_comment'] for CommentStore
417 $comment = $row['rc_comment'];
418 unset( $row['rc_comment'], $row['rc_comment_text'], $row['rc_comment_data'] );
419 $row +
= CommentStore
::getStore()->insert( $dbw, 'rc_comment', $comment );
421 # Convert mAttribs['rc_user'] etc for ActorMigration
422 $user = User
::newFromAnyId(
423 isset( $row['rc_user'] ) ?
$row['rc_user'] : null,
424 isset( $row['rc_user_text'] ) ?
$row['rc_user_text'] : null,
425 isset( $row['rc_actor'] ) ?
$row['rc_actor'] : null
427 unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
428 $row +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
430 # Don't reuse an existing rc_id for the new row, if one happens to be
431 # set for some reason.
432 unset( $row['rc_id'] );
435 $dbw->insert( 'recentchanges', $row, __METHOD__
);
438 $this->mAttribs
['rc_id'] = $dbw->insertId();
441 // Avoid PHP 7.1 warning from passing $this by reference
443 Hooks
::run( 'RecentChange_save', [ &$rc ] );
445 if ( count( $this->tags
) ) {
446 ChangeTags
::addTags( $this->tags
, $this->mAttribs
['rc_id'],
447 $this->mAttribs
['rc_this_oldid'], $this->mAttribs
['rc_logid'], null, $this );
450 if ( $send === self
::SEND_FEED
) {
451 // Emit the change to external applications via RCFeeds.
452 $this->notifyRCFeeds();
455 # E-mail notifications
456 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
457 $editor = $this->getPerformer();
458 $title = $this->getTitle();
460 // Never send an RC notification email about categorization changes
462 Hooks
::run( 'AbortEmailNotification', [ $editor, $title, $this ] ) &&
463 $this->mAttribs
['rc_type'] != RC_CATEGORIZE
465 // @FIXME: This would be better as an extension hook
466 // Send emails or email jobs once this row is safely committed
467 $dbw->onTransactionIdle(
468 function () use ( $editor, $title ) {
469 $enotif = new EmailNotification();
470 $enotif->notifyOnPageChange(
473 $this->mAttribs
['rc_timestamp'],
474 $this->mAttribs
['rc_comment'],
475 $this->mAttribs
['rc_minor'],
476 $this->mAttribs
['rc_last_oldid'],
477 $this->mExtra
['pageStatus']
485 // Update the cached list of active users
486 if ( $this->mAttribs
['rc_user'] > 0 ) {
487 JobQueueGroup
::singleton()->lazyPush( RecentChangesUpdateJob
::newCacheUpdateJob() );
492 * Notify all the feeds about the change.
493 * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
495 public function notifyRCFeeds( array $feeds = null ) {
497 if ( $feeds === null ) {
501 $performer = $this->getPerformer();
503 foreach ( $feeds as $params ) {
505 'omit_bots' => false,
506 'omit_anon' => false,
507 'omit_user' => false,
508 'omit_minor' => false,
509 'omit_patrolled' => false,
513 ( $params['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
514 ( $params['omit_anon'] && $performer->isAnon() ) ||
515 ( $params['omit_user'] && !$performer->isAnon() ) ||
516 ( $params['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
517 ( $params['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
518 $this->mAttribs
['rc_type'] == RC_EXTERNAL
523 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
524 $actionComment = $this->mExtra
['actionCommentIRC'];
526 $actionComment = null;
529 $feed = RCFeed
::factory( $params );
530 $feed->notify( $this, $actionComment );
536 * @deprecated since 1.29 Use RCFeed::factory() instead
537 * @param string $uri URI to get the engine object for
538 * @param array $params
539 * @return RCFeedEngine The engine object
540 * @throws MWException
542 public static function getEngine( $uri, $params = [] ) {
543 // TODO: Merge into RCFeed::factory().
545 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
547 throw new MWException( "Invalid RCFeed uri: '$uri'" );
549 if ( !isset( $wgRCEngines[$scheme] ) ) {
550 throw new MWException( "Unknown RCFeedEngine scheme: '$scheme'" );
552 if ( defined( 'MW_PHPUNIT_TEST' ) && is_object( $wgRCEngines[$scheme] ) ) {
553 return $wgRCEngines[$scheme];
555 return new $wgRCEngines[$scheme]( $params );
559 * Mark a given change as patrolled
561 * @param RecentChange|int $change RecentChange or corresponding rc_id
562 * @param bool $auto For automatic patrol
563 * @param string|string[] $tags Change tags to add to the patrol log entry
564 * ($user should be able to add the specified tags before this is called)
565 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
567 public static function markPatrolled( $change, $auto = false, $tags = null ) {
570 $change = $change instanceof RecentChange
572 : self
::newFromId( $change );
574 if ( !$change instanceof RecentChange
) {
578 return $change->doMarkPatrolled( $wgUser, $auto, $tags );
582 * Mark this RecentChange as patrolled
584 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
585 * 'markedaspatrollederror-noautopatrol' as errors
586 * @param User $user User object doing the action
587 * @param bool $auto For automatic patrol
588 * @param string|string[] $tags Change tags to add to the patrol log entry
589 * ($user should be able to add the specified tags before this is called)
590 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
592 public function doMarkPatrolled( User
$user, $auto = false, $tags = null ) {
593 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
596 // If recentchanges patrol is disabled, only new pages or new file versions
597 // can be patrolled, provided the appropriate config variable is set
598 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) &&
599 ( !$wgUseFilePatrol ||
!( $this->getAttribute( 'rc_type' ) == RC_LOG
&&
600 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
601 $errors[] = [ 'rcpatroldisabled' ];
603 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
604 $right = $auto ?
'autopatrol' : 'patrol';
605 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
606 if ( !Hooks
::run( 'MarkPatrolled',
607 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ] )
609 $errors[] = [ 'hookaborted' ];
611 // Users without the 'autopatrol' right can't patrol their
613 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
614 && !$user->isAllowed( 'autopatrol' )
616 $errors[] = [ 'markedaspatrollederror-noautopatrol' ];
621 // If the change was patrolled already, do nothing
622 if ( $this->getAttribute( 'rc_patrolled' ) ) {
625 // Actually set the 'patrolled' flag in RC
626 $this->reallyMarkPatrolled();
627 // Log this patrol event
628 PatrolLog
::record( $this, $auto, $user, $tags );
631 'MarkPatrolledComplete',
632 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ]
639 * Mark this RecentChange patrolled, without error checking
640 * @return int Number of affected rows
642 public function reallyMarkPatrolled() {
643 $dbw = wfGetDB( DB_MASTER
);
650 'rc_id' => $this->getAttribute( 'rc_id' )
654 // Invalidate the page cache after the page has been patrolled
655 // to make sure that the Patrol link isn't visible any longer!
656 $this->getTitle()->invalidateCache();
658 return $dbw->affectedRows();
662 * Makes an entry in the database corresponding to an edit
664 * @param string $timestamp
665 * @param Title &$title
668 * @param string $comment
670 * @param string $lastTimestamp
673 * @param int $oldSize
674 * @param int $newSize
678 * @return RecentChange
680 public static function notifyEdit(
681 $timestamp, &$title, $minor, &$user, $comment, $oldId, $lastTimestamp,
682 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
685 $rc = new RecentChange
;
686 $rc->mTitle
= $title;
687 $rc->mPerformer
= $user;
689 'rc_timestamp' => $timestamp,
690 'rc_namespace' => $title->getNamespace(),
691 'rc_title' => $title->getDBkey(),
692 'rc_type' => RC_EDIT
,
693 'rc_source' => self
::SRC_EDIT
,
694 'rc_minor' => $minor ?
1 : 0,
695 'rc_cur_id' => $title->getArticleID(),
696 'rc_user' => $user->getId(),
697 'rc_user_text' => $user->getName(),
698 'rc_actor' => $user->getActorId(),
699 'rc_comment' => &$comment,
700 'rc_comment_text' => &$comment,
701 'rc_comment_data' => null,
702 'rc_this_oldid' => $newId,
703 'rc_last_oldid' => $oldId,
704 'rc_bot' => $bot ?
1 : 0,
705 'rc_ip' => self
::checkIPAddress( $ip ),
706 'rc_patrolled' => intval( $patrol ),
707 'rc_new' => 0, # obsolete
708 'rc_old_len' => $oldSize,
709 'rc_new_len' => $newSize,
712 'rc_log_type' => null,
713 'rc_log_action' => '',
718 'prefixedDBkey' => $title->getPrefixedDBkey(),
719 'lastTimestamp' => $lastTimestamp,
720 'oldSize' => $oldSize,
721 'newSize' => $newSize,
722 'pageStatus' => 'changed'
725 DeferredUpdates
::addCallableUpdate(
726 function () use ( $rc, $tags ) {
727 $rc->addTags( $tags );
729 if ( $rc->mAttribs
['rc_patrolled'] ) {
730 PatrolLog
::record( $rc, true, $rc->getPerformer() );
733 DeferredUpdates
::POSTSEND
,
741 * Makes an entry in the database corresponding to page creation
742 * Note: the title object must be loaded with the new id using resetArticleID()
744 * @param string $timestamp
745 * @param Title &$title
748 * @param string $comment
755 * @return RecentChange
757 public static function notifyNew(
758 $timestamp, &$title, $minor, &$user, $comment, $bot,
759 $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
761 $rc = new RecentChange
;
762 $rc->mTitle
= $title;
763 $rc->mPerformer
= $user;
765 'rc_timestamp' => $timestamp,
766 'rc_namespace' => $title->getNamespace(),
767 'rc_title' => $title->getDBkey(),
769 'rc_source' => self
::SRC_NEW
,
770 'rc_minor' => $minor ?
1 : 0,
771 'rc_cur_id' => $title->getArticleID(),
772 'rc_user' => $user->getId(),
773 'rc_user_text' => $user->getName(),
774 'rc_actor' => $user->getActorId(),
775 'rc_comment' => &$comment,
776 'rc_comment_text' => &$comment,
777 'rc_comment_data' => null,
778 'rc_this_oldid' => $newId,
779 'rc_last_oldid' => 0,
780 'rc_bot' => $bot ?
1 : 0,
781 'rc_ip' => self
::checkIPAddress( $ip ),
782 'rc_patrolled' => intval( $patrol ),
783 'rc_new' => 1, # obsolete
785 'rc_new_len' => $size,
788 'rc_log_type' => null,
789 'rc_log_action' => '',
794 'prefixedDBkey' => $title->getPrefixedDBkey(),
795 'lastTimestamp' => 0,
798 'pageStatus' => 'created'
801 DeferredUpdates
::addCallableUpdate(
802 function () use ( $rc, $tags ) {
803 $rc->addTags( $tags );
805 if ( $rc->mAttribs
['rc_patrolled'] ) {
806 PatrolLog
::record( $rc, true, $rc->getPerformer() );
809 DeferredUpdates
::POSTSEND
,
817 * @param string $timestamp
818 * @param Title &$title
820 * @param string $actionComment
822 * @param string $type
823 * @param string $action
824 * @param Title $target
825 * @param string $logComment
826 * @param string $params
828 * @param string $actionCommentIRC
831 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
832 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
834 global $wgLogRestrictions;
836 # Don't add private logs to RC!
837 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
840 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
841 $target, $logComment, $params, $newId, $actionCommentIRC );
848 * @param string $timestamp
849 * @param Title &$title
851 * @param string $actionComment
853 * @param string $type
854 * @param string $action
855 * @param Title $target
856 * @param string $logComment
857 * @param string $params
859 * @param string $actionCommentIRC
860 * @param int $revId Id of associated revision, if any
861 * @param bool $isPatrollable Whether this log entry is patrollable
862 * @return RecentChange
864 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
865 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
866 $revId = 0, $isPatrollable = false ) {
869 # # Get pageStatus for email notification
870 switch ( $type . '-' . $action ) {
871 case 'delete-delete':
872 case 'delete-delete_redir':
873 $pageStatus = 'deleted';
876 case 'move-move_redir':
877 $pageStatus = 'moved';
879 case 'delete-restore':
880 $pageStatus = 'restored';
882 case 'upload-upload':
883 $pageStatus = 'created';
885 case 'upload-overwrite':
887 $pageStatus = 'changed';
891 // Allow unpatrolled status for patrollable log entries
892 $markPatrolled = $isPatrollable ?
$user->isAllowed( 'autopatrol' ) : true;
894 $rc = new RecentChange
;
895 $rc->mTitle
= $target;
896 $rc->mPerformer
= $user;
898 'rc_timestamp' => $timestamp,
899 'rc_namespace' => $target->getNamespace(),
900 'rc_title' => $target->getDBkey(),
902 'rc_source' => self
::SRC_LOG
,
904 'rc_cur_id' => $target->getArticleID(),
905 'rc_user' => $user->getId(),
906 'rc_user_text' => $user->getName(),
907 'rc_actor' => $user->getActorId(),
908 'rc_comment' => &$logComment,
909 'rc_comment_text' => &$logComment,
910 'rc_comment_data' => null,
911 'rc_this_oldid' => $revId,
912 'rc_last_oldid' => 0,
913 'rc_bot' => $user->isAllowed( 'bot' ) ?
(int)$wgRequest->getBool( 'bot', true ) : 0,
914 'rc_ip' => self
::checkIPAddress( $ip ),
915 'rc_patrolled' => $markPatrolled ?
1 : 0,
916 'rc_new' => 0, # obsolete
917 'rc_old_len' => null,
918 'rc_new_len' => null,
920 'rc_logid' => $newId,
921 'rc_log_type' => $type,
922 'rc_log_action' => $action,
923 'rc_params' => $params
927 'prefixedDBkey' => $title->getPrefixedDBkey(),
928 'lastTimestamp' => 0,
929 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
930 'pageStatus' => $pageStatus,
931 'actionCommentIRC' => $actionCommentIRC
938 * Constructs a RecentChange object for the given categorization
939 * This does not call save() on the object and thus does not write to the db
943 * @param string $timestamp Timestamp of the recent change to occur
944 * @param Title $categoryTitle Title of the category a page is being added to or removed from
945 * @param User $user User object of the user that made the change
946 * @param string $comment Change summary
947 * @param Title $pageTitle Title of the page that is being added or removed
948 * @param int $oldRevId Parent revision ID of this change
949 * @param int $newRevId Revision ID of this change
950 * @param string $lastTimestamp Parent revision timestamp of this change
951 * @param bool $bot true, if the change was made by a bot
952 * @param string $ip IP address of the user, if the change was made anonymously
953 * @param int $deleted Indicates whether the change has been deleted
954 * @param bool $added true, if the category was added, false for removed
956 * @return RecentChange
958 public static function newForCategorization(
960 Title
$categoryTitle,
972 // Done in a backwards compatible way.
974 'hidden-cat' => WikiCategoryPage
::factory( $categoryTitle )->isHidden()
976 if ( $added !== null ) {
977 $params['added'] = $added;
980 $rc = new RecentChange
;
981 $rc->mTitle
= $categoryTitle;
982 $rc->mPerformer
= $user;
984 'rc_timestamp' => $timestamp,
985 'rc_namespace' => $categoryTitle->getNamespace(),
986 'rc_title' => $categoryTitle->getDBkey(),
987 'rc_type' => RC_CATEGORIZE
,
988 'rc_source' => self
::SRC_CATEGORIZE
,
990 'rc_cur_id' => $pageTitle->getArticleID(),
991 'rc_user' => $user ?
$user->getId() : 0,
992 'rc_user_text' => $user ?
$user->getName() : '',
993 'rc_actor' => $user ?
$user->getActorId() : null,
994 'rc_comment' => &$comment,
995 'rc_comment_text' => &$comment,
996 'rc_comment_data' => null,
997 'rc_this_oldid' => $newRevId,
998 'rc_last_oldid' => $oldRevId,
999 'rc_bot' => $bot ?
1 : 0,
1000 'rc_ip' => self
::checkIPAddress( $ip ),
1001 'rc_patrolled' => 1, // Always patrolled, just like log entries
1002 'rc_new' => 0, # obsolete
1003 'rc_old_len' => null,
1004 'rc_new_len' => null,
1005 'rc_deleted' => $deleted,
1007 'rc_log_type' => null,
1008 'rc_log_action' => '',
1009 'rc_params' => serialize( $params )
1013 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
1014 'lastTimestamp' => $lastTimestamp,
1017 'pageStatus' => 'changed'
1024 * Get a parameter value
1028 * @param string $name parameter name
1031 public function getParam( $name ) {
1032 $params = $this->parseParams();
1033 return isset( $params[$name] ) ?
$params[$name] : null;
1037 * Initialises the members of this object from a mysql row object
1041 public function loadFromRow( $row ) {
1042 $this->mAttribs
= get_object_vars( $row );
1043 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
1044 // rc_deleted MUST be set
1045 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
;
1047 if ( isset( $this->mAttribs
['rc_ip'] ) ) {
1048 // Clean up CIDRs for Postgres per T164898. ("127.0.0.1" casts to "127.0.0.1/32")
1049 $n = strpos( $this->mAttribs
['rc_ip'], '/' );
1050 if ( $n !== false ) {
1051 $this->mAttribs
['rc_ip'] = substr( $this->mAttribs
['rc_ip'], 0, $n );
1055 $comment = CommentStore
::getStore()
1056 // Legacy because $row may have come from self::selectFields()
1057 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $row, true )
1059 $this->mAttribs
['rc_comment'] = &$comment;
1060 $this->mAttribs
['rc_comment_text'] = &$comment;
1061 $this->mAttribs
['rc_comment_data'] = null;
1063 $user = User
::newFromAnyId(
1064 isset( $this->mAttribs
['rc_user'] ) ?
$this->mAttribs
['rc_user'] : null,
1065 isset( $this->mAttribs
['rc_user_text'] ) ?
$this->mAttribs
['rc_user_text'] : null,
1066 isset( $this->mAttribs
['rc_actor'] ) ?
$this->mAttribs
['rc_actor'] : null
1068 $this->mAttribs
['rc_user'] = $user->getId();
1069 $this->mAttribs
['rc_user_text'] = $user->getName();
1070 $this->mAttribs
['rc_actor'] = $user->getActorId();
1074 * Get an attribute value
1076 * @param string $name Attribute name
1079 public function getAttribute( $name ) {
1080 if ( $name === 'rc_comment' ) {
1081 return CommentStore
::getStore()
1082 ->getComment( 'rc_comment', $this->mAttribs
, true )->text
;
1085 if ( $name === 'rc_user' ||
$name === 'rc_user_text' ||
$name === 'rc_actor' ) {
1086 $user = User
::newFromAnyId(
1087 isset( $this->mAttribs
['rc_user'] ) ?
$this->mAttribs
['rc_user'] : null,
1088 isset( $this->mAttribs
['rc_user_text'] ) ?
$this->mAttribs
['rc_user_text'] : null,
1089 isset( $this->mAttribs
['rc_actor'] ) ?
$this->mAttribs
['rc_actor'] : null
1091 if ( $name === 'rc_user' ) {
1092 return $user->getId();
1094 if ( $name === 'rc_user_text' ) {
1095 return $user->getName();
1097 if ( $name === 'rc_actor' ) {
1098 return $user->getActorId();
1102 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
1108 public function getAttributes() {
1109 return $this->mAttribs
;
1113 * Gets the end part of the diff URL associated with this object
1114 * Blank if no diff link should be displayed
1115 * @param bool $forceCur
1118 public function diffLinkTrail( $forceCur ) {
1119 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
1120 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
1121 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
1123 $trail .= '&diff=0';
1125 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
1135 * Returns the change size (HTML).
1136 * The lengths can be given optionally.
1141 public function getCharacterDifference( $old = 0, $new = 0 ) {
1143 $old = $this->mAttribs
['rc_old_len'];
1146 $new = $this->mAttribs
['rc_new_len'];
1148 if ( $old === null ||
$new === null ) {
1152 return ChangesList
::showCharacterDifference( $old, $new );
1155 private static function checkIPAddress( $ip ) {
1158 if ( !IP
::isIPAddress( $ip ) ) {
1159 throw new MWException( "Attempt to write \"" . $ip .
1160 "\" as an IP address into recent changes" );
1163 $ip = $wgRequest->getIP();
1173 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
1174 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
1175 * or rows which will be deleted soon shouldn't be included.
1177 * @param mixed $timestamp MWTimestamp compatible timestamp
1178 * @param int $tolerance Tolerance in seconds
1181 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
1184 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
1188 * Parses and returns the rc_params attribute
1192 * @return mixed|bool false on failed unserialization
1194 public function parseParams() {
1195 $rcParams = $this->getAttribute( 'rc_params' );
1197 Wikimedia\
suppressWarnings();
1198 $unserializedParams = unserialize( $rcParams );
1199 Wikimedia\restoreWarnings
();
1201 return $unserializedParams;
1205 * Tags to append to the recent change,
1206 * and associated revision/log
1210 * @param string|array $tags
1212 public function addTags( $tags ) {
1213 if ( is_string( $tags ) ) {
1214 $this->tags
[] = $tags;
1216 $this->tags
= array_merge( $tags, $this->tags
);