* added $wgEnableAPI
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blobe077da52214ed488f9988564d6f89828fd92904d
1 <?php
2 /**
3  * Rebuild recent changes table.
4  *
5  * @todo document
6  * @package MediaWiki
7  * @subpackage Maintenance
8  */
10 /** */
11 function rebuildRecentChangesTablePass1()
13         $fname = 'rebuildRecentChangesTablePass1';
14         $dbw =& wfGetDB( DB_MASTER );
15         extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
17         $dbw->delete( 'recentchanges', '*' );
19         print( "Loading from page and revision tables...\n" );
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         );
48 function rebuildRecentChangesTablePass2()
50         $dbw =& wfGetDB( DB_MASTER );
51         extract( $dbw->tableNames( 'recentchanges', 'revision' ) );
53         $ns = $id = $count = 0;
54         $title = $ct =  "";
56         print( "Updating links...\n" );
58         # Fill in the rc_last_oldid field, which points to the previous edit
59         #
60         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
61           "ORDER BY rc_cur_id,rc_timestamp";
62         $res = $dbw->query( $sql, DB_MASTER );
64         $lastCurId = 0;
65         $lastOldId = 0;
66         while ( $obj = $dbw->fetchObject( $res ) ) {
67                 $new = 0;
68                 if( $obj->rc_cur_id != $lastCurId ) {
69                         # Switch! Look up the previous last edit, if any
70                         $lastCurId = intval( $obj->rc_cur_id );
71                         $emit = $obj->rc_timestamp;
72                         $sql2 = "SELECT rev_id FROM $revision " .
73                                 "WHERE rev_page={$lastCurId} ".
74                                 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
75                         $res2 = $dbw->query( $sql2 );
76                         if( $row = $dbw->fetchObject( $res2 ) ) {
77                                 $lastOldId = intval( $row->rev_id );
78                         } else {
79                                 # No previous edit
80                                 $lastOldId = 0;
81                                 $new = 1;
82                         }
83                         $dbw->freeResult( $res2 );
84                 }
85                 if( $lastCurId == 0 ) {
86                         print "Uhhh, something wrong? No curid\n";
87                 } else {
88                         $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new " .
89                                 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
90                         $dbw->query( $sql3 );
91                         $lastOldId = intval( $obj->rc_this_oldid );
92                 }
93         }
94         $dbw->freeResult( $res );