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 * No uses left in Gerrit on 2013-11-19.
109 * @deprecated in 1.22
111 * @return RecentChange
113 public static function newFromCurRow( $row ) {
114 wfDeprecated( __METHOD__
, '1.22' );
115 $rc = new RecentChange
;
116 $rc->loadFromCurRow( $row );
117 $rc->notificationtimestamp
= false;
118 $rc->numberofWatchingusers
= false;
124 * Obtain the recent change with a given rc_id value
126 * @param int $rcid rc_id value to retrieve
127 * @return RecentChange
129 public static function newFromId( $rcid ) {
130 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
134 * Find the first recent change matching some specific conditions
136 * @param array $conds of conditions
137 * @param $fname Mixed: override the method name in profiling/logs
138 * @param $options Array Query options
139 * @return RecentChange
141 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
142 $dbr = wfGetDB( DB_SLAVE
);
143 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
144 if ( $row !== false ) {
145 return self
::newFromRow( $row );
152 * Return the list of recentchanges fields that should be selected to create
153 * a new recentchanges object.
156 public static function selectFields() {
188 * @param $attribs array
190 public function setAttribs( $attribs ) {
191 $this->mAttribs
= $attribs;
195 * @param $extra array
197 public function setExtra( $extra ) {
198 $this->mExtra
= $extra;
205 public function &getTitle() {
206 if ( $this->mTitle
=== false ) {
207 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
210 return $this->mTitle
;
214 * Get the User object of the person who performed this change.
218 public function getPerformer() {
219 if ( $this->mPerformer
=== false ) {
220 if ( $this->mAttribs
['rc_user'] ) {
221 $this->mPerformer
= User
::newFromID( $this->mAttribs
['rc_user'] );
223 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
227 return $this->mPerformer
;
231 * Writes the data in this object to the database
234 public function save( $noudp = false ) {
235 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
237 $dbw = wfGetDB( DB_MASTER
);
238 if ( !is_array( $this->mExtra
) ) {
239 $this->mExtra
= array();
241 $this->mExtra
['lang'] = $wgLocalInterwiki;
243 if ( !$wgPutIPinRC ) {
244 $this->mAttribs
['rc_ip'] = '';
247 # If our database is strict about IP addresses, use NULL instead of an empty string
248 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
249 unset( $this->mAttribs
['rc_ip'] );
252 # Trim spaces on user supplied text
253 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
255 # Make sure summary is truncated (whole multibyte characters)
256 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
258 # Fixup database timestamps
259 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
260 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
262 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
263 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id'] == 0 ) {
264 unset( $this->mAttribs
['rc_cur_id'] );
268 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
271 $this->mAttribs
['rc_id'] = $dbw->insertId();
274 wfRunHooks( 'RecentChange_save', array( &$this ) );
276 # Notify external application via UDP
278 $this->notifyRCFeeds();
281 # E-mail notifications
282 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
283 $editor = $this->getPerformer();
284 $title = $this->getTitle();
286 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
287 # @todo FIXME: This would be better as an extension hook
288 $enotif = new EmailNotification();
289 $enotif->notifyOnPageChange( $editor, $title,
290 $this->mAttribs
['rc_timestamp'],
291 $this->mAttribs
['rc_comment'],
292 $this->mAttribs
['rc_minor'],
293 $this->mAttribs
['rc_last_oldid'],
294 $this->mExtra
['pageStatus'] );
300 * @deprecated since 1.22, use notifyRCFeeds instead.
302 public function notifyRC2UDP() {
303 wfDeprecated( __METHOD__
, '1.22' );
304 $this->notifyRCFeeds();
308 * Send some text to UDP.
309 * @deprecated since 1.22
311 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
312 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
314 wfDeprecated( __METHOD__
, '1.22' );
316 # Assume default for standard RC case
317 $address = $address ?
$address : $wgRC2UDPAddress;
318 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
319 $port = $port ?
$port : $wgRC2UDPPort;
321 $engine = new UDPRCFeedEngine();
323 'uri' => "udp://$address:$port/$prefix",
324 'formatter' => 'IRCColourfulRCFeedFormatter',
325 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
328 $engine->send( $feed, $line );
332 * Notify all the feeds about the change.
334 public function notifyRCFeeds() {
337 foreach ( $wgRCFeeds as $feed ) {
338 $engine = self
::getEngine( $feed['uri'] );
340 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
341 $actionComment = $this->mExtra
['actionCommentIRC'];
343 $actionComment = null;
346 $omitBots = isset( $feed['omit_bots'] ) ?
$feed['omit_bots'] : false;
349 ( $omitBots && $this->mAttribs
['rc_bot'] ) ||
350 $this->mAttribs
['rc_type'] == RC_EXTERNAL
355 /** @var $formatter RCFeedFormatter */
356 $formatter = new $feed['formatter']();
357 $line = $formatter->getLine( $feed, $this, $actionComment );
359 $engine->send( $feed, $line );
364 * Gets the stream engine object for a given URI from $wgRCEngines
366 * @param string $uri URI to get the engine object for
367 * @throws MWException
368 * @return object The engine object
370 private static function getEngine( $uri ) {
373 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
375 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
378 if ( !isset( $wgRCEngines[$scheme] ) ) {
379 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
382 return new $wgRCEngines[$scheme];
386 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
388 public static function cleanupForIRC( $text ) {
389 wfDeprecated( __METHOD__
, '1.22' );
391 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
395 * Mark a given change as patrolled
397 * @param $change Mixed: RecentChange or corresponding rc_id
398 * @param $auto Boolean: for automatic patrol
399 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
401 public static function markPatrolled( $change, $auto = false ) {
404 $change = $change instanceof RecentChange
406 : RecentChange
::newFromId( $change );
408 if ( !$change instanceof RecentChange
) {
412 return $change->doMarkPatrolled( $wgUser, $auto );
416 * Mark this RecentChange as patrolled
418 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
419 * 'markedaspatrollederror-noautopatrol' as errors
420 * @param $user User object doing the action
421 * @param $auto Boolean: for automatic patrol
422 * @return array of permissions errors, see Title::getUserPermissionsErrors()
424 public function doMarkPatrolled( User
$user, $auto = false ) {
425 global $wgUseRCPatrol, $wgUseNPPatrol;
427 // If recentchanges patrol is disabled, only new pages
429 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
430 $errors[] = array( 'rcpatroldisabled' );
432 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
433 $right = $auto ?
'autopatrol' : 'patrol';
434 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
435 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
436 $errors[] = array( 'hookaborted' );
438 // Users without the 'autopatrol' right can't patrol their
440 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
441 && !$user->isAllowed( 'autopatrol' )
443 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
448 // If the change was patrolled already, do nothing
449 if ( $this->getAttribute( 'rc_patrolled' ) ) {
452 // Actually set the 'patrolled' flag in RC
453 $this->reallyMarkPatrolled();
454 // Log this patrol event
455 PatrolLog
::record( $this, $auto, $user );
456 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
462 * Mark this RecentChange patrolled, without error checking
463 * @return Integer: number of affected rows
465 public function reallyMarkPatrolled() {
466 $dbw = wfGetDB( DB_MASTER
);
473 'rc_id' => $this->getAttribute( 'rc_id' )
477 // Invalidate the page cache after the page has been patrolled
478 // to make sure that the Patrol link isn't visible any longer!
479 $this->getTitle()->invalidateCache();
481 return $dbw->affectedRows();
485 * Makes an entry in the database corresponding to an edit
488 * @param $title Title
493 * @param $lastTimestamp
496 * @param $oldSize int
497 * @param $newSize int
500 * @return RecentChange
502 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
503 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
504 $rc = new RecentChange
;
505 $rc->mTitle
= $title;
506 $rc->mPerformer
= $user;
507 $rc->mAttribs
= array(
508 'rc_timestamp' => $timestamp,
509 'rc_namespace' => $title->getNamespace(),
510 'rc_title' => $title->getDBkey(),
511 'rc_type' => RC_EDIT
,
512 'rc_source' => self
::SRC_EDIT
,
513 'rc_minor' => $minor ?
1 : 0,
514 'rc_cur_id' => $title->getArticleID(),
515 'rc_user' => $user->getId(),
516 'rc_user_text' => $user->getName(),
517 'rc_comment' => $comment,
518 'rc_this_oldid' => $newId,
519 'rc_last_oldid' => $oldId,
520 'rc_bot' => $bot ?
1 : 0,
521 'rc_ip' => self
::checkIPAddress( $ip ),
522 'rc_patrolled' => intval( $patrol ),
523 'rc_new' => 0, # obsolete
524 'rc_old_len' => $oldSize,
525 'rc_new_len' => $newSize,
528 'rc_log_type' => null,
529 'rc_log_action' => '',
534 'prefixedDBkey' => $title->getPrefixedDBkey(),
535 'lastTimestamp' => $lastTimestamp,
536 'oldSize' => $oldSize,
537 'newSize' => $newSize,
538 'pageStatus' => 'changed'
546 * Makes an entry in the database corresponding to page creation
547 * Note: the title object must be loaded with the new id using resetArticleID()
548 * @todo Document parameters and return
551 * @param $title Title
560 * @return RecentChange
562 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
563 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
564 $rc = new RecentChange
;
565 $rc->mTitle
= $title;
566 $rc->mPerformer
= $user;
567 $rc->mAttribs
= array(
568 'rc_timestamp' => $timestamp,
569 'rc_namespace' => $title->getNamespace(),
570 'rc_title' => $title->getDBkey(),
572 'rc_source' => self
::SRC_NEW
,
573 'rc_minor' => $minor ?
1 : 0,
574 'rc_cur_id' => $title->getArticleID(),
575 'rc_user' => $user->getId(),
576 'rc_user_text' => $user->getName(),
577 'rc_comment' => $comment,
578 'rc_this_oldid' => $newId,
579 'rc_last_oldid' => 0,
580 'rc_bot' => $bot ?
1 : 0,
581 'rc_ip' => self
::checkIPAddress( $ip ),
582 'rc_patrolled' => intval( $patrol ),
583 'rc_new' => 1, # obsolete
585 'rc_new_len' => $size,
588 'rc_log_type' => null,
589 'rc_log_action' => '',
594 'prefixedDBkey' => $title->getPrefixedDBkey(),
595 'lastTimestamp' => 0,
598 'pageStatus' => 'created'
609 * @param $actionComment
617 * @param $actionCommentIRC string
620 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
621 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
623 global $wgLogRestrictions;
625 # Don't add private logs to RC!
626 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
629 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
630 $target, $logComment, $params, $newId, $actionCommentIRC );
638 * @param $title Title
640 * @param $actionComment
644 * @param $target Title
648 * @param $actionCommentIRC string
649 * @return RecentChange
651 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
652 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
655 ## Get pageStatus for email notification
656 switch ( $type . '-' . $action ) {
657 case 'delete-delete':
658 $pageStatus = 'deleted';
661 case 'move-move_redir':
662 $pageStatus = 'moved';
664 case 'delete-restore':
665 $pageStatus = 'restored';
667 case 'upload-upload':
668 $pageStatus = 'created';
670 case 'upload-overwrite':
672 $pageStatus = 'changed';
676 $rc = new RecentChange
;
677 $rc->mTitle
= $target;
678 $rc->mPerformer
= $user;
679 $rc->mAttribs
= array(
680 'rc_timestamp' => $timestamp,
681 'rc_namespace' => $target->getNamespace(),
682 'rc_title' => $target->getDBkey(),
684 'rc_source' => self
::SRC_LOG
,
686 'rc_cur_id' => $target->getArticleID(),
687 'rc_user' => $user->getId(),
688 'rc_user_text' => $user->getName(),
689 'rc_comment' => $logComment,
690 'rc_this_oldid' => 0,
691 'rc_last_oldid' => 0,
692 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
693 'rc_ip' => self
::checkIPAddress( $ip ),
695 'rc_new' => 0, # obsolete
696 'rc_old_len' => null,
697 'rc_new_len' => null,
699 'rc_logid' => $newId,
700 'rc_log_type' => $type,
701 'rc_log_action' => $action,
702 'rc_params' => $params
706 'prefixedDBkey' => $title->getPrefixedDBkey(),
707 'lastTimestamp' => 0,
708 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
709 'pageStatus' => $pageStatus,
710 'actionCommentIRC' => $actionCommentIRC
717 * Initialises the members of this object from a mysql row object
721 public function loadFromRow( $row ) {
722 $this->mAttribs
= get_object_vars( $row );
723 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
724 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
728 * Makes a pseudo-RC entry from a cur row
730 * @deprecated in 1.22
733 public function loadFromCurRow( $row ) {
734 wfDeprecated( __METHOD__
, '1.22' );
735 $this->mAttribs
= array(
736 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
737 'rc_user' => $row->rev_user
,
738 'rc_user_text' => $row->rev_user_text
,
739 'rc_namespace' => $row->page_namespace
,
740 'rc_title' => $row->page_title
,
741 'rc_comment' => $row->rev_comment
,
742 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
743 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
744 'rc_source' => $row->page_is_new ? self
::SRC_NEW
: self
::SRC_EDIT
,
745 'rc_cur_id' => $row->page_id
,
746 'rc_this_oldid' => $row->rev_id
,
747 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
750 'rc_id' => $row->rc_id
,
751 'rc_patrolled' => $row->rc_patrolled
,
752 'rc_new' => $row->page_is_new
, # obsolete
753 'rc_old_len' => $row->rc_old_len
,
754 'rc_new_len' => $row->rc_new_len
,
755 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
756 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
757 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
758 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
759 'rc_deleted' => $row->rc_deleted
// MUST be set
764 * Get an attribute value
766 * @param string $name Attribute name
769 public function getAttribute( $name ) {
770 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
776 public function getAttributes() {
777 return $this->mAttribs
;
781 * Gets the end part of the diff URL associated with this object
782 * Blank if no diff link should be displayed
786 public function diffLinkTrail( $forceCur ) {
787 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
788 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
789 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
793 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
803 * Returns the change size (HTML).
804 * The lengths can be given optionally.
809 public function getCharacterDifference( $old = 0, $new = 0 ) {
811 $old = $this->mAttribs
['rc_old_len'];
814 $new = $this->mAttribs
['rc_new_len'];
816 if ( $old === null ||
$new === null ) {
820 return ChangesList
::showCharacterDifference( $old, $new );
824 * Purge expired changes from the recentchanges table
827 public static function purgeExpiredChanges() {
828 if ( wfReadOnly() ) {
832 $method = __METHOD__
;
833 $dbw = wfGetDB( DB_MASTER
);
834 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
837 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
840 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
846 private static function checkIPAddress( $ip ) {
849 if ( !IP
::isIPAddress( $ip ) ) {
850 throw new MWException( "Attempt to write \"" . $ip .
851 "\" as an IP address into recent changes" );
854 $ip = $wgRequest->getIP();
864 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
865 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
866 * or rows which will be deleted soon shouldn't be included.
868 * @param $timestamp mixed MWTimestamp compatible timestamp
869 * @param $tolerance integer Tolerance in seconds
872 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
875 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;