* Remove unneeded code
[lhc/web/wiklou.git] / includes / RecentChange.php
index c1f2722..c22c9ea 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-/**
- *
- */
 
 /**
  * Utility class for creating new RC entries
@@ -59,10 +56,10 @@ class RecentChange
                return $rc;
        }
 
-       public static function newFromCurRow( $row, $rc_this_oldid = 0 )
+       public static function newFromCurRow( $row )
        {
                $rc = new RecentChange;
-               $rc->loadFromCurRow( $row, $rc_this_oldid );
+               $rc->loadFromCurRow( $row );
                $rc->notificationtimestamp = false;
                $rc->numberofWatchingusers = false;
                return $rc;
@@ -143,7 +140,8 @@ class RecentChange
        # Writes the data in this object to the database
        function save()
        {
-               global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
+               global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, 
+               $wgRC2UDPPort, $wgRC2UDPPrefix, $wgRC2UDPOmitBots;
                $fname = 'RecentChange::save';
 
                $dbw = wfGetDB( DB_MASTER );
@@ -212,7 +210,7 @@ class RecentChange
                }
 
                # Notify external application via UDP
-               if ( $wgRC2UDPAddress ) {
+               if ( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
                        $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
                        if ( $conn ) {
                                $line = $wgRC2UDPPrefix . $this->getIRCLine();
@@ -222,12 +220,21 @@ class RecentChange
                }
 
                # E-mail notifications
-               global $wgUseEnotif, $wgUser;
-               if( $wgUseEnotif ) {
+               global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
+               if( $wgUseEnotif || $wgShowUpdatedMarker ) {
+                       // Users
+                       if( $this->mAttribs['rc_user'] ) {
+                               $editor = ($wgUser->getId() == $this->mAttribs['rc_user']) ? 
+                                       $wgUser : User::newFromID( $this->mAttribs['rc_user'] );
+                       // Anons
+                       } else {
+                               $editor = ($wgUser->getName() == $this->mAttribs['rc_user_text']) ? 
+                                       $wgUser : User::newFromName( $this->mAttribs['rc_user_text'], false );
+                       }
                        # FIXME: this would be better as an extension hook
                        $enotif = new EmailNotification();
                        $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
-                       $enotif->notifyOnPageChange( $wgUser, $title,
+                       $enotif->notifyOnPageChange( $editor, $title,
                                $this->mAttribs['rc_timestamp'],
                                $this->mAttribs['rc_comment'],
                                $this->mAttribs['rc_minor'],
@@ -242,12 +249,56 @@ class RecentChange
         * Mark a given change as patrolled
         *
         * @param mixed $change RecentChange or corresponding rc_id
-        * @returns integer number of affected rows
+        * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
         */
        public static function markPatrolled( $change ) {
-               $rcid = $change instanceof RecentChange
-                       ? $change->mAttribs['rc_id']
-                       : $change;
+               $change = $change instanceof RecentChange
+                       ? $change
+                       : RecentChange::newFromId($change);
+               if(!$change instanceof RecentChange)
+                       return null;
+               return $change->doMarkPatrolled();
+       }
+       
+       /**
+        * Mark this RecentChange as patrolled
+        *
+        * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
+        * @return array of permissions errors, see Title::getUserPermissionsErrors()
+        */
+       public function doMarkPatrolled() {
+               global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
+               $errors = array();
+               // If recentchanges patrol is disabled, only new pages
+               // can be patrolled
+               if(!$wgUseRCPatrol && (!$wgUseNPPatrol || $this->getAttribute('rc_type') != RC_NEW))
+                       $errors[] = array('rcpatroldisabled');
+               $errors = array_merge($errors, $this->getTitle()->getUserPermissionsErrors('patrol', $wgUser));
+               if(!wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)))
+                       $errors[] = array('hookaborted');               
+               // Users without the 'autopatrol' right can't patrol their
+               // own revisions
+               if($wgUser->getName() == $this->getAttribute('rc_user_text') &&
+                               !$wgUser->isAllowed('autopatrol'))
+                       $errors[] = array('markedaspatrollederror-noautopatrol');
+               if(!empty($errors))
+                       return $errors;
+
+               // If the change was patrolled already, do nothing
+               if($this->getAttribute('rc_patrolled'))
+                       return array();
+               $this->reallyMarkPatrolled();
+               PatrolLog::record($this);
+               wfRunHooks('MarkPatrolledComplete',
+                       array($this->getAttribute('rc_id'), &$wgUser, false));
+               return array();
+       }
+       
+       /**
+        * Mark this RecentChange patrolled, without error checking
+        * @return int Number of affected rows
+        */
+       public function reallyMarkPatrolled() {
                $dbw = wfGetDB( DB_MASTER );
                $dbw->update(
                        'recentchanges',
@@ -255,7 +306,7 @@ class RecentChange
                                'rc_patrolled' => 1
                        ),
                        array(
-                               'rc_id' => $rcid
+                               'rc_id' => $this->getAttribute('rc_id')
                        ),
                        __METHOD__
                );
@@ -283,7 +334,7 @@ class RecentChange
                        'rc_type'       => RC_EDIT,
                        'rc_minor'      => $minor ? 1 : 0,
                        'rc_cur_id'     => $title->getArticleID(),
-                       'rc_user'       => $user->getID(),
+                       'rc_user'       => $user->getId(),
                        'rc_user_text'  => $user->getName(),
                        'rc_comment'    => $comment,
                        'rc_this_oldid' => $newId,
@@ -337,7 +388,7 @@ class RecentChange
                        'rc_type'           => RC_NEW,
                        'rc_minor'          => $minor ? 1 : 0,
                        'rc_cur_id'         => $title->getArticleID(),
-                       'rc_user'           => $user->getID(),
+                       'rc_user'           => $user->getId(),
                        'rc_user_text'      => $user->getName(),
                        'rc_comment'        => $comment,
                        'rc_this_oldid'     => $newId,
@@ -388,7 +439,7 @@ class RecentChange
                        'rc_type'       => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
                        'rc_minor'      => 0,
                        'rc_cur_id'     => $oldTitle->getArticleID(),
-                       'rc_user'       => $user->getID(),
+                       'rc_user'       => $user->getId(),
                        'rc_user_text'  => $user->getName(),
                        'rc_comment'    => $comment,
                        'rc_this_oldid' => 0,
@@ -424,7 +475,6 @@ class RecentChange
                RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
        }
 
-       # A log entry is different to an edit in that previous revisions are not kept
        public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
           $type, $action, $target, $logComment, $params, $newId=0 )
        {
@@ -446,7 +496,7 @@ class RecentChange
                        'rc_type'       => RC_LOG,
                        'rc_minor'      => 0,
                        'rc_cur_id'     => $target->getArticleID(),
-                       'rc_user'       => $user->getID(),
+                       'rc_user'       => $user->getId(),
                        'rc_user_text'  => $user->getName(),
                        'rc_comment'    => $logComment,
                        'rc_this_oldid' => 0,
@@ -600,7 +650,7 @@ class RecentChange
                        $flag = $rc_log_action;
                } else {
                        $comment = $this->cleanupForIRC( $rc_comment );
-                       $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
+                       $flag = ($rc_new ? "N" : "") . ($rc_minor ? "M" : "") . ($rc_bot ? "B" : "");
                }
                # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
                # no colour (\003) switches back to the term default