DELAYED can be trouble, when edit rate exceeds insert rate
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blobd14a4b9f34f9c7391a60a28f0d1aa62e15074d8f
1 <?php
3 # Rebuild recent changes table.
6 function rebuildRecentChangesTablePass1()
8         $sql = "DELETE FROM recentchanges";
9         wfQuery( $sql, DB_WRITE );
11         print( "Loading from CUR table...\n" );
13         $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,rc_user," .
14           "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," .
15           "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT cur_timestamp," .
16           "cur_timestamp,cur_user,cur_user_text,cur_namespace,cur_title," .
17           "cur_comment,cur_minor_edit,0,cur_is_new,cur_id,0,0 FROM cur " .
18           "ORDER BY inverse_timestamp LIMIT 5000";
19         wfQuery( $sql, DB_WRITE );
21         print( "Loading from OLD table...\n" );
23         $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,rc_user," .
24       "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," .
25       "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT old_timestamp,cur_timestamp," .
26           "old_user,old_user_text,old_namespace,old_title,old_comment," .
27           "old_minor_edit,0,0,cur_id,old_id,0 FROM old,cur " .
28           "WHERE old_namespace=cur_namespace AND old_title=cur_title ORDER BY old.inverse_timestamp " .
29           "LIMIT 5000";
30         wfQuery( $sql, DB_WRITE );
32         $sql = "SELECT rc_timestamp FROM recentchanges " .
33           "ORDER BY rc_timestamp DESC LIMIT 5000,1";
34         $res = wfQuery( $sql, DB_WRITE );
35         $obj = wfFetchObject( $res );
36         $ts = $obj->rc_timestamp;
38         $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$ts}'";
39         wfQuery( $sql, DB_WRITE );
42 function rebuildRecentChangesTablePass2()
44         $ns = $id = $count = 0;
45         $title = $ct =  "";
47         print( "Updating links...\n" );
49         # Fill in the rc_last_oldid field, which points to the previous edit
50         #
51         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM recentchanges " .
52           "ORDER BY rc_cur_id,rc_timestamp";
53         $res = wfQuery( $sql, DB_WRITE );
55         $lastCurId = 0;
56         $lastOldId = 0;
57         while ( $obj = wfFetchObject( $res ) ) {
58                 $new = 0;
59                 if( $obj->rc_cur_id != $lastCurId ) {
60                         # Switch! Look up the previous last edit, if any
61                         $lastCurId = IntVal( $obj->rc_cur_id );
62                         $emit = wfInvertTimestamp( $obj->rc_timestamp );
63                         $sql2 = "SELECT old_id FROM old,cur " .
64                                 "WHERE old_namespace=cur_namespace AND old_title=cur_title AND cur_id={$lastCurId} ".
65                                 "AND old.inverse_timestamp>'{$emit}' ORDER BY old.inverse_timestamp LIMIT 1";
66                         $res2 = wfQuery( $sql2, DB_WRITE );
67                         if( $row = wfFetchObject( $res2 ) ) {
68                                 $lastOldId = IntVal( $row->old_id );
69                         } else {
70                                 # No previous edit
71                                 $lastOldId = 0;
72                                 $new = 1;
73                         }
74                         wfFreeResult( $res2 );
75                 }
76                 if( $lastCurId == 0 ) {
77                         print "Uhhh, something wrong? No curid\n";
78                 } else {
79                         $sql3 = "UPDATE recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
80                         wfQuery( $sql3, DB_WRITE );
81                         $lastOldId = IntVal( $obj->rc_this_oldid );
82                 }
83         }
84         wfFreeResult( $res );