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 $wgRC2UDPPrefix, $wgRC2UDPInterwikiPrefix;
301 wfDeprecated( __METHOD__
, '1.22' );
303 $engine = new UDPRCFeedEngine();
305 'uri' => "udp://$address:$port/$wgRC2UDPPrefix",
306 'formatter' => 'IRCColourfulRCFeedFormatter',
307 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
310 return $engine->send( $feed, $line );
314 * Notify all the feeds about the change.
316 public function notifyRCFeeds() {
319 foreach ( $wgRCFeeds as $feed ) {
320 $engine = self
::getEngine( $feed['uri'] );
322 if ( isset( $this->mExtras
['actionCommentIRC'] ) ) {
323 $actionComment = $this->mExtras
['actionCommentIRC'];
325 $actionComment = null;
328 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
331 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
332 $this->mAttribs
['rc_type'] == RC_EXTERNAL
337 $formatter = new $feed['formatter']();
338 $line = $formatter->getLine( $feed, $this, $actionComment );
340 $engine->send( $feed, $line );
345 * Gets the stream engine object for a given URI from $wgRCEngines
347 * @param $uri string URI to get the engine object for
348 * @return object The engine object
350 private static function getEngine( $uri ) {
353 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
355 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
358 if ( !isset( $wgRCEngines[$scheme] ) ) {
359 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
362 return new $wgRCEngines[$scheme];
366 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
368 public static function cleanupForIRC( $text ) {
369 wfDeprecated( __METHOD__
, '1.22' );
370 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
374 * Mark a given change as patrolled
376 * @param $change Mixed: RecentChange or corresponding rc_id
377 * @param $auto Boolean: for automatic patrol
378 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
380 public static function markPatrolled( $change, $auto = false ) {
383 $change = $change instanceof RecentChange
385 : RecentChange
::newFromId( $change );
387 if ( !$change instanceof RecentChange
) {
390 return $change->doMarkPatrolled( $wgUser, $auto );
394 * Mark this RecentChange as patrolled
396 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
397 * @param $user User object doing the action
398 * @param $auto Boolean: for automatic patrol
399 * @return array of permissions errors, see Title::getUserPermissionsErrors()
401 public function doMarkPatrolled( User
$user, $auto = false ) {
402 global $wgUseRCPatrol, $wgUseNPPatrol;
404 // If recentchanges patrol is disabled, only new pages
406 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
407 $errors[] = array( 'rcpatroldisabled' );
409 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
410 $right = $auto ?
'autopatrol' : 'patrol';
411 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
412 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
413 $errors[] = array( 'hookaborted' );
415 // Users without the 'autopatrol' right can't patrol their
417 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
418 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
423 // If the change was patrolled already, do nothing
424 if ( $this->getAttribute( 'rc_patrolled' ) ) {
427 // Actually set the 'patrolled' flag in RC
428 $this->reallyMarkPatrolled();
429 // Log this patrol event
430 PatrolLog
::record( $this, $auto, $user );
431 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
436 * Mark this RecentChange patrolled, without error checking
437 * @return Integer: number of affected rows
439 public function reallyMarkPatrolled() {
440 $dbw = wfGetDB( DB_MASTER
);
447 'rc_id' => $this->getAttribute( 'rc_id' )
451 // Invalidate the page cache after the page has been patrolled
452 // to make sure that the Patrol link isn't visible any longer!
453 $this->getTitle()->invalidateCache();
454 return $dbw->affectedRows();
458 * Makes an entry in the database corresponding to an edit
461 * @param $title Title
466 * @param $lastTimestamp
469 * @param $oldSize int
470 * @param $newSize int
473 * @return RecentChange
475 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
476 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
477 $rc = new RecentChange
;
478 $rc->mTitle
= $title;
479 $rc->mPerformer
= $user;
480 $rc->mAttribs
= array(
481 'rc_timestamp' => $timestamp,
482 'rc_cur_time' => $timestamp,
483 'rc_namespace' => $title->getNamespace(),
484 'rc_title' => $title->getDBkey(),
485 'rc_type' => RC_EDIT
,
486 'rc_minor' => $minor ?
1 : 0,
487 'rc_cur_id' => $title->getArticleID(),
488 'rc_user' => $user->getId(),
489 'rc_user_text' => $user->getName(),
490 'rc_comment' => $comment,
491 'rc_this_oldid' => $newId,
492 'rc_last_oldid' => $oldId,
493 'rc_bot' => $bot ?
1 : 0,
494 'rc_ip' => self
::checkIPAddress( $ip ),
495 'rc_patrolled' => intval( $patrol ),
496 'rc_new' => 0, # obsolete
497 'rc_old_len' => $oldSize,
498 'rc_new_len' => $newSize,
501 'rc_log_type' => null,
502 'rc_log_action' => '',
507 'prefixedDBkey' => $title->getPrefixedDBkey(),
508 'lastTimestamp' => $lastTimestamp,
509 'oldSize' => $oldSize,
510 'newSize' => $newSize,
511 'pageStatus' => 'changed'
518 * Makes an entry in the database corresponding to page creation
519 * Note: the title object must be loaded with the new id using resetArticleID()
520 * @todo Document parameters and return
523 * @param $title Title
532 * @return RecentChange
534 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
535 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
536 $rc = new RecentChange
;
537 $rc->mTitle
= $title;
538 $rc->mPerformer
= $user;
539 $rc->mAttribs
= array(
540 'rc_timestamp' => $timestamp,
541 'rc_cur_time' => $timestamp,
542 'rc_namespace' => $title->getNamespace(),
543 'rc_title' => $title->getDBkey(),
545 'rc_minor' => $minor ?
1 : 0,
546 'rc_cur_id' => $title->getArticleID(),
547 'rc_user' => $user->getId(),
548 'rc_user_text' => $user->getName(),
549 'rc_comment' => $comment,
550 'rc_this_oldid' => $newId,
551 'rc_last_oldid' => 0,
552 'rc_bot' => $bot ?
1 : 0,
553 'rc_ip' => self
::checkIPAddress( $ip ),
554 'rc_patrolled' => intval( $patrol ),
555 'rc_new' => 1, # obsolete
557 'rc_new_len' => $size,
560 'rc_log_type' => null,
561 'rc_log_action' => '',
566 'prefixedDBkey' => $title->getPrefixedDBkey(),
567 'lastTimestamp' => 0,
570 'pageStatus' => 'created'
580 * @param $actionComment
588 * @param $actionCommentIRC string
591 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
592 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
594 global $wgLogRestrictions;
595 # Don't add private logs to RC!
596 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
599 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
600 $target, $logComment, $params, $newId, $actionCommentIRC );
607 * @param $title Title
609 * @param $actionComment
613 * @param $target Title
617 * @param $actionCommentIRC string
618 * @return RecentChange
620 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
621 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
624 ## Get pageStatus for email notification
625 switch ( $type . '-' . $action ) {
626 case 'delete-delete':
627 $pageStatus = 'deleted';
630 case 'move-move_redir':
631 $pageStatus = 'moved';
633 case 'delete-restore':
634 $pageStatus = 'restored';
636 case 'upload-upload':
637 $pageStatus = 'created';
639 case 'upload-overwrite':
641 $pageStatus = 'changed';
645 $rc = new RecentChange
;
646 $rc->mTitle
= $target;
647 $rc->mPerformer
= $user;
648 $rc->mAttribs
= array(
649 'rc_timestamp' => $timestamp,
650 'rc_cur_time' => $timestamp,
651 'rc_namespace' => $target->getNamespace(),
652 'rc_title' => $target->getDBkey(),
655 'rc_cur_id' => $target->getArticleID(),
656 'rc_user' => $user->getId(),
657 'rc_user_text' => $user->getName(),
658 'rc_comment' => $logComment,
659 'rc_this_oldid' => 0,
660 'rc_last_oldid' => 0,
661 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
662 'rc_ip' => self
::checkIPAddress( $ip ),
664 'rc_new' => 0, # obsolete
665 'rc_old_len' => null,
666 'rc_new_len' => null,
668 'rc_logid' => $newId,
669 'rc_log_type' => $type,
670 'rc_log_action' => $action,
671 'rc_params' => $params
675 'prefixedDBkey' => $title->getPrefixedDBkey(),
676 'lastTimestamp' => 0,
677 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
678 'pageStatus' => $pageStatus,
679 'actionCommentIRC' => $actionCommentIRC
685 * Initialises the members of this object from a mysql row object
689 public function loadFromRow( $row ) {
690 $this->mAttribs
= get_object_vars( $row );
691 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
692 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
696 * Makes a pseudo-RC entry from a cur row
701 public function loadFromCurRow( $row ) {
702 wfDeprecated( __METHOD__
, '1.22' );
703 $this->mAttribs
= array(
704 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
705 'rc_cur_time' => $row->rev_timestamp
,
706 'rc_user' => $row->rev_user
,
707 'rc_user_text' => $row->rev_user_text
,
708 'rc_namespace' => $row->page_namespace
,
709 'rc_title' => $row->page_title
,
710 'rc_comment' => $row->rev_comment
,
711 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
712 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
713 'rc_cur_id' => $row->page_id
,
714 'rc_this_oldid' => $row->rev_id
,
715 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
718 'rc_id' => $row->rc_id
,
719 'rc_patrolled' => $row->rc_patrolled
,
720 'rc_new' => $row->page_is_new
, # obsolete
721 'rc_old_len' => $row->rc_old_len
,
722 'rc_new_len' => $row->rc_new_len
,
723 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
724 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
725 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
726 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
727 'rc_deleted' => $row->rc_deleted
// MUST be set
732 * Get an attribute value
734 * @param string $name Attribute name
737 public function getAttribute( $name ) {
738 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
744 public function getAttributes() {
745 return $this->mAttribs
;
749 * Gets the end part of the diff URL associated with this object
750 * Blank if no diff link should be displayed
754 public function diffLinkTrail( $forceCur ) {
755 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
756 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
757 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
761 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
770 * Returns the change size (HTML).
771 * The lengths can be given optionally.
776 public function getCharacterDifference( $old = 0, $new = 0 ) {
778 $old = $this->mAttribs
['rc_old_len'];
781 $new = $this->mAttribs
['rc_new_len'];
783 if ( $old === null ||
$new === null ) {
786 return ChangesList
::showCharacterDifference( $old, $new );
790 * Purge expired changes from the recentchanges table
793 public static function purgeExpiredChanges() {
794 if ( wfReadOnly() ) {
798 $method = __METHOD__
;
799 $dbw = wfGetDB( DB_MASTER
);
800 $dbw->onTransactionIdle( function() use ( $dbw, $method ) {
803 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
806 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
812 private static function checkIPAddress( $ip ) {
815 if ( !IP
::isIPAddress( $ip ) ) {
816 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
819 $ip = $wgRequest->getIP();
828 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
829 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
830 * or rows which will be deleted soon shouldn't be included.
832 * @param $timestamp mixed MWTimestamp compatible timestamp
833 * @param $tolerance integer Tolerance in seconds
836 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
838 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;