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