* remove incorrect message from haw
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blob125de551a7f8760a11f4a062c121b107b407ca69
1 <?php
2 /**
3  * Rebuild recent changes table.
4  *
5  * @todo document
6  * @addtogroup Maintenance
7  */
9 /** */
10 function rebuildRecentChangesTablePass1()
12         $fname = 'rebuildRecentChangesTablePass1';
13         $dbw = wfGetDB( DB_MASTER );
15         $dbw->delete( 'recentchanges', '*' );
17         print( "Loading from page and revision tables...\n" );
19         global $wgRCMaxAge;
20         $cutoff = time() - $wgRCMaxAge;
21         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
22                 array(
23                         'rc_timestamp'  => 'rev_timestamp',
24                         'rc_cur_time'   => 'rev_timestamp',
25                         'rc_user'       => 'rev_user',
26                         'rc_user_text'  => 'rev_user_text',
27                         'rc_namespace'  => 'page_namespace',
28                         'rc_title'      => 'page_title',
29                         'rc_comment'    => 'rev_comment',
30                         'rc_minor'      => 'rev_minor_edit',
31                         'rc_bot'        => 0,
32                         'rc_new'        => 'page_is_new',
33                         'rc_cur_id'     => 'page_id',
34                         'rc_this_oldid' => 'rev_id',
35                         'rc_last_oldid' => 0, // is this ok?
36                         'rc_type'       => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
37                 ), array(
38                         'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
39                         'rev_page=page_id'
40                 ), $fname,
41                 array(), // INSERT options
42                 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
43         );
46 function rebuildRecentChangesTablePass2()
48         $dbw = wfGetDB( DB_MASTER );
49         list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
51         print( "Updating links and size differences...\n" );
53         # Fill in the rc_last_oldid field, which points to the previous edit
54         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
55           "ORDER BY rc_cur_id,rc_timestamp";
56         $res = $dbw->query( $sql, DB_MASTER );
58         $lastCurId = 0;
59         $lastOldId = 0;
60         while ( $obj = $dbw->fetchObject( $res ) ) {
61                 $new = 0;
62                 if( $obj->rc_cur_id != $lastCurId ) {
63                         # Switch! Look up the previous last edit, if any
64                         $lastCurId = intval( $obj->rc_cur_id );
65                         $emit = $obj->rc_timestamp;
66                         $sql2 = "SELECT rev_id, rev_len FROM $revision " .
67                                 "WHERE rev_page={$lastCurId} ".
68                                 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
69                         $res2 = $dbw->query( $sql2 );
70                         if( $row = $dbw->fetchObject( $res2 ) ) {
71                                 $lastOldId = intval( $row->rev_id );
72                                 $lastSize = $row->rev_len; # Grab the last text size
73                         } else {
74                                 # No previous edit
75                                 $lastOldId = 0;
76                                 $lastSize = 'NULL';
77                                 $new = 1;
78                         }
79                         $dbw->freeResult( $res2 );
80                 }
81                 if( $lastCurId == 0 ) {
82                         print "Uhhh, something wrong? No curid\n";
83                 } else {
84                         # Grab the entry's text size
85                         $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
86                         $size = $size ? $size : 'NULL';
87                         
88                         $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
89                                 "rc_old_len='$lastSize',rc_new_len='$size' " .
90                                 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
91                         $dbw->query( $sql3 );
92                         
93                         $lastOldId = intval( $obj->rc_this_oldid );
94                 }
95         }
96         $dbw->freeResult( $res );
99 function rebuildRecentChangesTablePass3()
101         global $wgGroupPermissions, $wgUseRCPatrol;
102                         
103         $dbw = wfGetDB( DB_MASTER );
104         
105         list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
107         $botgroups = $autopatrolgroups = array();
108         foreach( $wgGroupPermissions as $group => $rights ) {
109                 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
110                         $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
111                 }
112                 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
113                         $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
114                 }
115         }
116         # Flag our recent bot edits
117         if( !empty($botgroups) ) {
118                 $botwhere = implode(',',$botgroups);
119                 $botusers = array();
121                 print( "Flagging bot account edits...\n" );
123                 # Find all users that are bots
124                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
125                         "WHERE ug_group IN($botwhere) AND user_id = ug_user";
126                 $res = $dbw->query( $sql, DB_MASTER );
128                 while( $obj = $dbw->fetchObject( $res ) ) {
129                         $botusers[] = $dbw->strencode( $obj->user_name );
130                 }
131                 # Fill in the rc_bot field
132                 if( !empty($botusers) ) {
133                         $botwhere = implode(',',$botusers);
134                         $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
135                                 "WHERE rc_user_text IN($botwhere)";
136                         $dbw->query( $sql2 );
137                 }
138         }
139         # Flag our recent autopatrolled edits
140         if( !empty($autopatrolgroups) ) {
141                 $patrolwhere = implode(',',$autopatrolgroups);
142                 $patrolusers = array();
144                 print( "Flagging auto-patrolled edits...\n" );
146                 # Find all users in RC with autopatrol rights
147                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
148                         "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
149                 $res = $dbw->query( $sql, DB_MASTER );
151                 while( $obj = $dbw->fetchObject( $res ) ) {
152                         $patrolusers[] = $dbw->strencode( $obj->user_name );
153                 }
154                 
155                 # Fill in the rc_patrolled field
156                 if( !empty($patrolusers) ) {
157                         $patrolwhere = implode(',',$patrolusers);
158                         $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
159                                 "WHERE rc_user_text IN($patrolwhere)";
160                         $dbw->query( $sql2 );
161                 }
162         }
163         
164         $dbw->freeResult( $res );