* add uniwiki/CreatePage extension to core
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.inc
1 <?php
2 /**
3 * Rebuild recent changes table.
4 *
5 * @file
6 * @todo document
7 * @ingroup Maintenance
8 */
9
10 /** Public entry; more passes might come in! :) */
11 function rebuildRecentChangesTable() {
12 rebuildRecentChangesTablePass1();
13 rebuildRecentChangesTablePass2();
14 rebuildRecentChangesTablePass3();
15 rebuildRecentChangesTablePass4();
16 }
17
18 /** */
19 function rebuildRecentChangesTablePass1()
20 {
21 $dbw = wfGetDB( DB_MASTER );
22
23 $dbw->delete( 'recentchanges', '*' );
24
25 print( "Loading from page and revision tables...\n" );
26
27 global $wgRCMaxAge;
28
29 print( '$wgRCMaxAge=' . $wgRCMaxAge );
30 $days = $wgRCMaxAge / 24 / 3600;
31 if ( intval($days) == $days ) {
32 print( " (" . $days . " days)\n" );
33 } else {
34 print( " (approx. " . intval($days) . " days)\n" );
35 }
36
37 $cutoff = time() - $wgRCMaxAge;
38 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
39 array(
40 'rc_timestamp' => 'rev_timestamp',
41 'rc_cur_time' => 'rev_timestamp',
42 'rc_user' => 'rev_user',
43 'rc_user_text' => 'rev_user_text',
44 'rc_namespace' => 'page_namespace',
45 'rc_title' => 'page_title',
46 'rc_comment' => 'rev_comment',
47 'rc_minor' => 'rev_minor_edit',
48 'rc_bot' => 0,
49 'rc_new' => 'page_is_new',
50 'rc_cur_id' => 'page_id',
51 'rc_this_oldid' => 'rev_id',
52 'rc_last_oldid' => 0, // is this ok?
53 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
54 'rc_deleted' => 'rev_deleted'
55 ), array(
56 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
57 'rev_page=page_id'
58 ), __METHOD__,
59 array(), // INSERT options
60 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
61 );
62 }
63
64 function rebuildRecentChangesTablePass2()
65 {
66 $dbw = wfGetDB( DB_MASTER );
67 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
68
69 print( "Updating links and size differences...\n" );
70
71 # Fill in the rc_last_oldid field, which points to the previous edit
72 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
73 "ORDER BY rc_cur_id,rc_timestamp";
74 $res = $dbw->query( $sql, DB_MASTER );
75
76 $lastCurId = 0;
77 $lastOldId = 0;
78 while ( $obj = $dbw->fetchObject( $res ) ) {
79 $new = 0;
80 if( $obj->rc_cur_id != $lastCurId ) {
81 # Switch! Look up the previous last edit, if any
82 $lastCurId = intval( $obj->rc_cur_id );
83 $emit = $obj->rc_timestamp;
84 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
85 "WHERE rev_page={$lastCurId} ".
86 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
87 $res2 = $dbw->query( $sql2 );
88 if( $row = $dbw->fetchObject( $res2 ) ) {
89 $lastOldId = intval($row->rev_id);
90 # Grab the last text size if available
91 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
92 } else {
93 # No previous edit
94 $lastOldId = 0;
95 $lastSize = 'NULL';
96 $new = 1; // probably true
97 }
98 $dbw->freeResult( $res2 );
99 }
100 if( $lastCurId == 0 ) {
101 print "Uhhh, something wrong? No curid\n";
102 } else {
103 # Grab the entry's text size
104 $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
105 $size = !is_null($size) ? intval($size) : 'NULL';
106
107 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
108 "rc_old_len=$lastSize,rc_new_len=$size " .
109 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
110 $dbw->query( $sql3 );
111
112 $lastOldId = intval( $obj->rc_this_oldid );
113 $lastSize = $size;
114 }
115 }
116 $dbw->freeResult( $res );
117 }
118
119 function rebuildRecentChangesTablePass3()
120 {
121 $dbw = wfGetDB( DB_MASTER );
122
123 print( "Loading from user, page, and logging tables...\n" );
124
125 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
126 // Some logs don't go in RC. This should check for that
127 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
128
129 // Escape...blah blah
130 $selectLogs = array();
131 foreach( $basicRCLogs as $logtype ) {
132 $safetype = $dbw->strencode( $logtype );
133 $selectLogs[] = "'$safetype'";
134 }
135
136 $cutoff = time() - $wgRCMaxAge;
137 list($logging, $page) = $dbw->tableNamesN( 'logging', 'page' );
138 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
139 array(
140 'rc_timestamp' => 'log_timestamp',
141 'rc_cur_time' => 'log_timestamp',
142 'rc_user' => 'log_user',
143 'rc_user_text' => 'user_name',
144 'rc_namespace' => 'log_namespace',
145 'rc_title' => 'log_title',
146 'rc_comment' => 'log_comment',
147 'rc_minor' => 0,
148 'rc_bot' => 0,
149 'rc_patrolled' => 1,
150 'rc_new' => 0,
151 'rc_this_oldid' => 0,
152 'rc_last_oldid' => 0,
153 'rc_type' => RC_LOG,
154 'rc_cur_id' => 'COALESCE(page_id, 0)',
155 'rc_log_type' => 'log_type',
156 'rc_log_action' => 'log_action',
157 'rc_logid' => 'log_id',
158 'rc_params' => 'log_params',
159 'rc_deleted' => 'log_deleted'
160 ), array(
161 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
162 'log_user=user_id',
163 'log_type IN(' . implode(',',$selectLogs) . ')'
164 ), __METHOD__,
165 array(), // INSERT options
166 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
167 );
168 }
169
170 function rebuildRecentChangesTablePass4()
171 {
172 global $wgGroupPermissions, $wgUseRCPatrol;
173
174 $dbw = wfGetDB( DB_MASTER );
175
176 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
177
178 $botgroups = $autopatrolgroups = array();
179 foreach( $wgGroupPermissions as $group => $rights ) {
180 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
181 $botgroups[] = $dbw->addQuotes( $group );
182 }
183 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
184 $autopatrolgroups[] = $dbw->addQuotes( $group );
185 }
186 }
187 # Flag our recent bot edits
188 if( !empty($botgroups) ) {
189 $botwhere = implode(',',$botgroups);
190 $botusers = array();
191
192 print( "Flagging bot account edits...\n" );
193
194 # Find all users that are bots
195 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
196 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
197 $res = $dbw->query( $sql, DB_MASTER );
198
199 while( $obj = $dbw->fetchObject( $res ) ) {
200 $botusers[] = $dbw->addQuotes( $obj->user_name );
201 }
202 # Fill in the rc_bot field
203 if( !empty($botusers) ) {
204 $botwhere = implode(',',$botusers);
205 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
206 "WHERE rc_user_text IN($botwhere)";
207 $dbw->query( $sql2 );
208 }
209 }
210 global $wgMiserMode;
211 # Flag our recent autopatrolled edits
212 if( !$wgMiserMode && !empty($autopatrolgroups) ) {
213 $patrolwhere = implode(',',$autopatrolgroups);
214 $patrolusers = array();
215
216 print( "Flagging auto-patrolled edits...\n" );
217
218 # Find all users in RC with autopatrol rights
219 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
220 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
221 $res = $dbw->query( $sql, DB_MASTER );
222
223 while( $obj = $dbw->fetchObject( $res ) ) {
224 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
225 }
226
227 # Fill in the rc_patrolled field
228 if( !empty($patrolusers) ) {
229 $patrolwhere = implode(',',$patrolusers);
230 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
231 "WHERE rc_user_text IN($patrolwhere)";
232 $dbw->query( $sql2 );
233 }
234 }
235
236 $dbw->freeResult( $res );
237 }