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 * Parsing text to RC_* constants
108 * @param string|array $type
109 * @throws MWException
110 * @return int|array RC_TYPE
112 public static function parseToRCType( $type ) {
113 if ( is_array( $type ) ) {
115 foreach ( $type as $t ) {
116 $retval[] = RecentChange
::parseToRCType( $t );
132 throw new MWException( "Unknown type '$type'" );
137 * Parsing RC_* constants to human-readable test
140 * @return string $type
142 public static function parseFromRCType( $rcType ) {
164 * Obtain the recent change with a given rc_id value
166 * @param int $rcid The rc_id value to retrieve
167 * @return RecentChange
169 public static function newFromId( $rcid ) {
170 return self
::newFromConds( array( 'rc_id' => $rcid ), __METHOD__
);
174 * Find the first recent change matching some specific conditions
176 * @param array $conds Array of conditions
177 * @param mixed $fname Override the method name in profiling/logs
178 * @param array $options Query options
179 * @return RecentChange
181 public static function newFromConds( $conds, $fname = __METHOD__
, $options = array() ) {
182 $dbr = wfGetDB( DB_SLAVE
);
183 $row = $dbr->selectRow( 'recentchanges', self
::selectFields(), $conds, $fname, $options );
184 if ( $row !== false ) {
185 return self
::newFromRow( $row );
192 * Return the list of recentchanges fields that should be selected to create
193 * a new recentchanges object.
196 public static function selectFields() {
228 * @param array $attribs
230 public function setAttribs( $attribs ) {
231 $this->mAttribs
= $attribs;
235 * @param array $extra
237 public function setExtra( $extra ) {
238 $this->mExtra
= $extra;
244 public function &getTitle() {
245 if ( $this->mTitle
=== false ) {
246 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
249 return $this->mTitle
;
253 * Get the User object of the person who performed this change.
257 public function getPerformer() {
258 if ( $this->mPerformer
=== false ) {
259 if ( $this->mAttribs
['rc_user'] ) {
260 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
262 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
266 return $this->mPerformer
;
270 * Writes the data in this object to the database
273 public function save( $noudp = false ) {
274 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
276 $dbw = wfGetDB( DB_MASTER
);
277 if ( !is_array( $this->mExtra
) ) {
278 $this->mExtra
= array();
281 if ( !$wgPutIPinRC ) {
282 $this->mAttribs
['rc_ip'] = '';
285 # If our database is strict about IP addresses, use NULL instead of an empty string
286 if ( $dbw->strictIPs() && $this->mAttribs
['rc_ip'] == '' ) {
287 unset( $this->mAttribs
['rc_ip'] );
290 # Trim spaces on user supplied text
291 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
293 # Make sure summary is truncated (whole multibyte characters)
294 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
296 # Fixup database timestamps
297 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
298 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
300 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
301 if ( $dbw->cascadingDeletes() && $this->mAttribs
['rc_cur_id'] == 0 ) {
302 unset( $this->mAttribs
['rc_cur_id'] );
306 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
309 $this->mAttribs
['rc_id'] = $dbw->insertId();
312 Hooks
::run( 'RecentChange_save', array( &$this ) );
314 # Notify external application via UDP
316 $this->notifyRCFeeds();
319 # E-mail notifications
320 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
321 $editor = $this->getPerformer();
322 $title = $this->getTitle();
324 if ( Hooks
::run( 'AbortEmailNotification', array( $editor, $title, $this ) ) ) {
325 # @todo FIXME: This would be better as an extension hook
326 $enotif = new EmailNotification();
327 $enotif->notifyOnPageChange( $editor, $title,
328 $this->mAttribs
['rc_timestamp'],
329 $this->mAttribs
['rc_comment'],
330 $this->mAttribs
['rc_minor'],
331 $this->mAttribs
['rc_last_oldid'],
332 $this->mExtra
['pageStatus'] );
338 * Notify all the feeds about the change.
339 * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
341 public function notifyRCFeeds( array $feeds = null ) {
343 if ( $feeds === null ) {
347 $performer = $this->getPerformer();
349 foreach ( $feeds as $feed ) {
351 'omit_bots' => false,
352 'omit_anon' => false,
353 'omit_user' => false,
354 'omit_minor' => false,
355 'omit_patrolled' => false,
359 ( $feed['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
360 ( $feed['omit_anon'] && $performer->isAnon() ) ||
361 ( $feed['omit_user'] && !$performer->isAnon() ) ||
362 ( $feed['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
363 ( $feed['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
364 $this->mAttribs
['rc_type'] == RC_EXTERNAL
369 $engine = self
::getEngine( $feed['uri'] );
371 if ( isset( $this->mExtra
['actionCommentIRC'] ) ) {
372 $actionComment = $this->mExtra
['actionCommentIRC'];
374 $actionComment = null;
377 /** @var $formatter RCFeedFormatter */
378 $formatter = is_object( $feed['formatter'] ) ?
$feed['formatter'] : new $feed['formatter']();
379 $line = $formatter->getLine( $feed, $this, $actionComment );
381 $engine->send( $feed, $line );
386 * Gets the stream engine object for a given URI from $wgRCEngines
388 * @param string $uri URI to get the engine object for
389 * @throws MWException
390 * @return RCFeedEngine The engine object
392 public static function getEngine( $uri ) {
395 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
397 throw new MWException( __FUNCTION__
. ": Invalid stream logger URI: '$uri'" );
400 if ( !isset( $wgRCEngines[$scheme] ) ) {
401 throw new MWException( __FUNCTION__
. ": Unknown stream logger URI scheme: $scheme" );
404 return new $wgRCEngines[$scheme];
408 * Mark a given change as patrolled
410 * @param RecentChange|int $change RecentChange or corresponding rc_id
411 * @param bool $auto For automatic patrol
412 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
414 public static function markPatrolled( $change, $auto = false ) {
417 $change = $change instanceof RecentChange
419 : RecentChange
::newFromId( $change );
421 if ( !$change instanceof RecentChange
) {
425 return $change->doMarkPatrolled( $wgUser, $auto );
429 * Mark this RecentChange as patrolled
431 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
432 * 'markedaspatrollederror-noautopatrol' as errors
433 * @param User $user User object doing the action
434 * @param bool $auto For automatic patrol
435 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
437 public function doMarkPatrolled( User
$user, $auto = false ) {
438 global $wgUseRCPatrol, $wgUseNPPatrol;
440 // If recentchanges patrol is disabled, only new pages
442 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) ) {
443 $errors[] = array( 'rcpatroldisabled' );
445 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
446 $right = $auto ?
'autopatrol' : 'patrol';
447 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
448 if ( !Hooks
::run( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
449 $errors[] = array( 'hookaborted' );
451 // Users without the 'autopatrol' right can't patrol their
453 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
454 && !$user->isAllowed( 'autopatrol' )
456 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
461 // If the change was patrolled already, do nothing
462 if ( $this->getAttribute( 'rc_patrolled' ) ) {
465 // Actually set the 'patrolled' flag in RC
466 $this->reallyMarkPatrolled();
467 // Log this patrol event
468 PatrolLog
::record( $this, $auto, $user );
469 Hooks
::run( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
475 * Mark this RecentChange patrolled, without error checking
476 * @return int Number of affected rows
478 public function reallyMarkPatrolled() {
479 $dbw = wfGetDB( DB_MASTER
);
486 'rc_id' => $this->getAttribute( 'rc_id' )
490 // Invalidate the page cache after the page has been patrolled
491 // to make sure that the Patrol link isn't visible any longer!
492 $this->getTitle()->invalidateCache();
494 return $dbw->affectedRows();
498 * Makes an entry in the database corresponding to an edit
500 * @param string $timestamp
501 * @param Title $title
504 * @param string $comment
506 * @param string $lastTimestamp
509 * @param int $oldSize
510 * @param int $newSize
513 * @return RecentChange
515 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
516 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
517 $rc = new RecentChange
;
518 $rc->mTitle
= $title;
519 $rc->mPerformer
= $user;
520 $rc->mAttribs
= array(
521 'rc_timestamp' => $timestamp,
522 'rc_namespace' => $title->getNamespace(),
523 'rc_title' => $title->getDBkey(),
524 'rc_type' => RC_EDIT
,
525 'rc_source' => self
::SRC_EDIT
,
526 'rc_minor' => $minor ?
1 : 0,
527 'rc_cur_id' => $title->getArticleID(),
528 'rc_user' => $user->getId(),
529 'rc_user_text' => $user->getName(),
530 'rc_comment' => $comment,
531 'rc_this_oldid' => $newId,
532 'rc_last_oldid' => $oldId,
533 'rc_bot' => $bot ?
1 : 0,
534 'rc_ip' => self
::checkIPAddress( $ip ),
535 'rc_patrolled' => intval( $patrol ),
536 'rc_new' => 0, # obsolete
537 'rc_old_len' => $oldSize,
538 'rc_new_len' => $newSize,
541 'rc_log_type' => null,
542 'rc_log_action' => '',
547 'prefixedDBkey' => $title->getPrefixedDBkey(),
548 'lastTimestamp' => $lastTimestamp,
549 'oldSize' => $oldSize,
550 'newSize' => $newSize,
551 'pageStatus' => 'changed'
559 * Makes an entry in the database corresponding to page creation
560 * Note: the title object must be loaded with the new id using resetArticleID()
562 * @param string $timestamp
563 * @param Title $title
566 * @param string $comment
572 * @return RecentChange
574 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
575 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
576 $rc = new RecentChange
;
577 $rc->mTitle
= $title;
578 $rc->mPerformer
= $user;
579 $rc->mAttribs
= array(
580 'rc_timestamp' => $timestamp,
581 'rc_namespace' => $title->getNamespace(),
582 'rc_title' => $title->getDBkey(),
584 'rc_source' => self
::SRC_NEW
,
585 'rc_minor' => $minor ?
1 : 0,
586 'rc_cur_id' => $title->getArticleID(),
587 'rc_user' => $user->getId(),
588 'rc_user_text' => $user->getName(),
589 'rc_comment' => $comment,
590 'rc_this_oldid' => $newId,
591 'rc_last_oldid' => 0,
592 'rc_bot' => $bot ?
1 : 0,
593 'rc_ip' => self
::checkIPAddress( $ip ),
594 'rc_patrolled' => intval( $patrol ),
595 'rc_new' => 1, # obsolete
597 'rc_new_len' => $size,
600 'rc_log_type' => null,
601 'rc_log_action' => '',
606 'prefixedDBkey' => $title->getPrefixedDBkey(),
607 'lastTimestamp' => 0,
610 'pageStatus' => 'created'
618 * @param string $timestamp
619 * @param Title $title
621 * @param string $actionComment
623 * @param string $type
624 * @param string $action
625 * @param Title $target
626 * @param string $logComment
627 * @param string $params
629 * @param string $actionCommentIRC
632 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
633 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
635 global $wgLogRestrictions;
637 # Don't add private logs to RC!
638 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
641 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
642 $target, $logComment, $params, $newId, $actionCommentIRC );
649 * @param string $timestamp
650 * @param Title $title
652 * @param string $actionComment
654 * @param string $type
655 * @param string $action
656 * @param Title $target
657 * @param string $logComment
658 * @param string $params
660 * @param string $actionCommentIRC
661 * @return RecentChange
663 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
664 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
667 ## Get pageStatus for email notification
668 switch ( $type . '-' . $action ) {
669 case 'delete-delete':
670 $pageStatus = 'deleted';
673 case 'move-move_redir':
674 $pageStatus = 'moved';
676 case 'delete-restore':
677 $pageStatus = 'restored';
679 case 'upload-upload':
680 $pageStatus = 'created';
682 case 'upload-overwrite':
684 $pageStatus = 'changed';
688 $rc = new RecentChange
;
689 $rc->mTitle
= $target;
690 $rc->mPerformer
= $user;
691 $rc->mAttribs
= array(
692 'rc_timestamp' => $timestamp,
693 'rc_namespace' => $target->getNamespace(),
694 'rc_title' => $target->getDBkey(),
696 'rc_source' => self
::SRC_LOG
,
698 'rc_cur_id' => $target->getArticleID(),
699 'rc_user' => $user->getId(),
700 'rc_user_text' => $user->getName(),
701 'rc_comment' => $logComment,
702 'rc_this_oldid' => 0,
703 'rc_last_oldid' => 0,
704 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
705 'rc_ip' => self
::checkIPAddress( $ip ),
707 'rc_new' => 0, # obsolete
708 'rc_old_len' => null,
709 'rc_new_len' => null,
711 'rc_logid' => $newId,
712 'rc_log_type' => $type,
713 'rc_log_action' => $action,
714 'rc_params' => $params
718 'prefixedDBkey' => $title->getPrefixedDBkey(),
719 'lastTimestamp' => 0,
720 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
721 'pageStatus' => $pageStatus,
722 'actionCommentIRC' => $actionCommentIRC
729 * Initialises the members of this object from a mysql row object
733 public function loadFromRow( $row ) {
734 $this->mAttribs
= get_object_vars( $row );
735 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
736 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
740 * Get an attribute value
742 * @param string $name Attribute name
745 public function getAttribute( $name ) {
746 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
752 public function getAttributes() {
753 return $this->mAttribs
;
757 * Gets the end part of the diff URL associated with this object
758 * Blank if no diff link should be displayed
759 * @param bool $forceCur
762 public function diffLinkTrail( $forceCur ) {
763 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
764 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
765 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
769 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
779 * Returns the change size (HTML).
780 * The lengths can be given optionally.
785 public function getCharacterDifference( $old = 0, $new = 0 ) {
787 $old = $this->mAttribs
['rc_old_len'];
790 $new = $this->mAttribs
['rc_new_len'];
792 if ( $old === null ||
$new === null ) {
796 return ChangesList
::showCharacterDifference( $old, $new );
799 private static function checkIPAddress( $ip ) {
802 if ( !IP
::isIPAddress( $ip ) ) {
803 throw new MWException( "Attempt to write \"" . $ip .
804 "\" as an IP address into recent changes" );
807 $ip = $wgRequest->getIP();
817 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
818 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
819 * or rows which will be deleted soon shouldn't be included.
821 * @param mixed $timestamp MWTimestamp compatible timestamp
822 * @param int $tolerance Tolerance in seconds
825 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
828 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;