Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / archives / upgradeLogging.php
blob0749bbf64b8a9a3aec7c36111585b125ad73fe0b
1 <?php
2 /**
3 * Replication-safe online upgrade for log_id/log_deleted fields.
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.
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.
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
20 * @file
21 * @ingroup MaintenanceArchive
24 require __DIR__ . '/../commandLine.inc';
26 /**
27 * Maintenance script that upgrade for log_id/log_deleted fields in a
28 * replication-safe way.
30 * @ingroup Maintenance
32 class UpdateLogging {
34 /**
35 * @var DatabaseBase
37 public $dbw;
38 public $batchSize = 1000;
39 public $minTs = false;
41 function execute() {
42 $this->dbw = wfGetDB( DB_MASTER );
43 $logging = $this->dbw->tableName( 'logging' );
44 $logging_1_10 = $this->dbw->tableName( 'logging_1_10' );
45 $logging_pre_1_10 = $this->dbw->tableName( 'logging_pre_1_10' );
47 if ( $this->dbw->tableExists( 'logging_pre_1_10' ) && !$this->dbw->tableExists( 'logging' ) ) {
48 # Fix previous aborted run
49 echo "Cleaning up from previous aborted run\n";
50 $this->dbw->query( "RENAME TABLE $logging_pre_1_10 TO $logging", __METHOD__ );
53 if ( $this->dbw->tableExists( 'logging_pre_1_10' ) ) {
54 echo "This script has already been run to completion\n";
55 return;
58 # Create the target table
59 if ( !$this->dbw->tableExists( 'logging_1_10' ) ) {
60 global $wgDBTableOptions;
62 $sql = <<<EOT
63 CREATE TABLE $logging_1_10 (
64 -- Log ID, for referring to this specific log entry, probably for deletion and such.
65 log_id int unsigned NOT NULL auto_increment,
67 -- Symbolic keys for the general log type and the action type
68 -- within the log. The output format will be controlled by the
69 -- action field, but only the type controls categorization.
70 log_type varbinary(10) NOT NULL default '',
71 log_action varbinary(10) NOT NULL default '',
73 -- Timestamp. Duh.
74 log_timestamp binary(14) NOT NULL default '19700101000000',
76 -- The user who performed this action; key to user_id
77 log_user int unsigned NOT NULL default 0,
79 -- Key to the page affected. Where a user is the target,
80 -- this will point to the user page.
81 log_namespace int NOT NULL default 0,
82 log_title varchar(255) binary NOT NULL default '',
84 -- Freeform text. Interpreted as edit history comments.
85 log_comment varchar(255) NOT NULL default '',
87 -- LF separated list of miscellaneous parameters
88 log_params blob NOT NULL,
90 -- rev_deleted for logs
91 log_deleted tinyint unsigned NOT NULL default '0',
93 PRIMARY KEY log_id (log_id),
94 KEY type_time (log_type, log_timestamp),
95 KEY user_time (log_user, log_timestamp),
96 KEY page_time (log_namespace, log_title, log_timestamp),
97 KEY times (log_timestamp)
99 ) $wgDBTableOptions
100 EOT;
101 echo "Creating table logging_1_10\n";
102 $this->dbw->query( $sql, __METHOD__ );
105 # Synchronise the tables
106 echo "Doing initial sync...\n";
107 $this->sync( 'logging', 'logging_1_10' );
108 echo "Sync done\n\n";
110 # Rename the old table away
111 echo "Renaming the old table to $logging_pre_1_10\n";
112 $this->dbw->query( "RENAME TABLE $logging TO $logging_pre_1_10", __METHOD__ );
114 # Copy remaining old rows
115 # Done before the new table is active so that $copyPos is accurate
116 echo "Doing final sync...\n";
117 $this->sync( 'logging_pre_1_10', 'logging_1_10' );
119 # Move the new table in
120 echo "Moving the new table in...\n";
121 $this->dbw->query( "RENAME TABLE $logging_1_10 TO $logging", __METHOD__ );
122 echo "Finished.\n";
126 * Copy all rows from $srcTable to $dstTable
128 function sync( $srcTable, $dstTable ) {
129 $batchSize = 1000;
130 $minTs = $this->dbw->selectField( $srcTable, 'MIN(log_timestamp)', false, __METHOD__ );
131 $minTsUnix = wfTimestamp( TS_UNIX, $minTs );
132 $numRowsCopied = 0;
134 while ( true ) {
135 $maxTs = $this->dbw->selectField( $srcTable, 'MAX(log_timestamp)', false, __METHOD__ );
136 $copyPos = $this->dbw->selectField( $dstTable, 'MAX(log_timestamp)', false, __METHOD__ );
137 $maxTsUnix = wfTimestamp( TS_UNIX, $maxTs );
138 $copyPosUnix = wfTimestamp( TS_UNIX, $copyPos );
140 if ( $copyPos === null ) {
141 $percent = 0;
142 } else {
143 $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
145 printf( "%s %.2f%%\n", $copyPos, $percent );
147 # Handle all entries with timestamp equal to $copyPos
148 if ( $copyPos !== null ) {
149 $numRowsCopied += $this->copyExactMatch( $srcTable, $dstTable, $copyPos );
152 # Now copy a batch of rows
153 if ( $copyPos === null ) {
154 $conds = false;
155 } else {
156 $conds = array( 'log_timestamp > ' . $this->dbw->addQuotes( $copyPos ) );
158 $srcRes = $this->dbw->select( $srcTable, '*', $conds, __METHOD__,
159 array( 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ) );
161 if ( ! $srcRes->numRows() ) {
162 # All done
163 break;
166 $batch = array();
167 foreach ( $srcRes as $srcRow ) {
168 $batch[] = (array)$srcRow;
170 $this->dbw->insert( $dstTable, $batch, __METHOD__ );
171 $numRowsCopied += count( $batch );
173 wfWaitForSlaves();
175 echo "Copied $numRowsCopied rows\n";
178 function copyExactMatch( $srcTable, $dstTable, $copyPos ) {
179 $numRowsCopied = 0;
180 $srcRes = $this->dbw->select( $srcTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__ );
181 $dstRes = $this->dbw->select( $dstTable, '*', array( 'log_timestamp' => $copyPos ), __METHOD__ );
183 if ( $srcRes->numRows() ) {
184 $srcRow = $srcRes->fetchObject();
185 $srcFields = array_keys( (array)$srcRow );
186 $srcRes->seek( 0 );
187 $dstRowsSeen = array();
189 # Make a hashtable of rows that already exist in the destination
190 foreach ( $dstRes as $dstRow ) {
191 $reducedDstRow = array();
192 foreach ( $srcFields as $field ) {
193 $reducedDstRow[$field] = $dstRow->$field;
195 $hash = md5( serialize( $reducedDstRow ) );
196 $dstRowsSeen[$hash] = true;
199 # Copy all the source rows that aren't already in the destination
200 foreach ( $srcRes as $srcRow ) {
201 $hash = md5( serialize( (array)$srcRow ) );
202 if ( !isset( $dstRowsSeen[$hash] ) ) {
203 $this->dbw->insert( $dstTable, (array)$srcRow, __METHOD__ );
204 $numRowsCopied++;
208 return $numRowsCopied;
212 $ul = new UpdateLogging;
213 $ul->execute();