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
64 * @todo document functions and variables
67 // Constants for the rc_source field. Extensions may also have
68 // their own source constants.
69 const SRC_EDIT
= 'mw.edit';
70 const SRC_NEW
= 'mw.new';
71 const SRC_LOG
= 'mw.log';
72 const SRC_EXTERNAL
= 'mw.external'; // obsolete
74 public $mAttribs = array();
75 public $mExtra = array();
80 public $mTitle = false;
85 private $mPerformer = false;
87 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentchangeslinked
88 public $notificationtimestamp;
91 * @var int Line number of recent change. Default -1.
99 * @return RecentChange
101 public static function newFromRow( $row ) {
102 $rc = new RecentChange
;
103 $rc->loadFromRow( $row );
109 * No uses left in Gerrit on 2013-11-19.
110 * @deprecated in 1.22
112 * @return RecentChange
114 public static function newFromCurRow( $row ) {
115 wfDeprecated( __METHOD__
, '1.22' );
116 $rc = new RecentChange
;
117 $rc->loadFromCurRow( $row );
118 $rc->notificationtimestamp
= false;
119 $rc->numberofWatchingusers
= false;
125 * Obtain the recent change with a given rc_id value
127 * @param int $rcid rc_id value to retrieve
128 * @return RecentChange
130 public static function newFromId( $rcid ) {
131 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
135 * Find the first recent change matching some specific conditions
137 * @param array $conds of conditions
138 * @param $fname Mixed: override the method name in profiling/logs
139 * @param $options Array Query options
140 * @return RecentChange
142 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
143 $dbr = wfGetDB( DB_SLAVE
);
144 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
145 if ( $row !== false ) {
146 return self
::newFromRow( $row );
153 * Return the list of recentchanges fields that should be selected to create
154 * a new recentchanges object.
157 public static function selectFields() {
189 * @param $attribs array
191 public function setAttribs( $attribs ) {
192 $this->mAttribs
= $attribs;
196 * @param $extra array
198 public function setExtra( $extra ) {
199 $this->mExtra
= $extra;
206 public function &getTitle() {
207 if ( $this->mTitle
=== false ) {
208 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
211 return $this->mTitle
;
215 * Get the User object of the person who performed this change.
219 public function getPerformer() {
220 if ( $this->mPerformer
=== false ) {
221 if ( $this->mAttribs
['rc_user'] ) {
222 $this->mPerformer
= User
::newFromID( $this->mAttribs
['rc_user'] );
224 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
228 return $this->mPerformer
;
232 * Writes the data in this object to the database
235 public function save( $noudp = false ) {
236 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
238 $dbw = wfGetDB( DB_MASTER
);
239 if ( !is_array( $this->mExtra
) ) {
240 $this->mExtra
= array();
242 $this->mExtra
['lang'] = $wgLocalInterwiki;
244 if ( !$wgPutIPinRC ) {
245 $this->mAttribs
['rc_ip'] = '';
248 # If our database is strict about IP addresses, use NULL instead of an empty string
249 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
250 unset( $this->mAttribs
['rc_ip'] );
253 # Trim spaces on user supplied text
254 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
256 # Make sure summary is truncated (whole multibyte characters)
257 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
259 # Fixup database timestamps
260 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
261 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
263 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
264 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id'] == 0 ) {
265 unset( $this->mAttribs
['rc_cur_id'] );
269 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
272 $this->mAttribs
['rc_id'] = $dbw->insertId();
275 wfRunHooks( 'RecentChange_save', array( &$this ) );
277 # Notify external application via UDP
279 $this->notifyRCFeeds();
282 # E-mail notifications
283 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
284 $editor = $this->getPerformer();
285 $title = $this->getTitle();
287 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
288 # @todo FIXME: This would be better as an extension hook
289 $enotif = new EmailNotification();
290 $enotif->notifyOnPageChange( $editor, $title,
291 $this->mAttribs
['rc_timestamp'],
292 $this->mAttribs
['rc_comment'],
293 $this->mAttribs
['rc_minor'],
294 $this->mAttribs
['rc_last_oldid'],
295 $this->mExtra
['pageStatus'] );
301 * @deprecated since 1.22, use notifyRCFeeds instead.
303 public function notifyRC2UDP() {
304 wfDeprecated( __METHOD__
, '1.22' );
305 $this->notifyRCFeeds();
309 * Send some text to UDP.
310 * @deprecated since 1.22
312 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
313 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
315 wfDeprecated( __METHOD__
, '1.22' );
317 # Assume default for standard RC case
318 $address = $address ?
$address : $wgRC2UDPAddress;
319 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
320 $port = $port ?
$port : $wgRC2UDPPort;
322 $engine = new UDPRCFeedEngine();
324 'uri' => "udp://$address:$port/$prefix",
325 'formatter' => 'IRCColourfulRCFeedFormatter',
326 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
329 $engine->send( $feed, $line );
333 * Notify all the feeds about the change.
335 public function notifyRCFeeds() {
338 foreach ( $wgRCFeeds as $feed ) {
339 $engine = self
::getEngine( $feed['uri'] );
341 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
342 $actionComment = $this->mExtra
['actionCommentIRC'];
344 $actionComment = null;
347 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
350 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
351 $this->mAttribs
['rc_type'] == RC_EXTERNAL
356 /** @var $formatter RCFeedFormatter */
357 $formatter = new $feed['formatter']();
358 $line = $formatter->getLine( $feed, $this, $actionComment );
360 $engine->send( $feed, $line );
365 * Gets the stream engine object for a given URI from $wgRCEngines
367 * @param string $uri URI to get the engine object for
368 * @throws MWException
369 * @return object The engine object
371 private static function getEngine( $uri ) {
374 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
376 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
379 if ( !isset( $wgRCEngines[$scheme] ) ) {
380 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
383 return new $wgRCEngines[$scheme];
387 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
389 public static function cleanupForIRC( $text ) {
390 wfDeprecated( __METHOD__
, '1.22' );
392 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
396 * Mark a given change as patrolled
398 * @param $change Mixed: RecentChange or corresponding rc_id
399 * @param $auto Boolean: for automatic patrol
400 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
402 public static function markPatrolled( $change, $auto = false ) {
405 $change = $change instanceof RecentChange
407 : RecentChange
::newFromId( $change );
409 if ( !$change instanceof RecentChange
) {
413 return $change->doMarkPatrolled( $wgUser, $auto );
417 * Mark this RecentChange as patrolled
419 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
420 * 'markedaspatrollederror-noautopatrol' as errors
421 * @param $user User object doing the action
422 * @param $auto Boolean: for automatic patrol
423 * @return array of permissions errors, see Title::getUserPermissionsErrors()
425 public function doMarkPatrolled( User
$user, $auto = false ) {
426 global $wgUseRCPatrol, $wgUseNPPatrol;
428 // If recentchanges patrol is disabled, only new pages
430 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
431 $errors[] = array( 'rcpatroldisabled' );
433 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
434 $right = $auto ?
'autopatrol' : 'patrol';
435 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
436 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
437 $errors[] = array( 'hookaborted' );
439 // Users without the 'autopatrol' right can't patrol their
441 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
442 && !$user->isAllowed( 'autopatrol' )
444 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
449 // If the change was patrolled already, do nothing
450 if ( $this->getAttribute( 'rc_patrolled' ) ) {
453 // Actually set the 'patrolled' flag in RC
454 $this->reallyMarkPatrolled();
455 // Log this patrol event
456 PatrolLog
::record( $this, $auto, $user );
457 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
463 * Mark this RecentChange patrolled, without error checking
464 * @return Integer: number of affected rows
466 public function reallyMarkPatrolled() {
467 $dbw = wfGetDB( DB_MASTER
);
474 'rc_id' => $this->getAttribute( 'rc_id' )
478 // Invalidate the page cache after the page has been patrolled
479 // to make sure that the Patrol link isn't visible any longer!
480 $this->getTitle()->invalidateCache();
482 return $dbw->affectedRows();
486 * Makes an entry in the database corresponding to an edit
489 * @param $title Title
494 * @param $lastTimestamp
497 * @param $oldSize int
498 * @param $newSize int
501 * @return RecentChange
503 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
504 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
505 $rc = new RecentChange
;
506 $rc->mTitle
= $title;
507 $rc->mPerformer
= $user;
508 $rc->mAttribs
= array(
509 'rc_timestamp' => $timestamp,
510 'rc_namespace' => $title->getNamespace(),
511 'rc_title' => $title->getDBkey(),
512 'rc_type' => RC_EDIT
,
513 'rc_source' => self
::SRC_EDIT
,
514 'rc_minor' => $minor ?
1 : 0,
515 'rc_cur_id' => $title->getArticleID(),
516 'rc_user' => $user->getId(),
517 'rc_user_text' => $user->getName(),
518 'rc_comment' => $comment,
519 'rc_this_oldid' => $newId,
520 'rc_last_oldid' => $oldId,
521 'rc_bot' => $bot ?
1 : 0,
522 'rc_ip' => self
::checkIPAddress( $ip ),
523 'rc_patrolled' => intval( $patrol ),
524 'rc_new' => 0, # obsolete
525 'rc_old_len' => $oldSize,
526 'rc_new_len' => $newSize,
529 'rc_log_type' => null,
530 'rc_log_action' => '',
535 'prefixedDBkey' => $title->getPrefixedDBkey(),
536 'lastTimestamp' => $lastTimestamp,
537 'oldSize' => $oldSize,
538 'newSize' => $newSize,
539 'pageStatus' => 'changed'
547 * Makes an entry in the database corresponding to page creation
548 * Note: the title object must be loaded with the new id using resetArticleID()
549 * @todo Document parameters and return
552 * @param $title Title
561 * @return RecentChange
563 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
564 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
565 $rc = new RecentChange
;
566 $rc->mTitle
= $title;
567 $rc->mPerformer
= $user;
568 $rc->mAttribs
= array(
569 'rc_timestamp' => $timestamp,
570 'rc_namespace' => $title->getNamespace(),
571 'rc_title' => $title->getDBkey(),
573 'rc_source' => self
::SRC_NEW
,
574 'rc_minor' => $minor ?
1 : 0,
575 'rc_cur_id' => $title->getArticleID(),
576 'rc_user' => $user->getId(),
577 'rc_user_text' => $user->getName(),
578 'rc_comment' => $comment,
579 'rc_this_oldid' => $newId,
580 'rc_last_oldid' => 0,
581 'rc_bot' => $bot ?
1 : 0,
582 'rc_ip' => self
::checkIPAddress( $ip ),
583 'rc_patrolled' => intval( $patrol ),
584 'rc_new' => 1, # obsolete
586 'rc_new_len' => $size,
589 'rc_log_type' => null,
590 'rc_log_action' => '',
595 'prefixedDBkey' => $title->getPrefixedDBkey(),
596 'lastTimestamp' => 0,
599 'pageStatus' => 'created'
610 * @param $actionComment
618 * @param $actionCommentIRC string
621 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
622 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
624 global $wgLogRestrictions;
626 # Don't add private logs to RC!
627 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
630 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
631 $target, $logComment, $params, $newId, $actionCommentIRC );
639 * @param $title Title
641 * @param $actionComment
645 * @param $target Title
649 * @param $actionCommentIRC string
650 * @return RecentChange
652 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
653 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
656 ## Get pageStatus for email notification
657 switch ( $type . '-' . $action ) {
658 case 'delete-delete':
659 $pageStatus = 'deleted';
662 case 'move-move_redir':
663 $pageStatus = 'moved';
665 case 'delete-restore':
666 $pageStatus = 'restored';
668 case 'upload-upload':
669 $pageStatus = 'created';
671 case 'upload-overwrite':
673 $pageStatus = 'changed';
677 $rc = new RecentChange
;
678 $rc->mTitle
= $target;
679 $rc->mPerformer
= $user;
680 $rc->mAttribs
= array(
681 'rc_timestamp' => $timestamp,
682 'rc_namespace' => $target->getNamespace(),
683 'rc_title' => $target->getDBkey(),
685 'rc_source' => self
::SRC_LOG
,
687 'rc_cur_id' => $target->getArticleID(),
688 'rc_user' => $user->getId(),
689 'rc_user_text' => $user->getName(),
690 'rc_comment' => $logComment,
691 'rc_this_oldid' => 0,
692 'rc_last_oldid' => 0,
693 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
694 'rc_ip' => self
::checkIPAddress( $ip ),
696 'rc_new' => 0, # obsolete
697 'rc_old_len' => null,
698 'rc_new_len' => null,
700 'rc_logid' => $newId,
701 'rc_log_type' => $type,
702 'rc_log_action' => $action,
703 'rc_params' => $params
707 'prefixedDBkey' => $title->getPrefixedDBkey(),
708 'lastTimestamp' => 0,
709 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
710 'pageStatus' => $pageStatus,
711 'actionCommentIRC' => $actionCommentIRC
718 * Initialises the members of this object from a mysql row object
722 public function loadFromRow( $row ) {
723 $this->mAttribs
= get_object_vars( $row );
724 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
725 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
729 * Makes a pseudo-RC entry from a cur row
731 * @deprecated in 1.22
734 public function loadFromCurRow( $row ) {
735 wfDeprecated( __METHOD__
, '1.22' );
736 $this->mAttribs
= array(
737 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
738 'rc_user' => $row->rev_user
,
739 'rc_user_text' => $row->rev_user_text
,
740 'rc_namespace' => $row->page_namespace
,
741 'rc_title' => $row->page_title
,
742 'rc_comment' => $row->rev_comment
,
743 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
744 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
745 'rc_source' => $row->page_is_new ? self
::SRC_NEW
: self
::SRC_EDIT
,
746 'rc_cur_id' => $row->page_id
,
747 'rc_this_oldid' => $row->rev_id
,
748 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
751 'rc_id' => $row->rc_id
,
752 'rc_patrolled' => $row->rc_patrolled
,
753 'rc_new' => $row->page_is_new
, # obsolete
754 'rc_old_len' => $row->rc_old_len
,
755 'rc_new_len' => $row->rc_new_len
,
756 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
757 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
758 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
759 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
760 'rc_deleted' => $row->rc_deleted
// MUST be set
765 * Get an attribute value
767 * @param string $name Attribute name
770 public function getAttribute( $name ) {
771 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
777 public function getAttributes() {
778 return $this->mAttribs
;
782 * Gets the end part of the diff URL associated with this object
783 * Blank if no diff link should be displayed
787 public function diffLinkTrail( $forceCur ) {
788 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
789 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
790 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
794 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
804 * Returns the change size (HTML).
805 * The lengths can be given optionally.
810 public function getCharacterDifference( $old = 0, $new = 0 ) {
812 $old = $this->mAttribs
['rc_old_len'];
815 $new = $this->mAttribs
['rc_new_len'];
817 if ( $old === null ||
$new === null ) {
821 return ChangesList
::showCharacterDifference( $old, $new );
825 * Purge expired changes from the recentchanges table
828 public static function purgeExpiredChanges() {
829 if ( wfReadOnly() ) {
833 $method = __METHOD__
;
834 $dbw = wfGetDB( DB_MASTER
);
835 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
838 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
841 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
847 private static function checkIPAddress( $ip ) {
850 if ( !IP
::isIPAddress( $ip ) ) {
851 throw new MWException( "Attempt to write \"" . $ip .
852 "\" as an IP address into recent changes" );
855 $ip = $wgRequest->getIP();
865 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
866 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
867 * or rows which will be deleted soon shouldn't be included.
869 * @param $timestamp mixed MWTimestamp compatible timestamp
870 * @param $tolerance integer Tolerance in seconds
873 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
876 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;