Refactor RC2UDP code some
[lhc/web/wiklou.git] / includes / RecentChange.php
index 58180bf..b1c70d8 100644 (file)
@@ -56,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;
@@ -140,8 +140,7 @@ class RecentChange
        # Writes the data in this object to the database
        function save()
        {
-               global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, 
-               $wgRC2UDPPort, $wgRC2UDPPrefix, $wgRC2UDPOmitBots;
+               global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
                $fname = 'RecentChange::save';
 
                $dbw = wfGetDB( DB_MASTER );
@@ -154,7 +153,7 @@ class RecentChange
                        $this->mAttribs['rc_ip'] = '';
                }
 
-               ## If our database is strict about IP addresses, use NULL instead of an empty string
+               # If our database is strict about IP addresses, use NULL instead of an empty string
                if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
                        unset( $this->mAttribs['rc_ip'] );
                }
@@ -211,12 +210,7 @@ class RecentChange
 
                # Notify external application via UDP
                if ( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
-                       $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
-                       if ( $conn ) {
-                               $line = $wgRC2UDPPrefix . $this->getIRCLine();
-                               socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
-                               socket_close( $conn );
-                       }
+                       wfRecentChange2UDP( $this->getIRCLine() );
                }
 
                # E-mail notifications
@@ -249,12 +243,70 @@ class RecentChange
         * Mark a given change as patrolled
         *
         * @param mixed $change RecentChange or corresponding rc_id
-        * @returns integer number of affected rows
+        * @param bool $auto for automatic patrol
+        * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
+        */
+       public static function markPatrolled( $change, $auto = false ) {
+               $change = $change instanceof RecentChange
+                       ? $change
+                       : RecentChange::newFromId($change);
+               if( !$change instanceof RecentChange ) {
+                       return null;
+               }
+               return $change->doMarkPatrolled( $auto );
+       }
+       
+       /**
+        * Mark this RecentChange as patrolled
+        *
+        * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
+        * @param bool $auto for automatic patrol
+        * @return array of permissions errors, see Title::getUserPermissionsErrors()
+        */
+       public function doMarkPatrolled( $auto = false ) {
+               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');
+               }
+
+               // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
+               $right = $auto ? 'autopatrol' : 'patrol';
+               $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $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( $errors ) {
+                       return $errors;
+               }
+
+               // If the change was patrolled already, do nothing
+               if( $this->getAttribute('rc_patrolled') )
+                       return array();
+
+               // Actually set the 'patrolled' flag in RC
+               $this->reallyMarkPatrolled();
+
+               // Log this patrol event
+               PatrolLog::record( $this, $auto );
+               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 static function markPatrolled( $change ) {
-               $rcid = $change instanceof RecentChange
-                       ? $change->mAttribs['rc_id']
-                       : $change;
+       public function reallyMarkPatrolled() {
                $dbw = wfGetDB( DB_MASTER );
                $dbw->update(
                        'recentchanges',
@@ -262,7 +314,7 @@ class RecentChange
                                'rc_patrolled' => 1
                        ),
                        array(
-                               'rc_id' => $rcid
+                               'rc_id' => $this->getAttribute('rc_id')
                        ),
                        __METHOD__
                );
@@ -271,8 +323,7 @@ class RecentChange
 
        # Makes an entry in the database corresponding to an edit
        public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
-               $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
-               $newId = 0)
+               $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
        {
                if ( !$ip ) {
                        $ip = wfGetIP();
@@ -317,7 +368,7 @@ class RecentChange
                        'newSize'       => $newSize,
                );
                $rc->save();
-               return( $rc->mAttribs['rc_id'] );
+               return $rc;
        }
 
        /**
@@ -371,7 +422,7 @@ class RecentChange
                        'newSize' => $size
                );
                $rc->save();
-               return( $rc->mAttribs['rc_id'] );
+               return $rc;     
        }
 
        # Makes an entry in the database corresponding to a rename
@@ -431,7 +482,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 )
        {