981ff8a90a597104475f4e7824605a00055ee015
4 * Replication-safe online upgrade script for log_id/log_deleted
7 require( dirname(__FILE__
).'/../commandLine.inc' );
11 var $batchSize = 1000;
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' );
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__
);
26 if ( $this->dbw
->tableExists( 'logging_pre_1_10' ) ) {
27 echo "This script has already been run to completion\n";
31 # Create the target table
32 if ( !$this->dbw
->tableExists( 'logging_1_10' ) ) {
33 global $wgDBTableOptions;
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,
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 '',
47 log_timestamp binary(14) NOT NULL default '19700101000000',
49 -- The user who performed this action; key to user_id
50 log_user int unsigned NOT NULL default 0,
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 '',
57 -- Freeform text. Interpreted as edit history comments.
58 log_comment varchar(255) NOT NULL default '',
60 -- LF separated list of miscellaneous parameters
61 log_params blob NOT NULL,
63 -- rev_deleted for logs
64 log_deleted tinyint unsigned NOT NULL default '0',
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)
74 echo "Creating table logging_1_10\n";
75 $this->dbw
->query( $sql, __METHOD__
);
78 # Synchronise the tables
79 echo "Doing initial sync...\n";
80 $this->sync( 'logging', 'logging_1_10' );
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__
);
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' );
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__
);
99 * Copy all rows from $srcTable to $dstTable
101 function sync( $srcTable, $dstTable ) {
103 $minTs = $this->dbw
->selectField( $srcTable, 'MIN(log_timestamp)', false, __METHOD__
);
104 $minTsUnix = wfTimestamp( TS_UNIX
, $minTs );
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 );
113 if ( $copyPos === null ) {
116 $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
118 printf( "%s %.2f%%\n", $copyPos, $percent );
120 # Handle all entries with timestamp equal to $copyPos
121 if ( $copyPos !== null ) {
122 $numRowsCopied +
= $this->copyExactMatch( $srcTable, $dstTable, $copyPos );
125 # Now copy a batch of rows
126 if ( $copyPos === null ) {
129 $conds = array( 'log_timestamp > ' . $this->dbw
->addQuotes( $copyPos ) );
131 $srcRes = $this->dbw
->select( $srcTable, '*', $conds, __METHOD__
,
132 array( 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ) );
134 if ( ! $srcRes->numRows() ) {
140 foreach ( $srcRes as $srcRow ) {
141 $batch[] = (array)$srcRow;
143 $this->dbw
->insert( $dstTable, $batch, __METHOD__
);
144 $numRowsCopied +
= count( $batch );
146 wfWaitForSlaves( 5 );
148 echo "Copied $numRowsCopied rows\n";
151 function copyExactMatch( $srcTable, $dstTable, $copyPos ) {
153 $srcRes = $this->dbw
->select( $srcTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__
);
154 $dstRes = $this->dbw
->select( $dstTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__
);
156 if ( $srcRes->numRows() ) {
157 $srcRow = $srcRes->fetchObject();
158 $srcFields = array_keys( (array)$srcRow );
160 $dstRowsSeen = array();
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;
168 $hash = md5( serialize( $reducedDstRow ) );
169 $dstRowsSeen[$hash] = true;
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__
);
181 return $numRowsCopied;
185 $ul = new UpdateLogging
;