For the maintenance/ directory files:
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.inc
1 <?php
2 /**
3 * Rebuild recent changes table.
4 *
5 * @todo document
6 * @package MediaWiki
7 * @subpackage Maintenance
8 */
9
10 /** */
11 function rebuildRecentChangesTablePass1()
12 {
13 $fname = 'rebuildRecentChangesTablePass1';
14 $dbw =& wfGetDB( DB_MASTER );
15 extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
16
17 $dbw->delete( 'recentchanges', '*' );
18
19 print( "Loading from page and revision tables...\n" );
20
21 global $wgRCMaxAge;
22 $cutoff = time() - $wgRCMaxAge;
23 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
24 array(
25 'rc_timestamp' => 'rev_timestamp',
26 'rc_cur_time' => 'rev_timestamp',
27 'rc_user' => 'rev_user',
28 'rc_user_text' => 'rev_user_text',
29 'rc_namespace' => 'page_namespace',
30 'rc_title' => 'page_title',
31 'rc_comment' => 'rev_comment',
32 'rc_minor' => 'rev_minor_edit',
33 'rc_bot' => 0,
34 'rc_new' => 'page_is_new',
35 'rc_cur_id' => 'page_id',
36 'rc_this_oldid' => 'rev_id',
37 'rc_last_oldid' => 0, // is this ok?
38 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
39 ), array(
40 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
41 'rev_page=page_id'
42 ), $fname,
43 array(), // INSERT options
44 array( 'ORDER BY' => 'rev_timestamp', 'LIMIT' => 5000 ) // SELECT options
45 );
46 }
47
48 function rebuildRecentChangesTablePass2()
49 {
50 $dbw =& wfGetDB( DB_MASTER );
51 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
52
53 print( "Updating links...\n" );
54
55 # Fill in the rc_last_oldid field, which points to the previous edit
56 #
57 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
58 "ORDER BY rc_cur_id,rc_timestamp";
59 $res = $dbw->query( $sql, DB_MASTER );
60
61 $lastCurId = 0;
62 $lastOldId = 0;
63 while ( $obj = $dbw->fetchObject( $res ) ) {
64 $new = 0;
65 if( $obj->rc_cur_id != $lastCurId ) {
66 # Switch! Look up the previous last edit, if any
67 $lastCurId = intval( $obj->rc_cur_id );
68 $emit = $obj->rc_timestamp;
69 $sql2 = "SELECT rev_id FROM $revision " .
70 "WHERE rev_page={$lastCurId} ".
71 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
72 $res2 = $dbw->query( $sql2 );
73 if( $row = $dbw->fetchObject( $res2 ) ) {
74 $lastOldId = intval( $row->rev_id );
75 } else {
76 # No previous edit
77 $lastOldId = 0;
78 $new = 1;
79 }
80 $dbw->freeResult( $res2 );
81 }
82 if( $lastCurId == 0 ) {
83 print "Uhhh, something wrong? No curid\n";
84 } else {
85 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new " .
86 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
87 $dbw->query( $sql3 );
88 $lastOldId = intval( $obj->rc_this_oldid );
89 }
90 }
91 $dbw->freeResult( $res );
92 }
93
94 ?>