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 * oldSize text size before the change
56 * newSize text size after the change
57 * pageStatus status of the page: created, deleted, moved, restored, changed
59 * temporary: not stored in the database
60 * notificationtimestamp
61 * numberofWatchingusers
64 // Constants for the rc_source field. Extensions may also have
65 // their own source constants.
66 const SRC_EDIT
= 'mw.edit';
67 const SRC_NEW
= 'mw.new';
68 const SRC_LOG
= 'mw.log';
69 const SRC_EXTERNAL
= 'mw.external'; // obsolete
71 public $mAttribs = array();
72 public $mExtra = array();
77 public $mTitle = false;
82 private $mPerformer = false;
84 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
85 public $notificationtimestamp;
88 * @var int Line number of recent change. Default -1.
96 * @return RecentChange
98 public static function newFromRow( $row ) {
99 $rc = new RecentChange
;
100 $rc->loadFromRow( $row );
106 * No uses left in Gerrit on 2013-11-19.
107 * @deprecated in 1.22
109 * @return RecentChange
111 public static function newFromCurRow( $row ) {
112 wfDeprecated( __METHOD__
, '1.22' );
113 $rc = new RecentChange
;
114 $rc->loadFromCurRow( $row );
115 $rc->notificationtimestamp
= false;
116 $rc->numberofWatchingusers
= false;
122 * Obtain the recent change with a given rc_id value
124 * @param int $rcid rc_id value to retrieve
125 * @return RecentChange
127 public static function newFromId( $rcid ) {
128 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
132 * Find the first recent change matching some specific conditions
134 * @param array $conds of conditions
135 * @param $fname Mixed: override the method name in profiling/logs
136 * @param $options Array Query options
137 * @return RecentChange
139 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
140 $dbr = wfGetDB( DB_SLAVE
);
141 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
142 if ( $row !== false ) {
143 return self
::newFromRow( $row );
150 * Return the list of recentchanges fields that should be selected to create
151 * a new recentchanges object.
154 public static function selectFields() {
186 * @param $attribs array
188 public function setAttribs( $attribs ) {
189 $this->mAttribs
= $attribs;
193 * @param $extra array
195 public function setExtra( $extra ) {
196 $this->mExtra
= $extra;
203 public function &getTitle() {
204 if ( $this->mTitle
=== false ) {
205 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
208 return $this->mTitle
;
212 * Get the User object of the person who performed this change.
216 public function getPerformer() {
217 if ( $this->mPerformer
=== false ) {
218 if ( $this->mAttribs
['rc_user'] ) {
219 $this->mPerformer
= User
::newFromID( $this->mAttribs
['rc_user'] );
221 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
225 return $this->mPerformer
;
229 * Writes the data in this object to the database
232 public function save( $noudp = false ) {
233 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
235 $dbw = wfGetDB( DB_MASTER
);
236 if ( !is_array( $this->mExtra
) ) {
237 $this->mExtra
= array();
240 if ( !$wgPutIPinRC ) {
241 $this->mAttribs
['rc_ip'] = '';
244 # If our database is strict about IP addresses, use NULL instead of an empty string
245 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
246 unset( $this->mAttribs
['rc_ip'] );
249 # Trim spaces on user supplied text
250 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
252 # Make sure summary is truncated (whole multibyte characters)
253 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
255 # Fixup database timestamps
256 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
257 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
259 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
260 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id'] == 0 ) {
261 unset( $this->mAttribs
['rc_cur_id'] );
265 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
268 $this->mAttribs
['rc_id'] = $dbw->insertId();
271 wfRunHooks( 'RecentChange_save', array( &$this ) );
273 # Notify external application via UDP
275 $this->notifyRCFeeds();
278 # E-mail notifications
279 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
280 $editor = $this->getPerformer();
281 $title = $this->getTitle();
283 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
284 # @todo FIXME: This would be better as an extension hook
285 $enotif = new EmailNotification();
286 $enotif->notifyOnPageChange( $editor, $title,
287 $this->mAttribs
['rc_timestamp'],
288 $this->mAttribs
['rc_comment'],
289 $this->mAttribs
['rc_minor'],
290 $this->mAttribs
['rc_last_oldid'],
291 $this->mExtra
['pageStatus'] );
297 * @deprecated since 1.22, use notifyRCFeeds instead.
299 public function notifyRC2UDP() {
300 wfDeprecated( __METHOD__
, '1.22' );
301 $this->notifyRCFeeds();
305 * Send some text to UDP.
306 * @deprecated since 1.22
308 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
309 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
311 wfDeprecated( __METHOD__
, '1.22' );
313 # Assume default for standard RC case
314 $address = $address ?
$address : $wgRC2UDPAddress;
315 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
316 $port = $port ?
$port : $wgRC2UDPPort;
318 $engine = new UDPRCFeedEngine();
320 'uri' => "udp://$address:$port/$prefix",
321 'formatter' => 'IRCColourfulRCFeedFormatter',
322 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
325 $engine->send( $feed, $line );
329 * Notify all the feeds about the change.
331 public function notifyRCFeeds() {
334 $performer = $this->getPerformer();
336 foreach ( $wgRCFeeds as $feed ) {
338 'omit_bots' => false,
339 'omit_anon' => false,
340 'omit_user' => false,
341 'omit_minor' => false,
342 'omit_patrolled' => false,
346 ( $feed['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
347 ( $feed['omit_anon'] && $performer->isAnon() ) ||
348 ( $feed['omit_user'] && !$performer->isAnon() ) ||
349 ( $feed['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
350 ( $feed['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
351 $this->mAttribs
['rc_type'] == RC_EXTERNAL
356 $engine = self
::getEngine( $feed['uri'] );
358 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
359 $actionComment = $this->mExtra
['actionCommentIRC'];
361 $actionComment = null;
364 /** @var $formatter RCFeedFormatter */
365 $formatter = new $feed['formatter']();
366 $line = $formatter->getLine( $feed, $this, $actionComment );
368 $engine->send( $feed, $line );
373 * Gets the stream engine object for a given URI from $wgRCEngines
375 * @param string $uri URI to get the engine object for
376 * @throws MWException
377 * @return RCFeedEngine The engine object
379 public static function getEngine( $uri ) {
382 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
384 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
387 if ( !isset( $wgRCEngines[$scheme] ) ) {
388 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
391 return new $wgRCEngines[$scheme];
395 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
397 public static function cleanupForIRC( $text ) {
398 wfDeprecated( __METHOD__
, '1.22' );
400 return IRCColourfulRCFeedFormatter
::cleanupForIRC( $text );
404 * Mark a given change as patrolled
406 * @param $change Mixed: RecentChange or corresponding rc_id
407 * @param $auto Boolean: for automatic patrol
408 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
410 public static function markPatrolled( $change, $auto = false ) {
413 $change = $change instanceof RecentChange
415 : RecentChange
::newFromId( $change );
417 if ( !$change instanceof RecentChange
) {
421 return $change->doMarkPatrolled( $wgUser, $auto );
425 * Mark this RecentChange as patrolled
427 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
428 * 'markedaspatrollederror-noautopatrol' as errors
429 * @param $user User object doing the action
430 * @param $auto Boolean: for automatic patrol
431 * @return array of permissions errors, see Title::getUserPermissionsErrors()
433 public function doMarkPatrolled( User
$user, $auto = false ) {
434 global $wgUseRCPatrol, $wgUseNPPatrol;
436 // If recentchanges patrol is disabled, only new pages
438 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
439 $errors[] = array( 'rcpatroldisabled' );
441 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
442 $right = $auto ?
'autopatrol' : 'patrol';
443 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
444 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
445 $errors[] = array( 'hookaborted' );
447 // Users without the 'autopatrol' right can't patrol their
449 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
450 && !$user->isAllowed( 'autopatrol' )
452 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
457 // If the change was patrolled already, do nothing
458 if ( $this->getAttribute( 'rc_patrolled' ) ) {
461 // Actually set the 'patrolled' flag in RC
462 $this->reallyMarkPatrolled();
463 // Log this patrol event
464 PatrolLog
::record( $this, $auto, $user );
465 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
471 * Mark this RecentChange patrolled, without error checking
472 * @return Integer: number of affected rows
474 public function reallyMarkPatrolled() {
475 $dbw = wfGetDB( DB_MASTER
);
482 'rc_id' => $this->getAttribute( 'rc_id' )
486 // Invalidate the page cache after the page has been patrolled
487 // to make sure that the Patrol link isn't visible any longer!
488 $this->getTitle()->invalidateCache();
490 return $dbw->affectedRows();
494 * Makes an entry in the database corresponding to an edit
497 * @param $title Title
502 * @param $lastTimestamp
505 * @param $oldSize int
506 * @param $newSize int
509 * @return RecentChange
511 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
512 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
513 $rc = new RecentChange
;
514 $rc->mTitle
= $title;
515 $rc->mPerformer
= $user;
516 $rc->mAttribs
= array(
517 'rc_timestamp' => $timestamp,
518 'rc_namespace' => $title->getNamespace(),
519 'rc_title' => $title->getDBkey(),
520 'rc_type' => RC_EDIT
,
521 'rc_source' => self
::SRC_EDIT
,
522 'rc_minor' => $minor ?
1 : 0,
523 'rc_cur_id' => $title->getArticleID(),
524 'rc_user' => $user->getId(),
525 'rc_user_text' => $user->getName(),
526 'rc_comment' => $comment,
527 'rc_this_oldid' => $newId,
528 'rc_last_oldid' => $oldId,
529 'rc_bot' => $bot ?
1 : 0,
530 'rc_ip' => self
::checkIPAddress( $ip ),
531 'rc_patrolled' => intval( $patrol ),
532 'rc_new' => 0, # obsolete
533 'rc_old_len' => $oldSize,
534 'rc_new_len' => $newSize,
537 'rc_log_type' => null,
538 'rc_log_action' => '',
543 'prefixedDBkey' => $title->getPrefixedDBkey(),
544 'lastTimestamp' => $lastTimestamp,
545 'oldSize' => $oldSize,
546 'newSize' => $newSize,
547 'pageStatus' => 'changed'
555 * Makes an entry in the database corresponding to page creation
556 * Note: the title object must be loaded with the new id using resetArticleID()
559 * @param $title Title
568 * @return RecentChange
570 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
571 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
572 $rc = new RecentChange
;
573 $rc->mTitle
= $title;
574 $rc->mPerformer
= $user;
575 $rc->mAttribs
= array(
576 'rc_timestamp' => $timestamp,
577 'rc_namespace' => $title->getNamespace(),
578 'rc_title' => $title->getDBkey(),
580 'rc_source' => self
::SRC_NEW
,
581 'rc_minor' => $minor ?
1 : 0,
582 'rc_cur_id' => $title->getArticleID(),
583 'rc_user' => $user->getId(),
584 'rc_user_text' => $user->getName(),
585 'rc_comment' => $comment,
586 'rc_this_oldid' => $newId,
587 'rc_last_oldid' => 0,
588 'rc_bot' => $bot ?
1 : 0,
589 'rc_ip' => self
::checkIPAddress( $ip ),
590 'rc_patrolled' => intval( $patrol ),
591 'rc_new' => 1, # obsolete
593 'rc_new_len' => $size,
596 'rc_log_type' => null,
597 'rc_log_action' => '',
602 'prefixedDBkey' => $title->getPrefixedDBkey(),
603 'lastTimestamp' => 0,
606 'pageStatus' => 'created'
617 * @param $actionComment
625 * @param $actionCommentIRC string
628 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
629 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
631 global $wgLogRestrictions;
633 # Don't add private logs to RC!
634 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
637 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
638 $target, $logComment, $params, $newId, $actionCommentIRC );
646 * @param $title Title
648 * @param $actionComment
652 * @param $target Title
656 * @param $actionCommentIRC string
657 * @return RecentChange
659 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
660 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
663 ## Get pageStatus for email notification
664 switch ( $type . '-' . $action ) {
665 case 'delete-delete':
666 $pageStatus = 'deleted';
669 case 'move-move_redir':
670 $pageStatus = 'moved';
672 case 'delete-restore':
673 $pageStatus = 'restored';
675 case 'upload-upload':
676 $pageStatus = 'created';
678 case 'upload-overwrite':
680 $pageStatus = 'changed';
684 $rc = new RecentChange
;
685 $rc->mTitle
= $target;
686 $rc->mPerformer
= $user;
687 $rc->mAttribs
= array(
688 'rc_timestamp' => $timestamp,
689 'rc_namespace' => $target->getNamespace(),
690 'rc_title' => $target->getDBkey(),
692 'rc_source' => self
::SRC_LOG
,
694 'rc_cur_id' => $target->getArticleID(),
695 'rc_user' => $user->getId(),
696 'rc_user_text' => $user->getName(),
697 'rc_comment' => $logComment,
698 'rc_this_oldid' => 0,
699 'rc_last_oldid' => 0,
700 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
701 'rc_ip' => self
::checkIPAddress( $ip ),
703 'rc_new' => 0, # obsolete
704 'rc_old_len' => null,
705 'rc_new_len' => null,
707 'rc_logid' => $newId,
708 'rc_log_type' => $type,
709 'rc_log_action' => $action,
710 'rc_params' => $params
714 'prefixedDBkey' => $title->getPrefixedDBkey(),
715 'lastTimestamp' => 0,
716 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
717 'pageStatus' => $pageStatus,
718 'actionCommentIRC' => $actionCommentIRC
725 * Initialises the members of this object from a mysql row object
729 public function loadFromRow( $row ) {
730 $this->mAttribs
= get_object_vars( $row );
731 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
732 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
736 * Makes a pseudo-RC entry from a cur row
738 * @deprecated in 1.22
741 public function loadFromCurRow( $row ) {
742 wfDeprecated( __METHOD__
, '1.22' );
743 $this->mAttribs
= array(
744 'rc_timestamp' => wfTimestamp( TS_MW
, $row->rev_timestamp
),
745 'rc_user' => $row->rev_user
,
746 'rc_user_text' => $row->rev_user_text
,
747 'rc_namespace' => $row->page_namespace
,
748 'rc_title' => $row->page_title
,
749 'rc_comment' => $row->rev_comment
,
750 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
751 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
752 'rc_source' => $row->page_is_new ? self
::SRC_NEW
: self
::SRC_EDIT
,
753 'rc_cur_id' => $row->page_id
,
754 'rc_this_oldid' => $row->rev_id
,
755 'rc_last_oldid' => isset( $row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
758 'rc_id' => $row->rc_id
,
759 'rc_patrolled' => $row->rc_patrolled
,
760 'rc_new' => $row->page_is_new
, # obsolete
761 'rc_old_len' => $row->rc_old_len
,
762 'rc_new_len' => $row->rc_new_len
,
763 'rc_params' => isset( $row->rc_params
) ?
$row->rc_params
: '',
764 'rc_log_type' => isset( $row->rc_log_type
) ?
$row->rc_log_type
: null,
765 'rc_log_action' => isset( $row->rc_log_action
) ?
$row->rc_log_action
: null,
766 'rc_logid' => isset( $row->rc_logid
) ?
$row->rc_logid
: 0,
767 'rc_deleted' => $row->rc_deleted
// MUST be set
772 * Get an attribute value
774 * @param string $name Attribute name
777 public function getAttribute( $name ) {
778 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
784 public function getAttributes() {
785 return $this->mAttribs
;
789 * Gets the end part of the diff URL associated with this object
790 * Blank if no diff link should be displayed
794 public function diffLinkTrail( $forceCur ) {
795 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
796 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
797 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
801 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
811 * Returns the change size (HTML).
812 * The lengths can be given optionally.
817 public function getCharacterDifference( $old = 0, $new = 0 ) {
819 $old = $this->mAttribs
['rc_old_len'];
822 $new = $this->mAttribs
['rc_new_len'];
824 if ( $old === null ||
$new === null ) {
828 return ChangesList
::showCharacterDifference( $old, $new );
832 * Purge expired changes from the recentchanges table
835 public static function purgeExpiredChanges() {
836 if ( wfReadOnly() ) {
840 $method = __METHOD__
;
841 $dbw = wfGetDB( DB_MASTER
);
842 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
845 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
848 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
854 private static function checkIPAddress( $ip ) {
857 if ( !IP
::isIPAddress( $ip ) ) {
858 throw new MWException( "Attempt to write \"" . $ip .
859 "\" as an IP address into recent changes" );
862 $ip = $wgRequest->getIP();
872 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
873 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
874 * or rows which will be deleted soon shouldn't be included.
876 * @param $timestamp mixed MWTimestamp compatible timestamp
877 * @param $tolerance integer Tolerance in seconds
880 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
883 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;