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
68 // Constants for the rc_source field. Extensions may also have
69 // their own source constants.
70 const SRC_EDIT
= 'mw.edit';
71 const SRC_NEW
= 'mw.new';
72 const SRC_LOG
= 'mw.log';
73 const SRC_EXTERNAL
= 'mw.external'; // obsolete
75 var $mAttribs = array(), $mExtra = array();
85 private $mPerformer = false;
90 var $mMovedToTitle = false;
91 var $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentchangeslinked
92 var $notificationtimestamp;
98 * @return RecentChange
100 public static function newFromRow( $row ) {
101 $rc = new RecentChange
;
102 $rc->loadFromRow( $row );
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 return $engine->send( $feed, $line );
331 * Notify all the feeds about the change.
333 public function notifyRCFeeds() {
336 foreach ( $wgRCFeeds as $feed ) {
337 $engine = self
::getEngine( $feed['uri'] );
339 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
340 $actionComment = $this->mExtra
['actionCommentIRC'];
342 $actionComment = null;
345 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
348 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
349 $this->mAttribs
['rc_type'] == RC_EXTERNAL
354 $formatter = new $feed['formatter']();
355 $line = $formatter->getLine( $feed, $this, $actionComment );
357 $engine->send( $feed, $line );
362 * Gets the stream engine object for a given URI from $wgRCEngines
364 * @param $uri string URI to get the engine object for
365 * @return object The engine object
367 private static function getEngine( $uri ) {
370 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
372 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
375 if ( !isset( $wgRCEngines[$scheme] ) ) {
376 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
379 return new $wgRCEngines[$scheme];
383 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
385 public static function cleanupForIRC( $text ) {
386 wfDeprecated( __METHOD__
, '1.22' );
388 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
392 * Mark a given change as patrolled
394 * @param $change Mixed: RecentChange or corresponding rc_id
395 * @param $auto Boolean: for automatic patrol
396 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
398 public static function markPatrolled( $change, $auto = false ) {
401 $change = $change instanceof RecentChange
403 : RecentChange
::newFromId( $change );
405 if ( !$change instanceof RecentChange
) {
409 return $change->doMarkPatrolled( $wgUser, $auto );
413 * Mark this RecentChange as patrolled
415 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
416 * 'markedaspatrollederror-noautopatrol' as errors
417 * @param $user User object doing the action
418 * @param $auto Boolean: for automatic patrol
419 * @return array of permissions errors, see Title::getUserPermissionsErrors()
421 public function doMarkPatrolled( User
$user, $auto = false ) {
422 global $wgUseRCPatrol, $wgUseNPPatrol;
424 // If recentchanges patrol is disabled, only new pages
426 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
427 $errors[] = array( 'rcpatroldisabled' );
429 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
430 $right = $auto ?
'autopatrol' : 'patrol';
431 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
432 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
433 $errors[] = array( 'hookaborted' );
435 // Users without the 'autopatrol' right can't patrol their
437 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
438 && !$user->isAllowed( 'autopatrol' )
440 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
445 // If the change was patrolled already, do nothing
446 if ( $this->getAttribute( 'rc_patrolled' ) ) {
449 // Actually set the 'patrolled' flag in RC
450 $this->reallyMarkPatrolled();
451 // Log this patrol event
452 PatrolLog
::record( $this, $auto, $user );
453 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
459 * Mark this RecentChange patrolled, without error checking
460 * @return Integer: number of affected rows
462 public function reallyMarkPatrolled() {
463 $dbw = wfGetDB( DB_MASTER
);
470 'rc_id' => $this->getAttribute( 'rc_id' )
474 // Invalidate the page cache after the page has been patrolled
475 // to make sure that the Patrol link isn't visible any longer!
476 $this->getTitle()->invalidateCache();
478 return $dbw->affectedRows();
482 * Makes an entry in the database corresponding to an edit
485 * @param $title Title
490 * @param $lastTimestamp
493 * @param $oldSize int
494 * @param $newSize int
497 * @return RecentChange
499 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
500 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
501 $rc = new RecentChange
;
502 $rc->mTitle
= $title;
503 $rc->mPerformer
= $user;
504 $rc->mAttribs
= array(
505 'rc_timestamp' => $timestamp,
506 'rc_namespace' => $title->getNamespace(),
507 'rc_title' => $title->getDBkey(),
508 'rc_type' => RC_EDIT
,
509 'rc_source' => self
::SRC_EDIT
,
510 'rc_minor' => $minor ?
1 : 0,
511 'rc_cur_id' => $title->getArticleID(),
512 'rc_user' => $user->getId(),
513 'rc_user_text' => $user->getName(),
514 'rc_comment' => $comment,
515 'rc_this_oldid' => $newId,
516 'rc_last_oldid' => $oldId,
517 'rc_bot' => $bot ?
1 : 0,
518 'rc_ip' => self
::checkIPAddress( $ip ),
519 'rc_patrolled' => intval( $patrol ),
520 'rc_new' => 0, # obsolete
521 'rc_old_len' => $oldSize,
522 'rc_new_len' => $newSize,
525 'rc_log_type' => null,
526 'rc_log_action' => '',
531 'prefixedDBkey' => $title->getPrefixedDBkey(),
532 'lastTimestamp' => $lastTimestamp,
533 'oldSize' => $oldSize,
534 'newSize' => $newSize,
535 'pageStatus' => 'changed'
543 * Makes an entry in the database corresponding to page creation
544 * Note: the title object must be loaded with the new id using resetArticleID()
545 * @todo Document parameters and return
548 * @param $title Title
557 * @return RecentChange
559 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
560 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
561 $rc = new RecentChange
;
562 $rc->mTitle
= $title;
563 $rc->mPerformer
= $user;
564 $rc->mAttribs
= array(
565 'rc_timestamp' => $timestamp,
566 'rc_namespace' => $title->getNamespace(),
567 'rc_title' => $title->getDBkey(),
569 'rc_source' => self
::SRC_NEW
,
570 'rc_minor' => $minor ?
1 : 0,
571 'rc_cur_id' => $title->getArticleID(),
572 'rc_user' => $user->getId(),
573 'rc_user_text' => $user->getName(),
574 'rc_comment' => $comment,
575 'rc_this_oldid' => $newId,
576 'rc_last_oldid' => 0,
577 'rc_bot' => $bot ?
1 : 0,
578 'rc_ip' => self
::checkIPAddress( $ip ),
579 'rc_patrolled' => intval( $patrol ),
580 'rc_new' => 1, # obsolete
582 'rc_new_len' => $size,
585 'rc_log_type' => null,
586 'rc_log_action' => '',
591 'prefixedDBkey' => $title->getPrefixedDBkey(),
592 'lastTimestamp' => 0,
595 'pageStatus' => 'created'
606 * @param $actionComment
614 * @param $actionCommentIRC string
617 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
618 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
620 global $wgLogRestrictions;
622 # Don't add private logs to RC!
623 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
626 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
627 $target, $logComment, $params, $newId, $actionCommentIRC );
635 * @param $title Title
637 * @param $actionComment
641 * @param $target Title
645 * @param $actionCommentIRC string
646 * @return RecentChange
648 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
649 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
652 ## Get pageStatus for email notification
653 switch ( $type . '-' . $action ) {
654 case 'delete-delete':
655 $pageStatus = 'deleted';
658 case 'move-move_redir':
659 $pageStatus = 'moved';
661 case 'delete-restore':
662 $pageStatus = 'restored';
664 case 'upload-upload':
665 $pageStatus = 'created';
667 case 'upload-overwrite':
669 $pageStatus = 'changed';
673 $rc = new RecentChange
;
674 $rc->mTitle
= $target;
675 $rc->mPerformer
= $user;
676 $rc->mAttribs
= array(
677 'rc_timestamp' => $timestamp,
678 'rc_namespace' => $target->getNamespace(),
679 'rc_title' => $target->getDBkey(),
681 'rc_source' => self
::SRC_LOG
,
683 'rc_cur_id' => $target->getArticleID(),
684 'rc_user' => $user->getId(),
685 'rc_user_text' => $user->getName(),
686 'rc_comment' => $logComment,
687 'rc_this_oldid' => 0,
688 'rc_last_oldid' => 0,
689 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
690 'rc_ip' => self
::checkIPAddress( $ip ),
692 'rc_new' => 0, # obsolete
693 'rc_old_len' => null,
694 'rc_new_len' => null,
696 'rc_logid' => $newId,
697 'rc_log_type' => $type,
698 'rc_log_action' => $action,
699 'rc_params' => $params
703 'prefixedDBkey' => $title->getPrefixedDBkey(),
704 'lastTimestamp' => 0,
705 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
706 'pageStatus' => $pageStatus,
707 'actionCommentIRC' => $actionCommentIRC
714 * Initialises the members of this object from a mysql row object
718 public function loadFromRow( $row ) {
719 $this->mAttribs
= get_object_vars( $row );
720 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
721 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
725 * Makes a pseudo-RC entry from a cur row
727 * @deprecated in 1.22
730 public function loadFromCurRow( $row ) {
731 wfDeprecated( __METHOD__
, '1.22' );
732 $this->mAttribs
= array(
733 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
734 'rc_user' => $row->rev_user
,
735 'rc_user_text' => $row->rev_user_text
,
736 'rc_namespace' => $row->page_namespace
,
737 'rc_title' => $row->page_title
,
738 'rc_comment' => $row->rev_comment
,
739 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
740 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
741 'rc_source' => $row->page_is_new ? self
::SRC_NEW
: self
::SRC_EDIT
,
742 'rc_cur_id' => $row->page_id
,
743 'rc_this_oldid' => $row->rev_id
,
744 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
747 'rc_id' => $row->rc_id
,
748 'rc_patrolled' => $row->rc_patrolled
,
749 'rc_new' => $row->page_is_new
, # obsolete
750 'rc_old_len' => $row->rc_old_len
,
751 'rc_new_len' => $row->rc_new_len
,
752 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
753 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
754 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
755 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
756 'rc_deleted' => $row->rc_deleted
// MUST be set
761 * Get an attribute value
763 * @param string $name Attribute name
766 public function getAttribute( $name ) {
767 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
773 public function getAttributes() {
774 return $this->mAttribs
;
778 * Gets the end part of the diff URL associated with this object
779 * Blank if no diff link should be displayed
783 public function diffLinkTrail( $forceCur ) {
784 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
785 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
786 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
790 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
800 * Returns the change size (HTML).
801 * The lengths can be given optionally.
806 public function getCharacterDifference( $old = 0, $new = 0 ) {
808 $old = $this->mAttribs
['rc_old_len'];
811 $new = $this->mAttribs
['rc_new_len'];
813 if ( $old === null ||
$new === null ) {
817 return ChangesList
::showCharacterDifference( $old, $new );
821 * Purge expired changes from the recentchanges table
824 public static function purgeExpiredChanges() {
825 if ( wfReadOnly() ) {
829 $method = __METHOD__
;
830 $dbw = wfGetDB( DB_MASTER
);
831 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
834 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
837 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
843 private static function checkIPAddress( $ip ) {
846 if ( !IP
::isIPAddress( $ip ) ) {
847 throw new MWException( "Attempt to write \"" . $ip .
848 "\" as an IP address into recent changes" );
851 $ip = $wgRequest->getIP();
861 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
862 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
863 * or rows which will be deleted soon shouldn't be included.
865 * @param $timestamp mixed MWTimestamp compatible timestamp
866 * @param $tolerance integer Tolerance in seconds
869 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
872 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;