GBrowse
view release on metacpan or search on metacpan
bin/gbrowse_metadb_config.pl view on Meta::CPAN
return;
}
return $userid;
}
# Check Files (Uploads ID, Data Source) - Makes sure a user's files are in the database, add them if not.
sub check_files {
my $userid = shift or die "No user ID given, please check the gbrowse_metadb_config.pl script.\n";
my $uploadsid = shift or die "No uploads ID given, please check the gbrowse_metadb_config.pl script.\n";
my $data_source = shift or die "No data source given, please check the gbrowse_metadb_config.pl script.\n";
# Get the files from the database.
my $files_in_db = $database->selectcol_arrayref("SELECT path FROM uploads WHERE userid=? AND data_source=?",
undef, $userid, $data_source);
my @files_in_db = @$files_in_db;
# Get the files in the folder.
my $path = $globals->user_dir($data_source, $uploadsid);
my @files_in_folder;
opendir D, $path;
while (my $dir = readdir(D)) {
next if $dir =~ /^\.+$/;
push @files_in_folder, $dir;
}
closedir(D);
my $all_ok = 1;
foreach my $file (@files_in_folder) {
my $found = grep(/$file/, @files_in_db);
unless ($found) {
print STDERR "\n" unless $line_counter++;
add_file($file, $userid, $uploadsid, $data_source, $file) &&
print STDERR "- File \"$file\" found in the \"$data_source/$uploadsid\" folder without metadata, added to database.\n";
$all_ok = 0;
}
}
return $all_ok;
}
# Fix Permissions () - Grants the web user the required privileges on all databases.
sub fix_permissions {
my (undef, $db_name) = $dsn =~ /.*:(database=)?([^;]+)/;
$db_name ||= "gbrowse_login";
($type) = $dsn =~ /^dbi:([^:]+)/i unless defined $type;
if ($type =~ /mysql/i) {
my ($db_user) = $dsn =~ /user=([^;]+)/i;
my ($db_pass) = $dsn =~ /password=([^;]+)/i;
$db_pass ||= '';
warn "GRANT ALL PRIVILEGES on $db_name.* TO '$db_user'\@'%' IDENTIFIED BY '$db_pass'";
$database->do("GRANT ALL PRIVILEGES on $db_name.* TO '$db_user'\@'%' IDENTIFIED BY '$db_pass'")
or die DBI->errstr;
}
}
sub fix_session_permissions {
print STDERR "Fixing permissions in sessions directory\n";
my $globals = Bio::Graphics::Browser2->open_globals;
my $session_dir = $globals->session_dir;
my $webuser = GBrowse::ConfigData->config('wwwuser');
system "sudo chown -R $webuser $session_dir";
}
sub fix_sqlite_permissions {
my (undef, $db_name) = $dsn =~ /.*:(database=)?([^;]+)/;
$db_name ||= "gbrowse_login";
my ($path) = $dsn =~ /dbname=([^;]+)/i;
($path) = $dsn =~ /DBI:SQLite:([^;]+)/i unless $path;
die "Couldn't figure out location of database index from $dsn" unless $path;
my $user = GBrowse::ConfigData->config('wwwuser');
my $group = get_group_from_user($user);
my $dir = dirname($path);
unless (-e $dir) {
my $parent = dirname($dir);
if (-w $parent) {
mkdir $parent;
} else {
print STDERR "Using sudo to create $parent directory. You may be prompted for your login password now.\n";
system "sudo mkdir $parent";
}
}
my $file_owner = -e $path ? getpwuid((stat($path))[4]) : '';
my $dir_owner = -e $dir ? getpwuid((stat($dir))[4]) : '';
# Check if we need to, to avoid unnecessary printing/sudos.
unless ($group) {
print STDERR "Unable to look up group for $user. Will not change ownerships on $path.\n";
print STDERR "You should do this manually to give the Apache web server read/write access to $path.\n";
return;
}
if (-e $path && $user ne $file_owner) {
print STDERR "Using sudo to set $path ownership to $user:$group. You may be prompted for your login password now.\n";
system "sudo chown $user $path" ;
system "sudo chgrp $group $path";
system "sudo chmod 0644 $path";
}
if (-e $dir && $user ne $dir_owner) {
print STDERR "Using sudo to set $dir ownership to $user:$group. You may be prompted for your login password now.\n";
system "sudo chown $user $dir";
system "sudo chgrp $group $dir";
system "sudo chmod 0755 $dir";
}
}
# Create Database() - Creates the database specified (or the default gbrowse_login database).
sub create_mysql_database {
my (undef, $db_name) = $dsn =~ /.*:(database=)?([^;]+)/;
$db_name ||= "gbrowse_login";
unless (DBI->connect($dsn)) {
if ($dsn =~ /mysql/i) {
print STDERR "Could not log into $db_name database, creating and/or fixing login permissions...\n";
my ($admin_user, $admin_pass);
if ($admin) {
($admin_user) = $admin =~ /^(.*):/;
($admin_pass) = $admin =~ /:(.*)$/;
}
$admin_user ||= prompt("Please enter the MySQL administrator user", "root");
$admin_pass ||= prompt("Please enter the MySQL administrator password", "",1);
my ($db_user) = $dsn =~ /user=([^;]+)/i;
my ($db_pass) = $dsn =~ /password=([^;]+)/i;
$db_pass ||= '';
my $test_dbi = DBI->connect("DBI:mysql:database=mysql;user=$admin_user;password=$admin_pass;");
$test_dbi->do("CREATE DATABASE IF NOT EXISTS $db_name");
$test_dbi->do("GRANT ALL PRIVILEGES on $db_name.* TO '$db_user'\@'%' IDENTIFIED BY '$db_pass'");
print STDERR "Database created!\n" unless DBI->errstr;
}
}
# SQLite will create the file/database upon first connection.
}
# Add File (Full Path, Owner's Uploads ID, Data Source) - Adds $file to the database under a specified owner.
# Database.pm's add_file() is dependant too many outside variables, not enough time to re-structure.
sub add_file {
my $filename = shift;
my $userid = shift;
my $uploadsid = shift;
my $data_source = shift;
my $full_path = shift;
my $imported = ($filename =~ /^(ftp|http|das)_/)? 1 : 0;
my $description = "";
my $shared = "private";
my $trackid = md5_hex($uploadsid.$filename.$data_source);
my $now = nowfun();
$database->do("INSERT INTO uploads (trackid, userid, path, description, imported, creation_date, modification_date, sharing_policy, data_source) VALUES (?, ?, ?, ?, ?, $now, $now, ?, ?)", undef, $trackid, $userid, $filename, $description, $import...
return $trackid;
}
# Now Function - return the database-dependent function for determining current date & time
sub nowfun {
return ($type =~ /sqlite/i)? "datetime('now','localtime')" : 'NOW()';
}
# Escape Enums (type string) - If the string contains an ENUM, returns a compatible data type that works with SQLite.
( run in 0.580 second using v1.01-cache-2.11-cpan-800906f7e73 )