--- loncom/debugging_tools/move_construction_spaces.pl 2011/10/27 03:31:50 1.3 +++ loncom/debugging_tools/move_construction_spaces.pl 2012/06/08 13:04:08 1.8 @@ -5,7 +5,7 @@ # Move Construction Spaces from /home/$user/public_html # to /home/httpd/html/priv/$domain/$user and vice versa # -# $Id: move_construction_spaces.pl,v 1.3 2011/10/27 03:31:50 raeburn Exp $ +# $Id: move_construction_spaces.pl,v 1.8 2012/06/08 13:04:08 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -38,6 +38,20 @@ use LONCAPA qw(:DEFAULT :match); use Apache::lonlocal; use File::Copy; use GDBM_File; +use DBI; + +my ($lonusersdir,$londocroot,$londaemons,$lonsqlaccess); + +BEGIN { + my $perlvar=&LONCAPA::Configuration::read_conf(); + if (ref($perlvar) eq 'HASH') { + $lonusersdir = $perlvar->{'lonUsersDir'}; + $londocroot = $perlvar->{'lonDocRoot'}; + $londaemons = $perlvar->{'lonDaemons'}; + $lonsqlaccess = $perlvar->{'lonSqlAccess'}; + } + undef($perlvar); +} my $lang = &Apache::lonlocal::choose_language(); &Apache::lonlocal::get_language_handle(undef,$lang); @@ -48,27 +62,30 @@ if ($< != 0) { exit; } -my $perlvar=&LONCAPA::Configuration::read_conf(); -my ($lonuserdir,$londocroot,$londaemons); -if (ref($perlvar) eq 'HASH') { - $lonuserdir = $perlvar->{'lonUsersDir'}; - $londocroot = $perlvar->{'lonDocRoot'}; - $londaemons = $perlvar->{'lonDaemons'}; +if ($lonusersdir eq '') { + print &mt('Could not determine location of [_1] directory.',"'lonUsersDir'")."\n". + &mt('Stopping')."\n"; + exit; +} + +if ($londocroot eq '') { + print &mt('Could not determine location of [_1] directory.',"'lonDocRoot'")."\n". + &mt('Stopping')."\n"; + exit; } -undef($perlvar); my $distro; -if ($londaemons ne '') { +if ($londaemons eq '') { + print &mt('Could not determine location of [_1] directory.',"'lonDaemons'")."\n". + &mt('Stopping')."\n"; + exit; +} else { if (-e "$londaemons/distprobe") { if (open(PIPE,"perl $londaemons/distprobe|")) { $distro = ; close(PIPE); } } -} else { - print &mt('Could not determine location of [_1] directory.',"'lonDaemons'")."\n". - &mt('Stopping')."\n"; - exit; } if ($distro eq '') { @@ -149,14 +166,6 @@ print "\n".&mt("Moving authors' Construc "'/home'","'$londocroot/priv/'")."\n\n". &mt('If there are ambiguities (i.e., the same username belongs to two domains), this will be flagged, and you will be able to decide how to proceed.')."\n"; -my $perlvar=&LONCAPA::Configuration::read_conf(); -my ($lonuserdir,$londocroot); -if (ref($perlvar) eq 'HASH') { - $lonuserdir = $perlvar->{'lonUsersDir'}; - $londocroot = $perlvar->{'lonDocRoot'}; -} -undef($perlvar); - my (undef,undef,$uid,$gid) = getpwnam('www'); my ($action) = ($parameter=~/^(move|undo)$/); if ($action eq '') { @@ -164,7 +173,7 @@ if ($action eq '') { } if ($action eq 'dryrun') { - print "\n". + print "\n\n". &mt('Running in exploratory mode ...')."\n\n". &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]', "'move'","'$londocroot/priv'","\n\nperl move_construction_spaces.pl move")."\n\n\n". @@ -243,18 +252,19 @@ if ($action eq 'move') { } my @machinedoms; -if ($lonuserdir) { +if ($lonusersdir) { my ($dir,$output); - if (opendir($dir,$lonuserdir)) { + if (opendir($dir,$lonusersdir)) { my @contents = (grep(!/^\.{1,2}$/,readdir($dir))); + closedir($dir); foreach my $item (@contents) { - if (-d "$lonuserdir/$item") { + if (-d "$lonusersdir/$item") { if ($item =~ /^$match_domain$/) { my $domain = $item; unless (grep(/^\Q$domain\E$/,@machinedoms)) { push(@machinedoms,$domain); } - my $dom_target="/home/httpd/html/priv/$domain"; + my $dom_target="$londocroot/priv/$domain"; if ($action eq 'move') { if (!-e $dom_target) { if (mkdir($dom_target,0755)) { @@ -275,7 +285,7 @@ if ($lonuserdir) { } } my %authors=(); - my $fname = "$lonuserdir/$domain/nohist_domainroles.db"; + my $fname = "$lonusersdir/$domain/nohist_domainroles.db"; my $dbref; if (-e $fname) { $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER()); @@ -294,9 +304,8 @@ if ($lonuserdir) { } } } - closedir($dir); } else { - $output = &mt('Could not open [_1].',"'$lonuserdir'")."\n"; + $output = &mt('Could not open [_1].',"'$lonusersdir'")."\n"; print $output; &stop_logging($logfh,$output); print &mt('Stopping')."\n"; @@ -309,10 +318,12 @@ if ($londocroot ne '') { my ($dir,$domdir); if (opendir($dir,"$londocroot/res")) { my @contents = (grep(!/^\.{1,2}$/,readdir($dir))); + closedir($dir); foreach my $dom (@contents) { if ((grep(/^\Q$dom\E/,@machinedoms)) && (-d "$londocroot/res/$dom")) { if (opendir($domdir,"$londocroot/res/$dom")) { my @unames = (grep(!/^\.{1,2}$/,readdir($domdir))); + closedir($domdir); foreach my $uname (@unames) { if ($uname =~ /^$match_username$/) { push(@{$pubusers{$uname}},$dom); @@ -332,10 +343,12 @@ if ($action eq 'undo') { my ($dir,$domdir); if (opendir($dir,"$londocroot/priv")) { my @contents = (grep(!/^\.{1,2}/,readdir($dir))); + closedir($dir); foreach my $dom (@contents) { next if (!-d "$londocroot/priv/$dom"); if (opendir($domdir,"$londocroot/priv/$dom")) { my @unames = (grep(!/^\.{1,2}$/,readdir($domdir))); + closedir($domdir); foreach my $uname (@unames) { if ($uname =~ /^$match_username$/) { push(@{$privspaces{$uname}},$dom); @@ -348,6 +361,7 @@ if ($action eq 'undo') { } foreach my $uname (keys(%privspaces)) { if (ref($privspaces{$uname}) eq 'ARRAY') { + my $output; if (@{$privspaces{$uname}} > 1) { my $displaydoms = join(', ',@{$privspaces{$uname}}); print &mt('Same username used for authors in multiple domains.')."\n". @@ -358,66 +372,208 @@ if ($action eq 'undo') { my $choice=; chomp($choice); if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) { - my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice); - print $output; - print $logfh $output; + $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice); } else { print &mt('Invalid choice of domain:')." $choice\n"; - my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; + $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; print $output; print $logfh $output; next; } } elsif (@{$privspaces{$uname}} == 1) { - my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]); - print $output; - print $logfh $output; + $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]); } else { print &mt('Username [_1] found in [_2] was not within a domain', "'$uname'","'$londocroot/priv'")."\n"; - my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; - print $output; - print $logfh $output; + $output = &mt('Skipping this user: [_1].',"'$uname'")."\n"; } + print $output; + print $logfh $output; } } + if (-d "$londocroot/priv") { + my $output; + if (opendir(my $dir,"$londocroot/priv")) { + my @doms = grep(!/^\.{1,2}/,readdir($dir)); + closedir($dir); + foreach my $dom (@doms) { + if (opendir(my $domdir,"$londocroot/priv/$dom")) { + my @contents = grep(!/^\.{1,2}/,readdir($domdir)); + closedir($domdir); + if (@contents == 0) { + if (rmdir("$londocroot/priv/$dom")) { + $output = &mt('Removed empty directory: [_1]', + "'$londocroot/priv/$dom'")."\n"; + } else { + $output = &mt('Failed to remove directory: [_1]', + "'$londocroot/priv/$dom'")."\n"; + } + } + } + } + } + my $warning = &mt('WARNING: Access to Construction Spaces in their old locations (i.e., in [_1]) via LON-CAPA with URLs of the form [_2] will not work until the directory at [_3] is moved or deleted.',"'/home//'","'/priv//'","'$londocroot/priv/'")."\n"; + if (opendir(my $dir,"$londocroot/priv")) { + my @contents = (grep(!/^\.{1,2}/,readdir($dir))); + closedir($dir); + if (@contents == 0) { + if (rmdir("$londocroot/priv")) { + $output .= &mt('Removed empty directory: [_1]', + "'$londocroot/priv'")."\n"; + } else { + $output .= &mt('Failed to remove directory: [_1]', + "'$londocroot/priv'")."\n". + $warning."\n"; + } + } else { + $output .= $warning."\n". + &mt('The attempt to remove the directory failed, because it is not empty.')."\n"; + } + } else { + $output .= $warning."\n". + &mt('The attempt to open the directory to see its contents failed, hence no attempt was made to remove it.')."\n"; + } + print $output; + print $logfh $output; + } &stop_logging($logfh); print "\n".&mt('Done')."\n"; exit; } +my @allskipped; +my %allmoved; +my ($dbh,$dbflag); + # Iterate over directories in /home if (opendir(my $dir,"/home")) { - foreach my $item (grep(!/^\.{1,2}$/,readdir($dir))) { + my @possibles = grep(!/^\.{1,2}$/,readdir($dir)); + closedir($dir); + foreach my $item (sort(@possibles)) { next if ($item eq 'www'); if ((-d "/home/$item") && ($item ne '')) { # Is there a public_html-directory? if (-d "/home/$item/public_html") { my $author = $item; - my ($domain,$skipped,$output); - if (ref($pubusers{$author}) eq 'ARRAY') { - ($domain,$skipped) = &choose_domain($action,$author,$pubusers{$author}); + my ($domain,$skipped,$output,$stopnow); + if (ref($allauthors{$author}) eq 'ARRAY') { + ($domain,$skipped,$stopnow) = + &choose_domain($action,$author,$allauthors{$author}); + if ($stopnow) { + if ($action ne 'dryrun') { + my $output = &mt('Stopped by user at author: [_1].', + "'$author'")/"\n"; + &stop_logging($logfh,$output); + } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } + print &mt('Stopped.')."\n"; + exit; + } } if (($domain eq '') && (!$skipped)) { - if (ref($allauthors{$author}) eq 'ARRAY') { - ($domain,$skipped) = &choose_domain($action,$author,$allauthors{$author}); + if (ref($pubusers{$author}) eq 'ARRAY') { + ($domain,$skipped,$stopnow) = + &choose_domain($action,$author,$pubusers{$author}); + } + if ($stopnow) { + if ($action ne 'dryrun') { + my $output = &mt('Stopped by user at author: [_1].', + "'$author'")/"\n"; + &stop_logging($logfh,$output); + } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } + print &mt('Stopped.')."\n"; + exit; + } + } + if (($domain eq '') && (!$skipped)) { + my @foundauthor = (); + foreach my $dom (@machinedoms) { + my $posspath = &LONCAPA::propath($dom,$author); + if (-e $posspath) { + my $rolesdbref; + my $fname = "$posspath/roles.db"; + if (-e "$fname") { + $rolesdbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER()); + if (!$rolesdbref) { + print &mt('Unable to tie to [_1].',"'$fname'")."\n"; + } elsif (ref($rolesdbref) eq 'HASH') { + foreach my $key (keys(%{$rolesdbref})) { + if ($key eq "/$dom/_au") { + unless(grep(/^\Q$dom\E$/,@foundauthor)) { + push(@foundauthor,$dom); + } + } + } + &LONCAPA::locking_hash_untie($rolesdbref); + } + } + } + } + if (@foundauthor > 0) { + ($domain,$skipped,$stopnow) = + &choose_domain($action,$author,\@foundauthor); + if ($stopnow) { + if ($action ne 'dryrun') { + my $output = &mt('Stopped by user at author: [_1].', + "'$author'")/"\n"; + &stop_logging($logfh,$output); + } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } + print &mt('Stopped.')."\n"; + exit; + } + } + } + if (($domain eq '') && (!$skipped)) { + if (!$dbflag) { + ($dbh,$dbflag) = &connect_mysql($lonsqlaccess); + } + if (defined($dbh)) { + my $foundusers = &search_allusers($dbh,$author); + if (ref($foundusers) eq 'HASH') { + ($domain,$skipped,$stopnow) = + &choose_domain($action,$author,$foundusers); + } + if ($stopnow) { + if ($action ne 'dryrun') { + my $output = &mt('Stopped by user at author: [_1].', + "'$author'")/"\n"; + &stop_logging($logfh,$output); + } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } + print &mt('Stopped.')."\n"; + exit; + } } } my $source_path="/home/$author/public_html"; - if ($domain) { + if ($domain) { my $target_path="$londocroot/priv/$domain/$author"; if ($action eq 'move') { if (move($source_path,$target_path)) { - chown($uid,$gid,$target_path); - chmod($target_path,0750); + my (undef,undef,$userid,$groupid) = getpwnam($author); + if ($userid eq '' && $groupid eq '' && $author ne '') { + chown($uid,$gid,$target_path); + } $output = &mt('Moved [_1] to [_2].', "'$source_path'","'$target_path'")."\n"; + push(@{$allmoved{$domain}},$author); my (undef,undef,$userid,$groupid) = getpwnam($author); if ($userid eq '' && $groupid eq '' && $author ne '') { &check_for_restore_files($londaemons,$author,$domain); if (opendir(my $homedir,"/home/$author")) { my @contents = grep(!/^\.{1,2}$/,readdir($homedir)); + closedir($homedir); if (@contents == 0) { if (rmdir("/home/$author/")) { $output .= &mt('Removed empty directory: [_1]', @@ -443,22 +599,33 @@ if (opendir(my $dir,"/home")) { print $output; print $logfh $output; } elsif ($action eq 'dryrun') { + push(@{$allmoved{$domain}},$author); print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; } } elsif ($skipped) { + push(@allskipped,$author); if ($action ne 'dryrun') { - print $logfh &mt('Skipping this user: [_1].',"'$author'")."\n"; + my $output = &mt('Skipping this user: [_1].',"'$author'")."\n"; + print $logfh $output; } } else { print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n". - &mt('Enter [_1]: do nothing, continue.','1')."\n". - &mt('Enter [_2]: stop.','2')."\n". + &mt('Enter [_1]: skip this user.','1')."\n". + &mt('Enter [_1]: stop.','2')."\n". &mt('or enter domain for user to be placed into')."\n". &mt('Your input: '); my $choice=; chomp($choice); - if ($choice ==1) { - print $logfh &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n"; + $choice =~ s/^\s+//; + $choice =~ s/\s+$//; + if ($choice == 1) { + my $output = &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n"; + print $output; + if ($action ne 'dryrun') { + print $logfh $output; + } + push(@allskipped,$author); + next; } if ($choice == 2) { print &mt('Stopped.')."\n"; @@ -467,13 +634,60 @@ if (opendir(my $dir,"/home")) { "'$author'")/"\n"; &stop_logging($logfh,$output); } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } exit; - } - if ($choice =~ /^$match_domain$/) { + } elsif ($choice =~ /^$match_domain$/) { + print &mt('You entered:')." $choice\n". + &mt('Is this ok? ~[Y/n~] '); + if (!&get_user_selection(1)) { + print &mt('Try again ...')."\n". + &mt('Enter [_1]: skip this user.','1')."\n". + &mt('Enter [_1]: stop.','2')."\n". + &mt('or enter domain for user to be placed into')."\n". + &mt('Your input: '); + $choice=; + chomp($choice); + $choice =~ s/^\s+//; + $choice =~ s/\s+$//; + if ($choice == 1) { + my $output = &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n"; + print $output; + if ($action ne 'dryrun') { + print $logfh $output; + } + push(@allskipped,$author); + next; + } + if ($choice == 2) { + print &mt('Stopped.')."\n"; + if ($action ne 'dryrun') { + my $output = &mt('Stopped by user because of author without domain: [_1].', + "'$author'")/"\n"; + &stop_logging($logfh,$output); + } + if ($dbflag == 1) { + &disconnect_mysql($dbh); + } + exit; + } elsif ($choice !~ /^$match_domain$/) { + print &mt('Invalid domain entered:')." $choice\n"; + my $output = &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n"; + print $output; + if ($action ne 'dryrun') { + print $logfh $output; + } + push(@allskipped,$author); + next; + } + } my $dompath="$londocroot/priv/$choice"; my $newpath="$londocroot/priv/$choice/$author"; unless (-e $dompath) { - print '*** '.&mt('WARNING: [_1] does not yet exist.',"'$dompath'")."\n"; + if ($action eq 'move') { + print '*** '.&mt('WARNING: [_1] does not yet exist.',"'$dompath'")."\n"; + } } if ($action eq 'move') { unless (-e $dompath) { @@ -485,7 +699,7 @@ if (opendir(my $dir,"/home")) { if (-e $dompath) { if (move($source_path,$newpath)) { chown($uid,$gid,$newpath); - chmod($newpath,0750); + chmod(0750,$newpath); $output = &mt('Moved [_1] to [_2].', "'$source_path'","'$newpath'")."\n"; } else { @@ -505,51 +719,98 @@ if (opendir(my $dir,"/home")) { } print &mt('Would make [_1].',"'$newpath'")."\n"; } + } else { + print &mt('Invalid domain:')." $choice\n"; + if ($action eq 'move') { + print $logfh &mt('Skipping -- no domain for user: [_1].',"'$author'")."\n"; + } + push(@allskipped,$author); + next; } } } } } } -if ($action ne 'dryrun') { - &stop_logging($logfh); + +my ($moveinfo,$skipcount); +if (keys(%allmoved) == 0) { + $moveinfo = &mt('None')."\n"; +} else { + foreach my $dom (sort(keys(%allmoved))) { + if (ref($allmoved{$dom}) eq 'ARRAY') { + $moveinfo .= "\n ".&mt('Domain: [_1], number of authors: [_2]', + "'$dom'",scalar(@{$allmoved{$dom}})); + } + } } -print "\n".&mt('Done.')."\n"; -sub choose_domain { - my ($action,$author,$domarrayref) = @_; - my ($domain,$skipped); - if (ref($domarrayref) eq 'ARRAY') { - if (@{$domarrayref} > 1) { - print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n". - &mt('Enter a number to choose what action to take.')."\n"; - my $num = 1; - for (my $i=0; $i<@{$domarrayref}; $i++) { - print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n"; - $num ++; - } - print &mt('To skip this user enter: [_1].',$num)."\n". - &mt('Your choice:').' '; - my $choice=; - chomp($choice); - if ($choice =~ /^\d+$/) { - if (($choice == $num) || ($choice > $num)) { - $skipped = 1; - } elsif (($choice < $num) && ($choice > 0)) { - $domain = $domarrayref->[$choice-1]; - } else { - print &mt('Invalid choice:')." $choice\n"; - $skipped = 1; - } - } else { - print &mt('Invalid choice:')." $choice\n"; - $skipped = 1; - } - } elsif (@{$domarrayref} == 1) { - $domain = $domarrayref->[0]; - } +$skipcount = scalar(@allskipped); + +print "\n"; +if ($action ne 'dryrun') { + my $output = &mt('You skipped: [_1].',$skipcount)."\n". + join("\n",sort(@allskipped))."\n\n". + &mt('Moved ... [_1]',$moveinfo); + print $output; + print $logfh $output; + &stop_logging($logfh); +} else { + print &mt('You would have skipped: [_1].',$skipcount)."\n". + join("\n",sort(@allskipped))."\n\n". + &mt('You would have moved ... [_1]',$moveinfo); +} +print "\n\n".&mt('Done.')."\n"; + +sub choose_domain { + my ($action,$author,$domref) = @_; + my ($domain,$skipped,$stopnow,@domains); + if (ref($domref) eq 'ARRAY') { + @domains = @{$domref}; + } elsif (ref($domref) eq 'HASH') { + @domains = sort(keys(%{$domref})); + } + if (@domains > 1) { + print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n". + &mt('Enter a number to choose what action to take.')."\n"; + my $num = 1; + print &mt('Enter [_1]: skip this user.',$num)."\n"; + for (my $i=0; $i<@domains; $i++) { + my $shown = $domains[$i]; + if (ref($domref) eq 'HASH') { + if ($domref->{$shown} ne '') { + $shown .= ' ('.$domref->{$shown}.') '; + } + } + $num ++; + print &mt('Enter [_1]: use domain - [_2].',$num,$shown)."\n"; + } + $num ++; + print &mt('Enter [_1]: stop.',$num)."\n"; + print &mt('Your choice:').' '; + my $choice=; + chomp($choice); + if ($choice =~ /^\d+$/) { + if ($choice == 1) { + $skipped = 1; + } elsif (($choice < $num) && ($choice > 1)) { + $domain = $domains[$choice-2]; + } elsif ($choice == $num) { + $stopnow = 1; + } else { + print &mt('Invalid choice:')." $choice\n". + &mt('Skipping this user.')."\n"; + $skipped = 1; + } + } else { + print &mt('Invalid choice:')." $choice\n". + &mt('Skipping this user.')."\n"; + $skipped = 1; + } + } elsif (@domains == 1) { + $domain = $domains[0]; } - return ($domain,$skipped); + return ($domain,$skipped,$stopnow); } sub move_priv_to_home { @@ -560,7 +821,7 @@ sub move_priv_to_home { my $target_path="/home/$uname/public_html"; if (!-e "/home/$uname") { my (undef,undef,$userid,$groupid) = getpwnam($uname); - if (mkdir("/home/$uname",0750)) { + if (mkdir("/home/$uname",0711)) { if ($userid ne '' && $groupid ne '') { chown($userid,$groupid,"/home/$uname"); } @@ -574,8 +835,12 @@ sub move_priv_to_home { if (!-e $target_path) { move($source_path,$target_path); chown($uid,$gid,$target_path); - chmod($target_path,2770); - $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + chmod(02770,$target_path); + if (-e $target_path && !-e $source_path) { + $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + } else { + $output = &mt('Failed to move [_1] to [_2].',"'$source_path'","'$target_path'")."\n"; + } } else { $output = &mt('Directory [_1] already exists -- not moving [_2].', "'$target_path'","'$source_path'")."\n"; @@ -629,6 +894,7 @@ sub check_for_restore_files { my ($londaemons,$author,$domain) = @_; if (opendir(my $homedir,"/home/$author")) { my @contents = grep(!/^\.{1,2}$/,readdir($homedir)); + closedir($homedir); if (@contents > 0) { if (grep(/^restore_\d+\.sh$/,@contents)) { if (!-e "$londaemons/logs/moved_construction_spaces") { @@ -667,3 +933,46 @@ sub check_for_restore_files { return; } +sub connect_mysql { + my ($lonsqlaccess) = @_; + my ($dbh,$dbflag); + eval { $dbh = DBI->connect("DBI:mysql:loncapa","www", + $lonsqlaccess, + {RaiseError =>0,PrintError=>0}); + }; + if ($@) { + $dbflag = -1; + } else { + if (defined($dbh)) { + $dbflag = 1; + } + } + return ($dbh,$dbflag); +} + +sub disconnect_mysql { + my ($dbh) = @_; + if (ref($dbh)) { + $dbh->disconnect; + } + return; +} + +sub search_allusers { + my ($dbh,$author) = @_; + my %fullnames; + if ((ref($dbh)) && ($author ne '')) { + eval { + my $statement = "SELECT domain, lastname, firstname FROM allusers WHERE username='$author'"; + my $sth = $dbh->prepare($statement); + $sth->execute(); + while ( my ($dom,$last,$first) = $sth->fetchrow_array()) { + if ($dom ne '') { + $fullnames{$dom} = "$first $last"; + } + } + $sth->finish; + }; + } + return \%fullnames; +}