root/gsdl/trunk/bin/script/buildcol.pl @ 14942

Revision 14942, 24.7 KB (checked in by davidb, 12 years ago)

Modification to support extensions. BEGIN block now adds perllib/ perllib/cpan to @INC for each extension.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3## @file buildcol.pl
4# This program will build a particular collection.
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22#
23# @note 11/04/03 Added usage datastructure - John Thompson
24#
25# @author New Zealand Digital Library Project unless otherwise stated
26# @copy 1999 New Zealand Digital Library Project
27#
28package buildcol;
29
30BEGIN {
31    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
32    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
33    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
34    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
35    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/XML/XPath");
36    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
37    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
38
39    if (defined $ENV{'GSDLEXTS'}) {
40    my @extensions = split(/:/,$ENV{'GSDLEXTS'});
41    foreach my $e (@extensions) {
42        my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
43
44        unshift (@INC, "$ext_prefix/perllib");
45        unshift (@INC, "$ext_prefix/perllib/cpan");
46    }
47    }
48}
49
50use colcfg;
51use util;
52use scriptutil;
53use FileHandle;
54use gsprintf;
55use printusage;
56use parse2;
57
58use strict;
59no strict 'refs'; # allow filehandles to be variables and vice versa
60no strict 'subs'; # allow barewords (eg STDERR) as function arguments
61
62# used in updating Collectionconfig.xml in create_images()
63use Twig;
64
65my $mode_list =
66    [ { 'name' => "all",
67        'desc' => "{buildcol.mode.all}" },
68      { 'name' => "compress_text",
69        'desc' => "{buildcol.mode.compress_text}" },
70      { 'name' => "build_index",
71        'desc' => "{buildcol.mode.build_index}" },
72      { 'name' => "infodb",
73        'desc' => "{buildcol.mode.infodb}" } ];
74
75my $sec_index_list =
76    [ {'name' => "never",
77       'desc' => "{buildcol.sections_index_document_metadata.never}" },
78      {'name' => "always",
79       'desc' => "{buildcol.sections_index_document_metadata.always}" },
80      {'name' => "unless_section_metadata_exists",
81       'desc' => "{buildcol.sections_index_document_metadata.unless_section_metadata_exists}" }
82      ];
83
84my $arguments =
85    [ { 'name' => "remove_empty_classifications",
86    'desc' => "{buildcol.remove_empty_classifications}",
87    'type' => "flag",
88    'reqd' => "no",
89    'modegli' => "2" },
90      { 'name' => "archivedir",
91    'desc' => "{buildcol.archivedir}",
92    'type' => "string",
93    'reqd' => "no",
94        'hiddengli' => "yes" },
95      { 'name' => "builddir",
96    'desc' => "{buildcol.builddir}",
97    'type' => "string",
98    'reqd' => "no",
99        'hiddengli' => "yes" },
100#     { 'name' => "cachedir",
101#   'desc' => "{buildcol.cachedir}",
102#   'type' => "string",
103#   'reqd' => "no" },
104      { 'name' => "collectdir",
105    'desc' => "{buildcol.collectdir}",
106    'type' => "string",
107    # parsearg left "" as default
108    #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
109    'reqd' => "no",
110        'hiddengli' => "yes" },
111      { 'name' => "site",
112    'desc' => "{buildcol.site}",
113    'type' => "string",
114    'deft' => "",
115    'reqd' => "no",
116        'hiddengli' => "yes" },
117      { 'name' => "create_images",
118    'desc' => "{buildcol.create_images}",
119    'type' => "flag",
120    'reqd' => "no",
121    'modegli' => "4" },
122      { 'name' => "debug",
123    'desc' => "{buildcol.debug}",
124    'type' => "flag",
125    'reqd' => "no",
126        'hiddengli' => "yes" },
127      { 'name' => "faillog",
128    'desc' => "{buildcol.faillog}",
129    'type' => "string",
130    # parsearg left "" as default
131    #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
132    'reqd' => "no",
133    'modegli' => "4" },
134      { 'name' => "index",
135    'desc' => "{buildcol.index}",
136    'type' => "string",
137    'reqd' => "no",
138    'modegli' => "4" },
139      { 'name' => "incremental",
140    'desc' => "{buildcol.incremental}",
141    'type' => "flag",
142    'hiddengli' => "yes" },
143      { 'name' => "keepold",
144    'desc' => "{buildcol.keepold}",
145    'type' => "flag",
146    'reqd' => "no",
147        #'modegli' => "3",
148    'hiddengli' => "yes" },
149      { 'name' => "removeold",
150    'desc' => "{buildcol.removeold}",
151    'type' => "flag",
152    'reqd' => "no",
153    #'modegli' => "3",
154    'hiddengli' => "yes"  },
155      { 'name' => "language",
156    'desc' => "{scripts.language}",
157    'type' => "string",
158    'reqd' => "no",
159    'modegli' => "4" },
160      { 'name' => "maxdocs",
161    'desc' => "{buildcol.maxdocs}",
162    'type' => "int",
163    'reqd' => "no",
164        'hiddengli' => "yes" },
165      { 'name' => "maxnumeric",
166    'desc' => "{buildcol.maxnumeric}",
167    'type' => "int",
168    'reqd' => "no",
169    'deft' => "4",
170    'range' => "4,512",
171    'modegli' => "3" },
172      { 'name' => "mode",
173    'desc' => "{buildcol.mode}",
174    'type' => "enum",
175    'list' => $mode_list,
176    # parsearg left "" as default
177#   'deft' => "all",
178    'reqd' => "no",
179    'modegli' => "4" },
180      { 'name' => "no_strip_html",
181    'desc' => "{buildcol.no_strip_html}",
182    'type' => "flag",
183    'reqd' => "no",
184    'modegli' => "4" },
185      { 'name' => "no_text",
186    'desc' => "{buildcol.no_text}",
187    'type' => "flag",
188    'reqd' => "no",
189    'modegli' => "3" },
190      { 'name' => "sections_index_document_metadata",
191    'desc' => "{buildcol.sections_index_document_metadata}",
192    'type' => "enum",
193    'list' => $sec_index_list,
194    'reqd' => "no",
195    'modegli' => "3" },
196      { 'name' => "out",
197    'desc' => "{buildcol.out}",
198    'type' => "string",
199    'deft' => "STDERR",
200    'reqd' => "no",
201        'hiddengli' => "yes" },
202      { 'name' => "verbosity",
203    'desc' => "{buildcol.verbosity}",
204    'type' => "int",
205    # parsearg left "" as default
206    #'deft' => "2",
207    'reqd' => "no",
208    'modegli' => "4" },
209      { 'name' => "gli",
210    'desc' => "",
211    'type' => "flag",
212    'reqd' => "no",
213    'hiddengli' => "yes" },
214      { 'name' => "xml",
215    'desc' => "{scripts.xml}",
216    'type' => "flag",
217    'reqd' => "no",
218    'hiddengli' => "yes" },
219      { 'name' => "disable_OAI",
220          'desc' => "{buildcol.disable_OAI}",
221          'type' => "flag",
222          'reqd' => "no",
223          'modegli' => "2",
224      'hiddengli' => "yes" }
225
226#      { 'name' => "incremental_dlc",
227#   'desc' => "{buildcol.incremental_dlc}",
228#   'type' => "flag",
229#   'reqd' => "no",
230#        'hiddengli' => "yes" }
231      ];
232
233my $options = { 'name' => "buildcol.pl",
234        'desc' => "{buildcol.desc}",
235        'args' => $arguments };
236
237
238# globals
239my $collection;
240my $configfilename;
241my $out;
242
243# used to signify "gs2"(default) or "gs3"
244my $gs_mode = "gs2";
245
246## @method gsprintf()
247#  Print a string to the screen after looking it up from a locale dependant
248#  strings file. This function is losely based on the idea of resource
249#  bundles as used in Java.
250#
251#  @param  $error The STDERR stream.
252#  @param  $text The string containing GS keys that should be replaced with
253#                their locale dependant equivilents.
254#  @param  $out The output stream.
255#  @return The locale-based string to output.
256#
257sub gsprintf()
258{
259    return &gsprintf::gsprintf(@_);
260}
261## gsprintf() ##
262
263&main();
264
265## @method main()
266#
267#  [Parses up and validates the arguments to the build process before creating
268#  the appropriate build process to do the actual work - John]
269#
270#  @note Added true incremental support - John Thompson, DL Consulting Ltd.
271#  @note There were several bugs regarding using directories other than
272#        "import" or "archives" during import and build quashed. - John
273#        Thompson, DL Consulting Ltd.
274#
275#  @param  $incremental_dlc If true indicates this build should not regenerate all
276#                       the index and metadata files, and should instead just
277#                       append the information found in the archives directory
278#                       to the existing files. If this requires some complex
279#                       work so as to correctly insert into a classifier so be
280#                       it. Of course none of this is done here - instead the
281#                       incremental argument is passed to the document
282#                       processor.
283#
284sub main
285{
286    # command line args
287    my ($verbosity, $archivedir, $cachedir, $builddir, $site, $maxdocs,
288    $debug, $mode, $indexname, $removeold, $keepold, $incremental,
289    $remove_empty_classifications,
290    $create_images, $collectdir, $build, $type, $textindex,
291    $no_strip_html, $no_text, $faillog, $gli, $index, $language,
292    $sections_index_document_metadata, $maxnumeric, $incremental_dlc,
293    $disable_OAI);
294
295    my $xml = 0;
296    $incremental_dlc = 0; # this is hidden for now as it doesn't work for standard greenstone at the moment
297    my $hashParsingResult = {};
298    # general options available to all plugins
299    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
300
301    # If parse returns -1 then something has gone wrong
302    if ($intArgLeftinAfterParsing == -1)
303    {
304    &PrintUsage::print_txt_usage($options, "{buildcol.params}");
305    die "\n";
306    }
307   
308    foreach my $strVariable (keys %$hashParsingResult)
309    {
310    eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
311    }
312
313    # If $language has been specified, load the appropriate resource bundle
314    # (Otherwise, the default resource bundle will be loaded automatically)
315    if ($language && $language =~ /\S/) {
316    &gsprintf::load_language_specific_resource_bundle($language);
317    }
318
319    if ($xml) {
320        &PrintUsage::print_xml_usage($options);
321    print "\n";
322    return;
323    }
324
325    if ($gli) { # the gli wants strings to be in UTF-8
326    &gsprintf::output_strings_in_UTF8;
327    }
328
329    # now check that we had exactly one leftover arg, which should be
330    # the collection name. We don't want to do this earlier, cos
331    # -xml arg doesn't need a collection name
332    # Or if the user specified -h, then we output the usage also
333    if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
334    {
335    &PrintUsage::print_txt_usage($options, "{buildcol.params}");
336    die "\n";
337    }
338   
339    $textindex = "";
340    my $close_out = 0;
341    if ($out !~ /^(STDERR|STDOUT)$/i) {
342    open (OUT, ">$out") ||
343        (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
344    $out = "buildcol::OUT";
345    $close_out = 1;
346    }
347    $out->autoflush(1);
348
349    # get and check the collection
350    if (($collection = &colcfg::use_collection($site, @ARGV, $collectdir)) eq "") {
351    &PrintUsage::print_txt_usage($options, "{buildcol.params}");
352    die "\n";
353    }
354
355    if ($faillog eq "") {
356    $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
357    }
358    # note that we're appending to the faillog here (import.pl clears it each time)
359    # this could potentially create a situation where the faillog keeps being added
360    # to over multiple builds (if the import process is being skipped)
361    open (FAILLOG, ">>$faillog") ||
362    (&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
363    $faillog = 'buildcol::FAILLOG';
364    $faillog->autoflush(1);
365
366    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
367    # Don't know why this didn't already happen, but now collection specific
368    # classify and plugins directory also added to include path
369    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/classify");
370    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/plugins");
371
372    # Read in the collection configuration file.
373    my ($collectcfg, $buildtype);
374    ($configfilename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
375    if ($gs_mode eq "gs2") {
376        $collectcfg = &colcfg::read_collect_cfg ($configfilename);
377    } elsif ($gs_mode eq "gs3") {
378    $collectcfg = &colcfg::read_collection_cfg_xml ($configfilename);
379    }
380
381    if ($verbosity !~ /\d+/) {
382    if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
383        $verbosity = $collectcfg->{'verbosity'};
384    } else {
385        $verbosity = 2; # the default
386    }
387    }
388    # we use searchtype for determining buildtype, but for old versions, use buildtype
389    if (defined $collectcfg->{'buildtype'}) {
390    $buildtype = $collectcfg->{'buildtype'};
391    } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
392    $buildtype = "mgpp";
393    } else {
394    $buildtype = "mg"; #mg is the default
395    }
396    if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
397    $archivedir = $collectcfg->{'archivedir'};
398    }
399    if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
400    $cachedir = $collectcfg->{'cachedir'};
401    }
402    if (defined $collectcfg->{'builddir'} && $builddir eq "") {
403    $builddir = $collectcfg->{'builddir'};
404    }
405    if ($maxdocs !~ /\-?\d+/) {
406    if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
407        $maxdocs = $collectcfg->{'maxdocs'};
408    } else {
409        $maxdocs = -1; # the default
410    }
411    }
412    if (defined $collectcfg->{'maxnumeric'} && $collectcfg->{'maxnumeric'} =~ /\d+/) {
413    $maxnumeric = $collectcfg->{'maxnumeric'};
414    }
415   
416    if ($maxnumeric < 4 || $maxnumeric > 512) {
417    $maxnumeric = 4;
418    }
419   
420    if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
421    $debug = 1;
422    }
423    if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
424    if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
425        $mode = $collectcfg->{'mode'};
426    } else {
427        $mode = "all"; # the default
428    }
429    }
430    if (defined $collectcfg->{'index'} && $indexname eq "") {
431    $indexname = $collectcfg->{'index'};
432    }
433    if (defined $collectcfg->{'no_text'} && $no_text == 0) {
434    if ($collectcfg->{'no_text'} =~ /^true$/i) {
435        $no_text = 1;
436    }
437    }
438    if (defined $collectcfg->{'no_strip_html'} && $no_strip_html == 0) {
439    if ($collectcfg->{'no_strip_html'} =~ /^true$/i) {
440        $no_strip_html = 1;
441    }
442    }
443    if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
444    if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
445        $remove_empty_classifications = 1;
446    }
447    }
448   
449   
450    if (defined $collectcfg->{'create_images'} && $collectcfg->{'create_images'} =~ /^true$/i) {
451    $create_images = 1;
452    }
453    if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
454    $textindex = $collectcfg->{'textcompress'};
455    }
456    if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
457    $gli = 1;
458    }
459
460    if ($sections_index_document_metadata !~ /\S/ && defined $collectcfg->{'sections_index_document_metadata'}) {
461    $sections_index_document_metadata = $collectcfg->{'sections_index_document_metadata'};
462    }
463   
464    if ($sections_index_document_metadata !~ /^(never|always|unless_section_metadata_exists)$/) {
465    $sections_index_document_metadata = "never";
466    }
467   
468    ($removeold, $keepold, $incremental) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, $incremental, "building", $collectcfg);
469 
470    $gli = 0 unless defined $gli;
471
472    # If the disable_OAI flag is not present, the option $disable_OAI with the value of 0 will be passed to basebuilder.pm
473    $disable_OAI = 0 unless defined $disable_OAI;
474   
475    # New argument to track whether build is incremental
476    $incremental = 0 unless defined $incremental;
477
478    print STDERR "<Build>\n" if $gli;
479
480    #set the text index
481    if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
482    if ($textindex eq "") {
483        $textindex = "text";
484    }
485    }
486    else {
487    $textindex = "section:text";
488    }
489
490    # create default images if required
491    if ($create_images) {
492    my $collection_name = $collection;
493    $collection_name = $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'}
494    if defined $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'};
495    &create_images ($collection_name);
496    }
497
498    # fill in the default archives and building directories if none
499    # were supplied, turn all \ into / and remove trailing /
500
501    my ($realarchivedir, $realbuilddir);
502    # Modified so that the archivedir, if provided as an argument, is made
503    # absolute if it isn't already
504    if ($archivedir eq "")
505      {
506        $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
507      }
508    else
509      {
510        $archivedir = &make_absolute($ENV{'GSDLCOLLECTDIR'}, $archivedir);
511      }
512    # End Mod
513    $archivedir =~ s/[\\\/]+/\//g;
514    $archivedir =~ s/\/$//;
515    $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building") if $builddir eq "";
516    $builddir =~ s/[\\\/]+/\//g;
517    $builddir =~ s/\/$//;
518
519    # update the archive cache if needed
520    if ($cachedir) {
521    &gsprintf($out, "{buildcol.updating_archive_cache}\n")
522        if ($verbosity >= 1);
523
524    $cachedir =~ s/[\\\/]+$//;
525    $cachedir .= "/collect/$collection" unless
526        $cachedir =~ /collect\/$collection/;
527
528    $realarchivedir = "$cachedir/archives";
529    $realbuilddir = "$cachedir/building";
530    &util::mk_all_dir ($realarchivedir);
531    &util::mk_all_dir ($realbuilddir);
532    &util::cachedir ($archivedir, $realarchivedir, $verbosity);
533
534    } else {
535    $realarchivedir = $archivedir;
536    $realbuilddir = $builddir;
537    }
538
539    # build it in realbuilddir
540    &util::mk_all_dir ($realbuilddir);
541
542    my ($buildertype, $builderdir,  $builder);
543    # if a builder class has been created for this collection, use it
544    # otherwise, use the mg or mgpp builder
545    if (-e "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib/custombuilder.pm") {
546    $builderdir = "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib";
547    $buildertype = "custombuilder";
548    } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/custombuilder.pm") {
549    $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
550    $buildertype = "custombuilder";
551    } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
552    $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
553    $buildertype = "${collection}builder";
554    } else {
555    $builderdir = "$ENV{'GSDLHOME'}/perllib";
556    if ($buildtype eq "lucene") {
557        $buildertype = "lucenebuilder";
558    }
559    elsif ($buildtype eq "mgpp") {
560        $buildertype = "mgppbuilder";
561    }
562    else {
563        $buildertype = "mgbuilder";
564    }
565    }
566   
567    require "$builderdir/$buildertype.pm";
568
569    eval("\$builder = new $buildertype(\$collection, " .
570     "\$realarchivedir, \$realbuilddir, \$verbosity, " .
571     "\$maxdocs, \$debug, \$keepold, \$incremental, \$incremental_dlc, " .
572     "\$remove_empty_classifications, " .
573     "\$out, \$no_text, \$faillog, \$gli, \$disable_OAI)");
574    die "$@" if $@;
575
576    $builder->init();
577    $builder->set_maxnumeric($maxnumeric);
578   
579    if (($buildertype eq "mgppbuilder") && $no_strip_html) {
580    $builder->set_strip_html(0);
581    }
582    if ($sections_index_document_metadata ne "never") {
583    $builder->set_sections_index_document_metadata($sections_index_document_metadata);
584    }
585       
586    if ($mode =~ /^all$/i) {
587    $builder->compress_text($textindex);
588    $builder->build_indexes($indexname);
589    $builder->make_infodatabase();
590    $builder->collect_specific();
591    } elsif ($mode =~ /^compress_text$/i) {
592    $builder->compress_text($textindex);
593    } elsif ($mode =~ /^build_index$/i) {
594    $builder->build_indexes($indexname);   
595    } elsif ($mode =~ /^infodb$/i) {
596    $builder->make_infodatabase();
597    } else {
598    (&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
599    }
600
601    $builder->make_auxiliary_files() if !$debug;
602    $builder->deinit();
603   
604    if (($realbuilddir ne $builddir) && !$debug) {
605    &gsprintf($out, "{buildcol.copying_back_cached_build}\n")
606        if ($verbosity >= 1);
607    &util::rm_r ($builddir);
608    &util::cp_r ($realbuilddir, $builddir);
609    }
610
611    close OUT if $close_out;
612    close FAILLOG;
613
614    print STDERR "</Build>\n" if $gli;
615}
616## main() ##
617
618## @method create_images()
619#
620#  [Used to create default cover images... from what I'm not quite sure - John]
621#
622sub create_images {
623    my ($collection_name) = @_;
624
625    my $image_script = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script", "gimp", "title_icon-1.2.pl");
626    if (!-e $image_script) {
627    &gsprintf($out, "{buildcol.no_image_script}", $image_script);
628    &gsprintf($out, "{buildcol.no_default_images}\n\n");
629    return;
630    }
631
632    my $imagedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "images");
633
634    &util::mk_all_dir ($imagedir);
635
636    # create the images
637    system ("$image_script -size 1.5 -image_dir \"$imagedir\" -filename $collection.gif -text \"$collection_name\"");
638    system ("$image_script -image_dir \"$imagedir\" -filename ${collection}sm.gif -text \"$collection_name\"");
639
640    if ($gs_mode eq "gs3") {
641    # update the CollectionConfig.xml file
642    # what we need to do is find in CollectionConfig.xml the two elements with attributes 'icon' and 'smallicon'
643    # and replace the node text with $collection.gif and ${collection}sm.gif respectively. If they don't exist,
644    # create new ones by using the image names.
645
646
647    my $found = 0; my $foundsm = 0;
648   
649    my $twig = XML::Twig->new();
650    $twig->parsefile($configfilename);
651    my $root = $twig->root;
652    my $displayItemList = $root->first_child('displayItemList');
653    my @displayItem = $displayItemList->children('displayItem');
654    foreach my $displayItem (@displayItem) {
655        my $att_value = $displayItem->{'att'}->{'name'};
656       
657        if($att_value eq 'icon') {
658        $found = 1;
659        }
660        elsif ( $att_value eq 'smallicon') {
661        $foundsm = 1;
662        }
663    }
664
665    if ($found eq 0) { print "write icon";
666        my $icon_displayItem_element = XML::Twig::Elt->new('displayItem');
667        $icon_displayItem_element->set_att('name', '$collection.gif');
668        $icon_displayItem_element->set_att('lang', 'en');
669        $icon_displayItem_element->set_text("$collection.gif");
670        $icon_displayItem_element->paste('last_child', $displayItemList);
671    }
672    if ($foundsm eq 0) {
673        my $smallicon_displayItem_element = XML::Twig::Elt->new('displayItem');
674        $smallicon_displayItem_element->set_att('name', 'smallicon');
675        $smallicon_displayItem_element->set_att('lang', 'en');
676        $smallicon_displayItem_element->set_text("${collection}sm.gif");
677        $smallicon_displayItem_element->paste('last_child', $displayItemList);
678       
679       
680    }
681    open (OUT, ">$configfilename") or die "cannot open file $configfilename for output: $!";
682    #select OUT;
683    $twig->print( \*OUT );
684    close OUT;
685    return;
686    }
687
688    # update the collect.cfg configuration file (this will need
689    # to be changed when the config file format changes)
690    if (!open (CFGFILE, $configfilename)) {
691    &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
692    &gsprintf($out, "{buildcol.unlinked_col_images}\n");
693    return;
694    }
695
696    my $line = ""; my $file = "";
697    my $found = 0; my $foundsm = 0;
698    while (defined ($line = <CFGFILE>)) {
699    if ($line =~ /collectionmeta\s+iconcollection\s+/) {
700        $line = "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n";
701        $found = 1;
702    } elsif ($line =~ /collectionmeta\s+iconcollectionsmall\s+/) {
703        $line = "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n";
704        $foundsm = 1;
705    }
706    $file .= $line;
707    }
708    close CFGFILE;
709
710    $file .= "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n" if !$found;
711    $file .= "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n" if !$foundsm;
712
713    if (!open (CFGFILE, ">$configfilename")) {
714    &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
715    &gsprintf($out, "{buildcol.unlinked_col_images}\n");
716    return;
717    }
718    print CFGFILE $file;
719    close CFGFILE;
720}
721## create_images() ##
722
723## @method make_absolute()
724#
725#  Ensure the given file path is absolute in respect to the given base path.
726#
727#  @param  $base_dir A string denoting the base path the given dir must be
728#                    absolute to.
729#  @param  $dir The directory to be made absolute as a string. Note that the
730#               dir may already be absolute, in which case it will remain
731#               unchanged.
732#  @return The now absolute form of the directory as a string.
733#
734#  @author John Thompson, DL Consulting Ltd.
735#  @copy 2006 DL Consulting Ltd.
736#
737sub make_absolute()
738  {
739    my ($base_dir, $dir) = @_;
740    $dir = $base_dir . "/$dir" if ($dir =~ m#^[^/]#);
741    $dir =~ s|^/tmp_mnt||;
742    1 while($dir =~ s|/[^/]*/\.\./|/|g);
743    $dir =~ s|/[.][.]?/|/|g;
744    $dir =~ tr|/|/|s;
745    return $dir;
746  }
747## make_absolute() ##
Note: See TracBrowser for help on using the browser.