Fix some of the rebuild scripts
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.inc
1 <?php
2
3 # Rebuild recent changes table.
4 #
5
6 function rebuildRecentChangesTablePass1()
7 {
8 $fname = 'rebuildRecentChangesTablePass1';
9 $dbw =& wfGetDB( DB_MASTER );
10 extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
11
12 $dbw->delete( 'recentchanges', '*' );
13
14 print( "Loading from CUR table...\n" );
15
16 $dbw->insertSelect( 'recentchanges', 'cur',
17 array(
18 'rc_timestamp' => 'cur_timestamp',
19 'rc_cur_time' => 'cur_timestamp',
20 'rc_user' => 'cur_user',
21 'rc_user_text' => 'cur_user_text',
22 'rc_namespace' => 'cur_namespace',
23 'rc_title' => 'cur_title',
24 'rc_comment' => 'cur_comment',
25 'rc_minor' => 'cur_minor_edit',
26 'rc_bot' => 0,
27 'rc_new' => 'cur_is_new',
28 'rc_cur_id' => 'cur_id',
29 'rc_this_oldid' => 0,
30 'rc_last_oldid' => 0,
31 'rc_type' => 'IF(cur_is_new,' . RC_NEW . ',' . RC_EDIT . ')'
32 ), '*', $fname, array( 'ORDER BY' => 'inverse_timestamp', 'LIMIT' => 5000 )
33 );
34
35 print( "Loading from OLD table...\n" );
36
37 $sql = "INSERT INTO $recentchanges (rc_timestamp,rc_cur_time,rc_user," .
38 "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," .
39 "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT old_timestamp,cur_timestamp," .
40 "old_user,old_user_text,old_namespace,old_title,old_comment," .
41 "old_minor_edit,0,0,cur_id,old_id,0 FROM $old,$cur " .
42 "WHERE old_namespace=cur_namespace AND old_title=cur_title ORDER BY $old.inverse_timestamp " .
43 "LIMIT 5000";
44 $dbw->query( $sql );
45
46 $sql = "SELECT rc_timestamp FROM $recentchanges " .
47 "ORDER BY rc_timestamp DESC LIMIT 5000,1";
48 $res = $dbw->query( $sql );
49 $obj = $dbw->fetchObject( $res );
50 $ts = $obj->rc_timestamp;
51
52 $sql = "DELETE FROM $recentchanges WHERE rc_timestamp < '{$ts}'";
53 $dbw->query( $sql );
54 }
55
56 function rebuildRecentChangesTablePass2()
57 {
58 $dbw =& wfGetDB( DB_MASTER );
59 extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
60
61 $ns = $id = $count = 0;
62 $title = $ct = "";
63
64 print( "Updating links...\n" );
65
66 # Fill in the rc_last_oldid field, which points to the previous edit
67 #
68 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
69 "ORDER BY rc_cur_id,rc_timestamp";
70 $res = $dbw->query( $sql, DB_MASTER );
71
72 $lastCurId = 0;
73 $lastOldId = 0;
74 while ( $obj = $dbw->fetchObject( $res ) ) {
75 $new = 0;
76 if( $obj->rc_cur_id != $lastCurId ) {
77 # Switch! Look up the previous last edit, if any
78 $lastCurId = IntVal( $obj->rc_cur_id );
79 $emit = wfInvertTimestamp( $obj->rc_timestamp );
80 $sql2 = "SELECT old_id FROM $old,$cur " .
81 "WHERE old_namespace=cur_namespace AND old_title=cur_title AND cur_id={$lastCurId} ".
82 "AND $old.inverse_timestamp>'{$emit}' ORDER BY $old.inverse_timestamp LIMIT 1";
83 $res2 = $dbw->query( $sql2 );
84 if( $row = $dbw->fetchObject( $res2 ) ) {
85 $lastOldId = IntVal( $row->old_id );
86 } else {
87 # No previous edit
88 $lastOldId = 0;
89 $new = 1;
90 }
91 $dbw->freeResult( $res2 );
92 }
93 if( $lastCurId == 0 ) {
94 print "Uhhh, something wrong? No curid\n";
95 } else {
96 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new " .
97 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
98 $dbw->query( $sql3 );
99 $lastOldId = IntVal( $obj->rc_this_oldid );
100 }
101 }
102 $dbw->freeResult( $res );
103 }
104
105 ?>