Readabalize code. Will fix bugs later today.

This commit is contained in:
Martin Kamerbeek 2007-02-26 10:16:56 +00:00
parent 9cacb28c69
commit a2b5a14ce3

View file

@ -178,18 +178,21 @@ if (!($^O =~ /^Win/i) && $> != 0 && !$override) {
my %ListAssetExists; my %ListAssetExists;
my %filelisthash; my %filelisthash;
print "Starting..." unless ($quiet); print "Starting..." unless ($quiet);
my $session = WebGUI::Session->open($webguiRoot,$configFile); my $session = WebGUI::Session->open($webguiRoot,$configFile);
$session->user({userId=>3}); $session->user({userId=>3});
print "OK\n" unless ($quiet); print "OK\n" unless ($quiet);
my $parent = WebGUI::Asset::File->newByDynamicClass($session, $parentAssetId); my $parent = WebGUI::Asset::File->newByDynamicClass($session, $parentAssetId);
if (defined $parent) { if (defined $parent) {
&buildListAssetExists($parent); &buildListAssetExists($parent);
} else { } else {
print "Warning: Parent asset '".$parentAssetId."' does not exist. Cannot import files.\n"; print "Warning: Parent asset '".$parentAssetId."' does not exist. Cannot import files.\n";
exit 5; exit 5;
} }
print "End of the childs detection\n" unless ($quiet); print "End of the childs detection\n" unless ($quiet);
# TB : wrap buildFileList in buildFileListWrap function for recursive search. # TB : wrap buildFileList in buildFileListWrap function for recursive search.
&addFiles(&buildFileListWrap($pathToFiles)); &addFiles(&buildFileListWrap($pathToFiles));
&setPrivileges(); &setPrivileges();
@ -197,6 +200,7 @@ print "End of the childs detection\n" unless ($quiet);
print "Cleaning up..." unless ($quiet); print "Cleaning up..." unless ($quiet);
$session->var->end(); $session->var->end();
$session->close(); $session->close();
print "OK\n" unless ($quiet); print "OK\n" unless ($quiet);
exit 0; exit 0;
@ -204,138 +208,160 @@ exit 0;
# addFiles(dbHandler, filelistHashRef, webguiSettingsHashRef, pathToCopyFrom) # addFiles(dbHandler, filelistHashRef, webguiSettingsHashRef, pathToCopyFrom)
#----------------------------------------- #-----------------------------------------
sub addFiles { sub addFiles {
my $filelist = shift; my $filelist = shift;
print "Adding files...\n" unless ($quiet); print "Adding files...\n" unless ($quiet);
foreach my $file (@{$filelist}) { foreach my $file (@{$filelist}) {
my $class = 'WebGUI::Asset::File'; my $class = 'WebGUI::Asset::File';
# TB : add the path relative to $pathToFile in the message.
print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet); # TB : add the path relative to $pathToFile in the message.
my $templateId = 'PBtmpl0000000000000024'; print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet);
if (isIn(lc($file->{ext}),@nailable)) {
$class = 'WebGUI::Asset::File::Image'; my $templateId = 'PBtmpl0000000000000024';
$templateId = 'PBtmpl0000000000000088' if (isIn(lc($file->{ext}),@nailable)) {
} $class = 'WebGUI::Asset::File::Image';
my $url = $parent->getUrl.'/'.$file->{filename}; $templateId = 'PBtmpl0000000000000088'
}
my $url = $parent->getUrl.'/'.$file->{filename};
# TB : Possibly detect if the Asset exists already. # TB : Possibly detect if the Asset exists already.
my ($replaceAsset, $replaceAssetId, $child) = (0,"",""); my ($replaceAsset, $replaceAssetId, $child) = (0,"","");
($replaceAsset, $replaceAssetId, $child) = &checkAssetExists($url) if ($overwrite); ($replaceAsset, $replaceAssetId, $child) = &checkAssetExists($url) if ($overwrite);
if ($replaceAsset == 1) { if ($replaceAsset == 1) {
# TB : If the Asset exists, just copy the file. # TB : If the Asset exists, just copy the file.
# To be check. # To be check.
my $storage = WebGUI::Storage->get($session, $replaceAssetId); my $storage = WebGUI::Storage->get($session, $replaceAssetId);
print "\t\tAsset exists already. Replace the file.\n" unless ($quiet); print "\t\tAsset exists already. Replace the file.\n" unless ($quiet);
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}"); my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
$child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image'); $child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
$child->setSize($storage->getFileSize($filename)); $child->setSize($storage->getFileSize($filename));
} else { }
print "\t\tCreate the new asset.\n" unless ($quiet); else {
my $storage = WebGUI::Storage->create($session); print "\t\tCreate the new asset.\n" unless ($quiet);
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}"); my $storage = WebGUI::Storage->create($session);
# TB : possibly remove the extension if the ignoreExtInName feature enabled. my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
my $filenameTitle = $filename;
$filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName); # TB : possibly remove the extension if the ignoreExtInName feature enabled.
my $newAsset = $parent->addChild({ my $filenameTitle = $filename;
className=>$class, $filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName);
title=>$filenameTitle,
menuTitle=>$filenameTitle, my $newAsset = $parent->addChild({
filename=>$filename, className => $class,
storageId=>$storage->getId, title => $filenameTitle,
isHidden=>1, menuTitle => $filenameTitle,
url=>$url, filename => $filename,
groupIdView=>$groupToView, storageId => $storage->getId,
groupIdEdit=>$groupToEdit, isHidden => 1,
templateId=>$templateId, url => $url,
endDate=>32472169200, groupIdView => $groupToView,
ownerUserId=>$owner groupIdEdit => $groupToEdit,
}); templateId => $templateId,
$newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image'); endDate => 32472169200,
$newAsset->setSize($storage->getFileSize($filename)); ownerUserId => $owner,
} });
}
print "Finished adding.\n" unless ($quiet); $newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
$newAsset->setSize($storage->getFileSize($filename));
}
}
print "Finished adding.\n" unless ($quiet);
} }
#----------------------------------------- #-----------------------------------------
# setPrivileges() # setPrivileges()
#----------------------------------------- #-----------------------------------------
sub setPrivileges { sub setPrivileges {
print "Setting filesystem privileges.\n" unless ($quiet); print "Setting filesystem privileges.\n" unless ($quiet);
if ($session->os->get("type") eq "Linuxish") {
unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) { if ($session->os->get("type") eq "Linuxish") {
print "Privileges set.\n" unless ($quiet); unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) {
} else { print "Privileges set.\n" unless ($quiet);
print "Could not set privileges.\n"; }
} else {
} else { print "Could not set privileges.\n";
print "Cannot set privileges on this platform.\n" unless ($quiet) }
} }
else {
print "Cannot set privileges on this platform.\n" unless ($quiet)
}
} }
#----------------------------------------- #-----------------------------------------
# buildFileListWrap(pathToImportFiles) # buildFileListWrap(pathToImportFiles)
#----------------------------------------- #-----------------------------------------
sub buildFileListWrap { sub buildFileListWrap {
my ($path) = @_; my ($path) = @_;
my (@filelist); my (@filelist);
print "Building file list.\n" unless ($quiet);
@filelist = &buildFileList($now,$path); print "Building file list.\n" unless ($quiet);
# &filelistCheck(@filelist); @filelist = &buildFileList($now,$path);
print "File list complete.\n" unless ($quiet); # &filelistCheck(@filelist);
return \@filelist; print "File list complete.\n" unless ($quiet);
return \@filelist;
} }
#----------------------------------------- #-----------------------------------------
# buildFileList(time,pathToImportFiles) # buildFileList(time,pathToImportFiles)
#----------------------------------------- #-----------------------------------------
sub buildFileList { sub buildFileList {
my ($now,$path) = @_; my ($now,$path) = @_;
my (@filelist, @files, $file, $filename, $ext); my (@filelist, @files, $file, $filename, $ext);
if (opendir(FILES,$path)) {
@files = readdir(FILES); if (opendir(FILES,$path)) {
foreach $file (@files) { @files = readdir(FILES);
next if ($file eq "." || $file eq "..");
my $fullpathfile = "$path$slash$file"; foreach $file (@files) {
if (-f "$fullpathfile") { next if ($file eq "." || $file eq "..");
$file =~ /(.*?)\.(.*?)$/; my $fullpathfile = "$path$slash$file";
$filename = $1; if (-f "$fullpathfile") {
$ext = $2; $file =~ /(.*?)\.(.*?)$/;
# TB : filter process : skip files due to options : skipOlderThan and findByExt $filename = $1;
next if (&skipFilter($fullpathfile,$ext,$now)); $ext = $2;
# TB : due to recursive search. Not really nice.
my $relpath = &trailFile("$path"); # TB : filter process : skip files due to options : skipOlderThan and findByExt
# TB : check is the filelist doesn't contains two times the same file (file with the same name) next if (&skipFilter($fullpathfile,$ext,$now));
# due to recursive call, this can happen.
if (exists $filelisthash{$file}) { # TB : due to recursive search. Not really nice.
print "Error: file \"$file\" exists at several locations. my $relpath = &trailFile("$path");
Both \"$filelisthash{$file}\" and \"$relpath\" contains it.
Exit at the first error of this type.\n" unless ($quiet); # TB : check is the filelist doesn't contains two times the same file (file with the same name)
exit 2; # due to recursive call, this can happen.
if (exists $filelisthash{$file}) {
print "Error: file \"$file\" exists at several locations.
Both \"$filelisthash{$file}\" and \"$relpath\" contains it.
Exit at the first error of this type.\n" unless ($quiet);
exit 2;
}
push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath});
$filelisthash{$file} = $relpath;
print "Found file $file.\n" unless ($quiet);
}
# TB : the recursive call
push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive);
}
closedir(FILES);
return @filelist;
}
else {
print "Error: Could not open folder $path.\n" unless ($quiet);
exit 2;
} }
push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath});
$filelisthash{$file} = $relpath;
print "Found file $file.\n" unless ($quiet);
}
# TB : the recursive call
push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive);
}
closedir(FILES);
return @filelist;
} else {
print "Error: Could not open folder $path.\n" unless ($quiet);
exit 2;
}
} }
#----------------------------------------- #-----------------------------------------
# getType(filename) # getType(filename)
#----------------------------------------- #-----------------------------------------
sub getType { sub getType {
my ($extension); my ($extension);
$extension = $_[0]; $extension = $_[0];
$extension =~ s/.*\.(.*?)$/$1/; $extension =~ s/.*\.(.*?)$/$1/;
return $extension;
return $extension;
} }
@ -343,22 +369,26 @@ sub getType {
# skipFilter(file,ext,time) # skipFilter(file,ext,time)
#----------------------------------------- #-----------------------------------------
sub skipFilter { sub skipFilter {
my ($file,$ext,$now) = @_; my ($file,$ext,$now) = @_;
# TB : stat in Windows has a strange behaviour relativelly to Unix
# the output of stat si an array of array. # TB : stat in Windows has a strange behaviour relativelly to Unix
# to be check on Unix if this work correctly. # the output of stat si an array of array.
my @dev = stat "$file"; # to be check on Unix if this work correctly.
# TB : option skipOlderThan my @dev = stat "$file";
if ($now - $dev[0][9] > $skipOlderThan) {
print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet); # TB : option skipOlderThan
return 1; if ($now - $dev[0][9] > $skipOlderThan) {
} print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet);
# TB : option findByExt return 1;
if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) { }
print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet);
return 1; # TB : option findByExt
} if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) {
return 0; print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet);
return 1;
}
return 0;
} }
@ -366,19 +396,23 @@ sub skipFilter {
# trailFile(path) # trailFile(path)
#----------------------------------------- #-----------------------------------------
sub trailFile { sub trailFile {
# TB : this is ugly, but unable to find a better way to do it. # TB : this is ugly, but unable to find a better way to do it.
# this is supposed to find the relative path to pathToFiles # this is supposed to find the relative path to pathToFiles
my ($path) = @_; my ($path) = @_;
my $pathToFiles_temp = $pathToFiles; my $pathToFiles_temp = $pathToFiles;
if ($^O =~ /Win/) {
$pathToFiles_temp =~ s/\\/\\\\/g; if ($^O =~ /Win/) {
} else { $pathToFiles_temp =~ s/\\/\\\\/g;
$pathToFiles_temp =~ s/\//\\\//g; }
} else {
$path =~ s/$pathToFiles_temp//; $pathToFiles_temp =~ s/\//\\\//g;
$path =~ s/.//; }
$path = "." if ($path eq "");
return $path; $path =~ s/$pathToFiles_temp//;
$path =~ s/.//;
$path = "." if ($path eq "");
return $path;
} }
@ -386,16 +420,18 @@ sub trailFile {
# checkAssetExists(self,url) # checkAssetExists(self,url)
#----------------------------------------- #-----------------------------------------
sub checkAssetExists { sub checkAssetExists {
my ($url) = @_; my ($url) = @_;
my $replaceAsset = 0; my $replaceAsset = 0;
my $replaceAssetId = ""; my $replaceAssetId = "";
my $child; my $child;
if (exists $ListAssetExists{$url}) {
$child = $ListAssetExists{$url}; if (exists $ListAssetExists{$url}) {
$replaceAsset = 1; $child = $ListAssetExists{$url};
$replaceAssetId = $child->getId; $replaceAsset = 1;
} $replaceAssetId = $child->getId;
return($replaceAsset, $replaceAssetId, $child); }
return($replaceAsset, $replaceAssetId, $child);
} }
@ -403,13 +439,14 @@ sub checkAssetExists {
# buildListAssetExists(self,url) # buildListAssetExists(self,url)
#----------------------------------------- #-----------------------------------------
sub buildListAssetExists { sub buildListAssetExists {
my ($parent) = @_; my ($parent) = @_;
my @allelement = $parent->getLineage(); my @allelement = $parent->getLineage();
foreach my $elem ( @{$allelement[0]} ) {
my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem); foreach my $elem ( @{$allelement[0]} ) {
if (defined $child) { my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem);
$ListAssetExists{$child->getUrl} = $child; if (defined $child) {
} $ListAssetExists{$child->getUrl} = $child;
} }
}
} }