Created
April 26, 2012 17:26
-
-
Save anonymous/2501115 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=head1 DESCRIPTION | |
=head1 SYNOPSIS | |
wgd util path/to/script.pl | |
=cut | |
our $webguiRoot; | |
BEGIN { | |
$webguiRoot = "/data/WebGUI"; | |
unshift( @INC, $webguiRoot . "/lib" ); | |
} | |
use strict; | |
use Getopt::Long; | |
use WebGUI::Session; | |
use WebGUI::Group; | |
use NETC; | |
use WebGUI::WebFlow::NeTC; | |
# Filename is "version" | |
my $toVersion = File::Basename::fileparse( $0, '.pl', '.skeleton' ); | |
my $quiet; # this line required | |
my $session = start(); # this line required | |
$SIG{DIE} = sub { use Carp; Carp::confess; }; | |
# upgrade functions go here | |
upgrade($session); | |
finish($session); # this line required | |
sub upgrade { | |
my $session = shift; | |
do { | |
print "Renaming stuff, if it exists... " unless $quiet; | |
if( my $asset = WebGUI::Asset->newByUrl( $session, 'url/url') ) { | |
$asset->update({ | |
url => 'url/new-url', | |
}); | |
my $assets = $asset->getLineage(['descendants'], { returnObjects => 1, }); | |
for my $asset (@$assets) { | |
my $url = $asset->get('url'); | |
print $url, '... '; | |
$url =~ s{/dep([^a-z])}{/mde$1}; | |
print " renamed to $url\n"; | |
$asset->update({ url => $url }); | |
} | |
print " DONE!\n" unless $quiet; | |
} else { | |
die "strange, couldn't find asset"; | |
} | |
}; | |
} | |
# -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- | |
#---------------------------------------------------------------------------- | |
# Add a package to the import node | |
sub addPackage { | |
my $session = shift; | |
my $file = shift; | |
# Make a storage location for the package | |
my $storage = WebGUI::Storage->createTemp($session); | |
$storage->addFileFromFilesystem($file); | |
# Import the package into the import node | |
my $package = | |
WebGUI::Asset->getImportNode($session)->importPackage($storage); | |
# Turn off the package flag, and set the default flag for templates added | |
my $assetIds = $package->getLineage( [ 'self', 'descendants' ] ); | |
for my $assetId ( @{$assetIds} ) { | |
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId ); | |
if ( !$asset ) { | |
"Couldn't instantiate asset with ID '$assetId'. Please check package '$file' for corruption.\n"; | |
next; | |
} | |
my $properties = { isPackage => 0 }; | |
if ( $asset->isa('WebGUI::Asset::Template') ) { | |
# $properties->{isDefault} = 1; | |
} | |
$asset->update($properties); | |
} | |
return; | |
} | |
#------------------------------------------------- | |
sub start { | |
my $configFile; | |
$| = 1; #disable output buffering | |
GetOptions( | |
'configFile=s' => \$configFile, | |
'quiet' => \$quiet | |
); | |
my $session = WebGUI::Session->open( $webguiRoot, $configFile ); | |
$session->user( { userId => 3 } ); | |
my $versionTag = WebGUI::VersionTag->getWorking($session); | |
$versionTag->set( { name => "Upgrade to " . $toVersion } ); | |
return $session; | |
} | |
#------------------------------------------------- | |
sub finish { | |
my $session = shift; | |
updateTemplates($session); | |
my $versionTag = WebGUI::VersionTag->getWorking($session); | |
$versionTag->commit; | |
# $session->db->write("insert into webguiVersion values (".$session->db->quote($toVersion).",'upgrade',".$session->datetime->time().")"); | |
$session->close(); | |
} | |
#------------------------------------------------- | |
sub updateTemplates { | |
my $session = shift; | |
use FindBin; | |
my $dir = "$FindBin::Bin/packages-$toVersion"; | |
return undef unless ( -d $dir ); | |
print "\tUpdating packages... " unless ($quiet); | |
opendir( DIR, $dir ); | |
my @files = readdir(DIR); | |
closedir(DIR); | |
my $newFolder = undef; | |
foreach my $file (@files) { | |
next unless ( $file =~ /\.wgpkg$/ ); | |
# Fix the filename to include a path | |
$file = "$dir/$file"; | |
addPackage( $session, $file ); | |
} | |
print "Done.\n" unless ($quiet); | |
} | |
#vim:ft=perl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment