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