SQL syntax error... Shame on me :(
[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 ), array(
54 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
55 'rev_page=page_id'
56 ), $fname,
57 array(), // INSERT options
58 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
59 );
60 }
61
62 function rebuildRecentChangesTablePass2()
63 {
64 $dbw = wfGetDB( DB_MASTER );
65 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
66
67 print( "Updating links and size differences...\n" );
68
69 # Fill in the rc_last_oldid field, which points to the previous edit
70 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
71 "ORDER BY rc_cur_id,rc_timestamp";
72 $res = $dbw->query( $sql, DB_MASTER );
73
74 $lastCurId = 0;
75 $lastOldId = 0;
76 while ( $obj = $dbw->fetchObject( $res ) ) {
77 $new = 0;
78 if( $obj->rc_cur_id != $lastCurId ) {
79 # Switch! Look up the previous last edit, if any
80 $lastCurId = intval( $obj->rc_cur_id );
81 $emit = $obj->rc_timestamp;
82 $sql2 = "SELECT rev_id, rev_len FROM $revision " .
83 "WHERE rev_page={$lastCurId} ".
84 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
85 $res2 = $dbw->query( $sql2 );
86 if( $row = $dbw->fetchObject( $res2 ) ) {
87 $lastOldId = intval( $row->rev_id );
88 $lastSize = $row->rev_len; # Grab the last text size
89 } else {
90 # No previous edit
91 $lastOldId = 0;
92 $lastSize = 'NULL';
93 $new = 1;
94 }
95 $dbw->freeResult( $res2 );
96 }
97 if( $lastCurId == 0 ) {
98 print "Uhhh, something wrong? No curid\n";
99 } else {
100 # Grab the entry's text size
101 $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
102 $size = $size ? $size : 'NULL';
103
104 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
105 "rc_old_len='$lastSize',rc_new_len='$size' " .
106 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
107 $dbw->query( $sql3 );
108
109 $lastOldId = intval( $obj->rc_this_oldid );
110 }
111 }
112 $dbw->freeResult( $res );
113 }
114
115 function rebuildRecentChangesTablePass3()
116 {
117 global $wgGroupPermissions, $wgUseRCPatrol;
118
119 $dbw = wfGetDB( DB_MASTER );
120
121 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
122
123 $botgroups = $autopatrolgroups = array();
124 foreach( $wgGroupPermissions as $group => $rights ) {
125 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
126 $botgroups[] = $dbw->addQuotes( $group );
127 }
128 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
129 $autopatrolgroups[] = $dbw->addQuotes( $group );
130 }
131 }
132 # Flag our recent bot edits
133 if( !empty($botgroups) ) {
134 $botwhere = implode(',',$botgroups);
135 $botusers = array();
136
137 print( "Flagging bot account edits...\n" );
138
139 # Find all users that are bots
140 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
141 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
142 $res = $dbw->query( $sql, DB_MASTER );
143
144 while( $obj = $dbw->fetchObject( $res ) ) {
145 $botusers[] = $dbw->addQuotes( $obj->user_name );
146 }
147 # Fill in the rc_bot field
148 if( !empty($botusers) ) {
149 $botwhere = implode(',',$botusers);
150 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
151 "WHERE rc_user_text IN($botwhere)";
152 $dbw->query( $sql2 );
153 }
154 }
155 # Flag our recent autopatrolled edits
156 if( !empty($autopatrolgroups) ) {
157 $patrolwhere = implode(',',$autopatrolgroups);
158 $patrolusers = array();
159
160 print( "Flagging auto-patrolled edits...\n" );
161
162 # Find all users in RC with autopatrol rights
163 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
164 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
165 $res = $dbw->query( $sql, DB_MASTER );
166
167 while( $obj = $dbw->fetchObject( $res ) ) {
168 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
169 }
170
171 # Fill in the rc_patrolled field
172 if( !empty($patrolusers) ) {
173 $patrolwhere = implode(',',$patrolusers);
174 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
175 "WHERE rc_user_text IN($patrolwhere)";
176 $dbw->query( $sql2 );
177 }
178 }
179
180 $dbw->freeResult( $res );
181 }
182
183 ?>