/usr/share/perl5/MediaWiki/DumpFile/SQL.pm is in libmediawiki-dumpfile-perl 0.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | package MediaWiki::DumpFile::SQL;
our $VERSION = '0.2.2';
use strict;
use warnings;
use Data::Dumper;
use Carp qw(croak);
use Scalar::Util qw(reftype);
use IO::Uncompress::AnyUncompress qw($AnyUncompressError);
#public methods
sub new {
my ($class, $file) = @_;
my $self = { };
if (! defined($file)) {
croak "must specify a filename or open filehandle";
}
bless($self, $class);
$self->{buffer} = [];
$self->{file} = $file;
$self->{fh} = undef;
$self->{table_name} = undef;
$self->{schema} = undef;
$self->{type_map} = undef;
$self->{table_statement} = undef;
$self->create_type_map;
$self->open_file;
$self->parse_table;
return $self;
}
sub next {
my ($self) = @_;
my $buffer = $self->{buffer};
my $next;
while(! defined($next = shift(@$buffer))) {
if (! $self->parse_more) {
return undef;
}
}
return $next;
}
sub table_name {
my ($self) = @_;
return $self->{table_name};
}
sub table_statement {
my ($self) = @_;
return $self->{table_statement};
}
sub schema {
my ($self) = @_;
return @{$self->{schema}};
}
#private methods
sub open_file {
my ($self) = @_;
my $file = $self->{file};
my $type = reftype($file);
my $fh;
$self->{fh} = $fh = IO::Uncompress::AnyUncompress->new($file);
my $line = <$fh>;
if ($line !~ m/^-- MySQL dump/) {
die "expected MySQL dump file";
}
return;
}
sub parse_table {
my ($self) = @_;
my $fh = $self->{fh};
my $found = 0;
my $table;
my $table_statement;
my @cols;
#find the CREATE TABLE line and get the table name
while(<$fh>) {
if (m/^CREATE TABLE `([^`]+)` \(/) {
$table = $1;
$table_statement = $_;
last;
}
}
die "expected CREATE TABLE" unless defined($table);
while(<$fh>) {
$table_statement .= $_;
if (m/^\)/) {
last;
} elsif (m/^\s+`([^`]+)` (\w+)/) {
#this regex ^^^^ matches column names and types
push(@cols, [$1, $2]);
}
}
if (! scalar(@cols)) {
die "Could not find columns for $table";
}
$self->{table_name} = $table;
$self->{schema} = \@cols;
$self->{table_statement} = $table_statement;
return 1;
}
#returns false at EOF or true if more data was parsed
sub parse_more {
my ($self) = @_;
my $fh = $self->{fh};
my $insert;
if (! defined($fh)) {
return 0;
}
while(1) {
$insert = <$fh>;
if (! defined($insert)) {
close($fh) or die "could not close: $!";
$self->{fh} = undef;
return 0;
}
if ($insert =~ m/^INSERT INTO/) {
$self->parse($insert);
return 1;
}
}
}
#this parses a complete INSERT line into the individual
#components
sub parse {
my ($self, $string) = @_;
my $buffer = $self->{buffer};
my $compiled = $self->compile_config;
my $found = 0;
$_ = $string;
#check the table name
m/^INSERT INTO `(.*?)` VALUES /g or die "expected header";
if ($self->{table_name} ne $1) {
die "table name mismatch: $1";
}
while(1) {
my %new;
my $depth = 0;
#apply the various regular expressions to the
#string in order
foreach my $handler (@$compiled) {
my ($col, $cb) = @$handler;
my $ret;
$depth++;
#these callbacks also use $_
eval { $ret = &$cb };
if ($@) {
die "parse error pos:" . pos() . " depth:$depth error: $@";
}
#column names starting with # are part of the parser, not user data
if ($col !~ m/^#/) {
$new{$col} = $ret;
}
}
push(@$buffer, \%new);
$found++;
if (m/\G, ?/gc) {
#^^^^ match the delimiter between rows
next;
} elsif (m/\G;$/gc) {
#^^^^ match end of statement
last;
} else {
die "expected delimter or end of statement. pos:" . pos;
}
}
return $found;
}
#functions for the parsing engine
#maps between MySQL types and our types
sub create_type_map {
my ($self) = @_;
$self->{type_map} = {
int => 'int',
tinyint => 'int',
bigint => 'int',
char => 'varchar',
varchar => 'varchar',
enum => 'varchar',
double => 'float',
timestamp => 'int',
blob => 'varchar',
mediumblob => 'varchar',
mediumtext => 'varchar',
tinyblob => 'varchar',
varbinary => 'varchar',
};
return 1;
}
#convert the schema into a list of callbacks
#that match the schema and extract data from it
sub compile_config {
my ($self) = @_;
my $schema = $self->{schema};
my @handlers;
push(@handlers, ['#start', new_start_data()]);
foreach (@$schema) {
my ($name, $type) = @$_;
my $oldtype = $type;
$type = $self->{type_map}->{lc($type)};
if (! defined($type)) {
die "type map failed for $oldtype";
}
if ($type eq 'int') {
push(@handlers, [$name, new_int()], ['#delim', new_delim()]);
} elsif ($type eq 'varchar') {
push(@handlers, [$name, new_varchar()], ['#delim', new_delim()]);
} elsif($type eq 'float') {
push(@handlers, [$name, new_float()], ['#delim', new_delim()]);
} else {
die "unknown type: $type";
}
}
pop(@handlers); #gets rid of that extra delimiter
push(@handlers, ['#end', new_end_data()]);
return \@handlers;
}
sub unescape {
my ($input) = @_;
if ($input eq '\\\\') {
return '\\';
} elsif ($input eq "\\'") {
return("'");
} elsif ($input eq '\\"') {
return '"';
} elsif ($input eq '\\n') {
return "\n";
} elsif ($input eq '\\t') {
return "\t";
} else {
die "can not unescape $input";
}
}
#functions that create callbacks that match and extract
#data from INSERT lines
#it is critical that these regular expressions use the /gc option
#or the parser will stop functioning as soon as a regex with
#out those options is encountered and debugging becomes
#almost impossible
sub new_int {
return sub {
m/\GNULL/gc and return undef;
m/\G(-?[\d]+)/gc or die "expected int"; return $1;
};
}
sub new_float {
return sub {
m/\GNULL/gc and return undef;
m/\G(-?[\d]+(?:\.[\d]+(e-?[\d]+)?)?)/gc or die "expected float"; return $1;
}
}
sub new_varchar {
return sub {
my $data;
m/\GNULL/gc and return undef;
#does not handle very long strings; crashes perl 5.8.9 causes 5.10.1 to error out
#m/\G'((\\.|[^'])*)'/gc or die "expected varchar";
#thanks somni!
m/'((?:[^\\']*(?:\\.[^\\']*)*))'/gc or die "expected varchar";
$data = $1;
$data =~ s/(\\.)/unescape($1)/e;
return $data;
}
}
sub new_delim {
return sub {
m/\G, ?/gc or die "expected delimiter"; return undef;
};
}
sub new_start_data {
return sub {
m/\G\(/gc or die "expected start of data set"; return undef;
};
}
sub new_end_data {
return sub {
m/\G\)/gc or die "expected end of data set"; return undef;
}
}
1;
__END__
=head1 NAME
MediaWiki::DumpFile::SQL - Process SQL dump files from a MediaWiki instance
=head1 SYNOPSIS
use MediaWiki::DumpFile::SQL;
$sql = MediaWiki::DumpFile::SQL->new('dumpfile.sql');
#many compression formats are supported
$sql = MediaWiki::DumpFile::SQL->new('dumpfile.sql.gz');
$sql = MediaWiki::DumpFile::SQL->new('dumpfile.sql.bz2');
$sql = MediaWiki::DumpFile::SQL->new(\*FH);
@schema = $sql->schema;
$name = $sql->table_name;
while(defined($row = $sql->next)) {
#do something with the data from the row
}
=head1 FUNCTIONS
=head2 new
This is the constructor for this package. It is called with a single parameter: the location of
a MediaWiki SQL dump file or a reference to an already open file handle.
Only the definition and data for the B<first> table in a SQL dump file is processed.
=head2 next
Returns a hash reference where the key is the column name from the table and the value of the hash is
the value for that column and row. Returns undef when there is no more data available.
=head2 table_name
Returns a string of the table name that was discovered in the dump file.
=head2 table_statement
Returns a string of the unparsed CREATE TABLE statement straight from the
dump file.
=head2 schema
Returns a list that represents the table schema as it was parsed by this module. Each item in the list
is a reference to an array with two elements. The first element is the name of the row and the second
element is the MySQL datatype associated with the row. The list is in an identical order as the definitions
in the CREATE TABLE statement.
=head1 AUTHOR
Tyler Riddle, C<< <triddle at gmail.com> >>
=head1 BUGS
Please see MediaWiki::DumpFile for information on how to report bugs in
this software.
=head1 COPYRIGHT & LICENSE
Copyright 2009 "Tyler Riddle".
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
|