5ec27aa4c72d917a9c9f7d6d008992247d063ba8
[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 extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
15
16 $dbw->delete( 'recentchanges', '*' );
17
18 print( "Loading from page and revision tables...\n" );
19
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 );
45 }
46
47 function rebuildRecentChangesTablePass2()
48 {
49 $dbw = wfGetDB( DB_MASTER );
50 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
51
52 print( "Updating links...\n" );
53
54 # Fill in the rc_last_oldid field, which points to the previous edit
55 #
56 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
57 "ORDER BY rc_cur_id,rc_timestamp";
58 $res = $dbw->query( $sql, DB_MASTER );
59
60 $lastCurId = 0;
61 $lastOldId = 0;
62 while ( $obj = $dbw->fetchObject( $res ) ) {
63 $new = 0;
64 if( $obj->rc_cur_id != $lastCurId ) {
65 # Switch! Look up the previous last edit, if any
66 $lastCurId = intval( $obj->rc_cur_id );
67 $emit = $obj->rc_timestamp;
68 $sql2 = "SELECT rev_id FROM $revision " .
69 "WHERE rev_page={$lastCurId} ".
70 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
71 $res2 = $dbw->query( $sql2 );
72 if( $row = $dbw->fetchObject( $res2 ) ) {
73 $lastOldId = intval( $row->rev_id );
74 } else {
75 # No previous edit
76 $lastOldId = 0;
77 $new = 1;
78 }
79 $dbw->freeResult( $res2 );
80 }
81 if( $lastCurId == 0 ) {
82 print "Uhhh, something wrong? No curid\n";
83 } else {
84 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new " .
85 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
86 $dbw->query( $sql3 );
87 $lastOldId = intval( $obj->rc_this_oldid );
88 }
89 }
90 $dbw->freeResult( $res );
91 }
92
93 function rebuildRecentChangesTablePass3()
94 {
95 global $wgGroupPermissions;
96
97 $dbw = wfGetDB( DB_MASTER );
98
99 list ($recentchanges, $usergroups) = $dbw->tableNamesN( 'recentchanges', 'user_groups' );
100
101 $botgroups = $autopatrolgroups = array();
102 foreach( $wgGroupPermissions as $group => $rights ) {
103 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
104 $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
105 }
106 if( isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
107 $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
108 }
109 }
110 # Flag our recent bot edits
111 if( !empty($botgroups) ) {
112 $botwhere = implode(',',$botgroups);
113 $botusers = array();
114
115 print( "Flagging bot account edits...\n" );
116
117 # Find all users in RC that are bots
118 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
119 "LEFT JOIN $usergroups ON rc_user=ug_user " .
120 "WHERE ug_group IN($botwhere)";
121 $res = $dbw->query( $sql, DB_MASTER );
122
123 while( $obj = $dbw->fetchObject( $res ) ) {
124 $botusers[] = $obj->rc_user;
125 }
126 # Fill in the rc_bot field
127 $botwhere = implode(',',$botusers);
128 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
129 "WHERE rc_user IN($botwhere)";
130 $dbw->query( $sql2 );
131 }
132 # Flag our recent autopatrolled edits
133 if( !empty($autopatrolgroups) ) {
134 $patrolwhere = implode(',',$autopatrolgroups);
135 $patrolusers = array();
136
137 print( "Flagging auto-patrolled edits...\n" );
138
139 # Find all users in RC with autopatrol rights
140 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
141 "LEFT JOIN $usergroups ON rc_user=ug_user " .
142 "WHERE ug_group IN($patrolwhere)";
143 $res = $dbw->query( $sql, DB_MASTER );
144
145 while( $obj = $dbw->fetchObject( $res ) ) {
146 $patrolusers[] = $obj->rc_user;
147 }
148
149 # Fill in the rc_patrolled field
150 $patrolwhere = implode(',',$patrolusers);
151 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
152 "WHERE rc_user IN($patrolwhere)";
153 $dbw->query( $sql2 );
154 }
155
156 $dbw->freeResult( $res );
157 }
158
159 ?>