46 $logging = $this->dbw->tableName(
'logging' );
47 $logging_1_10 = $this->dbw->tableName(
'logging_1_10' );
48 $logging_pre_1_10 = $this->dbw->tableName(
'logging_pre_1_10' );
50 if ( $this->dbw->tableExists(
'logging_pre_1_10', __METHOD__ )
51 && !$this->dbw->tableExists(
'logging', __METHOD__ )
53 # Fix previous aborted run
54 echo
"Cleaning up from previous aborted run\n";
55 $this->dbw->query(
"RENAME TABLE $logging_pre_1_10 TO $logging", __METHOD__ );
58 if ( $this->dbw->tableExists(
'logging_pre_1_10', __METHOD__ ) ) {
59 echo
"This script has already been run to completion\n";
64 # Create the target table
65 if ( !$this->dbw->tableExists(
'logging_1_10', __METHOD__ ) ) {
69CREATE TABLE $logging_1_10 (
70 -- Log ID,
for referring to
this specific log entry, probably
for deletion and such.
71 log_id
int unsigned NOT NULL auto_increment,
73 -- Symbolic keys
for the general log type and the action type
74 -- within the log. The output format will be controlled by the
75 -- action field, but only the type controls categorization.
76 log_type varbinary(10) NOT NULL
default '',
77 log_action varbinary(10) NOT NULL
default '',
80 log_timestamp binary(14) NOT NULL
default '19700101000000',
82 -- The user who performed
this action; key to user_id
83 log_user
int unsigned NOT NULL
default 0,
85 -- Key to the page affected. Where a user is the target,
86 --
this will point to the user page.
87 log_namespace
int NOT NULL
default 0,
88 log_title varchar(255) binary NOT NULL
default '',
90 -- Freeform text. Interpreted as edit history comments.
91 log_comment varchar(255) NOT NULL
default '',
93 -- LF separated list of miscellaneous parameters
94 log_params blob NOT NULL,
96 -- rev_deleted
for logs
97 log_deleted tinyint
unsigned NOT NULL
default '0',
99 PRIMARY KEY log_id (log_id),
100 KEY type_time (log_type, log_timestamp),
101 KEY user_time (log_user, log_timestamp),
102 KEY page_time (log_namespace, log_title, log_timestamp),
103 KEY times (log_timestamp)
107 echo
"Creating table logging_1_10\n";
108 $this->dbw->query( $sql, __METHOD__ );
111 # Synchronise the tables
112 echo
"Doing initial sync...\n";
113 $this->
sync(
'logging',
'logging_1_10' );
114 echo
"Sync done\n\n";
116 # Rename the old table away
117 echo
"Renaming the old table to $logging_pre_1_10\n";
118 $this->dbw->query(
"RENAME TABLE $logging TO $logging_pre_1_10", __METHOD__ );
120 # Copy remaining old rows
121 # Done before the new table is active so that $copyPos is accurate
122 echo
"Doing final sync...\n";
123 $this->
sync(
'logging_pre_1_10',
'logging_1_10' );
125 # Move the new table in
126 echo
"Moving the new table in...\n";
127 $this->dbw->query(
"RENAME TABLE $logging_1_10 TO $logging", __METHOD__ );
136 private function sync( $srcTable, $dstTable ) {
138 $minTs = $this->dbw->selectField( $srcTable,
'MIN(log_timestamp)',
'', __METHOD__ );
141 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
144 $maxTs = $this->dbw->selectField( $srcTable,
'MAX(log_timestamp)',
'', __METHOD__ );
145 $copyPos = $this->dbw->selectField( $dstTable,
'MAX(log_timestamp)',
'', __METHOD__ );
149 if ( $copyPos ===
null ) {
152 $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
154 printf(
"%s %.2f%%\n", $copyPos, $percent );
156 # Handle all entries with timestamp equal to $copyPos
157 if ( $copyPos !==
null ) {
158 $numRowsCopied += $this->
copyExactMatch( $srcTable, $dstTable, $copyPos );
161 # Now copy a batch of rows
162 if ( $copyPos ===
null ) {
165 $conds = [
'log_timestamp > ' . $this->dbw->addQuotes( $copyPos ) ];
167 $srcRes = $this->dbw->select( $srcTable,
'*', $conds, __METHOD__,
168 [
'LIMIT' =>
$batchSize,
'ORDER BY' =>
'log_timestamp' ] );
170 if ( !$srcRes->numRows() ) {
176 foreach ( $srcRes as $srcRow ) {
177 $batch[] = (array)$srcRow;
179 $this->dbw->insert( $dstTable, $batch, __METHOD__ );
180 $numRowsCopied += count( $batch );
182 $lbFactory->waitForReplication();
184 echo
"Copied $numRowsCopied rows\n";
189 $srcRes = $this->dbw->select( $srcTable,
'*', [
'log_timestamp' => $copyPos ], __METHOD__ );
190 $dstRes = $this->dbw->select( $dstTable,
'*', [
'log_timestamp' => $copyPos ], __METHOD__ );
192 if ( $srcRes->numRows() ) {
193 $srcRow = $srcRes->fetchObject();
194 $srcFields = array_keys( (array)$srcRow );
198 # Make a hashtable of rows that already exist in the destination
199 foreach ( $dstRes as $dstRow ) {
201 foreach ( $srcFields as $field ) {
202 $reducedDstRow[$field] = $dstRow->$field;
204 $hash = md5(
serialize( $reducedDstRow ) );
205 $dstRowsSeen[$hash] =
true;
208 # Copy all the source rows that aren't already in the destination
209 foreach ( $srcRes as $srcRow ) {
210 $hash = md5(
serialize( (array)$srcRow ) );
211 if ( !isset( $dstRowsSeen[$hash] ) ) {
212 $this->dbw->insert( $dstTable, (array)$srcRow, __METHOD__ );
218 return $numRowsCopied;