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_cur_time timestamp on the cur row
30 * rc_namespace namespace #
31 * rc_title non-prefixed db key
32 * rc_type is new entry, used to determine whether updating is necessary
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 var $mAttribs = array(), $mExtra = array();
77 private $mPerformer = false;
82 var $mMovedToTitle = false;
83 var $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentchangeslinked
84 var $notificationtimestamp;
90 * @return RecentChange
92 public static function newFromRow( $row ) {
93 $rc = new RecentChange
;
94 $rc->loadFromRow( $row );
101 * @return RecentChange
103 public static function newFromCurRow( $row ) {
104 wfDeprecated( __METHOD__
, '1.22' );
105 $rc = new RecentChange
;
106 $rc->loadFromCurRow( $row );
107 $rc->notificationtimestamp
= false;
108 $rc->numberofWatchingusers
= false;
113 * Obtain the recent change with a given rc_id value
115 * @param int $rcid rc_id value to retrieve
116 * @return RecentChange
118 public static function newFromId( $rcid ) {
119 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
123 * Find the first recent change matching some specific conditions
125 * @param array $conds of conditions
126 * @param $fname Mixed: override the method name in profiling/logs
127 * @param $options Array Query options
128 * @return RecentChange
130 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
131 $dbr = wfGetDB( DB_SLAVE
);
132 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
133 if ( $row !== false ) {
134 return self
::newFromRow( $row );
141 * Return the list of recentchanges fields that should be selected to create
142 * a new recentchanges object.
145 public static function selectFields() {
177 * @param $attribs array
179 public function setAttribs( $attribs ) {
180 $this->mAttribs
= $attribs;
184 * @param $extra array
186 public function setExtra( $extra ) {
187 $this->mExtra
= $extra;
194 public function &getTitle() {
195 if ( $this->mTitle
=== false ) {
196 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
198 return $this->mTitle
;
202 * Get the User object of the person who performed this change.
206 public function getPerformer() {
207 if ( $this->mPerformer
=== false ) {
208 if ( $this->mAttribs
['rc_user'] ) {
209 $this->mPerformer
= User
::newFromID( $this->mAttribs
['rc_user'] );
211 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
214 return $this->mPerformer
;
218 * Writes the data in this object to the database
221 public function save( $noudp = false ) {
222 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
224 $dbw = wfGetDB( DB_MASTER
);
225 if ( !is_array( $this->mExtra
) ) {
226 $this->mExtra
= array();
228 $this->mExtra
['lang'] = $wgLocalInterwiki;
230 if ( !$wgPutIPinRC ) {
231 $this->mAttribs
['rc_ip'] = '';
234 # If our database is strict about IP addresses, use NULL instead of an empty string
235 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
236 unset( $this->mAttribs
['rc_ip'] );
239 # Trim spaces on user supplied text
240 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
242 # Make sure summary is truncated (whole multibyte characters)
243 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
245 # Fixup database timestamps
246 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
247 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp( $this->mAttribs
['rc_cur_time'] );
248 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
250 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
251 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id'] == 0 ) {
252 unset( $this->mAttribs
['rc_cur_id'] );
256 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
259 $this->mAttribs
['rc_id'] = $dbw->insertId();
262 wfRunHooks( 'RecentChange_save', array( &$this ) );
264 # Notify external application via UDP
266 $this->notifyRCFeeds();
269 # E-mail notifications
270 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
271 $editor = $this->getPerformer();
272 $title = $this->getTitle();
274 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
275 # @todo FIXME: This would be better as an extension hook
276 $enotif = new EmailNotification();
277 $enotif->notifyOnPageChange( $editor, $title,
278 $this->mAttribs
['rc_timestamp'],
279 $this->mAttribs
['rc_comment'],
280 $this->mAttribs
['rc_minor'],
281 $this->mAttribs
['rc_last_oldid'],
282 $this->mExtra
['pageStatus'] );
288 * @deprecated since 1.22, use notifyRCFeeds instead.
290 public function notifyRC2UDP() {
291 wfDeprecated( __METHOD__
, '1.22' );
292 $this->notifyRCFeeds();
296 * Send some text to UDP.
297 * @deprecated since 1.22
299 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
300 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
302 wfDeprecated( __METHOD__
, '1.22' );
304 # Assume default for standard RC case
305 $address = $address ?
$address : $wgRC2UDPAddress;
306 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
307 $port = $port ?
$port : $wgRC2UDPPort;
309 $engine = new UDPRCFeedEngine();
311 'uri' => "udp://$address:$port/$prefix",
312 'formatter' => 'IRCColourfulRCFeedFormatter',
313 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
316 return $engine->send( $feed, $line );
320 * Notify all the feeds about the change.
322 public function notifyRCFeeds() {
325 foreach ( $wgRCFeeds as $feed ) {
326 $engine = self
::getEngine( $feed['uri'] );
328 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
329 $actionComment = $this->mExtra
['actionCommentIRC'];
331 $actionComment = null;
334 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
337 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
338 $this->mAttribs
['rc_type'] == RC_EXTERNAL
343 $formatter = new $feed['formatter']();
344 $line = $formatter->getLine( $feed, $this, $actionComment );
346 $engine->send( $feed, $line );
351 * Gets the stream engine object for a given URI from $wgRCEngines
353 * @param $uri string URI to get the engine object for
354 * @return object The engine object
356 private static function getEngine( $uri ) {
359 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
361 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
364 if ( !isset( $wgRCEngines[$scheme] ) ) {
365 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
368 return new $wgRCEngines[$scheme];
372 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
374 public static function cleanupForIRC( $text ) {
375 wfDeprecated( __METHOD__
, '1.22' );
376 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
380 * Mark a given change as patrolled
382 * @param $change Mixed: RecentChange or corresponding rc_id
383 * @param $auto Boolean: for automatic patrol
384 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
386 public static function markPatrolled( $change, $auto = false ) {
389 $change = $change instanceof RecentChange
391 : RecentChange
::newFromId( $change );
393 if ( !$change instanceof RecentChange
) {
396 return $change->doMarkPatrolled( $wgUser, $auto );
400 * Mark this RecentChange as patrolled
402 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
403 * @param $user User object doing the action
404 * @param $auto Boolean: for automatic patrol
405 * @return array of permissions errors, see Title::getUserPermissionsErrors()
407 public function doMarkPatrolled( User
$user, $auto = false ) {
408 global $wgUseRCPatrol, $wgUseNPPatrol;
410 // If recentchanges patrol is disabled, only new pages
412 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
413 $errors[] = array( 'rcpatroldisabled' );
415 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
416 $right = $auto ?
'autopatrol' : 'patrol';
417 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
418 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
419 $errors[] = array( 'hookaborted' );
421 // Users without the 'autopatrol' right can't patrol their
423 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
424 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
429 // If the change was patrolled already, do nothing
430 if ( $this->getAttribute( 'rc_patrolled' ) ) {
433 // Actually set the 'patrolled' flag in RC
434 $this->reallyMarkPatrolled();
435 // Log this patrol event
436 PatrolLog
::record( $this, $auto, $user );
437 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
442 * Mark this RecentChange patrolled, without error checking
443 * @return Integer: number of affected rows
445 public function reallyMarkPatrolled() {
446 $dbw = wfGetDB( DB_MASTER
);
453 'rc_id' => $this->getAttribute( 'rc_id' )
457 // Invalidate the page cache after the page has been patrolled
458 // to make sure that the Patrol link isn't visible any longer!
459 $this->getTitle()->invalidateCache();
460 return $dbw->affectedRows();
464 * Makes an entry in the database corresponding to an edit
467 * @param $title Title
472 * @param $lastTimestamp
475 * @param $oldSize int
476 * @param $newSize int
479 * @return RecentChange
481 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
482 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
483 $rc = new RecentChange
;
484 $rc->mTitle
= $title;
485 $rc->mPerformer
= $user;
486 $rc->mAttribs
= array(
487 'rc_timestamp' => $timestamp,
488 'rc_cur_time' => $timestamp,
489 'rc_namespace' => $title->getNamespace(),
490 'rc_title' => $title->getDBkey(),
491 'rc_type' => RC_EDIT
,
492 'rc_minor' => $minor ?
1 : 0,
493 'rc_cur_id' => $title->getArticleID(),
494 'rc_user' => $user->getId(),
495 'rc_user_text' => $user->getName(),
496 'rc_comment' => $comment,
497 'rc_this_oldid' => $newId,
498 'rc_last_oldid' => $oldId,
499 'rc_bot' => $bot ?
1 : 0,
500 'rc_ip' => self
::checkIPAddress( $ip ),
501 'rc_patrolled' => intval( $patrol ),
502 'rc_new' => 0, # obsolete
503 'rc_old_len' => $oldSize,
504 'rc_new_len' => $newSize,
507 'rc_log_type' => null,
508 'rc_log_action' => '',
513 'prefixedDBkey' => $title->getPrefixedDBkey(),
514 'lastTimestamp' => $lastTimestamp,
515 'oldSize' => $oldSize,
516 'newSize' => $newSize,
517 'pageStatus' => 'changed'
524 * Makes an entry in the database corresponding to page creation
525 * Note: the title object must be loaded with the new id using resetArticleID()
526 * @todo Document parameters and return
529 * @param $title Title
538 * @return RecentChange
540 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
541 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
542 $rc = new RecentChange
;
543 $rc->mTitle
= $title;
544 $rc->mPerformer
= $user;
545 $rc->mAttribs
= array(
546 'rc_timestamp' => $timestamp,
547 'rc_cur_time' => $timestamp,
548 'rc_namespace' => $title->getNamespace(),
549 'rc_title' => $title->getDBkey(),
551 'rc_minor' => $minor ?
1 : 0,
552 'rc_cur_id' => $title->getArticleID(),
553 'rc_user' => $user->getId(),
554 'rc_user_text' => $user->getName(),
555 'rc_comment' => $comment,
556 'rc_this_oldid' => $newId,
557 'rc_last_oldid' => 0,
558 'rc_bot' => $bot ?
1 : 0,
559 'rc_ip' => self
::checkIPAddress( $ip ),
560 'rc_patrolled' => intval( $patrol ),
561 'rc_new' => 1, # obsolete
563 'rc_new_len' => $size,
566 'rc_log_type' => null,
567 'rc_log_action' => '',
572 'prefixedDBkey' => $title->getPrefixedDBkey(),
573 'lastTimestamp' => 0,
576 'pageStatus' => 'created'
586 * @param $actionComment
594 * @param $actionCommentIRC string
597 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
598 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
600 global $wgLogRestrictions;
601 # Don't add private logs to RC!
602 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
605 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
606 $target, $logComment, $params, $newId, $actionCommentIRC );
613 * @param $title Title
615 * @param $actionComment
619 * @param $target Title
623 * @param $actionCommentIRC string
624 * @return RecentChange
626 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
627 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
630 ## Get pageStatus for email notification
631 switch ( $type . '-' . $action ) {
632 case 'delete-delete':
633 $pageStatus = 'deleted';
636 case 'move-move_redir':
637 $pageStatus = 'moved';
639 case 'delete-restore':
640 $pageStatus = 'restored';
642 case 'upload-upload':
643 $pageStatus = 'created';
645 case 'upload-overwrite':
647 $pageStatus = 'changed';
651 $rc = new RecentChange
;
652 $rc->mTitle
= $target;
653 $rc->mPerformer
= $user;
654 $rc->mAttribs
= array(
655 'rc_timestamp' => $timestamp,
656 'rc_cur_time' => $timestamp,
657 'rc_namespace' => $target->getNamespace(),
658 'rc_title' => $target->getDBkey(),
661 'rc_cur_id' => $target->getArticleID(),
662 'rc_user' => $user->getId(),
663 'rc_user_text' => $user->getName(),
664 'rc_comment' => $logComment,
665 'rc_this_oldid' => 0,
666 'rc_last_oldid' => 0,
667 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
668 'rc_ip' => self
::checkIPAddress( $ip ),
670 'rc_new' => 0, # obsolete
671 'rc_old_len' => null,
672 'rc_new_len' => null,
674 'rc_logid' => $newId,
675 'rc_log_type' => $type,
676 'rc_log_action' => $action,
677 'rc_params' => $params
681 'prefixedDBkey' => $title->getPrefixedDBkey(),
682 'lastTimestamp' => 0,
683 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
684 'pageStatus' => $pageStatus,
685 'actionCommentIRC' => $actionCommentIRC
691 * Initialises the members of this object from a mysql row object
695 public function loadFromRow( $row ) {
696 $this->mAttribs
= get_object_vars( $row );
697 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
698 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
702 * Makes a pseudo-RC entry from a cur row
707 public function loadFromCurRow( $row ) {
708 wfDeprecated( __METHOD__
, '1.22' );
709 $this->mAttribs
= array(
710 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
711 'rc_cur_time' => $row->rev_timestamp
,
712 'rc_user' => $row->rev_user
,
713 'rc_user_text' => $row->rev_user_text
,
714 'rc_namespace' => $row->page_namespace
,
715 'rc_title' => $row->page_title
,
716 'rc_comment' => $row->rev_comment
,
717 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
718 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
719 'rc_cur_id' => $row->page_id
,
720 'rc_this_oldid' => $row->rev_id
,
721 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
724 'rc_id' => $row->rc_id
,
725 'rc_patrolled' => $row->rc_patrolled
,
726 'rc_new' => $row->page_is_new
, # obsolete
727 'rc_old_len' => $row->rc_old_len
,
728 'rc_new_len' => $row->rc_new_len
,
729 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
730 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
731 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
732 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
733 'rc_deleted' => $row->rc_deleted
// MUST be set
738 * Get an attribute value
740 * @param string $name Attribute name
743 public function getAttribute( $name ) {
744 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
750 public function getAttributes() {
751 return $this->mAttribs
;
755 * Gets the end part of the diff URL associated with this object
756 * Blank if no diff link should be displayed
760 public function diffLinkTrail( $forceCur ) {
761 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
762 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
763 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
767 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
776 * Returns the change size (HTML).
777 * The lengths can be given optionally.
782 public function getCharacterDifference( $old = 0, $new = 0 ) {
784 $old = $this->mAttribs
['rc_old_len'];
787 $new = $this->mAttribs
['rc_new_len'];
789 if ( $old === null ||
$new === null ) {
792 return ChangesList
::showCharacterDifference( $old, $new );
796 * Purge expired changes from the recentchanges table
799 public static function purgeExpiredChanges() {
800 if ( wfReadOnly() ) {
804 $method = __METHOD__
;
805 $dbw = wfGetDB( DB_MASTER
);
806 $dbw->onTransactionIdle( function() use ( $dbw, $method ) {
809 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
812 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
818 private static function checkIPAddress( $ip ) {
821 if ( !IP
::isIPAddress( $ip ) ) {
822 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
825 $ip = $wgRequest->getIP();
834 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
835 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
836 * or rows which will be deleted soon shouldn't be included.
838 * @param $timestamp mixed MWTimestamp compatible timestamp
839 * @param $tolerance integer Tolerance in seconds
842 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
844 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;