Part of bug 26280: added license headers to PHP files in maintenance
[lhc/web/wiklou.git] / maintenance / archives / upgradeLogging.php
1 <?php
2 /**
3 * Replication-safe online upgrade script for log_id/log_deleted
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup MaintenanceArchive
22 */
23
24 require( dirname( __FILE__ ) . '/../commandLine.inc' );
25
26 class UpdateLogging {
27 var $dbw;
28 var $batchSize = 1000;
29 var $minTs = false;
30
31 function execute() {
32 $this->dbw = wfGetDB( DB_MASTER );
33 $logging = $this->dbw->tableName( 'logging' );
34 $logging_1_10 = $this->dbw->tableName( 'logging_1_10' );
35 $logging_pre_1_10 = $this->dbw->tableName( 'logging_pre_1_10' );
36
37 if ( $this->dbw->tableExists( 'logging_pre_1_10' ) && !$this->dbw->tableExists( 'logging' ) ) {
38 # Fix previous aborted run
39 echo "Cleaning up from previous aborted run\n";
40 $this->dbw->query( "RENAME TABLE $logging_pre_1_10 TO $logging", __METHOD__ );
41 }
42
43 if ( $this->dbw->tableExists( 'logging_pre_1_10' ) ) {
44 echo "This script has already been run to completion\n";
45 return;
46 }
47
48 # Create the target table
49 if ( !$this->dbw->tableExists( 'logging_1_10' ) ) {
50 global $wgDBTableOptions;
51
52 $sql = <<<EOT
53 CREATE TABLE $logging_1_10 (
54 -- Log ID, for referring to this specific log entry, probably for deletion and such.
55 log_id int unsigned NOT NULL auto_increment,
56
57 -- Symbolic keys for the general log type and the action type
58 -- within the log. The output format will be controlled by the
59 -- action field, but only the type controls categorization.
60 log_type varbinary(10) NOT NULL default '',
61 log_action varbinary(10) NOT NULL default '',
62
63 -- Timestamp. Duh.
64 log_timestamp binary(14) NOT NULL default '19700101000000',
65
66 -- The user who performed this action; key to user_id
67 log_user int unsigned NOT NULL default 0,
68
69 -- Key to the page affected. Where a user is the target,
70 -- this will point to the user page.
71 log_namespace int NOT NULL default 0,
72 log_title varchar(255) binary NOT NULL default '',
73
74 -- Freeform text. Interpreted as edit history comments.
75 log_comment varchar(255) NOT NULL default '',
76
77 -- LF separated list of miscellaneous parameters
78 log_params blob NOT NULL,
79
80 -- rev_deleted for logs
81 log_deleted tinyint unsigned NOT NULL default '0',
82
83 PRIMARY KEY log_id (log_id),
84 KEY type_time (log_type, log_timestamp),
85 KEY user_time (log_user, log_timestamp),
86 KEY page_time (log_namespace, log_title, log_timestamp),
87 KEY times (log_timestamp)
88
89 ) $wgDBTableOptions
90 EOT;
91 echo "Creating table logging_1_10\n";
92 $this->dbw->query( $sql, __METHOD__ );
93 }
94
95 # Synchronise the tables
96 echo "Doing initial sync...\n";
97 $this->sync( 'logging', 'logging_1_10' );
98 echo "Sync done\n\n";
99
100 # Rename the old table away
101 echo "Renaming the old table to $logging_pre_1_10\n";
102 $this->dbw->query( "RENAME TABLE $logging TO $logging_pre_1_10", __METHOD__ );
103
104 # Copy remaining old rows
105 # Done before the new table is active so that $copyPos is accurate
106 echo "Doing final sync...\n";
107 $this->sync( 'logging_pre_1_10', 'logging_1_10' );
108
109 # Move the new table in
110 echo "Moving the new table in...\n";
111 $this->dbw->query( "RENAME TABLE $logging_1_10 TO $logging", __METHOD__ );
112 echo "Finished.\n";
113 }
114
115 /**
116 * Copy all rows from $srcTable to $dstTable
117 */
118 function sync( $srcTable, $dstTable ) {
119 $batchSize = 1000;
120 $minTs = $this->dbw->selectField( $srcTable, 'MIN(log_timestamp)', false, __METHOD__ );
121 $minTsUnix = wfTimestamp( TS_UNIX, $minTs );
122 $numRowsCopied = 0;
123
124 while ( true ) {
125 $maxTs = $this->dbw->selectField( $srcTable, 'MAX(log_timestamp)', false, __METHOD__ );
126 $copyPos = $this->dbw->selectField( $dstTable, 'MAX(log_timestamp)', false, __METHOD__ );
127 $maxTsUnix = wfTimestamp( TS_UNIX, $maxTs );
128 $copyPosUnix = wfTimestamp( TS_UNIX, $copyPos );
129
130 if ( $copyPos === null ) {
131 $percent = 0;
132 } else {
133 $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
134 }
135 printf( "%s %.2f%%\n", $copyPos, $percent );
136
137 # Handle all entries with timestamp equal to $copyPos
138 if ( $copyPos !== null ) {
139 $numRowsCopied += $this->copyExactMatch( $srcTable, $dstTable, $copyPos );
140 }
141
142 # Now copy a batch of rows
143 if ( $copyPos === null ) {
144 $conds = false;
145 } else {
146 $conds = array( 'log_timestamp > ' . $this->dbw->addQuotes( $copyPos ) );
147 }
148 $srcRes = $this->dbw->select( $srcTable, '*', $conds, __METHOD__,
149 array( 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ) );
150
151 if ( ! $srcRes->numRows() ) {
152 # All done
153 break;
154 }
155
156 $batch = array();
157 foreach ( $srcRes as $srcRow ) {
158 $batch[] = (array)$srcRow;
159 }
160 $this->dbw->insert( $dstTable, $batch, __METHOD__ );
161 $numRowsCopied += count( $batch );
162
163 wfWaitForSlaves( 5 );
164 }
165 echo "Copied $numRowsCopied rows\n";
166 }
167
168 function copyExactMatch( $srcTable, $dstTable, $copyPos ) {
169 $numRowsCopied = 0;
170 $srcRes = $this->dbw->select( $srcTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__ );
171 $dstRes = $this->dbw->select( $dstTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__ );
172
173 if ( $srcRes->numRows() ) {
174 $srcRow = $srcRes->fetchObject();
175 $srcFields = array_keys( (array)$srcRow );
176 $srcRes->seek( 0 );
177 $dstRowsSeen = array();
178
179 # Make a hashtable of rows that already exist in the destination
180 foreach ( $dstRes as $dstRow ) {
181 $reducedDstRow = array();
182 foreach ( $srcFields as $field ) {
183 $reducedDstRow[$field] = $dstRow->$field;
184 }
185 $hash = md5( serialize( $reducedDstRow ) );
186 $dstRowsSeen[$hash] = true;
187 }
188
189 # Copy all the source rows that aren't already in the destination
190 foreach ( $srcRes as $srcRow ) {
191 $hash = md5( serialize( (array)$srcRow ) );
192 if ( !isset( $dstRowsSeen[$hash] ) ) {
193 $this->dbw->insert( $dstTable, (array)$srcRow, __METHOD__ );
194 $numRowsCopied++;
195 }
196 }
197 }
198 return $numRowsCopied;
199 }
200 }
201
202 $ul = new UpdateLogging;
203 $ul->execute();
204