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