Diff for /loncom/debugging_tools/move_construction_spaces.pl between versions 1.2 and 1.8

version 1.2, 2011/10/26 17:35:10 version 1.8, 2012/06/08 13:04:08
Line 1 Line 1
 #!/usr/bin/perl  #!/usr/bin/perl
 #  #
   # The LearningOnline Network
   #
 # Move Construction Spaces from /home/$user/public_html  # Move Construction Spaces from /home/$user/public_html
 # to /home/httpd/html/priv/$domain/$user and vice versa  # to /home/httpd/html/priv/$domain/$user and vice versa
 #   #
   # $Id$
   #
   # Copyright Michigan State University Board of Trustees
   #
   # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   #
   # LON-CAPA is free software; you can redistribute it and/or modify
   # it under the terms of the GNU General Public License as published by
   # the Free Software Foundation; either version 2 of the License, or
   # (at your option) any later version.
   #
   # LON-CAPA is distributed in the hope that it will be useful,
   # but WITHOUT ANY WARRANTY; without even the implied warranty of
   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   # GNU General Public License for more details.
   #
   # You should have received a copy of the GNU General Public License
   # along with LON-CAPA; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   #
   # /home/httpd/html/adm/gpl.txt
   #
   # http://www.lon-capa.org/
   #
   #################################################
   
 use strict;  use strict;
 use lib '/home/httpd/lib/perl/';  use lib '/home/httpd/lib/perl/';
Line 11  use LONCAPA qw(:DEFAULT :match); Line 38  use LONCAPA qw(:DEFAULT :match);
 use Apache::lonlocal;  use Apache::lonlocal;
 use File::Copy;  use File::Copy;
 use GDBM_File;  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();  my $lang = &Apache::lonlocal::choose_language();
 &Apache::lonlocal::get_language_handle(undef,$lang);  &Apache::lonlocal::get_language_handle(undef,$lang);
   
 if ($< != 0) {  if ($< != 0) {
     print(&mt('You must be root in order to move Construction Spaces.').      print &mt('You must be root in order to move Construction Spaces.')."\n".
          "\n");            &mt('Stopping')."\n";
       exit;
   }
   
   if ($lonusersdir eq '') {
       print &mt('Could not determine location of [_1] directory.',"'lonUsersDir'")."\n".
             &mt('Stopping')."\n";
     exit;      exit;
 }  }
   
 my $perlvar=&LONCAPA::Configuration::read_conf();  if ($londocroot eq '') {
 my ($lonuserdir,$londocroot);      print &mt('Could not determine location of [_1] directory.',"'lonDocRoot'")."\n".
 if (ref($perlvar) eq 'HASH') {            &mt('Stopping')."\n";
     $lonuserdir = $perlvar->{'lonUsersDir'};      exit;
     $londocroot = $perlvar->{'lonDocRoot'};  }
   
   my $distro;
   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 = <PIPE>;
               close(PIPE);
           }
       }
   }
   
   if ($distro eq '') {
       print &mt('Could not determine Linux distro.')."\n".
             &mt('Stopping')."\n";
       exit;
   } else {
       my $stopapachecmd = '/etc/init.d/httpd stop';
       my $apacheprocess = '/usr/sbin/httpd';
       my $stopapachecmd = '/etc/init.d/httpd stop';
       my $proc_owner = 'root';
       if ($distro =~ /^(suse|sles)/) {
           if ($distro =~ /^(suse|sles)9/) {
               $stopapachecmd = '/etc/init.d/apache stop';
           } else {
               $apacheprocess = '/usr/sbin/httpd2';
               $stopapachecmd = '/etc/init.d/apache2 stop';
           }
       } elsif ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
           $apacheprocess = '/usr/sbin/apache2';
           $stopapachecmd = '/etc/init.d/apache2 stop';
       } elsif ($distro =~ /^(?:fedora)(\d+)/) {
           my $version = $1;
           if ($version >= 16) {
               $stopapachecmd = '/bin/systemctl stop httpd.service';
           }
       }
       if (open(PIPE,"ps -ef |grep '$apacheprocess' |grep -v grep 2>&1 |")) {
           my $status = <PIPE>;
           close(PIPE);
           chomp($status);
           if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
               print "\n".
                     &mt('You need to stop the Apache daemon before moving Construction Spaces.')."\n".
                     &mt('To do so use the following command: [_1]',"\n\n$stopapachecmd")."\n\n".
                     &mt('Now stopping the move_construction_spaces.pl script.')."\n";
               exit;
           }
       } else {
           print &mt('Could not determine if Apache daemon is running.')."\n";
       }
   }
   
   my $stoploncontrol = '/etc/init.d/loncontrol stop';
   if (open(PIPE,"ps -ef |grep lond |grep -v grep 2>&1 |")) {
       my $status = <PIPE>;
       close(PIPE);
       chomp($status);
       if ($status =~ /^www\s+\d+\s+/) {
           print "\n".
                 &mt('You need to stop the LON-CAPA daemons before moving Construction Spaces.')."\n".
                 &mt('To do so use the following command: [_1]',"\n\n$stoploncontrol")."\n\n".
                 &mt('Now stopping the move_construction_spaces.pl script.')."\n";
           exit;        
       }
 }  }
 undef($perlvar);  
   
 # Abort if more than one argument.  # Abort if more than one argument.
   
Line 52  print "\n".&mt("Moving authors' Construc Line 166  print "\n".&mt("Moving authors' Construc
           "'/home'","'$londocroot/priv/'")."\n\n".            "'/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";        &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 (undef,undef,$uid,$gid) = getpwnam('www');
 my ($action) = ($parameter=~/^(move|undo)$/);  my ($action) = ($parameter=~/^(move|undo)$/);
 if ($action eq '') {  if ($action eq '') {
Line 67  if ($action eq '') { Line 173  if ($action eq '') {
 }  }
   
 if ($action eq 'dryrun') {  if ($action eq 'dryrun') {
     print "\n".      print "\n\n".
           &mt('Running in exploratory mode.')."\n".            &mt('Running in exploratory mode ...')."\n\n".
           &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',            &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',
               "'move'","'$londocroot/priv'","\nperl move_construction_spaces.pl move")."\n\n".                "'move'","'$londocroot/priv'","\n\nperl move_construction_spaces.pl move")."\n\n\n".
           &mt('Run with argument [_1] to move Construction spaces back to [_2], i.e., [_3]',            &mt('Run with argument [_1] to move Construction spaces back to [_2], i.e., [_3]',
               "'undo'","'/home'","\nperl move_construction_spaces.pl undo")."\n\n".                "'undo'","'/home'","\n\nperl move_construction_spaces.pl undo")."\n\n\n".
           &mt('Continue? ~[y/N~] ');            &mt('Continue? ~[y/N~] ');
     if (!&get_user_selection()) {      if (!&get_user_selection()) {
         exit;          exit;
       } else {
           print "\n";
     }      }
 } else {  } else {
     print "\n ***".&mt('Running in a mode where changes will be made.')."\n";      print "\n *** ".&mt('Running in a mode where changes will be made.')." ***\n";
     if ($action eq 'move') {      if ($action eq 'move') {
         print "\n".          print "\n".
               &mt('Mode is [_1] -- directories will be moved to [_2].',                &mt('Mode is [_1] -- directories will be moved to [_2].',
Line 91  if ($action eq 'dryrun') { Line 199  if ($action eq 'dryrun') {
     print &mt('Continue? ~[y/N~] ');      print &mt('Continue? ~[y/N~] ');
     if (!&get_user_selection()) {      if (!&get_user_selection()) {
         exit;          exit;
       } else {
           print "\n";
       }
   }
   
   my $logfh;
   if ($action ne 'dryrun') {
       if (!open($logfh,">>$londaemons/logs/move_construction_spaces.log")) {
           print &mt('Could not open log file: [_1] for writing.',
                     "'$londaemons/logs/move_construction_spaces.log'")."\n".
                 &mt('Stopping.')."\n";
       } else {
           &start_logging($logfh,$action);
     }      }
 }  }
   
Line 99  my %allauthors; Line 220  my %allauthors;
 my %pubusers;  my %pubusers;
   
 if ($action eq 'move') {  if ($action eq 'move') {
       my $output;
     if (-d "$londocroot/priv") {      if (-d "$londocroot/priv") {
         print "\n".          $output = &mt('New Construction Spaces directory: [_1] already exists.',
               &mt('New Construction Spaces directory: [_1] already exists.',                        "'$londocroot/priv'")."\n";
                   "'$londocroot/priv'")."\n";          print $output;
           print $logfh $output;
     } else {      } else {
         print "\n".          $output = &mt('Creating new directory: [_1] for Construction Spaces.',
               &mt('Creating new directory: [_1] for Construction Spaces.',                        "'$londocroot/priv'")."\n";
                   "'$londocroot/priv'")."\n";          if (mkdir("$londocroot/priv",0750)) {
         if (mkdir("$londocroot/priv",0755)) {  
             if (chown($uid,$gid,"$londocroot/priv")) {              if (chown($uid,$gid,"$londocroot/priv")) {
                 print &mt('Creation Successful')."\n";                  $output .= &mt('Creation Successful')."\n";
                   print $output;
                   print $logfh $output;
             } else {              } else {
                 print &mt('Failed to change ownership to [_1].',"'$uid:$gid'")."\n".                  $output .= &mt('Failed to change ownership to [_1].',"'$uid:$gid'")."\n";
                 &mt('Stopping')."\n";                  print $output;
                   &stop_logging($logfh,$output);
                   print &mt('Stopping')."\n";
                 exit;                  exit;
             }              }
         } else {          } else {
             print &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n".              $output .=  &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n";
                   &mt('Stopping')."\n";              print $output;
               &stop_logging($logfh,$output);
               print &mt('Stopping')."\n";
             exit;              exit;
         }          }
     }      }
 }  }
   
 my @machinedoms;  my @machinedoms;
 if ($lonuserdir) {  if ($lonusersdir) {
     my $dir;      my ($dir,$output);
     if (opendir($dir,$lonuserdir)) {      if (opendir($dir,$lonusersdir)) {
         my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));          my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
           closedir($dir);
         foreach my $item (@contents) {          foreach my $item (@contents) {
             if (-d "$lonuserdir/$item") {              if (-d "$lonusersdir/$item") {
                 if ($item =~ /^$match_domain$/) {                  if ($item =~ /^$match_domain$/) {
                     my $domain = $item;                      my $domain = $item;
                     unless (grep(/^\Q$domain\E$/,@machinedoms)) {                      unless (grep(/^\Q$domain\E$/,@machinedoms)) {
                         push(@machinedoms,$domain);                            push(@machinedoms,$domain);  
                     }                      }
                     my $dom_target="/home/httpd/html/priv/$domain";                      my $dom_target="$londocroot/priv/$domain";
                     if ($action eq 'move') {                      if ($action eq 'move') {
                         if (!-e $dom_target) {                          if (!-e $dom_target) {
                             if (mkdir($dom_target,0755)) {                              if (mkdir($dom_target,0755)) {
                                 chown($uid,$gid,$dom_target);                                  chown($uid,$gid,$dom_target);
                                 print &mt('Made [_1].',"'$dom_target'")."\n";                                  $output = &mt('Made [_1].',"'$dom_target'")."\n";
                                   print $output;
                                   print $logfh $output;
                             } else {                              } else {
                                 print &mt('Failed to make [_1].',"'$dom_target'")."\n".                                  $output = &mt('Failed to make [_1].',"'$dom_target'")."\n";
                                       &mt('Stopping')."\n";                                  print $output;
                                   print $logfh $output;
                                   &stop_logging($logfh,$output);
                                   print &mt('Stopping')."\n";
                                 exit;                                  exit;
                             }                              }
                         } elsif ($action eq 'dryrun') {                          } elsif ($action eq 'dryrun') {
Line 151  if ($lonuserdir) { Line 285  if ($lonuserdir) {
                         }                          }
                     }                      }
                     my %authors=();                      my %authors=();
                     my $fname = "$lonuserdir/$domain/nohist_domainroles.db";                      my $fname = "$lonusersdir/$domain/nohist_domainroles.db";
                     my $dbref;                      my $dbref;
                     if (-e $fname) {                      if (-e $fname) {
                         $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());                          $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
Line 170  if ($lonuserdir) { Line 304  if ($lonuserdir) {
                 }                  }
             }              }
         }          }
         closedir($dir);  
     } else {      } else {
         print &mt('Could not open [_1].',"'$lonuserdir'")."\n".          $output = &mt('Could not open [_1].',"'$lonusersdir'")."\n";
               &mt('Stopping')."\n";          print $output;
           &stop_logging($logfh,$output);
           print &mt('Stopping')."\n";
         exit;          exit;
     }      }
 }  }
Line 183  if ($londocroot ne '') { Line 318  if ($londocroot ne '') {
         my ($dir,$domdir);          my ($dir,$domdir);
         if (opendir($dir,"$londocroot/res")) {          if (opendir($dir,"$londocroot/res")) {
             my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));              my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
               closedir($dir);
             foreach my $dom (@contents) {              foreach my $dom (@contents) {
                 if ((grep(/^\Q$dom\E/,@machinedoms)) && (-d "$londocroot/res/$dom")) {                  if ((grep(/^\Q$dom\E/,@machinedoms)) && (-d "$londocroot/res/$dom")) {
                     if (opendir($domdir,"$londocroot/res/$dom")) {                      if (opendir($domdir,"$londocroot/res/$dom")) {
                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));                          my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
                           closedir($domdir);
                         foreach my $uname (@unames) {                          foreach my $uname (@unames) {
                             if ($uname =~ /^$match_username$/) {                              if ($uname =~ /^$match_username$/) {
                                 push(@{$pubusers{$uname}},$dom);                                  push(@{$pubusers{$uname}},$dom);
Line 206  if ($action eq 'undo') { Line 343  if ($action eq 'undo') {
             my ($dir,$domdir);              my ($dir,$domdir);
             if (opendir($dir,"$londocroot/priv")) {              if (opendir($dir,"$londocroot/priv")) {
                 my @contents = (grep(!/^\.{1,2}/,readdir($dir)));                  my @contents = (grep(!/^\.{1,2}/,readdir($dir)));
                   closedir($dir);
                 foreach my $dom (@contents) {                  foreach my $dom (@contents) {
                     next if (!-d "$londocroot/priv/$dom");                      next if (!-d "$londocroot/priv/$dom");
                     if (opendir($domdir,"$londocroot/priv/$dom")) {                      if (opendir($domdir,"$londocroot/priv/$dom")) {
                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));                          my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
                           closedir($domdir);
                         foreach my $uname (@unames) {                          foreach my $uname (@unames) {
                             if ($uname =~ /^$match_username$/) {                              if ($uname =~ /^$match_username$/) {
                                 push(@{$privspaces{$uname}},$dom);                                  push(@{$privspaces{$uname}},$dom);
Line 222  if ($action eq 'undo') { Line 361  if ($action eq 'undo') {
     }      }
     foreach my $uname (keys(%privspaces)) {      foreach my $uname (keys(%privspaces)) {
         if (ref($privspaces{$uname}) eq 'ARRAY') {          if (ref($privspaces{$uname}) eq 'ARRAY') {
               my $output;
             if (@{$privspaces{$uname}} > 1) {              if (@{$privspaces{$uname}} > 1) {
                 my $displaydoms = join(', ',@{$privspaces{$uname}});                  my $displaydoms = join(', ',@{$privspaces{$uname}});
                 print &mt('Same username used for authors in multiple domains.')."\n".                  print &mt('Same username used for authors in multiple domains.')."\n".
Line 232  if ($action eq 'undo') { Line 372  if ($action eq 'undo') {
                 my $choice=<STDIN>;                  my $choice=<STDIN>;
                 chomp($choice);                  chomp($choice);
                 if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {                  if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
                     &move_priv_to_home($londocroot,$uname,$choice);                      $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice);
                 } else {                  } else {
                     print &mt('Invalid choice of domain:')." $choice\n".                      print &mt('Invalid choice of domain:')." $choice\n";
                           &mt('Skipping this user: [_1].',"'$uname'")."\n";                      $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
                       print $output;
                       print $logfh $output;
                     next;                      next;
                 }                  }
             } elsif (@{$privspaces{$uname}} == 1) {              } elsif (@{$privspaces{$uname}} == 1) {
                     &move_priv_to_home($londocroot,$uname,$privspaces{$uname}[0]);                  $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]);
             } else {              } else {
                 print &mt('Username [_1] found in [_2] was not within a domain',                  print &mt('Username [_1] found in [_2] was not within a domain',
                           "'$uname'","'$londocroot/priv'")."\n";                            "'$uname'","'$londocroot/priv'")."\n";
                   $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
             }              }
               print $output;
               print $logfh $output;
         }          }
     }      }
     print &mt('Done')."\n";      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/<user>/'","'/priv/<user>/'","'$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;      exit;
 }  }
   
   my @allskipped;
   my %allmoved;
   my ($dbh,$dbflag);
   
 # Iterate over directories in /home  # Iterate over directories in /home
 if (opendir(my $dir,"/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');          next if ($item eq 'www');
         if (-d "/home/$item") {          if ((-d "/home/$item") && ($item ne '')) {
 # Is there a public_html-directory?  # Is there a public_html-directory?
             if (-d "/home/$item/public_html") {              if (-d "/home/$item/public_html") {
                 my $author = $item;                  my $author = $item;
                 my ($domain,$skipped);                  my ($domain,$skipped,$output,$stopnow);
                 if (ref($pubusers{$author}) eq 'ARRAY') {                  if (ref($allauthors{$author}) eq 'ARRAY') {
                     ($domain,$skipped) = &choose_domain($action,$author,$pubusers{$author});                      ($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($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 (($domain eq '') && (!$skipped)) {
                     if (ref($allauthors{$author}) eq 'ARRAY') {                      if (!$dbflag) {
                         ($domain,$skipped) = &choose_domain($action,$author,$allauthors{$author});                          ($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;
                           }
                     }                      }
                 }                  }
                 if ($domain) {                   my $source_path="/home/$author/public_html";
                     my $source_path="/home/$author/public_html";                  if ($domain) {
                     my $target_path="$londocroot/priv/$domain/$author";                      my $target_path="$londocroot/priv/$domain/$author";
                     if ($action eq 'move') {                      if ($action eq 'move') {
                         move($source_path,$target_path);                          if (move($source_path,$target_path)) {
                             chown($uid,$gid,$target_path);                              my (undef,undef,$userid,$groupid) = getpwnam($author);
                             chmod($target_path,0755);                              if ($userid eq '' && $groupid eq '' && $author ne '') {
                         print &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";                                  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]',
                                                              "'/home/$author/'")."\n";
                                           } else {
                                               $output .= &mt('Failed to remove directory: [_1]',
                                                              "'/home/$author/'")."\n";
                                           }
                                       } else {
                                           $output .= &mt('Not removing directory [_1] as it still contains: [_2]',
                                                      "'/home/$author/'",
                                                      "\n".join("\n",@contents)."\n");
                                       }
                                   }
                               } else {
                                   $output .= &mt('Not removing directory [_1] for UNIX user account',
                                                  "'/home/$author/'")."\n";  
                               }
                           } else {
                               $output = &mt('Failed to move [_1] to [_2].',
                                             "'$source_path'","'$target_path'")."\n";
                           }
                           print $output;
                           print $logfh $output;
                     } elsif ($action eq 'dryrun') {                      } elsif ($action eq 'dryrun') {
                           push(@{$allmoved{$domain}},$author);
                         print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";                          print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
                     }                      }
                 } elsif (!$skipped) {                  } elsif ($skipped) {
                       push(@allskipped,$author); 
                       if ($action ne 'dryrun') {
                           my $output = &mt('Skipping this user: [_1].',"'$author'")."\n";
                           print $logfh $output;
                       }
                   } else {
                     print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".                      print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".
                           &mt('Enter [_1]: do nothing, continue.','1')."\n".                            &mt('Enter [_1]: skip this user.','1')."\n".
                           &mt('Enter [_2]: stop.','2')."\n".                            &mt('Enter [_1]: stop.','2')."\n".
                           &mt('or enter domain for user to be placed into')."\n".                            &mt('or enter domain for user to be placed into')."\n".
                           &mt('Your input: ');                            &mt('Your input: ');
                     my $choice=<STDIN>;                      my $choice=<STDIN>;
                     chomp($choice);                      chomp($choice);
                     next if ($choice ==1);                      $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) {                      if ($choice == 2) {
                         print &mt('Stopped.')."\n";                          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;                          exit;
                     }                       } elsif ($choice =~ /^$match_domain$/) {
                     if ($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=<STDIN>;
                               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 $dompath="$londocroot/priv/$choice";
                         my $newpath="$londocroot/priv/$choice/$author";                          my $newpath="$londocroot/priv/$choice/$author";
                         unless (-e $dompath) {                          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') {                          if ($action eq 'move') {
                             print &mt('Making author [_1] in domain [_2].',"'$author'","'$choice'")."\n";  
                             unless (-e $dompath) {                              unless (-e $dompath) {
                                 print &mt('Making [_1].',"'$dompath'")."\n";                                  $output .= &mt('Making [_1].',"'$dompath'")."\n";
                                 mkdir($dompath,0755);                                  if (mkdir($dompath,0755)) {
                                 chown($uid,$gid,$dompath);                                      chown($uid,$gid,$dompath);
                             }                                  }
                             print &mt('Making [_1].',"'$newpath'")."\n";                              }
                             mkdir($newpath,0755);                              if (-e $dompath) {
                             chown($uid,$gid,$newpath);                                  if (move($source_path,$newpath)) {
                                       chown($uid,$gid,$newpath);
                                       chmod(0750,$newpath);
                                       $output = &mt('Moved [_1] to [_2].',
                                                     "'$source_path'","'$newpath'")."\n";
                                   } else {
                                       $output = &mt('Failed to move [_1] to [_2].',
                                                     "'$source_path'","'$newpath'")."\n"; 
                                   }
                                   print $output;
                                   print $logfh $output;
                               } else {
                                   $output = &mt('Failed to move [_1] to [_2] -- missing [_3].',
                                                 "'$source_path'","'$newpath'","'$dompath'")."\n";
                               }
                         } elsif ($action eq 'dryrun') {                          } elsif ($action eq 'dryrun') {
                            print &mt('Would make author [_1] in domain [_2].',"'$author'","'$choice'")."\n";                             print &mt('Would make author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
                            unless (-e $dompath) {                             unless (-e $dompath) {
Line 314  if (opendir(my $dir,"/home")) { Line 719  if (opendir(my $dir,"/home")) {
                            }                             }
                            print &mt('Would make [_1].',"'$newpath'")."\n";                             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;
                     }                      }
                 }                  }
             }              }
         }          }
     }      }
 }  }
 print "\nDone.\n";  
   
 sub choose_domain {   my ($moveinfo,$skipcount);
     my ($action,$author,$domarrayref) = @_;  if (keys(%allmoved) == 0) {
     my ($domain,$skipped);      $moveinfo = &mt('None')."\n";
     if (ref($domarrayref) eq 'ARRAY') {  } else {
          if (@{$domarrayref} > 1) {      foreach my $dom (sort(keys(%allmoved))) {
              print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".          if (ref($allmoved{$dom}) eq 'ARRAY') {
                    &mt('Enter a number to choose what action to take.')."\n";              $moveinfo .= "\n      ".&mt('Domain: [_1], number of authors: [_2]',
              my $num = 1;                                          "'$dom'",scalar(@{$allmoved{$dom}}));
              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".  $skipcount = scalar(@allskipped);
                    &mt('Your choice:').' ';  
              my $choice=<STDIN>;  print "\n";
              chomp($choice);  if ($action ne 'dryrun') {
              if ($choice =~ /^\d+$/) {      my $output = &mt('You skipped: [_1].',$skipcount)."\n".
                  if (($choice == $num) || ($choice > $num)) {                   join("\n",sort(@allskipped))."\n\n".
                      $skipped = 1;                          &mt('Moved ... [_1]',$moveinfo);
                  } elsif (($choice < $num) && ($choice > 0)) {      print $output;
                      $domain = $domarrayref->[$choice-1];      print $logfh $output;
                  } else {      &stop_logging($logfh);
                      print &mt('Invalid choice:')." $choice\n";  } else {
                      $skipped = 1;      print &mt('You would have skipped: [_1].',$skipcount)."\n".
                  }            join("\n",sort(@allskipped))."\n\n".
              } else {            &mt('You would have moved ... [_1]',$moveinfo);
                  print &mt('Invalid choice:')." $choice\n";  }
                  $skipped = 1;  print "\n\n".&mt('Done.')."\n";
              }  
          } elsif (@{$domarrayref} == 1) {  sub choose_domain {
              $domain = $domarrayref->[0];      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=<STDIN>;
           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 {  sub move_priv_to_home {
     my ($londocroot,$uname,$domain) = @_;      my ($londocroot,$uid,$gid,$uname,$domain) = @_;
       my $output;
     if ($uname =~ /^$match_username$/ && $domain =~ /^$match_domain$/) {      if ($uname =~ /^$match_username$/ && $domain =~ /^$match_domain$/) {
         my $source_path="$londocroot/priv/$domain/$uname";          my $source_path="$londocroot/priv/$domain/$uname";
         my $target_path="/home/$uname/public_html";          my $target_path="/home/$uname/public_html";
         if (!-e "/home/$uname") {          if (!-e "/home/$uname") {
             if (mkdir("/home/$uname",0755)) {              my (undef,undef,$userid,$groupid) = getpwnam($uname);
                 chown($uid,$gid,"/home/$uname");              if (mkdir("/home/$uname",0711)) {
                   if ($userid ne '' && $groupid ne '') {
                       chown($userid,$groupid,"/home/$uname");
                   }
             } else {              } else {
                 print &mt('Failed to create directory [_1] -- not moving [_2].',                  $output = &mt('Failed to create directory [_1] -- not moving [_2].',
                           "'/home/$uname'","'$source_path'")."\n";                            "'/home/$uname'","'$source_path'")."\n";
                   return $output;
             }              }
         }          }
         if (!-e $target_path) {          if (-e "/home/$uname") {
              move($source_path,$target_path);              if (!-e $target_path) {
              chown($uid,$gid,$target_path);                  move($source_path,$target_path);
              chmod($target_path,0755);                  chown($uid,$gid,$target_path);
              print &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";                  chmod(02770,$target_path);
         } else {                  if (-e $target_path && !-e $source_path) {
              print &mt('Directory [_1] already exists -- not moving [_2].',                      $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
                        "'$target_path'","'$source_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";
               }
         }          }
     }      }
     return;      return $output;
 }  }
   
 sub get_user_selection {  sub get_user_selection {
Line 403  sub get_user_selection { Line 869  sub get_user_selection {
     return $do_action;      return $do_action;
 }  }
   
   sub start_logging {
       my ($fh,$action) = @_;
       my $start = localtime(time);
       print $fh "*****************************************************\n".
                 &mt('[_1] - mode is [_2].',
                     'move_construction_spaces.pl',"'$action'")."\n".
                 &mt('Started -- time: [_1]',$start)."\n".
                 "*****************************************************\n\n";
       return;
   }
   
   sub stop_logging {
       my ($fh) = @_;
       my $end = localtime(time);
       print $fh "*****************************************************\n".
                  &mt('Ended -- time: [_1]',$end)."\n".
                 "*****************************************************\n\n\n";
       close($fh);
       return;
   }
   
   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") { 
                       mkdir("$londaemons/logs/moved_construction_spaces",0755);
                   }
                   if (!-e "$londaemons/logs/moved_construction_spaces/$domain") {
                       mkdir("$londaemons/logs/moved_construction_spaces/$domain",0755);
                   }
                   if (-e "$londaemons/logs/moved_construction_spaces/$domain") {
                       if (open(my $restorefh,">>$londaemons/logs/moved_construction_spaces/$domain/$author")) {
                           foreach my $item (@contents) {
                               if ($item =~ /^restore_\d+\.sh$/) {
                                   my @stats = stat("/home/$author/$item");
                                   my $lastmod = $stats[9];
                                   if (open(my $fh,"</home/$author/$item")) {
                                       print $restorefh
                                             "*******************************\n".
                                             "$item -- ".localtime(time)."\n".
                                             "*******************************\n";
                                       while (<$fh>) {
                                           print $restorefh $_;
                                       }
                                       print $restorefh
                                             "*******************************\n\n";
                                       close($fh);
                                       unlink("/home/$author/$item");
                                   }
                               }
                           }
                           close($restorefh); 
                       }
                   }
               }
           }
       }
       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;
   }

Removed from v.1.2  
changed lines
  Added in v.1.8


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>