diff options
Diffstat (limited to 'bin/archive')
-rwxr-xr-x | bin/archive/export_playlist.pl | 80 | ||||
-rwxr-xr-x | bin/archive/import_playlist.pl | 118 | ||||
-rwxr-xr-x | bin/archive/migrate_user.pl | 56 |
3 files changed, 254 insertions, 0 deletions
diff --git a/bin/archive/export_playlist.pl b/bin/archive/export_playlist.pl new file mode 100755 index 00000000..c6e0ccaf --- /dev/null +++ b/bin/archive/export_playlist.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl -w +# +# Exports playlists from ampache +# +# Fill in the site specific database connection parameters below before running +# + +use DBI; + +# Configure database connection parameters +my $db = "ampache"; # database +my $user = ""; # database user +my $pw = ""; # database user password + + +if ($#ARGV < 0) { + print "Usage: $0 <filename>\n"; + print " Exports Ampache playlists to <filename>.\n"; + exit; +} + + +open(OUT, "> $ARGV[0]") or die("Could not open '$ARGV[0]' for write - $!"); + +# Build DSNs +my $dsn = "dbi:mysql:database=$db;"; + +# Connect to database +my $dbh= DBI->connect($dsn, $user, $pw, + { RaiseError => 1, AutoCommit => 0 }); + + +# Prepare statements +my $sth = $dbh->prepare("SELECT id, name, owner, type FROM playlist"); +my $sth2 = $dbh->prepare("SELECT username FROM user + WHERE id = ?"); +my $sth3 = $dbh->prepare("SELECT song.file + FROM playlist_data, song + WHERE playlist_data.playlist = ? + AND playlist_data.song = song.id"); + +# Execute select and loop through results +$sth->execute(); +my $count = 0; +my ($id,$name,$owner,$type,$date,$file,$track); +while(($id,$name,$owner,$type) = $sth->fetchrow_array) { + if ($count > 0) { + # Use a blank line as a separator between playlists + print OUT "\n"; + } + + $count++; + + if ($owner =~ /^\d+$/) { + # Fetch username instead of id for owner + $sth2->execute($owner); + $owner = "unknown" unless (($owner) = $sth2->fetchrow_array); + $sth2->finish; + } + + # Date is not present in old ampache's + $date = 0 if (! defined($date)); + + print OUT "Name: $name\n"; + print OUT "Owner: $owner\n"; + print OUT "Type: $type\n"; + + # Grab songs for this playlist + $sth3->execute($id); + while(($file) = $sth3->fetchrow_array) { + print OUT "File: $file\n"; + } +} + +print "Exported $count playlists.\n"; + +# Clean up +$dbh->disconnect; +close(OUT); + diff --git a/bin/archive/import_playlist.pl b/bin/archive/import_playlist.pl new file mode 100755 index 00000000..4ce0e7b5 --- /dev/null +++ b/bin/archive/import_playlist.pl @@ -0,0 +1,118 @@ +#!/usr/bin/perl -w +# +# Imports playlists into ampache (from export_playlist.pl output) +# +# Fill in the site specific database connection parameters below before running +# + +use DBI; +use Data::Dumper; + +# Configure database connection parameters +my $db = "ampache3_1"; # database +my $user = ""; # database user +my $pw = ""; # database user password + + +if ($#ARGV < 0) { + print "Usage: $0 <filename>\n"; + print " Imports Ampache playlists from <filename>.\n"; + print " The format of <filename> should match the output of export_playlist.pl.\n"; + exit; +} + + +open(IN, "$ARGV[0]") or die("Could not open '$ARGV[0]' for read - $!"); + +# Build DSNs +my $dsn = "dbi:mysql:database=$db;"; + +# Connect to database +my $dbh = DBI->connect($dsn, $user, $pw, + { RaiseError => 1, AutoCommit => 0 }); + + +# Structure to contain playlists +my @playlists; + +# Parse file +my $i = 0; +while($line = <IN>) { + chomp $line; + + if ($line eq "") { + # Blank line means new playlist + $i++; + next; + } + + if ($line =~ /^ID: (.*)$/) { + $playlists[$i]->{id} = $1; + } + + if ($line =~ /^Name: (.*)$/) { + $playlists[$i]->{name} = $1; + } + + if ($line =~ /^Owner: (.*)$/) { + $playlists[$i]->{owner} = $1; + } + + if ($line =~ /^Type: (.*)$/) { + $playlists[$i]->{type} = $1; + } + + if ($line =~ /^File: (.*)$/) { + push @{$playlists[$i]->{files}}, $1; + } +} +close(IN); + +# Prepare statements +my $sth = $dbh->prepare("SELECT id FROM user + WHERE username = ?"); +my $sth2 = $dbh->prepare("INSERT INTO playlist + (name, owner, type) + values (?, ?, ?)"); +my $sth3 = $dbh->prepare("SELECT id FROM song + WHERE file = ?"); +my $sth4 = $dbh->prepare("INSERT INTO playlist_data + (playlist, song, track) + values (?, ?, ?)"); + +# Insert records into Ampache +my ($id,$name,$owner,$type,$file,$songid); +my $count = 0; +for ($i = 0; $i < $#playlists + 1; $i++) { + $count++; + + $name = $playlists[$i]->{name}; + + $sth->execute($playlists[$i]->{owner}); + $owner = 0 unless (($owner) = $sth->fetchrow_array); + $sth->finish; + + $type = $playlists[$i]->{type}; + + print "Importing playlist '$name'...\n"; + + # Create base playlist entry + $sth2->execute($name, $owner, $type); + $id = $dbh->{mysql_insertid}; + + # And add files to it + while($file = pop(@{$playlists[$i]->{files}})) { + $sth3->execute($file); + next unless (($songid) = $sth3->fetchrow_array); + $sth3->finish; + + $sth4->execute($id,$songid,0); + } +} + +print "Imported $count playlists.\n"; + +# Clean up +$dbh->disconnect; +close(IN); + diff --git a/bin/archive/migrate_user.pl b/bin/archive/migrate_user.pl new file mode 100755 index 00000000..cf36c868 --- /dev/null +++ b/bin/archive/migrate_user.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl -w +# +# Migrates users from Ampache v3.0 to Ampache v3.1. +# +# Fill in the site specific database connection parameters below before running +# + +use DBI; + +# Configure database connection parameters +my $db_old = "ampache"; # old database +my $db_new = "ampache3_1"; # new database +my $user_old = ""; # old database user +my $user_new = ""; # new database user +my $pw_old = "!"; # old database user password +my $pw_new = "!"; # new database user password + + +# Build DSNs +my $dsn_new = "dbi:mysql:database=$db_new;"; +my $dsn_old = "dbi:mysql:database=$db_old;"; + + +# Connect to old and new databases +my $dbh_new = DBI->connect($dsn_new, $user_new, $pw_new, + { RaiseError => 1, AutoCommit => 0 }); + +my $dbh_old = DBI->connect($dsn_old, $user_old, $pw_old, + { RaiseError => 1, AutoCommit => 0 }); + + +# Prepare select and insert statements +my $sth = $dbh_old->prepare("SELECT username, fullname, email, password, access + FROM user"); + +my $sth_update = $dbh_new->prepare("INSERT INTO user + (username, fullname, email, password, access, offset_limit) + VALUES (?, ?, ?, ?, ?, 50)"); + + +# Execute select and loop through results +$sth->execute(); +my ($f1,$f2,$f3,$f4,$f5); +my $count = 0; +while(($f1,$f2,$f3,$f4,$f5) = $sth->fetchrow_array) { + $sth_update->execute($f1,$f2,$f3,$f4,$f5); + $count++; +} + +print "Migrated $count users.\n"; + + +# Clean up +$dbh_old->disconnect; +$dbh_new->disconnect; + |