/usr/bin/wg-galleryImport is in webgui 7.10.29-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | #!/usr/bin/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
# -------------------------------------------------------------------
use strict;
use File::Basename ();
use File::Spec;
my $webguiRoot;
BEGIN {
$webguiRoot = '/usr/share/webgui';
unshift @INC, File::Spec->catdir($webguiRoot, 'lib');
}
$|=1;
use Carp qw( carp croak );
use File::Find;
use Getopt::Long;
use Pod::Usage;
use Scalar::Util qw( blessed );
use WebGUI::Asset::Wobject::Collaboration;
use WebGUI::Asset::Wobject::GalleryAlbum;
use WebGUI::Asset::Wobject::Gallery;
use WebGUI::Asset::Wobject::Folder;
use WebGUI::Asset::Post::Thread;
use WebGUI::Storage;
# custom flags
my ($fromAssetId, $fromPath, $fromAssetUrl, $toId, $toUrl) = undef;
my $tags = [];
# init
my $session = start();
# load gallery
my $gallery = undef;
if (defined $toId) {
$gallery = WebGUI::Asset::Wobject::Gallery->new($session, $toId);
}
else {
$gallery = WebGUI::Asset::Wobject::Gallery->newByUrl($session, $toUrl);
}
if ( $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery') ) {
# import from
if (defined $fromPath) {
if (-d $fromPath) {
addAlbumFromFilesystem($gallery,$fromPath);
}
else {
pod2usage("$0: You must specify a valid directory to import from.");
}
}
else {
my $fromAsset = undef;
if (defined $fromAssetId) {
$fromAsset = WebGUI::Asset->newByDynamicClass($session, $fromAssetId);
}
else {
$fromAsset = WebGUI::Asset->newByUrl($session, $fromAssetUrl);
}
if ($fromAsset && $fromAsset->isa("WebGUI::Asset::Wobject::Folder")) {
addAlbumFromFolder($gallery, $fromAsset);
}
elsif ($fromAsset && $fromAsset->isa("WebGUI::Asset::Wobject::Collaboration")) {
addAlbumFromCollaboration($gallery, $fromAsset);
}
elsif ($fromAsset && $fromAsset->isa("WebGUI::Asset::Post::Thread")) {
addAlbumFromThread($gallery, $fromAsset);
}
else {
pod2usage("$0: You must specify a valid asset to import from.");
}
}
}
else {
pod2usage("$0: You must specify a gallery asset to import into.");
}
# cleanup
finish($session);
#----------------------------------------------------------------------------
# addAlbumFromCollaboration ( gallery, collab )
#
# Add an album or albums to the gallery from the given Collaboration System.
# gallery is an instanciated Gallery asset. collab is an instanciated
# Collaboration System asset.
#
# Will add one album for every thread in the Collaboration System. Will call
# addAlbumFromThread to do its dirty work.
sub addAlbumFromCollaboration {
my $gallery = shift;
my $collab = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Collaboration System asset"
unless blessed $collab && $collab->isa('WebGUI::Asset::Wobject::Collaboration');
my $threads
= $collab->getLineage(['descendants'], {
returnObjects => 1,
includeOnlyClasses => ['WebGUI::Asset::Post::Thread'],
statesToInclude => ['published'],
statusToInclude => ['approved', 'archived', 'pending'],
});
for my $thread ( @$threads ) {
addAlbumFromThread( $gallery, $thread );
}
return undef;
}
#----------------------------------------------------------------------------
# addAlbumFromFilesystem ( gallery, root )
#
# Add an album to the gallery from the file system. gallery is an
# instanciated Gallery asset. root is a location on the file system.
sub addAlbumFromFilesystem {
my $gallery = shift;
my $root = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be a path to a folder on the filesystem"
unless -d $root;
# define the import process
my $recurseFilesystem = sub {
if (-d $File::Find::name) {
# find photos
print "Searching ".$File::Find::name." for photos.\n";
my @photos = ();
if (opendir my $folder, $File::Find::name) {
my @files = readdir $folder;
closedir $folder;
foreach my $file (@files) {
unless (-d $File::Find::name."/".$file) {
if ($file =~ m/\.(jpg|gif|png|jpeg)$/i) {
push(@photos, $file);
}
}
}
}
# import if we found anything
if (scalar(@photos)) {
# get album name
my $albumName = $File::Find::name;
$albumName =~ s{.*\/([A-Za-z0-9\.-_'\,\s]+)$}{$1};
# create album
print "Creating album $albumName\n";
my $addOptions = { skipAutoCommitWorkflows => 1 };
my $album = $gallery->addChild({
className => 'WebGUI::Asset::Wobject::GalleryAlbum',
menuTitle => $albumName,
title => $albumName,
url => $gallery->get('url') . "/" . $albumName,
keywords => $tags,
}, undef, undef, $addOptions );
# do the import
print "\tImporting ".scalar(@photos)." files from ".$File::Find::name." into album called ".$albumName.".\n";
foreach my $filename (@photos) {
print "\t\t".$filename."\n";
my $className = $gallery->getAssetClassForFile( $filename );
if ( !$className ) {
warn "Skipping $filename because Gallery doesn't handle this file type";
next;
}
my ($title) = $filename =~ m{(.*)\.[^.]*$};
my $asset = $album->addChild({
className => $className,
menuTitle => $title,
title => $title,
url => $album->get('url') . "/" . $title,
keywords => $tags,
}, undef, undef, $addOptions );
$asset->setFile( $File::Find::name."/".$filename );
}
}
}
};
# run the search and import process
File::Find::find({wanted=>$recurseFilesystem,no_chdir=>1} , $root);
return undef;
}
#----------------------------------------------------------------------------
# addAlbumFromFolder ( gallery, folder )
#
# Add an album from a Folder asset filled with File assets. gallery is an
# instance of a Gallery asset. folder is an instance of a Folder asset.
sub addAlbumFromFolder {
my $gallery = shift;
my $folder = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Folder asset"
unless blessed $folder && $folder->isa('WebGUI::Asset::Wobject::Folder');
my $session = $gallery->session;
my $addOptions = { skipAutoCommitWorkflows => 1 };
# Create the new album
my $album = $gallery->addChild({
className => 'WebGUI::Asset::Wobject::GalleryAlbum',
description => $folder->get('description'),
menuTitle => $folder->get('menuTitle'),
createdBy => $folder->get('createdBy'),
creationDate => $folder->get('creationDate'),
ownerUserId => $folder->get('ownerUserId'),
synopsis => $folder->get('synopsis'),
title => $folder->get('title'),
keywords => $tags,
url => $gallery->get('url') . "/" . $folder->get('title'),
}, undef, $folder->get('revisionDate'), $addOptions );
my $fileIds
= $folder->getLineage( ['children'], {
joinClass => 'WebGUI::Asset::File',
} );
for my $fileId ( @{ $fileIds } ) {
my $oldFile = WebGUI::Asset->newByDynamicClass( $session, $fileId );
my $oldStorage = $oldFile->getStorageLocation;
my $className = $gallery->getAssetClassForFile( $oldStorage->getPath( $oldFile->get('filename') ) );
if ( !$className ) {
warn "Skipping " . $oldFile->get('filename') . " Gallery doesn't handle this file type";
next;
}
my $newFile = $album->addChild({
className => $className,
createdBy => $oldFile->get('createdBy'),
creationDate => $oldFile->get('creationDate'),
menuTitle => $oldFile->get('menuTitle'),
ownerUserId => $oldFile->get('ownerUserId'),
synopsis => $oldFile->get('synopsis'),
keywords => $tags,
title => $oldFile->get('title'),
url => $album->get('url') . "/" . $oldFile->get('menuTitle'),
}, undef, $oldFile->get('revisionDate'), $addOptions );
$newFile->setFile( $oldStorage->getPath( $oldFile->get('filename') ) );
}
return undef;
}
#----------------------------------------------------------------------------
# addAlbumFromThread ( gallery, thread )
#
# Add an album to the gallery from the given Collaboration System thread.
# gallery is an instanciated Gallery asset. thread is an instanciated
# Thread asset.
sub addAlbumFromThread {
my $gallery = shift;
my $thread = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Thread asset"
unless blessed $thread && $thread->isa('WebGUI::Asset::Post::Thread');
my $session = $gallery->session;
my $addOptions = { skipAutoCommitWorkflows => 1 };
# Create the new album
my $album = $gallery->addChild({
className => 'WebGUI::Asset::Wobject::GalleryAlbum',
description => $thread->get('content'),
menuTitle => $thread->get('menuTitle'),
createdBy => $thread->get('createdBy'),
creationDate => $thread->get('creationDate'),
ownerUserId => $thread->get('ownerUserId'),
synopsis => $thread->get('synopsis'),
keywords => $tags,
title => $thread->get('title'),
url => $gallery->get('url') . "/" . $thread->get('title'),
userDefined1 => $thread->get('userDefined1'),
userDefined2 => $thread->get('userDefined2'),
userDefined3 => $thread->get('userDefined3'),
userDefined4 => $thread->get('userDefined4'),
userDefined5 => $thread->get('userDefined5'),
}, undef, $thread->get('revisionDate'), $addOptions );
for my $post ( @{ $thread->getPosts } ) {
if ( my $storageId = $post->get('storageId') ) {
# Use WebGUI::Storage to avoid thumbnails if there
my $storage = WebGUI::Storage->get( $session, $storageId );
for my $filename ( @{$storage->getFiles} ) {
my $className = $gallery->getAssetClassForFile( $filename );
if ( !$className ) {
warn "Skipping $filename because Gallery doesn't handle this file type";
next;
}
# Get rid of that file extention
my ($title) = $filename =~ m{(.*)\.[^.]*$};
# Don't repeat the thread
my $synopsis
= $post->get('content') ne $thread->get('content')
? $post->get('content')
: undef
;
my $file = $album->addChild({
className => $className,
createdBy => $post->get('createdBy'),
creationDate => $post->get('creationDate'),
menuTitle => $title,
ownerUserId => $post->get('ownerUserId'),
synopsis => $synopsis,
title => $title,
url => $album->get('url') . "/" . $title,
keywords => $tags,
userDefined1 => $post->get('userDefined1'),
userDefined2 => $post->get('userDefined2'),
userDefined3 => $post->get('userDefined3'),
userDefined4 => $post->get('userDefined4'),
userDefined5 => $post->get('userDefined5'),
}, undef, $post->get('revisionDate'), $addOptions );
$file->setFile( $storage->getPath( $filename ) );
}
}
}
return undef;
}
#----------------------------------------------------------------------------
sub finish {
my $session = shift;
my $versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->commit;
$session->var->end;
$session->close;
}
#----------------------------------------------------------------------------
sub start {
$| = 1; #disable output buffering
my ($configFile, $help);
GetOptions(
'configFile=s' => \$configFile,
'help' => \$help,
'tags=s{1,10}' => $tags,
'toUrl=s' => \$toUrl,
'fromAssetUrl=s' => \$fromAssetUrl,
'toId=s' => \$toId,
'fromAssetId=s' => \$fromAssetId,
'fromPath=s' => \$fromPath,
);
# Show usage
if ($help) {
pod2usage( verbose => 2);
}
unless ($configFile) {
pod2usage("$0: Must specify a --configFile");
}
my $session = WebGUI::Session->open("/usr/share/webgui",$configFile);
$session->user({userId=>3});
my $versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->set({name => 'Import Albums into Gallery'});
return $session;
}
__END__
=head1 NAME
galleryImport.pl - Import media into a Gallery asset from various sources.
=head1 SYNOPSIS
perl galleryImport.pl --configFile=www.example.com.conf --fromAssetId=XXXXXXXXXXXXXXXXXXXXXX --toId=XXXXXXXXXXXXXXXXXXXXXX
perl galleryImport.pl --help
=head1 DESCRIPTION
This WebGUI utility script imports files from the filesystem, and other assets
into a Gallery asset. It automatically generates thumbnails and metadata just
as if the files were uploaded through the user interface.
Files with JPG, JPEG, GIF, and PNG extensions are supported.
The thumbnails are created using L<Image::Magick> for image transformations.
Exactly one --from* and exactly one --to* parameter are required.
=over
=item B<--configFile filename>
Specify the config file name of the site you wish to perform this import on.
=item B<--fromAssetId assetId>
Specify the asset id of an asset to import the files from. The asset type is
automatically discerned.
The supported asset types are Collaboration System, Thread, and Folder.
=item B<--fromAssetUrl url>
Specify the URL of an asset to import the files from. The asset type is
automatically discerned. The B<url> is the Asset URL parameter, not a fully
qualified URL.
The supported asset types are Collaboration System, Thread, and Folder.
=item B<--fromPath path>
Specify the absolute B<path> to a folder containing folders images and other
folders with images. The folder name is used to create an album name, and the
files contained in the folder are added as photos in the folder.
=item B<--help>
Shows this documentation, then exits.
=item B<--tags keyword keyword keyword>
Attach keyword tags to the photos and albums created. One to 10 keywords may
be specified. You may specify multi-word tags by wrapping them in quotes.
Eg: --tags=green old "very expensive"
=item B<--toId assetId>
Specify the B<assetId> of the Gallery to create albums in.
=item B<--toUrl url>
Specify the B<url> of the Gallery to create albums in. The URL is the asset
URL parameter of the Gallery, and not the fully qualified URL.
=back
=head1 AUTHOR
Copyright 2001-2009 Plain Black Corporation.
=cut
|