3 ## Convert data from a MySQL mediawiki database into a Postgres mediawiki database
6 ## NOTE: It is probably easier to dump your wiki using maintenance/dumpBackup.php
7 ## and then import it with maintenance/importDump.php
9 ## If having UTF-8 problems, there are reports that adding --compatible=postgresql
17 use vars
qw(%table %tz %special @torder $COM);
20 ## The following options can be changed via command line arguments:
24 ## If the following are zero-length, we omit their arguments entirely:
26 my $MYSQLPASSWORD = '';
29 ## Name of the dump file created
30 my $MYSQLDUMPFILE = 'mediawiki_upgrade.pg';
32 ## How verbose should this script be (0, 1, or 2)
38 Usage: $0 --db=<dbname> --user=<user> [OPTION]...
39 Example: $0 --db=wikidb --user=wikiuser --pass=sushi
41 Converts a MediaWiki schema from MySQL to Postgres
43 db Name of the MySQL database
44 user MySQL database username
45 pass MySQL database password
46 host MySQL database host
47 socket MySQL database socket
48 verbose Verbosity, increases with multiple uses
54 'user=s' => \$MYSQLUSER,
55 'pass=s' => \$MYSQLPASSWORD,
56 'host=s' => \$MYSQLHOST,
57 'socket=s' => \$MYSQLSOCKET,
58 'verbose+' => \$verbose,
64 or ! length $MYSQLUSER
67 ## The Postgres schema file: should not be changed
68 my $PG_SCHEMA = 'tables.sql';
70 ## What version we default to when we can't parse the old schema
71 my $MW_DEFAULT_VERSION = 110;
73 ## Try and find a working version of mysqldump
74 $verbose and warn "Locating the mysqldump executable\n";
75 my @MYSQLDUMP = ('/usr/local/bin/mysqldump', '/usr/bin/mysqldump');
77 for my $mytry (@MYSQLDUMP) {
79 -x $mytry or die qq{Not an executable file: "$mytry"\n};
80 my $version = qx{$mytry -V};
81 $version =~ /^mysqldump\s+Ver\s+\d+/ or die qq{Program at "$mytry" does not act like mysqldump\n};
84 $MYSQLDUMP or die qq{Could not find the mysqldump program\n};
86 ## Flags we use for mysqldump
87 my @MYSQLDUMPARGS = qw(
90 --skip-extended-insert
100 $verbose and warn "Checking that mysqldump can handle our flags\n";
101 ## Make sure this version can handle all the flags we want.
102 ## Combine with user dump below
103 my $MYSQLDUMPARGS = join ' ' => @MYSQLDUMPARGS;
104 ## Argh. Any way to make this work on Win32?
105 my $version = qx{$MYSQLDUMP $MYSQLDUMPARGS 2>&1};
106 if ($version =~ /unknown option/) {
107 die qq{Sorry
, you need to
use a newer version of the mysqldump program than the one at
"$MYSQLDUMP"\n};
110 push @MYSQLDUMPARGS, "--user=$MYSQLUSER";
111 length $MYSQLPASSWORD and push @MYSQLDUMPARGS, "--password=$MYSQLPASSWORD";
112 length $MYSQLHOST and push @MYSQLDUMPARGS, "--host=$MYSQLHOST";
114 ## Open the dump file to hold the mysqldump output
115 open my $mdump, '+>', $MYSQLDUMPFILE or die qq{Could
not open "$MYSQLDUMPFILE": $!\n};
116 print qq{Writing file
"$MYSQLDUMPFILE"\n};
118 open my $mfork2, '-|' or exec $MYSQLDUMP, @MYSQLDUMPARGS, '--no-data', $MYSQLDB;
119 my $oldselect = select $mdump;
121 print while <$mfork2>;
123 ## Slurp in the current schema
128 $current_schema = <$mdump>;
133 warn qq{Trying to determine database version
...\n} if $verbose;
135 my $current_version = 0;
136 if ($current_schema =~ /CREATE TABLE \S+cur /) {
137 $current_version = 103;
139 elsif ($current_schema =~ /CREATE TABLE \S+brokenlinks /) {
140 $current_version = 104;
142 elsif ($current_schema !~ /CREATE TABLE \S+templatelinks /) {
143 $current_version = 105;
145 elsif ($current_schema !~ /CREATE TABLE \S+validate /) {
146 $current_version = 106;
148 elsif ($current_schema !~ /ipb_auto tinyint/) {
149 $current_version = 107;
151 elsif ($current_schema !~ /CREATE TABLE \S+profiling /) {
152 $current_version = 108;
154 elsif ($current_schema !~ /CREATE TABLE \S+querycachetwo /) {
155 $current_version = 109;
158 $current_version = $MW_DEFAULT_VERSION;
161 if (!$current_version) {
162 warn qq{WARNING
! Could
not figure out the old version
, assuming MediaWiki
$MW_DEFAULT_VERSION\n};
163 $current_version = $MW_DEFAULT_VERSION;
166 ## Check for a table prefix:
167 my $table_prefix = '';
168 if ($current_schema =~ /CREATE TABLE (\S+)querycache /) {
172 warn qq{Old schema is from MediaWiki version
$current_version\n} if $verbose;
173 warn qq{Table prefix is
"$table_prefix"\n} if $verbose and length $table_prefix;
175 $verbose and warn qq{Writing file
"$MYSQLDUMPFILE"\n};
176 my $now = scalar localtime;
178 $MYSQLHOST and $conninfo .= "\n-- host $MYSQLHOST";
179 $MYSQLSOCKET and $conninfo .= "\n-- socket $MYSQLSOCKET";
182 -- Dump of MySQL Mediawiki tables
for import into a Postgres Mediawiki schema
183 -- Performed by the program
: $0
184 -- Version
: $VERSION (subversion
}.q{$LastChangedRevision$}.qq{)
185 -- Author: Greg Sabino Mullane <greg\@turnstep.com> Comments welcome
187 -- This file was created: $now
188 -- Executable used: $MYSQLDUMP
189 -- Connection information:
190 -- database: $MYSQLDB
191 -- user: $MYSQLUSER$conninfo
193 -- This file can be imported manually with psql like so:
194 -- psql -p port# -h hostname -U username -f $MYSQLDUMPFILE databasename
195 -- This will overwrite any existing MediaWiki information, so be careful
199 ## psql specific stuff
203 SET client_min_messages = 'WARNING';
204 SET timezone = 'GMT';
207 warn qq{Reading in the Postgres schema information\n} if $verbose;
208 open my $schema, '<', $PG_SCHEMA
209 or die qq{Could not open "$PG_SCHEMA": make sure this script is run from maintenance/postgres/\n};
212 if (/CREATE TABLE\s+(\S+)/) {
215 $verbose > 1 and warn qq{ Found table $t\n};
217 elsif (/^ +(\w+)\s+TIMESTAMP/) {
219 $verbose > 1 and warn qq{ Got a timestamp for column $1\n};
221 elsif (/REFERENCES\s*([^( ]+)/) {
223 exists $table{$ref} or die qq{No parent table $ref found for $t\n};
227 close $schema or die qq{Could not close "$PG_SCHEMA": $!\n};
229 ## Read in special cases and table/version information
230 $verbose and warn qq{Reading in schema exception information\n};
233 if (/^VERSION\s+(\d+\.\d+):\s+(.+)/) {
234 my $list = join '|' => split /\s+/ => $2;
235 $version_tables{$1} = qr{\b$list\b};
238 next unless /^(\w+)\s*(.*)/;
239 $special{$1} = $2||'';
240 $special{$2} = $1 if length $2;
243 ## Determine the order of tables based on foreign key constraints
244 $verbose and warn qq{Figuring out order of tables to dump\n};
249 T: for my $t (sort keys %table) {
250 next if exists $dumped{$t} and $dumped{$t} >= 1;
252 for my $dep (sort keys %{$table{$t}}) {
253 next T if ! exists $dumped{$dep} or $dumped{$dep} < 0;
255 $dumped{$t} = -1 if ! exists $dumped{$t};
256 ## Skip certain tables that are not imported
257 next if exists $special{$t} and !$special{$t};
258 push @torder, $special{$t} || $t;
262 for (values %dumped) { $_+=2; }
263 die "Too many loops!\n" if $bail++ > 1000;
267 ## Prepare the Postgres database for the move
268 $verbose and warn qq{Writing Postgres transformation information\n};
270 print "\n-- Empty out all existing tables\n";
271 $verbose and warn qq{Writing truncates to empty existing tables\n};
274 for my $t (@torder, 'objectcache', 'querycache') {
276 my $tname = $special{$t}||$t;
277 printf qq{TRUNCATE TABLE %-20s\n}, qq{"$tname"};
281 print qq{-- Temporarily rename pagecontent to "text"\n};
282 print qq{ALTER TABLE pagecontent RENAME TO "text";\n\n};
284 print qq{-- Allow rc_ip to contain empty string, will convert at end\n};
285 print qq{ALTER TABLE recentchanges ALTER rc_ip TYPE text USING host(rc_ip);\n\n};
287 print "-- Changing all timestamp fields to handle raw integers\n";
288 for my $t (sort keys %tz) {
289 next if $t eq 'archive2';
290 for my $c (sort keys %{$tz{$t}}) {
291 printf "ALTER TABLE %-18s ALTER %-25s TYPE TEXT;\n", $t, $c;
297 INSERT INTO page VALUES (0,-1,'Dummy Page','',0,0,0,default,now(),0,10);
300 ## If we have a table _prefix, we need to temporarily rename all of our Postgres
301 ## tables temporarily for the import. Perhaps consider making this an auto-schema
302 ## thing in the future.
303 if (length $table_prefix) {
304 print qq{\n\n-- Temporarily renaming tables to accomodate the table_prefix
"$table_prefix"\n\n};
305 for my $t (@torder) {
307 my $tname = $special{$t}||$t;
308 printf qq{ALTER TABLE
%-18s RENAME TO
"${table_prefix}$tname"\n}, qq{"$tname"};
313 ## Try and dump the ill-named "user" table:
314 ## We do this table alone because "user" is a reserved word.
317 SET escape_string_warning TO 'off';
320 -- Postgres uses a table name of "mwuser" instead of "user"
322 -- Create a dummy user to satisfy fk contraints especially with revisions
323 SELECT setval('user_user_id_seq',0,'false');
325 VALUES (DEFAULT,'Anonymous','',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,now(),now());
329 push @MYSQLDUMPARGS, '--no-create-info';
331 $verbose and warn qq{Dumping "user" table\n};
332 $verbose > 2 and warn Dumper \@MYSQLDUMPARGS;
333 my $usertable = "${table_prefix}user";
334 open my $mfork, '-|' or exec $MYSQLDUMP, @MYSQLDUMPARGS, $MYSQLDB, $usertable;
335 ## Unfortunately, there is no easy way to catch errors
338 ++$numusers and print if s/INSERT INTO $usertable/INSERT INTO mwuser/;
342 warn qq{No users found, probably a connection error.\n};
343 print qq{ERROR: No users found, connection failed, or table "$usertable" does not exist. Dump aborted.\n};
344 close $mdump or die qq{Could not close "$MYSQLDUMPFILE": $!\n};
347 print "\n-- Users loaded: $numusers\n\n-- Loading rest of the mediawiki schema:\n";
349 warn qq{Dumping all other tables from the MySQL schema\n} if $verbose;
351 ## Dump the rest of the tables, in chunks based on constraints
352 ## We do not need the user table:
353 my @dumplist = grep { $_ ne 'user'} @torder;
358 my $tname = shift @dumplist;
359 ## XXX Make this dynamic below
360 for my $ver (sort {$b <=> $a } keys %version_tables) {
361 redo PICKATABLE if $tname =~ $version_tables{$ver};
363 $tname = "${table_prefix}$tname" if length $table_prefix;
364 next if $tname !~ /^\w/;
366 $verbose and warn " $tname...\n";
367 pop @alist and last if index($alist[-1],'---') >= 0;
371 ## Dump everything else
372 open my $mfork2, '-|' or exec $MYSQLDUMP, @MYSQLDUMPARGS, $MYSQLDB, @alist;
373 print while <$mfork2>;
375 warn qq{Finished dumping from MySQL\n} if $verbose;
380 warn qq{Writing information to return Postgres database to normal\n} if $verbose;
381 print qq{ALTER TABLE "${table_prefix}text" RENAME TO pagecontent;\n};
382 print qq{ALTER TABLE ${table_prefix}recentchanges ALTER rc_ip TYPE cidr USING\n};
383 print qq{ CASE WHEN rc_ip = '' THEN NULL ELSE rc_ip::cidr END;\n};
385 ## Return tables to their original names if a table prefix was used.
386 if (length $table_prefix) {
387 print qq{\n\n-- Renaming tables by removing table prefix "$table_prefix"\n\n};
390 $maxsize = length "$_$table_prefix" if length "$_$table_prefix" > $maxsize;
392 for my $t (@torder) {
393 next if $t eq '---' or $t eq 'text';
394 my $tname = $special{$t}||$t;
395 printf qq{ALTER TABLE %*s RENAME TO "$tname"\n}, $maxsize+1, qq{"${table_prefix}$tname"};
399 print qq{\n\n--Returning timestamps to normal\n};
400 for my $t (sort keys %tz) {
401 next if $t eq 'archive2';
402 for my $c (sort keys %{$tz{$t}}) {
403 printf "ALTER TABLE %-18s ALTER %-25s TYPE timestamptz\n".
404 " USING TO_TIMESTAMP($c,'YYYYMMDDHHMISS');\n", $t, $c;
410 SELECT setval('filearchive_fa_id_seq', 1+coalesce(max(fa_id) ,0),false) FROM filearchive;
411 SELECT setval('ipblocks_ipb_id_val', 1+coalesce(max(ipb_id) ,0),false) FROM ipblocks;
412 SELECT setval('job_job_id_seq', 1+coalesce(max(job_id) ,0),false) FROM job;
413 SELECT setval('log_log_id_seq', 1+coalesce(max(log_id) ,0),false) FROM logging;
414 SELECT setval('page_page_id_seq', 1+coalesce(max(page_id),0),false) FROM page;
415 SELECT setval('pr_id_val', 1+coalesce(max(pr_id) ,0),false) FROM page_restrictions;
416 SELECT setval('rc_rc_id_seq', 1+coalesce(max(rc_id) ,0),false) FROM recentchanges;
417 SELECT setval('rev_rev_id_val', 1+coalesce(max(rev_id) ,0),false) FROM revision;
418 SELECT setval('text_old_id_val', 1+coalesce(max(old_id) ,0),false) FROM pagecontent;
419 SELECT setval('trackbacks_tb_id_seq', 1+coalesce(max(tb_id) ,0),false) FROM trackbacks;
420 SELECT setval('user_user_id_seq', 1+coalesce(max(user_id),0),false) FROM mwuser;
423 ## Finally, make a record in the mediawiki_version table about this import
425 INSERT INTO mediawiki_version
(type
,mw_version
,notes
) VALUES
('MySQL import','??',
426 'Imported from file created on $now. Old version: $current_version');
429 print "COMMIT;\n\\o\n\n-- End of dump\n\n";
431 close $mdump or die qq{Could
not close "$MYSQLDUMPFILE": $!\n};
436 ## Known remappings: either indicate the MySQL name,
437 ## or leave blank if it should be skipped
445 ## Which tables to ignore depending on the version
446 VERSION
1.5: trackback
447 VERSION
1.6: externallinks job templatelinks transcache
448 VERSION
1.7: filearchive langlinks querycache_info
449 VERSION
1.9: querycachetwo page_restrictions redirect