#!/usr/bin/env perl #------------------------------------------------------------------- # WebGUI is Copyright 2001-2009 Plain Black Corporation. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- $|++; # disable output buffering our ($webguiRoot, $configFile, $help, $man, $quiet); BEGIN { $webguiRoot = ".."; unshift (@INC, $webguiRoot."/lib"); } use strict; use Pod::Usage; use Getopt::Long; use Text::CSV_XS; use WebGUI::Session; use WebGUI::Asset::Wobject::Collaboration; use WebGUI::Asset::Post; use WebGUI::Asset::Post::Thread; use WebGUI::User; # Settings and defaults # First, ID || url of parent asset under which all posts will be appended as children my $parentId; my $parentUrl; # Second, username for who asset will be posted by; defaults to admin; can be overridden in import file my $postsUsername = 'admin'; # Third, get path to file that holds import data my $postsFile; # Excape character in Text::CSV_XS is quote, so need to override my $escapeCharacter = '\\'; # Class name for type of asset to add; can be overridden per asset in import file my $className = "WebGUI::Asset::Post"; # Get parameters here, including $help GetOptions( 'configFile=s' => \$configFile, 'help' => \$help, 'man' => \$man, 'quiet' => \$quiet, 'parentId:s' => \$parentId, 'parentUrl:s' => \$parentUrl, 'postsUsername:s' => \$postsUsername, 'postsFile=s' => \$postsFile, 'escapeCharacter:s' => \$escapeCharacter, 'className:s' => \$className ); pod2usage( verbose => 1 ) if $help; pod2usage( verbose => 2 ) if $man; pod2usage( msg => "Must specify a config file!" ) unless $configFile; # Data checks my $errors = 0; unless ($parentId || $parentUrl) { print "Error: must specify ID or url of parent asset\n"; $errors = 1; } unless ($postsFile) { print "Error: must specify import file\n"; $errors = 1; } exit if $errors; my $session = start($webguiRoot,$configFile); # Get parent thread to which posts (comments) will be appended my $parent; if ($parentUrl) { $parent = WebGUI::Asset->newByUrl($session,$parentUrl); } else { $parent = WebGUI::Asset->new($session,$parentId); } print "\n\nStarting up..." unless ($quiet); # Note that Text::CSV_XS default escape character is quote! Need to change, # and need to escape escape character. my $csv = Text::CSV_XS->new({ binary => 1, escape_char => $escapeCharacter }); open my $io, "<", $postsFile or die "$postsFile: $!"; print "OK\n" unless ($quiet); my $lineNumber = 0; my %options = { skipAutoCommitWorkflows => 1, skipNotification=> 1 }; $csv->column_names($csv->getline($io)); while(my $hr = $csv->getline_hr($io)) { my $revisionDate = undef; $lineNumber++; print "Importing item #$lineNumber..." unless ($quiet); # set up general properties that may be overridden by import file my $properties = { className => $className, username => $postsUsername, }; while(my($k,$v)=each %$hr) { if ($k eq "revisionDate" || $k eq "creationDate") { # test for revisionDate which is handled differently $revisionDate = $v; } else { # property values, but skip all together if null data for field if ($v) { $properties->{$k} = $v; } } } # Now, simply append children to parent using WebGUI::Asset->addChild() my $newPost = $parent->addChild($properties, undef, $revisionDate, \%options); print "OK\n" unless ($quiet); ### testing print "Creation Date: $revisionDate\n"; print "Properties:\n"; while(my($k,$v)=each %$properties) { print "$k: $v\n"; } print "\n\n"; ### end testing } $csv->eof or $csv->error_diag; close $io; # Clean up (commit version tag!) print "Cleaning up..." unless ($quiet); finish($session); print "OK\n" unless ($quiet); #---------------------------------------------------------------------------- # Your sub here #---------------------------------------------------------------------------- sub start { my $webguiRoot = shift; my $configFile = shift; my $session = WebGUI::Session->open($webguiRoot,$configFile); $session->user({userId=>3}); my $versionTag = WebGUI::VersionTag->getWorking($session); my $tagName = "Auto Import " . time; $versionTag->set({name => $tagName}); return $session; } #---------------------------------------------------------------------------- sub finish { my $session = shift; my $versionTag = WebGUI::VersionTag->getWorking($session); $versionTag->commit; $session->var->end; $session->close; } __END__ =head1 NAME utility - A template for WebGUI utility scripts =head1 SYNOPSIS utility --configFile config.conf ... utility --help =head1 DESCRIPTION This WebGUI utility script helps you... =head1 ARGUMENTS =head1 OPTIONS =over =item B<--configFile config.conf> The WebGUI config file to use. Only the file name needs to be specified, since it will be looked up inside WebGUI's configuration directory. This parameter is required. =item B<--help> Shows a short summary and usage =item B<--man> Shows this document =back =head1 AUTHOR Copyright 2001-2009 Plain Black Corporation. =cut #vim:ft=perl