Merge "doc: various updates"
[lhc/web/wiklou.git] / maintenance / archives / upgradeLogging.php
1 <?php
2 /**
3 * Replication-safe online upgrade for log_id/log_deleted fields.
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 __DIR__ . '/../commandLine.inc';
25
26 /**
27 * Maintenance script that upgrade for log_id/log_deleted fields in a
28 * replication-safe way.
29 *
30 * @ingroup Maintenance
31 */
32 class UpdateLogging {
33
34 /**
35 * @var DatabaseBase
36 */
37 public $dbw;
38 public $batchSize = 1000;
39 public $minTs = false;
40
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' );
46
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__ );
51 }
52
53 if ( $this->dbw->tableExists( 'logging_pre_1_10' ) ) {
54 echo "This script has already been run to completion\n";
55 return;
56 }
57
58 # Create the target table
59 if ( !$this->dbw->tableExists( 'logging_1_10' ) ) {
60 global $wgDBTableOptions;
61
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,
66
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 '',
72
73 -- Timestamp. Duh.
74 log_timestamp binary(14) NOT NULL default '19700101000000',
75
76 -- The user who performed this action; key to user_id
77 log_user int unsigned NOT NULL default 0,
78
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 '',
83
84 -- Freeform text. Interpreted as edit history comments.
85 log_comment varchar(255) NOT NULL default '',
86
87 -- LF separated list of miscellaneous parameters
88 log_params blob NOT NULL,
89
90 -- rev_deleted for logs
91 log_deleted tinyint unsigned NOT NULL default '0',
92
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)
98
99 ) $wgDBTableOptions
100 EOT;
101 echo "Creating table logging_1_10\n";
102 $this->dbw->query( $sql, __METHOD__ );
103 }
104
105 # Synchronise the tables
106 echo "Doing initial sync...\n";
107 $this->sync( 'logging', 'logging_1_10' );
108 echo "Sync done\n\n";
109
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__ );
113
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' );
118
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";
123 }
124
125 /**
126 * Copy all rows from $srcTable to $dstTable
127 */
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;
133
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 );
139
140 if ( $copyPos === null ) {
141 $percent = 0;
142 } else {
143 $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
144 }
145 printf( "%s %.2f%%\n", $copyPos, $percent );
146
147 # Handle all entries with timestamp equal to $copyPos
148 if ( $copyPos !== null ) {
149 $numRowsCopied += $this->copyExactMatch( $srcTable, $dstTable, $copyPos );
150 }
151
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 ) );
157 }
158 $srcRes = $this->dbw->select( $srcTable, '*', $conds, __METHOD__,
159 array( 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ) );
160
161 if ( ! $srcRes->numRows() ) {
162 # All done
163 break;
164 }
165
166 $batch = array();
167 foreach ( $srcRes as $srcRow ) {
168 $batch[] = (array)$srcRow;
169 }
170 $this->dbw->insert( $dstTable, $batch, __METHOD__ );
171 $numRowsCopied += count( $batch );
172
173 wfWaitForSlaves();
174 }
175 echo "Copied $numRowsCopied rows\n";
176 }
177
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__ );
182
183 if ( $srcRes->numRows() ) {
184 $srcRow = $srcRes->fetchObject();
185 $srcFields = array_keys( (array)$srcRow );
186 $srcRes->seek( 0 );
187 $dstRowsSeen = array();
188
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;
194 }
195 $hash = md5( serialize( $reducedDstRow ) );
196 $dstRowsSeen[$hash] = true;
197 }
198
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++;
205 }
206 }
207 }
208 return $numRowsCopied;
209 }
210 }
211
212 $ul = new UpdateLogging;
213 $ul->execute();