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_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
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 * lang the interwiki prefix, automatically set in save()
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
65 // Constants for the rc_source field. Extensions may also have
66 // their own source constants.
67 const SRC_EDIT
= 'mw.edit';
68 const SRC_NEW
= 'mw.new';
69 const SRC_LOG
= 'mw.log';
70 const SRC_EXTERNAL
= 'mw.external'; // obsolete
72 public $mAttribs = array();
73 public $mExtra = array();
78 public $mTitle = false;
83 private $mPerformer = false;
85 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
86 public $notificationtimestamp;
89 * @var int Line number of recent change. Default -1.
97 * @return RecentChange
99 public static function newFromRow( $row ) {
100 $rc = new RecentChange
;
101 $rc->loadFromRow( $row );
107 * No uses left in Gerrit on 2013-11-19.
108 * @deprecated in 1.22
110 * @return RecentChange
112 public static function newFromCurRow( $row ) {
113 wfDeprecated( __METHOD__
, '1.22' );
114 $rc = new RecentChange
;
115 $rc->loadFromCurRow( $row );
116 $rc->notificationtimestamp
= false;
117 $rc->numberofWatchingusers
= false;
123 * Obtain the recent change with a given rc_id value
125 * @param int $rcid rc_id value to retrieve
126 * @return RecentChange
128 public static function newFromId( $rcid ) {
129 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
133 * Find the first recent change matching some specific conditions
135 * @param array $conds of conditions
136 * @param $fname Mixed: override the method name in profiling/logs
137 * @param $options Array Query options
138 * @return RecentChange
140 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
141 $dbr = wfGetDB( DB_SLAVE
);
142 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
143 if ( $row !== false ) {
144 return self
::newFromRow( $row );
151 * Return the list of recentchanges fields that should be selected to create
152 * a new recentchanges object.
155 public static function selectFields() {
187 * @param $attribs array
189 public function setAttribs( $attribs ) {
190 $this->mAttribs
= $attribs;
194 * @param $extra array
196 public function setExtra( $extra ) {
197 $this->mExtra
= $extra;
204 public function &getTitle() {
205 if ( $this->mTitle
=== false ) {
206 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
209 return $this->mTitle
;
213 * Get the User object of the person who performed this change.
217 public function getPerformer() {
218 if ( $this->mPerformer
=== false ) {
219 if ( $this->mAttribs
['rc_user'] ) {
220 $this->mPerformer
= User
::newFromID( $this->mAttribs
['rc_user'] );
222 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
226 return $this->mPerformer
;
230 * Writes the data in this object to the database
233 public function save( $noudp = false ) {
234 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
236 $dbw = wfGetDB( DB_MASTER
);
237 if ( !is_array( $this->mExtra
) ) {
238 $this->mExtra
= array();
240 $this->mExtra
['lang'] = $wgLocalInterwiki;
242 if ( !$wgPutIPinRC ) {
243 $this->mAttribs
['rc_ip'] = '';
246 # If our database is strict about IP addresses, use NULL instead of an empty string
247 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
248 unset( $this->mAttribs
['rc_ip'] );
251 # Trim spaces on user supplied text
252 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
254 # Make sure summary is truncated (whole multibyte characters)
255 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
257 # Fixup database timestamps
258 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
259 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
261 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
262 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id'] == 0 ) {
263 unset( $this->mAttribs
['rc_cur_id'] );
267 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
270 $this->mAttribs
['rc_id'] = $dbw->insertId();
273 wfRunHooks( 'RecentChange_save', array( &$this ) );
275 # Notify external application via UDP
277 $this->notifyRCFeeds();
280 # E-mail notifications
281 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
282 $editor = $this->getPerformer();
283 $title = $this->getTitle();
285 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
286 # @todo FIXME: This would be better as an extension hook
287 $enotif = new EmailNotification();
288 $enotif->notifyOnPageChange( $editor, $title,
289 $this->mAttribs
['rc_timestamp'],
290 $this->mAttribs
['rc_comment'],
291 $this->mAttribs
['rc_minor'],
292 $this->mAttribs
['rc_last_oldid'],
293 $this->mExtra
['pageStatus'] );
299 * @deprecated since 1.22, use notifyRCFeeds instead.
301 public function notifyRC2UDP() {
302 wfDeprecated( __METHOD__
, '1.22' );
303 $this->notifyRCFeeds();
307 * Send some text to UDP.
308 * @deprecated since 1.22
310 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
311 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
313 wfDeprecated( __METHOD__
, '1.22' );
315 # Assume default for standard RC case
316 $address = $address ?
$address : $wgRC2UDPAddress;
317 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
318 $port = $port ?
$port : $wgRC2UDPPort;
320 $engine = new UDPRCFeedEngine();
322 'uri' => "udp://$address:$port/$prefix",
323 'formatter' => 'IRCColourfulRCFeedFormatter',
324 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
327 $engine->send( $feed, $line );
331 * Notify all the feeds about the change.
333 public function notifyRCFeeds() {
336 foreach ( $wgRCFeeds as $feed ) {
337 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
340 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
341 $this->mAttribs
['rc_type'] == RC_EXTERNAL
346 $engine = self
::getEngine( $feed['uri'] );
348 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
349 $actionComment = $this->mExtra
['actionCommentIRC'];
351 $actionComment = null;
354 /** @var $formatter RCFeedFormatter */
355 $formatter = new $feed['formatter']();
356 $line = $formatter->getLine( $feed, $this, $actionComment );
358 $engine->send( $feed, $line );
363 * Gets the stream engine object for a given URI from $wgRCEngines
365 * @param string $uri URI to get the engine object for
366 * @throws MWException
367 * @return RCFeedEngine The engine object
369 public static function getEngine( $uri ) {
372 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
374 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
377 if ( !isset( $wgRCEngines[$scheme] ) ) {
378 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
381 return new $wgRCEngines[$scheme];
385 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
387 public static function cleanupForIRC( $text ) {
388 wfDeprecated( __METHOD__
, '1.22' );
390 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
394 * Mark a given change as patrolled
396 * @param $change Mixed: RecentChange or corresponding rc_id
397 * @param $auto Boolean: for automatic patrol
398 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
400 public static function markPatrolled( $change, $auto = false ) {
403 $change = $change instanceof RecentChange
405 : RecentChange
::newFromId( $change );
407 if ( !$change instanceof RecentChange
) {
411 return $change->doMarkPatrolled( $wgUser, $auto );
415 * Mark this RecentChange as patrolled
417 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
418 * 'markedaspatrollederror-noautopatrol' as errors
419 * @param $user User object doing the action
420 * @param $auto Boolean: for automatic patrol
421 * @return array of permissions errors, see Title::getUserPermissionsErrors()
423 public function doMarkPatrolled( User
$user, $auto = false ) {
424 global $wgUseRCPatrol, $wgUseNPPatrol;
426 // If recentchanges patrol is disabled, only new pages
428 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
429 $errors[] = array( 'rcpatroldisabled' );
431 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
432 $right = $auto ?
'autopatrol' : 'patrol';
433 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
434 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
435 $errors[] = array( 'hookaborted' );
437 // Users without the 'autopatrol' right can't patrol their
439 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
440 && !$user->isAllowed( 'autopatrol' )
442 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
447 // If the change was patrolled already, do nothing
448 if ( $this->getAttribute( 'rc_patrolled' ) ) {
451 // Actually set the 'patrolled' flag in RC
452 $this->reallyMarkPatrolled();
453 // Log this patrol event
454 PatrolLog
::record( $this, $auto, $user );
455 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
461 * Mark this RecentChange patrolled, without error checking
462 * @return Integer: number of affected rows
464 public function reallyMarkPatrolled() {
465 $dbw = wfGetDB( DB_MASTER
);
472 'rc_id' => $this->getAttribute( 'rc_id' )
476 // Invalidate the page cache after the page has been patrolled
477 // to make sure that the Patrol link isn't visible any longer!
478 $this->getTitle()->invalidateCache();
480 return $dbw->affectedRows();
484 * Makes an entry in the database corresponding to an edit
487 * @param $title Title
492 * @param $lastTimestamp
495 * @param $oldSize int
496 * @param $newSize int
499 * @return RecentChange
501 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
502 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
503 $rc = new RecentChange
;
504 $rc->mTitle
= $title;
505 $rc->mPerformer
= $user;
506 $rc->mAttribs
= array(
507 'rc_timestamp' => $timestamp,
508 'rc_namespace' => $title->getNamespace(),
509 'rc_title' => $title->getDBkey(),
510 'rc_type' => RC_EDIT
,
511 'rc_source' => self
::SRC_EDIT
,
512 'rc_minor' => $minor ?
1 : 0,
513 'rc_cur_id' => $title->getArticleID(),
514 'rc_user' => $user->getId(),
515 'rc_user_text' => $user->getName(),
516 'rc_comment' => $comment,
517 'rc_this_oldid' => $newId,
518 'rc_last_oldid' => $oldId,
519 'rc_bot' => $bot ?
1 : 0,
520 'rc_ip' => self
::checkIPAddress( $ip ),
521 'rc_patrolled' => intval( $patrol ),
522 'rc_new' => 0, # obsolete
523 'rc_old_len' => $oldSize,
524 'rc_new_len' => $newSize,
527 'rc_log_type' => null,
528 'rc_log_action' => '',
533 'prefixedDBkey' => $title->getPrefixedDBkey(),
534 'lastTimestamp' => $lastTimestamp,
535 'oldSize' => $oldSize,
536 'newSize' => $newSize,
537 'pageStatus' => 'changed'
545 * Makes an entry in the database corresponding to page creation
546 * Note: the title object must be loaded with the new id using resetArticleID()
549 * @param $title Title
558 * @return RecentChange
560 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
561 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
562 $rc = new RecentChange
;
563 $rc->mTitle
= $title;
564 $rc->mPerformer
= $user;
565 $rc->mAttribs
= array(
566 'rc_timestamp' => $timestamp,
567 'rc_namespace' => $title->getNamespace(),
568 'rc_title' => $title->getDBkey(),
570 'rc_source' => self
::SRC_NEW
,
571 'rc_minor' => $minor ?
1 : 0,
572 'rc_cur_id' => $title->getArticleID(),
573 'rc_user' => $user->getId(),
574 'rc_user_text' => $user->getName(),
575 'rc_comment' => $comment,
576 'rc_this_oldid' => $newId,
577 'rc_last_oldid' => 0,
578 'rc_bot' => $bot ?
1 : 0,
579 'rc_ip' => self
::checkIPAddress( $ip ),
580 'rc_patrolled' => intval( $patrol ),
581 'rc_new' => 1, # obsolete
583 'rc_new_len' => $size,
586 'rc_log_type' => null,
587 'rc_log_action' => '',
592 'prefixedDBkey' => $title->getPrefixedDBkey(),
593 'lastTimestamp' => 0,
596 'pageStatus' => 'created'
607 * @param $actionComment
615 * @param $actionCommentIRC string
618 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
619 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
621 global $wgLogRestrictions;
623 # Don't add private logs to RC!
624 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
627 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
628 $target, $logComment, $params, $newId, $actionCommentIRC );
636 * @param $title Title
638 * @param $actionComment
642 * @param $target Title
646 * @param $actionCommentIRC string
647 * @return RecentChange
649 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
650 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
653 ## Get pageStatus for email notification
654 switch ( $type . '-' . $action ) {
655 case 'delete-delete':
656 $pageStatus = 'deleted';
659 case 'move-move_redir':
660 $pageStatus = 'moved';
662 case 'delete-restore':
663 $pageStatus = 'restored';
665 case 'upload-upload':
666 $pageStatus = 'created';
668 case 'upload-overwrite':
670 $pageStatus = 'changed';
674 $rc = new RecentChange
;
675 $rc->mTitle
= $target;
676 $rc->mPerformer
= $user;
677 $rc->mAttribs
= array(
678 'rc_timestamp' => $timestamp,
679 'rc_namespace' => $target->getNamespace(),
680 'rc_title' => $target->getDBkey(),
682 'rc_source' => self
::SRC_LOG
,
684 'rc_cur_id' => $target->getArticleID(),
685 'rc_user' => $user->getId(),
686 'rc_user_text' => $user->getName(),
687 'rc_comment' => $logComment,
688 'rc_this_oldid' => 0,
689 'rc_last_oldid' => 0,
690 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
691 'rc_ip' => self
::checkIPAddress( $ip ),
693 'rc_new' => 0, # obsolete
694 'rc_old_len' => null,
695 'rc_new_len' => null,
697 'rc_logid' => $newId,
698 'rc_log_type' => $type,
699 'rc_log_action' => $action,
700 'rc_params' => $params
704 'prefixedDBkey' => $title->getPrefixedDBkey(),
705 'lastTimestamp' => 0,
706 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
707 'pageStatus' => $pageStatus,
708 'actionCommentIRC' => $actionCommentIRC
715 * Initialises the members of this object from a mysql row object
719 public function loadFromRow( $row ) {
720 $this->mAttribs
= get_object_vars( $row );
721 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
722 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
726 * Makes a pseudo-RC entry from a cur row
728 * @deprecated in 1.22
731 public function loadFromCurRow( $row ) {
732 wfDeprecated( __METHOD__
, '1.22' );
733 $this->mAttribs
= array(
734 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
735 'rc_user' => $row->rev_user
,
736 'rc_user_text' => $row->rev_user_text
,
737 'rc_namespace' => $row->page_namespace
,
738 'rc_title' => $row->page_title
,
739 'rc_comment' => $row->rev_comment
,
740 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
741 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
742 'rc_source' => $row->page_is_new ? self
::SRC_NEW
: self
::SRC_EDIT
,
743 'rc_cur_id' => $row->page_id
,
744 'rc_this_oldid' => $row->rev_id
,
745 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
748 'rc_id' => $row->rc_id
,
749 'rc_patrolled' => $row->rc_patrolled
,
750 'rc_new' => $row->page_is_new
, # obsolete
751 'rc_old_len' => $row->rc_old_len
,
752 'rc_new_len' => $row->rc_new_len
,
753 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
754 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
755 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
756 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
757 'rc_deleted' => $row->rc_deleted
// MUST be set
762 * Get an attribute value
764 * @param string $name Attribute name
767 public function getAttribute( $name ) {
768 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
774 public function getAttributes() {
775 return $this->mAttribs
;
779 * Gets the end part of the diff URL associated with this object
780 * Blank if no diff link should be displayed
784 public function diffLinkTrail( $forceCur ) {
785 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
786 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
787 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
791 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
801 * Returns the change size (HTML).
802 * The lengths can be given optionally.
807 public function getCharacterDifference( $old = 0, $new = 0 ) {
809 $old = $this->mAttribs
['rc_old_len'];
812 $new = $this->mAttribs
['rc_new_len'];
814 if ( $old === null ||
$new === null ) {
818 return ChangesList
::showCharacterDifference( $old, $new );
822 * Purge expired changes from the recentchanges table
825 public static function purgeExpiredChanges() {
826 if ( wfReadOnly() ) {
830 $method = __METHOD__
;
831 $dbw = wfGetDB( DB_MASTER
);
832 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
835 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
838 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
844 private static function checkIPAddress( $ip ) {
847 if ( !IP
::isIPAddress( $ip ) ) {
848 throw new MWException( "Attempt to write \"" . $ip .
849 "\" as an IP address into recent changes" );
852 $ip = $wgRequest->getIP();
862 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
863 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
864 * or rows which will be deleted soon shouldn't be included.
866 * @param $timestamp mixed MWTimestamp compatible timestamp
867 * @param $tolerance integer Tolerance in seconds
870 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
873 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;