Fix bug when using Mediawiki Installer.
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.inc
index 586c9b6..087c8de 100644 (file)
@@ -6,18 +6,33 @@
  * @addtogroup Maintenance
  */
 
+/** Public entry; more passes might come in! :) */
+function rebuildRecentChangesTable() {
+       rebuildRecentChangesTablePass1();
+       rebuildRecentChangesTablePass2();
+       rebuildRecentChangesTablePass3();
+}
+
 /** */
 function rebuildRecentChangesTablePass1()
 {
        $fname = 'rebuildRecentChangesTablePass1';
        $dbw = wfGetDB( DB_MASTER );
-       extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
 
        $dbw->delete( 'recentchanges', '*' );
 
        print( "Loading from page and revision tables...\n" );
 
        global $wgRCMaxAge;
+
+       print( '$wgRCMaxAge=' . $wgRCMaxAge );
+       $days = $wgRCMaxAge / 24 / 3600;
+       if ( intval($days) == $days ) {
+                       print( " (" . $days . " days)\n" );
+       } else {
+                       print( " (approx. " .  intval($days) . " days)\n" );
+       }
+
        $cutoff = time() - $wgRCMaxAge;
        $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
                array(
@@ -49,10 +64,9 @@ function rebuildRecentChangesTablePass2()
        $dbw = wfGetDB( DB_MASTER );
        list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
 
-       print( "Updating links...\n" );
+       print( "Updating links and size differences...\n" );
 
        # Fill in the rc_last_oldid field, which points to the previous edit
-       #
        $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
          "ORDER BY rc_cur_id,rc_timestamp";
        $res = $dbw->query( $sql, DB_MASTER );
@@ -65,15 +79,17 @@ function rebuildRecentChangesTablePass2()
                        # Switch! Look up the previous last edit, if any
                        $lastCurId = intval( $obj->rc_cur_id );
                        $emit = $obj->rc_timestamp;
-                       $sql2 = "SELECT rev_id FROM $revision " .
+                       $sql2 = "SELECT rev_id, rev_len FROM $revision " .
                                "WHERE rev_page={$lastCurId} ".
                                "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
                        $res2 = $dbw->query( $sql2 );
                        if( $row = $dbw->fetchObject( $res2 ) ) {
                                $lastOldId = intval( $row->rev_id );
+                               $lastSize = $row->rev_len; # Grab the last text size
                        } else {
                                # No previous edit
                                $lastOldId = 0;
+                               $lastSize = 'NULL';
                                $new = 1;
                        }
                        $dbw->freeResult( $res2 );
@@ -81,9 +97,15 @@ function rebuildRecentChangesTablePass2()
                if( $lastCurId == 0 ) {
                        print "Uhhh, something wrong? No curid\n";
                } else {
-                       $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new " .
+                       # Grab the entry's text size
+                       $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
+                       $size = $size ? $size : 'NULL';
+                       
+                       $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
+                               "rc_old_len='$lastSize',rc_new_len='$size' " .
                                "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
                        $dbw->query( $sql3 );
+                       
                        $lastOldId = intval( $obj->rc_this_oldid );
                }
        }
@@ -92,22 +114,69 @@ function rebuildRecentChangesTablePass2()
 
 function rebuildRecentChangesTablePass3()
 {
+       global $wgGroupPermissions, $wgUseRCPatrol;
+                       
        $dbw = wfGetDB( DB_MASTER );
-       list ($recentchanges, $usergroups) = $dbw->tableNamesN( 'recentchanges', 'user_groups' );
+       
+       list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
 
-       print( "Flagging bot account edits...\n" );
+       $botgroups = $autopatrolgroups = array();
+       foreach( $wgGroupPermissions as $group => $rights ) {
+               if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
+                       $botgroups[] = $dbw->addQuotes( $group );
+               }
+               if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
+                       $autopatrolgroups[] = $dbw->addQuotes( $group );
+               }
+       }
+       # Flag our recent bot edits
+       if( !empty($botgroups) ) {
+               $botwhere = implode(',',$botgroups);
+               $botusers = array();
 
-       # Fill in the rc_bot field
-       $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
-               "LEFT JOIN $usergroups ON rc_user=ug_user " . 
-               "WHERE ug_group='bot'";
-       $res = $dbw->query( $sql, DB_MASTER );
+               print( "Flagging bot account edits...\n" );
 
-       while ( $obj = $dbw->fetchObject( $res ) ) {
-               $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
-                               "WHERE rc_user={$obj->rc_user}";
-               $dbw->query( $sql2 );
+               # Find all users that are bots
+               $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
+                       "WHERE ug_group IN($botwhere) AND user_id = ug_user";
+               $res = $dbw->query( $sql, DB_MASTER );
+
+               while( $obj = $dbw->fetchObject( $res ) ) {
+                       $botusers[] = $dbw->addQuotes( $obj->user_name );
+               }
+               # Fill in the rc_bot field
+               if( !empty($botusers) ) {
+                       $botwhere = implode(',',$botusers);
+                       $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
+                               "WHERE rc_user_text IN($botwhere)";
+                       $dbw->query( $sql2 );
+               }
+       }
+       # Flag our recent autopatrolled edits
+       if( !empty($autopatrolgroups) ) {
+               $patrolwhere = implode(',',$autopatrolgroups);
+               $patrolusers = array();
+
+               print( "Flagging auto-patrolled edits...\n" );
+
+               # Find all users in RC with autopatrol rights
+               $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
+                       "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
+               $res = $dbw->query( $sql, DB_MASTER );
+
+               while( $obj = $dbw->fetchObject( $res ) ) {
+                       $patrolusers[] = $dbw->addQuotes( $obj->user_name );
+               }
+               
+               # Fill in the rc_patrolled field
+               if( !empty($patrolusers) ) {
+                       $patrolwhere = implode(',',$patrolusers);
+                       $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
+                               "WHERE rc_user_text IN($patrolwhere)";
+                       $dbw->query( $sql2 );
+               }
        }
+       
        $dbw->freeResult( $res );
 }