Fixing a variable name, and a PHP warning.
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blob5101815385d20efb380113773a8d0983b4479291
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 );
14         extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
16         $dbw->delete( 'recentchanges', '*' );
18         print( "Loading from page and revision tables...\n" );
20         global $wgRCMaxAge;
21         $cutoff = time() - $wgRCMaxAge;
22         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
23                 array(
24                         'rc_timestamp'  => 'rev_timestamp',
25                         'rc_cur_time'   => 'rev_timestamp',
26                         'rc_user'       => 'rev_user',
27                         'rc_user_text'  => 'rev_user_text',
28                         'rc_namespace'  => 'page_namespace',
29                         'rc_title'      => 'page_title',
30                         'rc_comment'    => 'rev_comment',
31                         'rc_minor'      => 'rev_minor_edit',
32                         'rc_bot'        => 0,
33                         'rc_new'        => 'page_is_new',
34                         'rc_cur_id'     => 'page_id',
35                         'rc_this_oldid' => 'rev_id',
36                         'rc_last_oldid' => 0, // is this ok?
37                         'rc_type'       => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
38                 ), array(
39                         'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
40                         'rev_page=page_id'
41                 ), $fname,
42                 array(), // INSERT options
43                 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
44         );
47 function rebuildRecentChangesTablePass2()
49         $dbw = wfGetDB( DB_MASTER );
50         list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
52         print( "Updating links and size differences...\n" );
54         # Fill in the rc_last_oldid field, which points to the previous edit
55         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
56           "ORDER BY rc_cur_id,rc_timestamp";
57         $res = $dbw->query( $sql, DB_MASTER );
59         $lastCurId = 0;
60         $lastOldId = 0;
61         while ( $obj = $dbw->fetchObject( $res ) ) {
62                 $new = 0;
63                 if( $obj->rc_cur_id != $lastCurId ) {
64                         # Switch! Look up the previous last edit, if any
65                         $lastCurId = intval( $obj->rc_cur_id );
66                         $emit = $obj->rc_timestamp;
67                         $sql2 = "SELECT rev_id, rev_len FROM $revision " .
68                                 "WHERE rev_page={$lastCurId} ".
69                                 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
70                         $res2 = $dbw->query( $sql2 );
71                         if( $row = $dbw->fetchObject( $res2 ) ) {
72                                 $lastOldId = intval( $row->rev_id );
73                                 $lastSize = $row->rev_len; # Grab the last text size
74                         } else {
75                                 # No previous edit
76                                 $lastOldId = 0;
77                                 $lastSize = 'NULL';
78                                 $new = 1;
79                         }
80                         $dbw->freeResult( $res2 );
81                 }
82                 if( $lastCurId == 0 ) {
83                         print "Uhhh, something wrong? No curid\n";
84                 } else {
85                         # Grab the entry's text size
86                         $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
87                         $size = $size ? $size : 'NULL';
88                         
89                         $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
90                                 "rc_old_len='$lastSize',rc_new_len='$size' " .
91                                 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
92                         $dbw->query( $sql3 );
93                         
94                         $lastOldId = intval( $obj->rc_this_oldid );
95                 }
96         }
97         $dbw->freeResult( $res );
100 function rebuildRecentChangesTablePass3()
102         global $wgGroupPermissions, $wgUseRCPatrol;
103                         
104         $dbw = wfGetDB( DB_MASTER );
105         
106         list ($recentchanges, $usergroups) = $dbw->tableNamesN( 'recentchanges', 'user_groups' );
108         $botgroups = $autopatrolgroups = array();
109         foreach( $wgGroupPermissions as $group => $rights ) {
110                 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
111                         $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
112                 }
113                 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
114                         $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
115                 }
116         }
117         # Flag our recent bot edits
118         if( !empty($botgroups) ) {
119                 $botwhere = implode(',',$botgroups);
120                 $botusers = array();
122                 print( "Flagging bot account edits...\n" );
124                 # Find all users in RC that are bots
125                 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
126                         "LEFT JOIN $usergroups ON rc_user=ug_user " . 
127                         "WHERE ug_group IN($botwhere)";
128                 $res = $dbw->query( $sql, DB_MASTER );
130                 while( $obj = $dbw->fetchObject( $res ) ) {
131                         $botusers[] = $obj->rc_user;
132                 }
133                 # Fill in the rc_bot field
134                 if( !empty($botusers) ) {
135                         $botwhere = implode(',',$botusers);
136                         $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
137                                 "WHERE rc_user IN($botwhere)";
138                         $dbw->query( $sql2 );
139                 }
140         }
141         # Flag our recent autopatrolled edits
142         if( !empty($autopatrolgroups) ) {
143                 $patrolwhere = implode(',',$autopatrolgroups);
144                 $patrolusers = array();
146                 print( "Flagging auto-patrolled edits...\n" );
148                 # Find all users in RC with autopatrol rights
149                 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
150                         "LEFT JOIN $usergroups ON rc_user=ug_user " . 
151                         "WHERE ug_group IN($patrolwhere)";
152                 $res = $dbw->query( $sql, DB_MASTER );
154                 while( $obj = $dbw->fetchObject( $res ) ) {
155                         $patrolusers[] = $obj->rc_user;
156                 }
157                 
158                 # Fill in the rc_patrolled field
159                 if( !empty($patrolusers) ) {
160                         $patrolwhere = implode(',',$patrolusers);
161                         $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
162                                 "WHERE rc_user IN($patrolwhere)";
163                         $dbw->query( $sql2 );
164                 }
165         }
166         
167         $dbw->freeResult( $res );