Merge "Implement static public Parser::getExternalLinkRel"
[lhc/web/wiklou.git] / includes / RecentChange.php
index debec69..87fa428 100644 (file)
@@ -55,6 +55,7 @@
  *  lang            the interwiki prefix, automatically set in save()
  *  oldSize         text size before the change
  *  newSize         text size after the change
+ *  pageStatus      status of the page: created, deleted, moved, restored, changed
  *
  * temporary:       not stored in the database
  *      notificationtimestamp
@@ -125,7 +126,7 @@ class RecentChange {
         */
        public static function newFromConds( $conds, $fname = __METHOD__ ) {
                $dbr = wfGetDB( DB_SLAVE );
-               $row = $dbr->selectRow( 'recentchanges', '*', $conds, $fname );
+               $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname );
                if ( $row !== false ) {
                        return self::newFromRow( $row );
                } else {
@@ -133,6 +134,40 @@ class RecentChange {
                }
        }
 
+       /**
+        * Return the list of recentchanges fields that should be selected to create
+        * a new recentchanges object.
+        * @return array
+        */
+       public static function selectFields() {
+               return array(
+                       'rc_id',
+                       'rc_timestamp',
+                       'rc_cur_time',
+                       'rc_user',
+                       'rc_user_text',
+                       'rc_namespace',
+                       'rc_title',
+                       'rc_comment',
+                       'rc_minor',
+                       'rc_bot',
+                       'rc_new',
+                       'rc_cur_id',
+                       'rc_this_oldid',
+                       'rc_last_oldid',
+                       'rc_type',
+                       'rc_patrolled',
+                       'rc_ip',
+                       'rc_old_len',
+                       'rc_new_len',
+                       'rc_deleted',
+                       'rc_logid',
+                       'rc_log_type',
+                       'rc_log_action',
+                       'rc_params',
+               );
+       }
+
        # Accessors
 
        /**
@@ -154,7 +189,7 @@ class RecentChange {
         * @return Title
         */
        public function &getTitle() {
-               if( $this->mTitle === false ) {
+               if ( $this->mTitle === false ) {
                        $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
                        # Make sure the correct page ID is process cached
                        $this->mTitle->resetArticleID( $this->mAttribs['rc_cur_id'] );
@@ -186,17 +221,17 @@ class RecentChange {
                global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
 
                $dbw = wfGetDB( DB_MASTER );
-               if( !is_array($this->mExtra) ) {
+               if ( !is_array( $this->mExtra ) ) {
                        $this->mExtra = array();
                }
                $this->mExtra['lang'] = $wgLocalInterwiki;
 
-               if( !$wgPutIPinRC ) {
+               if ( !$wgPutIPinRC ) {
                        $this->mAttribs['rc_ip'] = '';
                }
 
                # If our database is strict about IP addresses, use NULL instead of an empty string
-               if( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
+               if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
                        unset( $this->mAttribs['rc_ip'] );
                }
 
@@ -204,12 +239,12 @@ class RecentChange {
                $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
 
                # Fixup database timestamps
-               $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
-               $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
+               $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
+               $this->mAttribs['rc_cur_time'] = $dbw->timestamp( $this->mAttribs['rc_cur_time'] );
                $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
 
                ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
-               if( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
+               if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id'] == 0 ) {
                        unset( $this->mAttribs['rc_cur_id'] );
                }
 
@@ -228,18 +263,19 @@ class RecentChange {
                }
 
                # E-mail notifications
-               if( $wgUseEnotif || $wgShowUpdatedMarker ) {
+               if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
                        $editor = $this->getPerformer();
                        $title = $this->getTitle();
 
-                       if ( wfRunHooks( 'AbortEmailNotification', array($editor, $title) ) ) {
+                       if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
                                # @todo FIXME: This would be better as an extension hook
                                $enotif = new EmailNotification();
                                $enotif->notifyOnPageChange( $editor, $title,
                                        $this->mAttribs['rc_timestamp'],
                                        $this->mAttribs['rc_comment'],
                                        $this->mAttribs['rc_minor'],
-                                       $this->mAttribs['rc_last_oldid'] );
+                                       $this->mAttribs['rc_last_oldid'],
+                                       $this->mExtra['pageStatus'] );
                        }
                }
        }
@@ -247,7 +283,7 @@ class RecentChange {
        public function notifyRC2UDP() {
                global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
                # Notify external application via UDP
-               if( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
+               if ( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
                        self::sendToUDP( $this->getIRCLine() );
                }
        }
@@ -268,12 +304,12 @@ class RecentChange {
                $prefix = $prefix ? $prefix : $wgRC2UDPPrefix;
                $port = $port ? $port : $wgRC2UDPPort;
                # Notify external application via UDP
-               if( $address ) {
+               if ( $address ) {
                        $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
-                       if( $conn ) {
+                       if ( $conn ) {
                                $line = $prefix . $line;
                                wfDebug( __METHOD__ . ": sending UDP line: $line\n" );
-                               socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
+                               socket_sendto( $conn, $line, strlen( $line ), 0, $address, $port );
                                socket_close( $conn );
                                return true;
                        } else {
@@ -304,9 +340,9 @@ class RecentChange {
 
                $change = $change instanceof RecentChange
                        ? $change
-                       : RecentChange::newFromId($change);
+                       : RecentChange::newFromId( $change );
 
-               if( !$change instanceof RecentChange ) {
+               if ( !$change instanceof RecentChange ) {
                        return null;
                }
                return $change->doMarkPatrolled( $wgUser, $auto );
@@ -325,32 +361,32 @@ class RecentChange {
                $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');
+               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, $user ) );
-               if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
-                       $errors[] = array('hookaborted');
+               if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
+                       $errors[] = array( 'hookaborted' );
                }
                // Users without the 'autopatrol' right can't patrol their
                // own revisions
-               if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
-                       $errors[] = array('markedaspatrollederror-noautopatrol');
+               if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
+                       $errors[] = array( 'markedaspatrollederror-noautopatrol' );
                }
-               if( $errors ) {
+               if ( $errors ) {
                        return $errors;
                }
                // If the change was patrolled already, do nothing
-               if( $this->getAttribute('rc_patrolled') ) {
+               if ( $this->getAttribute( 'rc_patrolled' ) ) {
                        return array();
                }
                // Actually set the 'patrolled' flag in RC
                $this->reallyMarkPatrolled();
                // Log this patrol event
                PatrolLog::record( $this, $auto, $user );
-               wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
+               wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
                return array();
        }
 
@@ -366,7 +402,7 @@ class RecentChange {
                                'rc_patrolled' => 1
                        ),
                        array(
-                               'rc_id' => $this->getAttribute('rc_id')
+                               'rc_id' => $this->getAttribute( 'rc_id' )
                        ),
                        __METHOD__
                );
@@ -392,7 +428,7 @@ class RecentChange {
         * @return RecentChange
         */
        public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
-               $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
+               $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
                $rc = new RecentChange;
                $rc->mTitle = $title;
                $rc->mPerformer = $user;
@@ -411,7 +447,7 @@ class RecentChange {
                        'rc_last_oldid' => $oldId,
                        'rc_bot'        => $bot ? 1 : 0,
                        'rc_ip'         => self::checkIPAddress( $ip ),
-                       'rc_patrolled'  => intval($patrol),
+                       'rc_patrolled'  => intval( $patrol ),
                        'rc_new'        => 0,  # obsolete
                        'rc_old_len'    => $oldSize,
                        'rc_new_len'    => $newSize,
@@ -427,6 +463,7 @@ class RecentChange {
                        'lastTimestamp' => $lastTimestamp,
                        'oldSize'       => $oldSize,
                        'newSize'       => $newSize,
+                       'pageStatus'   => 'changed'
                );
                $rc->save();
                return $rc;
@@ -450,7 +487,7 @@ class RecentChange {
         * @return RecentChange
         */
        public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
-               $ip='', $size=0, $newId=0, $patrol=0 ) {
+               $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
                $rc = new RecentChange;
                $rc->mTitle = $title;
                $rc->mPerformer = $user;
@@ -469,7 +506,7 @@ class RecentChange {
                        'rc_last_oldid'     => 0,
                        'rc_bot'            => $bot ? 1 : 0,
                        'rc_ip'             => self::checkIPAddress( $ip ),
-                       'rc_patrolled'      => intval($patrol),
+                       'rc_patrolled'      => intval( $patrol ),
                        'rc_new'            => 1, # obsolete
                        'rc_old_len'        => 0,
                        'rc_new_len'        => $size,
@@ -484,7 +521,8 @@ class RecentChange {
                        'prefixedDBkey' => $title->getPrefixedDBkey(),
                        'lastTimestamp' => 0,
                        'oldSize' => 0,
-                       'newSize' => $size
+                       'newSize' => $size,
+                       'pageStatus' => 'created'
                );
                $rc->save();
                return $rc;
@@ -506,11 +544,11 @@ class RecentChange {
         * @return bool
         */
        public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
-               $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' )
+               $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
        {
                global $wgLogRestrictions;
                # Don't add private logs to RC!
-               if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
+               if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
                        return false;
                }
                $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
@@ -535,9 +573,30 @@ class RecentChange {
         * @return RecentChange
         */
        public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
-               $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) {
+               $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
                global $wgRequest;
 
+               ## Get pageStatus for email notification
+               switch ( $type . '-' . $action ) {
+                       case 'delete-delete':
+                               $pageStatus = 'deleted';
+                               break;
+                       case 'move-move':
+                       case 'move-move_redir':
+                               $pageStatus = 'moved';
+                               break;
+                       case 'delete-restore':
+                               $pageStatus = 'restored';
+                               break;
+                       case 'upload-upload':
+                               $pageStatus = 'created';
+                               break;
+                       case 'upload-overwrite':
+                       default:
+                               $pageStatus = 'changed';
+                               break;
+               }
+
                $rc = new RecentChange;
                $rc->mTitle = $target;
                $rc->mPerformer = $user;
@@ -571,6 +630,7 @@ class RecentChange {
                        'prefixedDBkey' => $title->getPrefixedDBkey(),
                        'lastTimestamp' => 0,
                        'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
+                       'pageStatus'    => $pageStatus,
                        'actionCommentIRC' => $actionCommentIRC
                );
                return $rc;
@@ -583,7 +643,7 @@ class RecentChange {
         */
        public function loadFromRow( $row ) {
                $this->mAttribs = get_object_vars( $row );
-               $this->mAttribs['rc_timestamp'] = wfTimestamp(TS_MW, $this->mAttribs['rc_timestamp']);
+               $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
                $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
        }
 
@@ -594,7 +654,7 @@ class RecentChange {
         */
        public function loadFromCurRow( $row ) {
                $this->mAttribs = array(
-                       'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
+                       'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
                        'rc_cur_time' => $row->rev_timestamp,
                        'rc_user' => $row->rev_user,
                        'rc_user_text' => $row->rev_user_text,
@@ -604,19 +664,19 @@ class RecentChange {
                        'rc_minor' => $row->rev_minor_edit ? 1 : 0,
                        'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
                        'rc_cur_id' => $row->page_id,
-                       'rc_this_oldid' => $row->rev_id,
-                       'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
-                       'rc_bot'        => 0,
+                       'rc_this_oldid' => $row->rev_id,
+                       'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
+                       'rc_bot' => 0,
                        'rc_ip' => '',
                        'rc_id' => $row->rc_id,
                        'rc_patrolled' => $row->rc_patrolled,
                        'rc_new' => $row->page_is_new, # obsolete
                        'rc_old_len' => $row->rc_old_len,
                        'rc_new_len' => $row->rc_new_len,
-                       'rc_params' => isset($row->rc_params) ? $row->rc_params : '',
-                       'rc_log_type' => isset($row->rc_log_type) ? $row->rc_log_type : null,
-                       'rc_log_action' => isset($row->rc_log_action) ? $row->rc_log_action : null,
-                       'rc_log_id' => isset($row->rc_log_id) ? $row->rc_log_id: 0,
+                       'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
+                       'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
+                       'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
+                       'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
                        'rc_deleted' => $row->rc_deleted // MUST be set
                );
        }
@@ -645,13 +705,13 @@ class RecentChange {
         * @return string
         */
        public function diffLinkTrail( $forceCur ) {
-               if( $this->mAttribs['rc_type'] == RC_EDIT ) {
-                       $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
-                               "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
-                       if( $forceCur ) {
+               if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
+                       $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
+                               "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
+                       if ( $forceCur ) {
                                $trail .= '&diff=0' ;
                        } else {
-                               $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
+                               $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
                        }
                } else {
                        $trail = '';
@@ -666,7 +726,7 @@ class RecentChange {
                global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
                        $wgCanonicalServer, $wgScript;
 
-               if( $this->mAttribs['rc_type'] == RC_LOG ) {
+               if ( $this->mAttribs['rc_type'] == RC_LOG ) {
                        // Don't use SpecialPage::getTitleFor, backwards compatibility with
                        // IRC API which expects "Log".
                        $titleObj = Title::newFromText( 'Log/' . $this->mAttribs['rc_log_type'], NS_SPECIAL );
@@ -676,11 +736,11 @@ class RecentChange {
                $title = $titleObj->getPrefixedText();
                $title = self::cleanupForIRC( $title );
 
-               if( $this->mAttribs['rc_type'] == RC_LOG ) {
+               if ( $this->mAttribs['rc_type'] == RC_LOG ) {
                        $url = '';
                } else {
                        $url = $wgCanonicalServer . $wgScript;
-                       if( $this->mAttribs['rc_type'] == RC_NEW ) {
+                       if ( $this->mAttribs['rc_type'] == RC_NEW ) {
                                $query = '?oldid=' . $this->mAttribs['rc_this_oldid'];
                        } else {
                                $query = '?diff=' . $this->mAttribs['rc_this_oldid'] . '&oldid=' . $this->mAttribs['rc_last_oldid'];
@@ -693,11 +753,11 @@ class RecentChange {
                        $url .= $query;
                }
 
-               if( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) {
+               if ( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) {
                        $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len'];
-                       if($szdiff < -500) {
+                       if ( $szdiff < -500 ) {
                                $szdiff = "\002$szdiff\002";
-                       } elseif($szdiff >= 0) {
+                       } elseif ( $szdiff >= 0 ) {
                                $szdiff = '+' . $szdiff ;
                        }
                        // @todo i18n with parentheses in content language?
@@ -750,13 +810,13 @@ class RecentChange {
         * @return string
         */
        public function getCharacterDifference( $old = 0, $new = 0 ) {
-               if( $old === 0 ) {
+               if ( $old === 0 ) {
                        $old = $this->mAttribs['rc_old_len'];
                }
-               if( $new === 0 ) {
+               if ( $new === 0 ) {
                        $new = $this->mAttribs['rc_new_len'];
                }
-               if( $old === null || $new === null ) {
+               if ( $old === null || $new === null ) {
                        return '';
                }
                return ChangesList::showCharacterDifference( $old, $new );
@@ -770,8 +830,9 @@ class RecentChange {
                        }
                } else {
                        $ip = $wgRequest->getIP();
-                       if( !$ip )
+                       if ( !$ip ) {
                                $ip = '';
+                       }
                }
                return $ip;
        }