--- Begin Message ---
Package: zoph
Version: 0.3.3-8
Severity: wishlist
Hello,
I have made a number of changes to zophExport, which does things the way I
think "they should be done"(TM). I expect some of my changes might be
controversial, and may not be the best solution in any case, but it is my
preference.
My changes are:
* Character set issues already in bug #285018.
* File::Copy is meant to preserve the file date and time (according to my
reading of the documentation), but all files got the current date and time
instead. I found this annoying. (zophImport probably has the same issue). I
replaced the function with my hacked function:
sub copy($$) {
my ($src,$dst) = @_;
if (system("/bin/cp","-pu",$src,$dst)==0) {
return 1;
} else {
return 0;
}
}
(Note: cp -u only copies files that are newer).
* Appending .tmp to the bins export directory may have seemed a good idea, but
prevented me from writing to the directory name I wanted, I changed this.
* I don't like the default of writing photos not in an album to the top
directory, I changed this so they aren't written at all (not extensively
tested).
* I added two new database fields to the "albums" table for exporting:
- name of directory to export to, as directory name doesn't have to be the
same as the title under bins. Note: for the top level directory should
probably be "" (not null). If directory is null, then the name is used
instead.
- sample image id, so a bins album can have a sample image associated with
it.
* I modified zophExport to use these fields. The first field was straight
forward, the second field was kind of complicated to implement. I made the
assumption that the image exists (e.g. if you restrict export to only one album
and the sample image isn't in that album it won't get exported, but the
reference will point to where it should have gone if it was exported).
* I didn't modify anything else to use these fields.
* the $root/album.xml is no longer treated as a special case, it is generated
in the same code as any album.xml.
Issue I haven't fixed:
* Single photo could appear in multiple albums. Ideally this photo should go to
seperate directories. This is kind of yucky. Maybe a symlink could be used to
save disk space? May not help for bins though. Also my sample_image_id
field could be an issue here, too.
I have attached my current version of the patch. This is subject to change
without notice. I would NOT recommend applying it as is, as I don't claim my
solutions are the best solutions.
In keeping with common open source standards, the code is not yet documented
<grin>, if you don't understand anything please ask.
--- cut ---
--- /usr/bin/zophExport 2004-05-03 04:04:03.000000000 +1000
+++ bin/zophExport 2004-12-13 10:46:03.000000000 +1100
@@ -16,7 +16,15 @@
use strict;
use Getopt::Long;
use DBI;
-use File::Copy;
+
+sub copy($$) {
+ my ($src,$dst) = @_;
+ if (system("/bin/cp","-pu",$src,$dst)==0) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
$| = 1;
@@ -197,21 +205,8 @@
or die "Could not make directory: $export_dir\n";
}
- if ($format eq "bins") {
- # make an album.xml for the top dir
- (my $title = $export_dir) =~ s/.*\///;
- $title =~ s/_/ /g;
-
- $export_dir .= ".tmp";
-
- # copy files to a temp directory. bins creates its own copies.
- if (not -d $export_dir) {
- mkdir($export_dir, 0755)
- or die "Could not make directory: $export_dir\n";
- }
-
- makeAlbumXml($export_dir, $title, "-");
-
+ if ($export_dir ne "/") {
+ $export_dir = $export_dir."/";
}
if (not $sql) {
@@ -254,8 +249,7 @@
}
elsif ($format eq "bins") {
print "Now run bins. For example:\n";
- (my $orig_dir = $export_dir) =~ s/\.tmp$//;
- print "bins \"$export_dir\" \"$orig_dir\"\n";
+ print "bins \"$export_dir\" \"new_dir\"\n";
print "When bins finishes, you may delete $export_dir.\n";
}
}
@@ -325,6 +319,61 @@
return $sql;
}
+sub get_album_info {
+ my ($album_id) = @_;
+
+ my @ancestors = getAncestorNames($album_id);
+ my ($person) = @_;
+ my $path = "";
+ my $root_level = 0;
+
+ foreach my $ancestor (@ancestors) {
+ if ($ancestor->{"directory"} ne "") {
+ $path .= $ancestor->{"directory"} . "/";
+ $root_level = $root_level + 1;
+ }
+ $ancestor->{"path"} = $path;
+ $ancestor->{"root_level"} = $root_level;
+ }
+
+ return @ancestors;
+}
+
+sub get_photo_album_id {
+ my ($photo_id) = @_;
+
+ my @album_ids = getRelatedIds($photo_id, "album_id", "photo_albums");
+ if (@album_ids) {
+ return $album_ids[0]; # take first (not always desireable)
+ } else {
+ return undef;
+ }
+}
+
+#
+# Gets the relative path for where particular photo will go.
+#
+sub getRelativePath {
+ my ($photo_id) = @_;
+
+ # if --noalbumDirs was passed, don't make album directories
+ if (not $album_dirs) {
+ return ".";
+ }
+
+ my $album_id = get_photo_album_id($photo_id);
+ if (defined($album_id)) {
+ my $path;
+ my @ancestors = get_album_info($album_id);
+ foreach my $ancestor (@ancestors) {
+ $path = $ancestor->{"path"};
+ }
+ return $path;
+ } else {
+ return undef;
+ }
+}
+
#
# Gets the path for where to put a particular photo.
# Creates new directories if needed.
@@ -337,48 +386,109 @@
return $export_dir;
}
- my $export_path = $export_dir;
-
- my @album_ids = getRelatedIds($photo_id, "album_id", "photo_albums");
- if (@album_ids) {
- my $album_id = $album_ids[0]; # take first (not always desireable)
-
+ my $album_id = get_photo_album_id($photo_id);
+ if (defined($album_id)) {
+ my $export_path;
if (defined($export_hash{$album_id})) {
$export_path = $export_hash{$album_id};
}
else {
- my @ancestors = getAncestorNames($album_id, "album", "albums");
+ my @ancestors = get_album_info($album_id);
foreach my $ancestor (@ancestors) {
- $export_path .= '/' . $ancestor;
-
+ my $title = $ancestor->{"album"};
+ my $sample_image_id = $ancestor->{"sample_image_id"};
+ my $description = $ancestor->{"album_description"};
+ my $path = $ancestor->{"path"};
+ $export_path = $export_dir.$path;
+ my $root_level = $ancestor->{"root_level"};
+
+ my $rebuild_index = 0;
+
if (not -d $export_path) {
mkdir($export_path, 0755)
or die "Could not make directory: $export_path\n";
(my $msg = $export_path) =~ s/\Q$export_dir\E\/?//;
$msg =~ s/\// > /g;
- print "\n$msg\n";
+ print "\n$msg ($title)\n";
+
+ $rebuild_index = 1;
+ }
+
+ if ($export_path eq $export_dir) {
+ $rebuild_index = 1;
+ }
+
+ if (!$rebuild_index and $format eq "bins" and
+ ! -f "$export_path/album.xml" ) {
+ $rebuild_index = 1;
+ }
+ if ($rebuild_index) {
if ($format eq "bins") {
# create an album.xml file
- # this will fail if there are two albums with
- # the same name. This is often empty anyway.
- my $description = getName("'$ancestor'", "album",
- "album_description", "albums", 1);
- if (not $description) { $description = " "; }
-
- makeAlbumXml($export_path, $ancestor, $description);
+ my $sample_image;
+ if (defined($sample_image_id)) {
+ $sql = "SELECT name ".
+ "FROM photos ".
+ "WHERE photo_id=".
+ $dbh->quote($sample_image_id);
+ my $sth = $dbh->prepare($sql);
+ $sth->execute()
+ or die "Error: Could not execute ".
+ "sql: $sql\n";
+
+ my $photo_hash = $sth->fetchrow_hashref();
+
+ if (!defined($photo_hash)) {
+ die "Cannot find sample image
$sample_image_id";
+ }
+
+ my $image_path = getRelativePath($sample_image_id);
+
+ if (!defined($image_path)) {
+ die "Cannot find path for $sample_image_id";
+ }
+
+ my $continue = 1;
+ my $path1 = $path;
+ my $path2 = $image_path;
+ while ($root_level > 0 and $continue) {
+ my $dir1;
+ my $dir2;
+ ($dir1,$path1) =
+ ($path1 =~ /^([^\/]*)\/(.*)$/);
+ ($dir2,$path2) =
+ ($path2 =~ /^([^\/]*)\/(.*)$/);
+
+ if ($dir1 eq $dir2) {
+ $root_level = $root_level - 1;
+ $image_path = $path2;
+ } else {
+ $continue = 0;
+ }
+ }
+
+ $sample_image = "../"x$root_level.
+ $image_path.$photo_hash->{"name"};
+ }
+
+ makeAlbumXml($export_path,
+ $title,
+ $description,
+ $sample_image);
}
}
}
$export_hash{$album_id} = $export_path;
}
+ return $export_path;
+ } else {
+ return undef;
}
-
- return $export_path;
}
#
@@ -561,24 +671,28 @@
}
#
-# Recursively get children ids (for albums or categories).
+# Recursively get children ids for albums.
#
sub getAncestorNames {
- my ($id, $field, $table) = @_;
+ my ($id) = @_;
my $sql =
- "select parent_$field" . "_id, $field from $table where $field" . "_id
= " .
- $dbh->quote($id);
+ "select parent_album_id, album_id, ".
+ "album, directory, sample_image_id, album_description ".
+ "from albums where album_id = " . $dbh->quote($id);
my $sth = $dbh->prepare($sql);
$sth->execute() or die "Error: Could not execute sql: $sql\n";
my @ancestors = ();
- if (my ($parent_id, $name) = $sth->fetchrow_array()) {
- if ($parent_id > 0) {
- push @ancestors, getAncestorNames($parent_id, $field, $table);
- push @ancestors, $name;
+ if (my $album_hash = $sth->fetchrow_hashref()) {
+ if ($album_hash->{"parent_album_id"} > 0) {
+ push @ancestors,
getAncestorNames($album_hash->{"parent_album_id"});
}
+ if (!defined($album_hash->{"directory"})) {
+ $album_hash->{"directory"} = $album_hash->{"album"};
+ }
+ push @ancestors, $album_hash;
}
return @ancestors;
@@ -615,6 +729,7 @@
my $path = $photo_hash->{'path'};
my $export_path = getExportPath($photo_id);
+ if (!defined($export_path)) { return };
unless (copy("$image_dir/$path/$name", "$export_path/$name")) {
warn "Could not copy file: $image_dir/$path/$name\n";
@@ -693,13 +808,14 @@
my $path = $photo_hash->{'path'};
my $export_path = getExportPath($photo_id);
+ if (!defined($export_path)) { return };
unless (copy("$image_dir/$path/$name", "$export_path/$name")) {
warn "Could not copy file: $image_dir/$path/$name\n";
return;
}
- open IMAGE_XML, ">$export_path/$name.xml"
+ open IMAGE_XML, "| recode 'ISO-8859-1..utf-8' >$export_path/$name.xml"
or die "Could not open $name.xml file\n";
print IMAGE_XML "<?xml version=\"1.0\" encoding=\"UTF-8\"?><image>\n";
@@ -764,6 +880,7 @@
my $path = $photo_hash->{'path'};
my $export_path = getExportPath($photo_id);
+ if (!defined($export_path)) { return };
unless (copy("$image_dir/$path/$name", "$export_path/$name")) {
warn "Could not copy file: $image_dir/$path/$name\n";
@@ -932,20 +1049,36 @@
# Makes an album.xml file for the BINS program.
#
sub makeAlbumXml {
- my ($path, $title, $description) = @_;
+ my ($path, $title, $description, $sample_image) = @_;
- if (open ALBUM_XML, ">$path/album.xml") {
+ if (open ALBUM_XML, "| recode 'ISO-8859-1..utf-8' >$path/album.xml") {
print ALBUM_XML <<"(XML)";
<?xml version="1.0" encoding="UTF-8"?>
<album>
<description>
+(XML)
+
+ if (defined($description)) {
+ print ALBUM_XML <<"(XML)";
<field name="longdesc">
$description
</field>
+(XML)
+ }
+ print ALBUM_XML <<"(XML)";
<field name="title">
$title
</field>
+(XML)
+ if (defined($sample_image)) {
+ print ALBUM_XML <<"(XML)";
+ <field name="sampleimage">
+ $sample_image
+ </field>
+(XML)
+}
+ print ALBUM_XML <<"(XML)";
</description>
<bins>
</bins>
--- cut ---
-- System Information:
Debian Release: 3.1
APT prefers testing
APT policy: (500, 'testing')
Architecture: i386 (i686)
Kernel: Linux 2.4.26
Locale: LANG=C, LC_CTYPE=C (charmap=ANSI_X3.4-1968)
Versions of packages zoph depends on:
ii apache 1.3.33-2 Versatile, high-performance HTTP s
ii imagemagick 6:6.0.6.2-1.5 Image manipulation programs
ii jhead 2.2-1 Manipulate the non-image part of E
ii libdbd-mysql-perl 2.9003-3 A Perl5 database interface to the
ii libdbi-perl 1.45-1 The Perl5 Database Interface by Ti
ii libimage-size-perl 2.992-1 determine the size of images in se
ii mysql-common 4.0.22-2 mysql database common files (e.g.
ii mysql-server 4.0.22-2 mysql database server binaries
ii perl 5.8.4-3 Larry Wall's Practical Extraction
ii php4 4:4.3.9-1 server-side, HTML-embedded scripti
ii php4-gd2 [php4-gd] 3:4.3.2+rc3-2 GD module (with GD2) for php4
ii php4-mysql 4:4.3.9-1 MySQL module for php4
ii unzip 5.51-2 De-archiver for .zip files
ii wwwconfig-common 0.0.41 Debian web auto configuration
-- no debconf information
--- End Message ---