/usr/share/perl5/Wiki/Toolkit/Setup/Database.pm is in libwiki-toolkit-perl 0.85-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 | package Wiki::Toolkit::Setup::Database;
use strict;
use vars qw( $VERSION @SUPPORTED_SCHEMAS);
$VERSION = 0.09;
@SUPPORTED_SCHEMAS = qw(8 9 10);
=head1 NAME
Wiki::Toolkit::Setup::Database - parent class for database storage setup
classes for Wiki::Toolkit
=cut
sub fetch_upgrade_old_to_8 {
# Compatible with old_to_10
fetch_upgrade_old_to_10(@_);
}
sub fetch_upgrade_old_to_9 {
# Compatible with old_to_10
fetch_upgrade_old_to_10(@_);
}
# Fetch from the old style database, ready for an upgrade to db version 10
sub fetch_upgrade_old_to_10 {
my $dbh = shift;
my %nodes;
my %metadatas;
my %contents;
my @internal_links;
my %ids;
print "Grabbing and upgrading old data... ";
# Grab all the nodes, and give them an ID
my $sth = $dbh->prepare("SELECT name,version,text,modified FROM node");
$sth->execute;
my $id = 0;
while( my($name,$version,$text,$modified) = $sth->fetchrow_array) {
my %node;
$id++;
$node{'name'} = $name;
$node{'version'} = $version;
$node{'text'} = $text;
$node{'modified'} = $modified;
$node{'id'} = $id;
$node{'moderate'} = 0;
$nodes{$name} = \%node;
$ids{$name} = $id;
}
print " read $id nodes... ";
# Grab all the content, and upgrade to ID from name
$sth = $dbh->prepare("SELECT name,version,text,modified,comment FROM content");
$sth->execute;
while ( my($name,$version,$text,$modified,$comment) = $sth->fetchrow_array) {
my $id = $ids{$name};
if($id) {
my %content;
$content{'node_id'} = $id;
$content{'version'} = $version;
$content{'text'} = $text;
$content{'modified'} = $modified;
$content{'comment'} = $comment;
$content{'moderated'} = 1;
$contents{$id."-".$version} = \%content;
} else {
warn("There was no node entry for content with name '$name', unable to migrate it!");
}
}
print " read ".(scalar keys %contents)." contents... ";
# Grab all the metadata, and upgrade to ID from node
$sth = $dbh->prepare("SELECT node,version,metadata_type,metadata_value FROM metadata");
$sth->execute;
my $i = 0;
while( my($node,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) {
my $id = $ids{$node};
if($id) {
my %metadata;
$metadata{'node_id'} = $id;
$metadata{'version'} = $version;
$metadata{'metadata_type'} = $metadata_type;
$metadata{'metadata_value'} = $metadata_value;
$metadatas{$id."-".($i++)} = \%metadata;
} else {
warn("There was no node entry for metadata with name (node) '$node', unable to migrate it!");
}
}
# Grab all the internal links
$sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links");
$sth->execute;
while( my($link_from,$link_to) = $sth->fetchrow_array) {
my %il;
$il{'link_from'} = $link_from;
$il{'link_to'} = $link_to;
push @internal_links, \%il;
}
print "done\n";
# Return it all
return (\%nodes,\%contents,\%metadatas,\@internal_links,\%ids);
}
sub fetch_upgrade_8_to_9 {
# Compatible with 8_to_10
fetch_upgrade_8_to_10(@_);
}
# Fetch from schema version 8, and upgrade to version 10
sub fetch_upgrade_8_to_10 {
my $dbh = shift;
my %nodes;
my %metadatas;
my %contents;
my @internal_links;
print "Grabbing and upgrading old data... ";
# Grab all the nodes
my $sth = $dbh->prepare("SELECT id,name,version,text,modified FROM node");
$sth->execute;
while( my($id,$name,$version,$text,$modified) = $sth->fetchrow_array) {
my %node;
$node{'name'} = $name;
$node{'version'} = $version;
$node{'text'} = $text;
$node{'modified'} = $modified;
$node{'id'} = $id;
$node{'moderate'} = 0;
$nodes{$name} = \%node;
}
# Grab all the content
$sth = $dbh->prepare("SELECT node_id,version,text,modified,comment FROM content");
$sth->execute;
while ( my($node_id,$version,$text,$modified,$comment) = $sth->fetchrow_array) {
my %content;
$content{'node_id'} = $node_id;
$content{'version'} = $version;
$content{'text'} = $text;
$content{'modified'} = $modified;
$content{'comment'} = $comment;
$content{'moderated'} = 1;
$contents{$node_id."-".$version} = \%content;
}
# Grab all the metadata
$sth = $dbh->prepare("SELECT node_id,version,metadata_type,metadata_value FROM metadata");
$sth->execute;
my $i = 0;
while( my($node_id,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) {
my %metadata;
$metadata{'node_id'} = $node_id;
$metadata{'version'} = $version;
$metadata{'metadata_type'} = $metadata_type;
$metadata{'metadata_value'} = $metadata_value;
$metadatas{$node_id."-".($i++)} = \%metadata;
}
# Grab all the internal links
$sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links");
$sth->execute;
while( my($link_from,$link_to) = $sth->fetchrow_array) {
my %il;
$il{'link_from'} = $link_from;
$il{'link_to'} = $link_to;
push @internal_links, \%il;
}
print "done\n";
# Return it all
return (\%nodes,\%contents,\%metadatas,\@internal_links);
}
# Fetch from schema version 9, and upgrade to version 10
sub fetch_upgrade_9_to_10 {
my $dbh = shift;
my %nodes;
my %metadatas;
my %contents;
my @internal_links;
print "Grabbing and upgrading old data... ";
# Grab all the nodes
my $sth = $dbh->prepare("SELECT id,name,version,text,modified,moderate FROM node");
$sth->execute;
while( my($id,$name,$version,$text,$modified,$moderate) = $sth->fetchrow_array) {
my %node;
$node{'name'} = $name;
$node{'version'} = $version;
$node{'text'} = $text;
$node{'modified'} = $modified;
$node{'id'} = $id;
$node{'moderate'} = $moderate;
$nodes{$name} = \%node;
}
# Grab all the content
$sth = $dbh->prepare("SELECT node_id,version,text,modified,comment,moderated FROM content");
$sth->execute;
while ( my($node_id,$version,$text,$modified,$comment,$moderated) = $sth->fetchrow_array) {
my %content;
$content{'node_id'} = $node_id;
$content{'version'} = $version;
$content{'text'} = $text;
$content{'modified'} = $modified;
$content{'comment'} = $comment;
$content{'moderated'} = $moderated;
$contents{$node_id."-".$version} = \%content;
}
# Grab all the metadata
$sth = $dbh->prepare("SELECT node_id,version,metadata_type,metadata_value FROM metadata");
$sth->execute;
my $i = 0;
while( my($node_id,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) {
my %metadata;
$metadata{'node_id'} = $node_id;
$metadata{'version'} = $version;
$metadata{'metadata_type'} = $metadata_type;
$metadata{'metadata_value'} = $metadata_value;
$metadatas{$node_id."-".($i++)} = \%metadata;
}
# Grab all the internal links
$sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links");
$sth->execute;
while( my($link_from,$link_to) = $sth->fetchrow_array) {
my %il;
$il{'link_from'} = $link_from;
$il{'link_to'} = $link_to;
push @internal_links, \%il;
}
print "done\n";
# Return it all
return (\%nodes,\%contents,\%metadatas,\@internal_links);
}
# Get the version of the database schema
sub get_database_version {
my $dbh = shift;
my $sql = "SELECT version FROM schema_info";
my $sth;
eval{ $sth = $dbh->prepare($sql) };
if($@) { return "old"; }
eval{ $sth->execute };
if($@) { return "old"; }
my ($cur_schema) = $sth->fetchrow_array;
unless($cur_schema) { return "old"; }
return $cur_schema;
}
# Is an upgrade to the database required?
sub get_database_upgrade_required {
my ($dbh,$new_version) = @_;
# Get the schema version
my $schema_version = get_database_version($dbh);
# Compare it
if($schema_version eq $new_version) {
# At latest version
return undef;
} elsif ($schema_version eq 'old' or $schema_version < $new_version) {
return $schema_version."_to_".$new_version;
} else {
die "Aiee! We seem to be trying to downgrade the database schema from $schema_version to $new_version. Aborting.\n";
}
}
# Put the latest data into the latest database structure
sub bulk_data_insert {
my ($dbh, $nodesref, $contentsref, $metadataref, $internallinksref) = @_;
print "Bulk inserting upgraded data... ";
# Add nodes
my $sth = $dbh->prepare("INSERT INTO node (id,name,version,text,modified,moderate) VALUES (?,?,?,?,?,?)");
foreach my $name (keys %$nodesref) {
my %node = %{$nodesref->{$name}};
$sth->execute($node{'id'},
$node{'name'},
$node{'version'},
$node{'text'},
$node{'modified'},
$node{'moderate'});
}
print "added ".(scalar keys %$nodesref)." nodes... ";
# Add content
$sth = $dbh->prepare("INSERT INTO content (node_id,version,text,modified,comment,moderated) VALUES (?,?,?,?,?,?)");
foreach my $key (keys %$contentsref) {
my %content = %{$contentsref->{$key}};
$sth->execute($content{'node_id'},
$content{'version'},
$content{'text'},
$content{'modified'},
$content{'comment'},
$content{'moderated'});
}
# Add metadata
$sth = $dbh->prepare("INSERT INTO metadata (node_id,version,metadata_type,metadata_value) VALUES (?,?,?,?)");
foreach my $key (keys %$metadataref) {
my %metadata = %{$metadataref->{$key}};
$sth->execute($metadata{'node_id'},
$metadata{'version'},
$metadata{'metadata_type'},
$metadata{'metadata_value'});
}
# Add internal links
$sth = $dbh->prepare("INSERT INTO internal_links (link_from,link_to) VALUES (?,?)");
foreach my $ilr (@$internallinksref) {
my %il = %{$ilr};
$sth->execute($il{'link_from'},
$il{'link_to'});
}
print "done\n";
}
sub perm_check {
my $dbh = shift;
# If we can do all this, we'll be able to do a bulk upgrade too
eval {
my $sth = $dbh->prepare("CREATE TABLE dbtest (test int)");
$sth->execute;
$sth = $dbh->prepare("CREATE INDEX dbtest_index ON dbtest (test)");
$sth->execute;
$sth = $dbh->prepare("DROP TABLE dbtest");
$sth->execute;
};
return $@;
}
|