*Clean up deletion of revisions and remove some gaps
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.inc
1 <?php
2 /**
3 * Rebuild recent changes table.
4 *
5 * @todo document
6 * @addtogroup Maintenance
7 */
8
9 /** */
10 function rebuildRecentChangesTablePass1()
11 {
12 $fname = 'rebuildRecentChangesTablePass1';
13 $dbw = wfGetDB( DB_MASTER );
14
15 $dbw->delete( 'recentchanges', '*' );
16
17 print( "Loading from page and revision tables...\n" );
18
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 'rc_deleted' => 'rev_deleted'
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 );
45 }
46
47 function rebuildRecentChangesTablePass2()
48 {
49 $dbw = wfGetDB( DB_MASTER );
50 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
51
52 print( "Updating links and size differences...\n" );
53
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 );
58
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 );
98 }
99
100 function rebuildRecentChangesTablePass3()
101 {
102 $fname = 'rebuildRecentChangesTablePass3';
103 $dbw = wfGetDB( DB_MASTER );
104
105 print( "Loading from user, page, and logging tables...\n" );
106
107 global $wgRCMaxAge, $wgLogRestrictions;
108
109 // Exclude non-public logs
110 $avoidLogs = array();
111 if( isset($wgLogRestrictions) ) {
112 foreach ( $wgLogRestrictions as $logtype => $right ) {
113 // Do not show private logs when not specifically requested
114 if ( $right !='*' ) {
115 $safetype =$dbw->strencode( $logtype );
116 $avoidLogs[] = "'$safetype'";
117 }
118 }
119 }
120 // Some logs don't go in RC. This can't really detect all of those ... :(
121 $avoidLogs[] = "'patrol'"; // hack...we don't want this here
122
123 if( !empty($avoidLogs) ) {
124 $skipLogs = 'log_type NOT IN(' . implode(',',$avoidLogs) . ')';
125 } else {
126 $skipLogs = '1 = 1';
127 }
128
129 $cutoff = time() - $wgRCMaxAge;
130 $dbw->insertSelect( 'recentchanges', array( 'logging', 'page', 'user' ),
131 array(
132 'rc_timestamp' => 'log_timestamp',
133 'rc_cur_time' => 'log_timestamp',
134 'rc_user' => 'log_user',
135 'rc_user_text' => 'user_name',
136 'rc_namespace' => 'log_namespace',
137 'rc_title' => 'log_title',
138 'rc_comment' => 'log_comment',
139 'rc_minor' => 0,
140 'rc_bot' => 0,
141 'rc_new' => 0,
142 'rc_cur_id' => 0,
143 'rc_this_oldid' => 0,
144 'rc_last_oldid' => 0,
145 'rc_type' => RC_LOG,
146 'rc_cur_id' => 'page_id',
147 'rc_log_type' => 'log_type',
148 'rc_log_action' => 'log_action',
149 'rc_logid' => 'log_id',
150 'rc_params' => 'log_params',
151 'rc_deleted' => 'log_deleted'
152 ), array(
153 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
154 'log_user=user_id',
155 'log_namespace=page_namespace',
156 'log_title=page_title',
157 $skipLogs
158 ), $fname,
159 array(), // INSERT options
160 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
161 );
162 }
163
164 function rebuildRecentChangesTablePass4()
165 {
166 global $wgGroupPermissions, $wgUseRCPatrol;
167
168 $dbw = wfGetDB( DB_MASTER );
169
170 list ($recentchanges, $usergroups) = $dbw->tableNamesN( 'recentchanges', 'user_groups' );
171
172 $botgroups = $autopatrolgroups = array();
173 foreach( $wgGroupPermissions as $group => $rights ) {
174 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
175 $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
176 }
177 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
178 $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
179 }
180 }
181 # Flag our recent bot edits
182 if( !empty($botgroups) ) {
183 $botwhere = implode(',',$botgroups);
184 $botusers = array();
185
186 print( "Flagging bot account edits...\n" );
187
188 # Find all users in RC that are bots
189 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
190 "LEFT JOIN $usergroups ON rc_user=ug_user " .
191 "WHERE ug_group IN($botwhere)";
192 $res = $dbw->query( $sql, DB_MASTER );
193
194 while( $obj = $dbw->fetchObject( $res ) ) {
195 $botusers[] = $obj->rc_user;
196 }
197 # Fill in the rc_bot field
198 if( !empty($botusers) ) {
199 $botwhere = implode(',',$botusers);
200 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
201 "WHERE rc_user IN($botwhere)";
202 $dbw->query( $sql2 );
203 }
204 }
205 # Flag our recent autopatrolled edits
206 if( !empty($autopatrolgroups) ) {
207 $patrolwhere = implode(',',$autopatrolgroups);
208 $patrolusers = array();
209
210 print( "Flagging auto-patrolled edits...\n" );
211
212 # Find all users in RC with autopatrol rights
213 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
214 "LEFT JOIN $usergroups ON rc_user=ug_user " .
215 "WHERE ug_group IN($patrolwhere)";
216 $res = $dbw->query( $sql, DB_MASTER );
217
218 while( $obj = $dbw->fetchObject( $res ) ) {
219 $patrolusers[] = $obj->rc_user;
220 }
221
222 # Fill in the rc_patrolled field
223 if( !empty($patrolusers) ) {
224 $patrolwhere = implode(',',$patrolusers);
225 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
226 "WHERE rc_user IN($patrolwhere)";
227 $dbw->query( $sql2 );
228 }
229 }
230
231 $dbw->freeResult( $res );
232 }
233
234 ?>