root/main/trunk/greenstone3/bin/script/convert_coll_from_gs2.pl @ 25669

Revision 25669, 27.8 KB (checked in by ak19, 7 years ago)

If ImagePlugin? or PagedImagePlugin? are in the PluginList?, the conversion process now creates a replaceListRef for gs2-image, so that format statements can make references to Thumb icon and Image macros.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3BEGIN {
4    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
5    die "GSDL3HOME not set\n" unless defined $ENV{'GSDL3HOME'};
6    die "GSDL3SRCHOME not set\n" unless defined $ENV{'GSDL3SRCHOME'};
7    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
8    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
9    unshift (@INC, "$ENV{'GSDL3SRCHOME'}/lib/perl/cpan");
10}
11
12use colcfg;
13use util;
14use parsargv;
15use FileHandle;
16use XML::Writer;
17#can't get this to work on windows
18#use GDBM_File;
19
20use strict;
21
22&main();
23sub print_usage() {
24    print STDOUT "Usage: convert_coll_from_gs2.pl [options] coll-name\n";
25    print STDOUT "options:\n";
26   
27    print STDOUT "   -collectdir         Directory where collection lives.\n";
28    print STDOUT "   -verbosity          Controls the amount of output.\n";
29    print STDOUT "   -defaultlang        The language that is considered the default (for display text etc). defaults to 'en'\n\n";
30}
31
32
33sub main {
34
35    my ($defaultlang, $verbosity, $collectdir);
36    # note that no defaults are passed for most options as they're set
37    # later (after we check the collect.cfg file)
38    if (!&parsargv::parse(\@ARGV,
39              'verbosity/\d+/', \$verbosity,
40              'collectdir/.*/', \$collectdir,
41              'defaultlang/.*/', \$defaultlang)) {
42    &print_usage();
43    die "\n";
44    }
45
46    # get and check the collection name
47    my ($collection) = @ARGV;
48    if (!defined($collection) || $collection eq "") {
49    die "No collection specified\n";
50    }
51    if ($collection eq "gs2model") {
52    die "You cant convert the model collection\n";
53    }
54   
55    if (!defined $collectdir || $collectdir eq "") {
56    $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
57    }
58
59    if (!defined $defaultlang || $defaultlang eq "") {
60    $defaultlang = 'en';
61    }
62    # add on the coll name
63    $collectdir = &util::filename_cat ($collectdir, $collection);
64   
65    my $collconfigfilename = &util::filename_cat ($collectdir, "etc", "collect.cfg");
66    print STDOUT "coll config=$collconfigfilename\n";
67    my $collectcfg;
68    if (-e $collconfigfilename) {
69    $collectcfg = &colcfg::read_collect_cfg ($collconfigfilename);
70
71    } else {
72    print STDERR "collect.cfg not found!!";
73    die "\n";
74    }
75   
76
77    my $buildconfigfilename = &util::filename_cat ($collectdir, "index", "build.cfg");
78    my $buildcfg;
79    if (-e $buildconfigfilename) {
80    $buildcfg = &colcfg::read_build_cfg ($buildconfigfilename);
81
82    } else {
83    print STDERR "build.cfg not found!!";
84    die "\n";
85    }
86   
87
88     
89    my $colloutfile = &util::filename_cat ($collectdir, "etc", "collectionConfig.xml");
90    if (-e $colloutfile) {
91    print STDOUT "collectionConfig file already exists! overwriting it!\n";
92   
93    }
94   
95    my $buildoutfile = &util::filename_cat ($collectdir, "index", "buildConfig.xml");
96    if (-e $buildoutfile) {
97    print STDOUT "buildConfig file already exists! overwriting it!\n";
98   
99    }
100
101#    my $db_file = &util::filename_cat ($collectdir, "index", "text", "$collection.ldb");
102    my $database;
103#    if (-e $db_file) {
104#   $database = &open_database($db_file);
105#    } else {
106#   print STDERR "gdbm database file $db_file not found!!";
107#   die "\n";
108#    }
109   
110    my $buildtype;
111    if (defined $buildcfg->{'buildtype'}) {
112    $buildtype = $buildcfg->{'buildtype'};
113    } else {
114    $buildtype = 'mg';
115    }
116   
117    my $indexstem = undef;
118    if (defined $buildcfg->{'indexstem'}) {
119    $indexstem = $buildcfg->{'indexstem'};
120    }
121
122    my $buildoutput = new IO::File(">$buildoutfile");
123    binmode($buildoutput,":utf8");
124    my $buildwriter = new XML::Writer(OUTPUT => $buildoutput, NEWLINES => 1);
125   
126    $buildwriter->xmlDecl("UTF-8");
127    $buildwriter->startTag('buildConfig', 'xmlns:gsf'=>"http://www.greenstone.org/greenstone3/schema/ConfigFormat");
128   
129    my $colloutput = new IO::File(">$colloutfile");
130    binmode($colloutput,":utf8");
131    my $collwriter = new XML::Writer(OUTPUT => $colloutput, NEWLINES => 1);
132   
133    $collwriter->xmlDecl("UTF-8");
134    $collwriter->startTag('CollectionConfig', 'xmlns:gsf'=>"http://www.greenstone.org/greenstone3/schema/ConfigFormat", 'xmlns:xsl'=>'http://www.w3.org/1999/XSL/Transform');
135   
136    #output the collection metadata to the collectionConfig file
137    $collwriter->startTag('metadataList');
138   
139    my $creator = $collectcfg->{'creator'};
140    &output_metadata($collwriter,'default', 'creator', $creator);
141    my $public =$collectcfg->{'public'};
142    &output_metadata($collwriter,'default', 'public', $public);
143   
144    $collwriter->endTag('metadataList');
145
146    #output the display collectionmeta to collectionConfig.xml
147   
148    my $collectionmeta = $collectcfg->{'collectionmeta'};
149    if (defined $collectionmeta) {
150    my %name_map = ('collectionname', 'name',
151             'collectionextra', 'description',
152             'iconcollection', 'icon',
153             'iconcollectionsmall', 'smallicon');
154   
155    $collwriter->startTag('displayItemList');
156    foreach my $entry ( keys %$collectionmeta) {
157        if (defined $name_map{$entry}) {
158        my $name= $name_map{$entry};
159        foreach my $lang (keys %{$collectionmeta->{$entry}}) {
160            my $value = $collectionmeta->{$entry}->{$lang};
161            if ($entry =~ /^icon/) {
162            $value = format_icon_value($value);
163            } else {
164            $value = tidy_up_display_item($value);
165            }
166            &output_display($collwriter, $name, $lang, $value);
167        }
168        }
169    }
170    $collwriter->endTag('displayItemList');
171    }
172   
173    # output building metadata to build config file
174    my $numdocs = $buildcfg->{'numdocs'};
175    $buildwriter->startTag('metadataList');
176    &output_metadata($buildwriter,'', 'numDocs', $numdocs);
177    &output_metadata($buildwriter,'', 'buildType', $buildtype);
178    $buildwriter->endTag('metadataList');
179   
180   
181    #indexes
182    # maps index name to shortname
183    my $indexmap = {};
184    # keeps the order for indexes
185    my @indexlist = ();
186    my $defaultindex = "";
187    my $first = 1;
188    my $maptype = "indexfieldmap";
189    if ($buildtype eq "mg") {
190    $maptype = "indexmap";
191    }
192    if (defined $buildcfg->{$maptype}) {
193    my $indexmap_t = $buildcfg->{$maptype};
194    foreach my $i (@$indexmap_t) {
195        my ($k, $v) = $i =~ /^(.*)\-\>(.*)$/;
196        $indexmap->{$k} = $v;
197        push @indexlist, $k;
198        if ($first == 1) {
199        $defaultindex = $k;
200        $first = 0;
201        }   
202    }
203    } else {
204    print STDERR "$maptype not defined\n";
205    }
206    # we use the shortname for default index
207    if (defined $collectcfg->{'defaultindex'}) {
208    $defaultindex = $collectcfg->{'defaultindex'};
209    #$defaultindex = $indexmap->{$defaultindex};
210    }
211   
212    #  levels
213    my $levelmap = {};
214    my @levellist = ();
215    my $default_search_level = "";
216    my $default_search_level_shortname = "";
217    my $default_retrieve_level = "Sec";
218    $first = 1;
219    if ($buildtype eq "mgpp" || $buildtype eq "lucene") {
220    if (defined $buildcfg->{'levelmap'}) {
221        my $levelmap_t = $buildcfg->{'levelmap'};
222        foreach my $l (@$levelmap_t) {
223        my ($k, $v) = $l =~ /^(.*)\-\>(.*)$/;
224        $levelmap->{$k} = $v;
225        push @levellist, $k;
226        if ($first) {
227            $default_search_level = $k;
228            $default_search_level_shortname = $v;
229            $first = 0;
230        }
231        }
232    }
233
234    if (defined $collectcfg->{'defaultlevel'}) {
235        $default_search_level = $collectcfg->{'defaultlevel'};
236        #$default_search_level = $levelmap->{$default_search_level};
237        $default_search_level_shortname = $levelmap->{$default_search_level};
238    }
239    if (defined $buildcfg->{'textlevel'}) {
240        $default_retrieve_level = $buildcfg->{'textlevel'};
241    }
242    }
243    # format stuff
244    my $format = $collectcfg->{'format'};
245
246    #output the search stuff to coll cfg
247    $collwriter->startTag('search','type'=>$buildtype);
248    foreach my $i (keys %$indexmap) {
249    $collwriter->startTag('index', 'name'=>$i);
250    #find the coll meta stuff
251    my $indexdisplay = ".$i";
252    foreach my $lang (keys %{$collectionmeta->{$indexdisplay}}) {
253        my $value = $collectionmeta->{$indexdisplay}->{$lang};
254        output_display($collwriter, 'name', $lang, $i);
255    }
256    $collwriter->endTag('index');
257    }
258
259    #output the defaultIndex to coll cfg
260    $collwriter->emptyTag('defaultIndex','name'=>$defaultindex);
261
262    #indexSubcollection
263    my $indexsubcollections = $collectcfg->{'indexsubcollections'};
264   
265    if (defined $indexsubcollections) {
266    my $indexsubcollections_t= $collectcfg->{'indexsubcollections'};
267    foreach my $i ( @$indexsubcollections_t) {
268        $collwriter->startTag('indexSubcollection', 'name'=>$i);
269        &output_display($collwriter, 'name', $defaultlang, $i);
270        $collwriter->endTag('indexSubcollection');
271    }
272    }
273
274    #subcollection
275    my $subcollection = $collectcfg->{'subcollection'};
276    if (defined $subcollection){
277    foreach my $entry (keys %$subcollection){
278        my $value = $subcollection->{$entry};
279        $collwriter->emptyTag('subcollection','filter'=>$value,'name'=>$entry);
280    }
281    }
282
283    #indexlanguage
284    my $languages = $collectcfg->{'languages'};
285    if (defined $languages){
286    my $languages_t = $collectcfg->{'languages'};
287    foreach my $i (@$languages_t){
288        $collwriter->startTag('indexLanguage','name'=>$i);
289         &output_display($collwriter, 'name', $defaultlang, $i);
290        $collwriter->endTag('indexLanguage');
291    }
292    }
293
294    #  level stuff for mgpp/lucene
295    if ($buildtype eq 'mgpp' || $buildtype eq 'lucene'){
296    foreach my $l (keys %$levelmap) {
297        $collwriter->startTag('level', 'name'=>$l);
298        #find the coll meta stuff
299        my $leveldisplay = ".$l";
300        foreach my $lang (keys %{$collectionmeta->{$leveldisplay}}) {
301        my $value = $collectionmeta->{$leveldisplay}->{$lang};
302        output_display($collwriter, 'name', $lang, $value);
303        }
304        $collwriter->endTag('level');
305    }
306    $collwriter->emptyTag('defaultLevel', 'name'=>$default_search_level);
307    }
308   
309    # add in the search type
310    if (defined $format->{'SearchTypes'}){
311    $collwriter->startTag('format', 'name'=>"searchType");
312    $collwriter->charactersXML($format->{'SearchTypes'});
313    $collwriter->endTag('format');
314    }
315
316    # add in the format stuff
317    if (defined $format->{'SearchVList'}) {
318   
319    $collwriter->startTag('format');
320    write_format($collwriter, $format->{'SearchVList'}, "document");
321    $collwriter->endTag('format');
322    }
323    elsif (defined $format->{'VList'}) {
324    $collwriter->startTag('format');
325    write_format($collwriter, $format->{'VList'}, "document");
326    $collwriter->endTag('format');
327    }   
328   
329    $collwriter->endTag('search');
330
331    # import plugins
332    # if ImagePlugin is added, then need to add in a replaceListRef element for gs2-image
333    my $contains_image_plugin = 0;
334
335    my $plugins = $collectcfg->{'plugin'};
336   
337    if (defined $plugins){
338    $collwriter->startTag('import');
339    $collwriter->startTag('pluginList');
340    foreach my $pl (@$plugins) {
341        my ($pluginname) = @$pl[0];
342        if ($pluginname =~ m/^(ImagePlugin|ImagePlug|PagedImagePlugin)$/) {
343        $contains_image_plugin = 1;
344        }
345        $collwriter->startTag('plugin','name'=>$pluginname);
346
347        for (my $i=1; $i<scalar(@$pl); $i++) {
348        my $arg =@$pl[$i];
349        if ($arg =~ /^-/){
350            my $option_name=@$pl[$i];
351            my $option_value=@$pl[$i+1];
352            if (defined $option_value){
353            if ($option_value =~ /^-/){
354                $collwriter->startTag('option','name'=>$option_name);
355                $collwriter->endTag('option');
356            }else{
357                $collwriter->startTag('option','name'=>$option_name,'value'=>$option_value);
358                $collwriter->endTag('option');
359            }
360            }
361        }
362        }
363        $collwriter->endTag('plugin');
364    }
365    $collwriter->endTag('pluginList');
366    $collwriter->endTag('import');
367    }
368
369    $buildwriter->startTag('serviceRackList');
370   
371    my @levels = ();
372    my $defaultlevel;
373    my $service_type = "MG";
374    if ($buildtype eq 'mgpp') {
375    $service_type = "MGPP";
376    } elsif ($buildtype eq "lucene") {
377    $service_type = "Lucene";
378    }
379
380 #indexSubcollectionList
381
382    my $subcollectionmap = $buildcfg->{'subcollectionmap'};
383    my $firstsubcollection = 1;
384    my $defaultsubcollection = "";
385    my @subcollist;
386    my $subcolmap = {};
387
388    if (defined $buildcfg->{'subcollectionmap'}) {
389    my $subcolmap_t = $buildcfg->{'subcollectionmap'};
390
391    foreach my $l (@$subcolmap_t) {
392        my @pair = split(/->/, $l);
393        $subcolmap->{$pair[0]} = $pair[1];
394        push @subcollist, $pair[0];
395        if ($firstsubcollection==1) {
396        $defaultsubcollection = $pair[1];
397        $firstsubcollection = 0;
398        }   
399    }
400
401    }
402
403
404    #do the retrieve service
405    $buildwriter->startTag('serviceRack', 'name'=>"GS2".$service_type."Retrieve");
406    if ($buildtype eq 'mgpp' || $buildtype eq 'lucene') {
407    $buildwriter->emptyTag('defaultLevel', 'shortname'=>$default_retrieve_level);
408    } elsif ($buildtype eq "mg") {
409    $buildwriter->emptyTag('defaultIndex', 'shortname'=>$defaultindex);
410    }
411   
412    if ((defined $defaultsubcollection) && ($defaultsubcollection ne "")) {
413    $buildwriter->emptyTag('defaultIndexSubcollection', 'shortname'=>$defaultsubcollection);
414    }
415
416    if (defined $indexstem) {
417    $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
418    }
419    # close off the Retrieve service
420    $buildwriter->endTag('serviceRack');
421
422    # add in the classifiers if needed
423
424    my $count = 1;
425    my $phind = 0;
426    my $started_classifiers = 0;
427    if (defined $collectcfg->{'classify'}) {
428    $collwriter->startTag('browse');
429    # add in default format if necessary
430    if (defined $format->{"VList"} || defined $format->{"HList"}) {
431        # global formats
432        $collwriter->startTag('format');
433        if (defined $format->{"VList"}) {
434        # VLIst applies to both classifier and doc nodes
435        write_format($collwriter, $format->{"VList"}, "document");
436        write_format($collwriter, $format->{"VList"}, "classifier");
437        }
438        if (defined $format->{"HList"}) {
439        # hlist is only for classifier nodes
440        write_format($collwriter, $format->{"HList"}, "horizontal");
441        }
442        $collwriter->endTag('format');
443    }
444    my $classifiers = $collectcfg->{'classify'};
445    foreach my $cl (@$classifiers) {
446        my $name = "CL$count";
447        $count++;
448        my ($classname) = @$cl[0];
449        if ($classname =~ /^phind$/i) {
450        $phind=1;
451        #should add it into coll config classifiers
452        next;
453        }
454       
455        my $horizontalAtTop = &isHorizontalClassifier($database, $name);
456        if (not $started_classifiers) {
457        $buildwriter->startTag('serviceRack', 'name'=>'GS2Browse');
458        if (defined $indexstem) {
459            $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
460        }
461
462        $buildwriter->startTag('classifierList');       
463        $started_classifiers = 1;
464        }
465        my $content = ''; #use buttonname first, then metadata
466        if ($classname eq "DateList") {
467        $content = "Date";
468        } else {
469        for (my $i=0; $i<scalar(@$cl); $i++) {
470            my $arg = @$cl[$i];
471            if ($arg eq "-buttonname"){
472            $content = @$cl[$i+1];
473            last;
474            } elsif ($arg eq "-metadata") {
475            $content = @$cl[$i+1];
476            }
477           
478        }
479        }
480        if ($horizontalAtTop) {
481        $buildwriter->emptyTag('classifier', 'name'=>$name, 'content'=>$content, 'horizontalAtTop'=>'true');
482
483        } else {
484        $buildwriter->emptyTag('classifier', 'name'=>$name, 'content'=>$content);
485        }
486       
487       
488       # $collwriter->startTag('classifier', 'name'=>$name);
489        $collwriter->startTag('classifier', 'name'=>$classname);
490        for (my $i=1; $i<scalar(@$cl); $i++) {
491        my $arg =@$cl[$i];
492        if ($arg =~ /^-/){
493            my $option_name=@$cl[$i];
494            my $option_value=@$cl[$i+1];
495            if (defined $option_value){
496            if ($option_value=~ /^-/){
497                $collwriter->startTag('option','name'=>$option_name);
498                $collwriter->endTag('option');
499            }else{
500                $collwriter->startTag('option','name'=>$option_name,'value'=>$option_value);
501                $collwriter->endTag('option');
502            }
503            }
504        }
505        }
506
507        my $vlist = $name."VList";
508        my $hlist = $name."HList";
509        my $dlist = "";
510        if ($classname eq "DateList") {
511        $dlist = "DateList";
512        }
513        # need to work out how to split into classifier and document
514        if (defined $format->{$vlist} || defined $format->{$hlist} || defined $format->{$dlist}) {
515        $collwriter->startTag('format');
516        if (defined $format->{$vlist}) {
517            write_format($collwriter, $format->{$vlist}, "document");
518            write_format($collwriter, $format->{$vlist}, "classifier");
519        }
520        if (defined $format->{$hlist}) {
521            write_format($collwriter, $format->{$hlist}, "horizontal");
522        }
523       
524        if (defined $format->{$dlist}) {
525            write_format($collwriter, $format->{$dlist}, "document");
526        }
527        $collwriter->endTag('format');
528        }
529        $collwriter->endTag('classifier');
530    } #foreach classifier
531    if ($started_classifiers) {
532        # end the classifiers
533        $buildwriter->endTag('classifierList');
534        # close off the Browse service
535        $buildwriter->endTag('serviceRack');
536    }
537   
538    $collwriter->endTag('browse');
539    }
540   
541   
542    # the phind classifier is a separate service
543    if ($phind) {
544    # if phind classifier
545    $buildwriter->emptyTag('serviceRack', 'name'=>'PhindPhraseBrowse');
546    }
547   
548    # do the search service
549    $buildwriter->startTag('serviceRack', 'name'=>'GS2'.$service_type.'Search');
550    #$buildwriter->emptyTag('defaultIndex', 'shortname'=>$defaultindex);
551    $buildwriter->startTag('indexList');
552    #for each index
553    foreach my $i (@indexlist) {
554    my $index = $indexmap->{$i};
555    $buildwriter->emptyTag('index', 'name'=>$i, 'shortname'=>$index);
556    }   
557    $buildwriter->endTag('indexList');
558    if (defined $indexstem) {
559    $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
560    }
561   
562    # index options
563    if ($buildtype eq 'mg' || $buildtype eq 'mgpp') {
564    $buildwriter->startTag('indexOptionList');
565    my $stemindexes = 3; # default is stem and casefold
566    if (defined $buildcfg->{'stemindexes'} && $buildcfg->{'stemindexes'} =~ /^\d+$/ ) {
567        $stemindexes = $buildcfg->{'stemindexes'};
568    }
569    $buildwriter->emptyTag('indexOption', 'name'=>'stemIndexes', 'value'=>$stemindexes);
570   
571    my $maxnumeric = 4; # default
572    if (defined $buildcfg->{'maxnumeric'} && $buildcfg->{'maxnumeric'} =~ /^\d+$/) {
573        $maxnumeric = $buildcfg->{'maxnumeric'};
574    }
575    $buildwriter->emptyTag('indexOption', 'name'=>'maxnumeric', 'value'=>$maxnumeric);
576   
577    $buildwriter->endTag('indexOptionList');
578    }
579
580    if ($buildtype eq 'mgpp' || $buildtype eq 'lucene') {
581       
582    # level info
583    $buildwriter->emptyTag('defaultLevel', 'shortname'=>$default_search_level_shortname);
584    $buildwriter->emptyTag('defaultGDBMLevel', 'shortname'=>$default_retrieve_level);
585    $buildwriter->startTag('levelList');
586    foreach my $l (@levellist) {
587        my $level = $levelmap->{$l};
588        $buildwriter->emptyTag('level', 'name'=>$l, 'shortname'=>$level);
589    }   
590    $buildwriter->endTag('levelList');
591   
592    # do the search types if there
593    if (defined $collectcfg->{'searchtype'}) {
594        $buildwriter->startTag('searchTypeList');
595        foreach my $st (@{$collectcfg->{'searchtype'}}) {
596        $buildwriter->emptyTag('searchType', 'name'=>$st);
597        }
598        $buildwriter->endTag('searchTypeList');
599    } elsif (defined $format->{'SearchTypes'}) {
600        #check format statement
601        my $searchtype = $format->{'SearchTypes'};
602        $buildwriter->startTag('searchTypeList');
603        if ($searchtype =~ /form/) {
604        $buildwriter->emptyTag('searchType', 'name'=>'form');
605        }
606        if ($searchtype =~ /plain/) {
607        $buildwriter->emptyTag('searchType', 'name'=>'plain');
608        }
609        $buildwriter->endTag('searchTypeList');
610    }
611    }
612   
613    #indexLanguageList
614    my $indexlanguages = $collectcfg->{'languages'};
615    my $firstindexlanguage = 1;
616    my $defaultindexlanguage_shortname;
617    if (defined $indexlanguages){
618    $buildwriter->startTag('indexLanguageList');
619    my $languages_t = $collectcfg->{'languages'};
620    foreach my $i (@$languages_t){
621        $buildwriter->startTag('indexLanguage','name'=>$i);
622         &output_display($buildwriter, 'name', $i, $i);
623        $buildwriter->endTag('indexLanguage');
624        if ($firstindexlanguage==1){
625        $defaultindexlanguage_shortname = $i;
626        $firstindexlanguage=0;
627        }
628    }
629    $buildwriter->endTag('indexLanguageList');
630    $buildwriter->startTag('defaultIndexLanguage', 'name'=>$defaultindexlanguage_shortname,'shortname'=>$defaultindexlanguage_shortname);
631    $buildwriter->endTag('defaultIndexLanguage');
632    }
633
634  #  my $defaultsubcollection = "";
635   # my @subcollist;
636
637    if (scalar(@subcollist)>0){
638
639    $buildwriter->startTag('indexSubcollectionList');
640    foreach my $i (keys %$subcolmap){
641        my $short_name = $subcolmap->{$i};
642        $buildwriter->emptyTag('indexSubcollection', 'name'=>$i, 'shortname'=>$short_name);
643    }
644
645    $buildwriter->endTag('indexSubcollectionList');
646    $buildwriter->emptyTag('defaultIndexSubcollection', 'shortname'=>$defaultsubcollection);
647    }
648
649   
650    $buildwriter->endTag('serviceRack');
651   
652    $buildwriter->endTag('serviceRackList');
653    $buildwriter->endTag('buildConfig');
654
655    # we add in the default replace list just in case we have macros in the
656    # collection
657    $collwriter->emptyTag('replaceListRef', 'id'=>'gs2-standard');
658    $collwriter->emptyTag('replaceListRef', 'id'=>'gs2-image') if  $contains_image_plugin;
659    $collwriter->endTag('CollectionConfig');
660    $collwriter->end();
661    $buildwriter->end();
662    $buildoutput->close();
663    $colloutput->close();
664    &close_database($database);
665}
666
667
668sub output_metadata {
669    my ($writer, $lang, $metaname,  $metavalue) = @_;
670    $lang = 'en' if $lang eq 'default';
671    if ($lang ne ""){
672    $writer->startTag('metadata', 'lang'=>$lang, 'name'=>$metaname);
673    }else{
674    $writer->startTag('metadata', 'name'=>$metaname);
675    }
676    $writer->characters($metavalue);
677    $writer->endTag('metadata');
678}
679
680sub output_display {
681    my ($writer, $name, $lang, $value) = @_;
682    $lang = 'en' if $lang eq 'default';
683    if ($lang =~ /^\[/) {
684    ($lang) = $lang =~ /\[l=(.*)\]/;
685    }
686
687    $writer->startTag('displayItem', 'name'=>$name, 'lang'=>$lang);
688    $writer->characters($value);
689    $writer->endTag('displayItem');
690}
691sub format_icon_value {
692    my ($value) = @_;
693    if ($value =~ /^_/) {
694    my ($newvalue) = $value =~ /images\/(.*)$/;
695    if ($newvalue) {
696        return $newvalue;
697    }
698    }
699    return $value;
700}
701
702sub tidy_up_display_item {
703    my ($value) = @_;
704    # remove \n
705    $value =~ s/\\n//g;
706    # replace \' with '
707    $value =~ s/\\\'/\'/g;
708    # replace \" with "
709    $value =~ s/\\\"/\"/g;
710    # replace _httpprefix_ with _httpsite_
711    $value =~ s/_httpprefix_/_httpsite_/g;
712    $value =~ s/_gwcgi_//g;
713    $value =~ s/[a-z][a-z]?=_cgiarg[a-z][a-z]?_&?//g;
714    $value =~ s/&p=/&sa=/g;
715    return $value;
716}
717
718sub write_format {
719    my ($writer, $old_format, $node_type) = @_;
720    # replace \' with '
721    $old_format =~ s/\\\'/\'/g;
722    # replace \" with "
723    $old_format =~ s/\\\"/\"/g;
724    #convert [] to <gsf:...>
725    # assume no nesting {If} or {Or} for now
726    $old_format =~ s/\{If\}\{([^\}]*)\}/&format_if($1, $node_type)/eg;
727    $old_format =~ s/\{Or\}\{([^\}]*)\}/&format_or($1)/eg;
728    $old_format =~ s/\[Text\]/\<gsf:text\/\>/g;
729    $old_format =~ s/\[num\]/\<gsf:num\/\>/g;
730    $old_format =~ s/\[link\]/\<gsf:link type=\'$node_type\'\>/g;
731    $old_format =~ s/\[\/link\]/\<\/gsf:link\>/g;
732    $old_format =~ s/\[srclink\]/\<gsf:link type=\'source\'\>/g;
733    $old_format =~ s/\[\/srclink\]/\<\/gsf:link\>/g;
734    $old_format =~ s/\[icon\]/\<gsf:icon type=\'$node_type\'\/\>/g;
735    $old_format =~ s/\[srcicon\]/\<gsf:icon type=\'source\'\/\>/g;
736         
737    # what to do with hightlight??
738    $old_format =~ s/\[\/?highlight\]//g;
739
740    #now do the rest of the [] which are assumed to be metadata
741    $old_format =~ s/\[([^\]]*)\]/&format_metadata($1)/eg;
742 
743    # some html tidy
744    #turn <br> into <br />
745    $old_format =~ s/\<br\>/\<br \/\>/g;
746    #turn <p> into <p />
747    $old_format =~ s/\<p\>/\<p \/\>/g;
748   
749    #put quotes around any atts
750    $old_format =~ s/=([a-z]+)([> ])/=\'$1\'$2/g;
751
752    if ($node_type eq "document") {
753    $writer->startTag('gsf:template', 'match'=>'documentNode');
754    $writer->charactersXML($old_format);
755    $writer->endTag('gsf:template');
756    } elsif ($node_type eq "classifier") {
757    $writer->startTag('gsf:template', 'match'=>'classifierNode');
758    $writer->charactersXML($old_format);
759    $writer->endTag('gsf:template');
760    } elsif ($node_type eq "horizontal") { 
761    $writer->startTag('gsf:template', 'match'=>'classifierNode', 'mode'=>'horizontal');
762    $writer->charactersXML($old_format);
763    $writer->endTag('gsf:template');
764
765    }
766}
767
768sub format_metadata {
769    my ($metadata_string) = @_;
770
771    #print STDERR "original meta = $metadata_string\n";
772   
773    # what shall we do with cgisafe??
774    my $cgisafe = $metadata_string =~ s/^cgisafe://;
775
776    my ($select) = $metadata_string =~ /^(parent|sibling)/;
777    $metadata_string =~ s/^(parent|sibling)//;
778    my ($scope, $delim);
779   
780    if ($select) {
781    ($scope) = $metadata_string =~ /^\((Top|All)/;
782    $metadata_string =~ s/^\((Top|All)\)?//;
783    if ($scope) {
784        ($delim) = $metadata_string =~ /^\'([^\']*)\'\)/;
785        $metadata_string =~ s/^\'([^\']*)\'\)//;
786    }
787    }
788    $metadata_string =~ s/^://;
789    # remove ex.
790    $metadata_string =~ s/^ex\.//;
791   
792    #print STDERR "select=$select, scope=$scope, delim=|$delim|, meta = $metadata_string\n";
793   
794    my $new_format = "<gsf:metadata name='$metadata_string' ";
795    if (defined $select) {
796    if ($select eq "sibling") {
797        $new_format .= "multiple='true' ";
798        if (defined $delim) {
799        $new_format .= "separator='$delim' ";
800        }
801    } elsif ($select eq "parent"){
802        if (defined $scope) {
803        if ($scope eq "Top") {
804            $new_format .= "select='root' ";
805        } elsif ($scope eq "All") {
806            $new_format .= "select='ancestors' ";
807            if (defined $delim) {
808            $new_format .= "separator='$delim' ";
809            }
810        }
811        } else {
812        $new_format .= "select='parent' ";
813        }   
814    }
815    }
816    $new_format .= "/>";
817    #print STDERR "$new_format\n";
818    return $new_format;
819   
820}
821
822sub format_if {
823
824    my ($if_string, $node_type) = @_;
825    #print STDERR "if string = $if_string\n";
826
827    my @parts = split /,/, $if_string;
828    my $test = $parts[0];
829    my $true_option = $parts[1];
830    my $false_option;
831    if (scalar (@parts) == 3) {
832    $false_option = $parts[2];
833    }
834    $test =~ s/^\s*//;
835    $test =~ s/\s*$//;
836    my ($test_meta, $test_type, $test_value);
837    if ($test =~ /^(\[.+\])$/) {
838    $test_meta = $1;
839    $test_type = 'exists';
840    } else {
841    my ($lhs, $exp, $rhs) = $test =~ /^(.+)\s+(eq|ne|lt|gt|le|ge|sw|ew)\s+(.+)$/;
842    #print STDERR "lhs, exp, rhs = $lhs, $exp, $rhs\n";
843    if ($exp eq "eq") {
844        $test_type = "equals";
845    } elsif ($exp eq "sw") {
846        $test_type = "startsWith";
847    } elsif ($exp eq "ew") {
848        $test_type = "endsWith";
849    } elsif ($exp eq "ne") {
850        $test_type = "notEquals";
851    } elsif ($exp eq "lt") {
852        $test_type = "lessThan";
853    }elsif ($exp eq "gt") {
854        $test_type = "greaterThan";
855    }elsif ($exp eq "le") {
856        $test_type = "lessThanOrEquals";
857    }elsif ($exp eq "ge") {
858        $test_type = "greaterThanOrEquals";
859    }
860    if ($lhs =~ /^\[.+\]$/) {
861        $test_meta = $lhs;
862        $test_value = $rhs;
863    } else {
864        # assume rhs has meta
865        $test_meta = $rhs;
866        $test_value = $lhs;
867    }
868   
869    #remove beginning and end quotes
870    $test_value =~ s/^[\'\"]//;
871    $test_value =~ s/[\'\"]$//;
872    }
873    my $test_atts = "test='$test_type' ";
874    if (defined $test_value) {
875    $test_atts .= "test-value='$test_value' ";
876    }
877    #print STDERR "test, true, false = $test, $true_option, $false_option\n";
878    my $new_format = "<gsf:switch>$test_meta";
879    $new_format .= "<gsf:when $test_atts>$true_option</gsf:when>";
880    if (defined $false_option) {
881    $new_format .="<gsf:otherwise>$false_option</gsf:otherwise>";
882    }
883    $new_format .= "</gsf:switch>";
884   
885    #print STDERR "new format = $new_format\n";
886    return $new_format;
887}
888
889sub format_or {
890    my ($or_string) = @_;
891    my @meta_list = split (',', $or_string);
892    return "" unless scalar (@meta_list);
893    my $new_format = "<gsf:choose-metadata>";
894    foreach my $m (@meta_list) {
895    if ($m =~ /^\[(.*)\]$/) {
896        $new_format .= &format_metadata($1);
897    } else {
898        # a default value
899        $new_format .= "<gsf:default>$m</gsf:default>";
900        last;
901    }
902    }
903    $new_format .= "</gsf:choose-metadata>";
904    return $new_format;
905}
906
907sub open_database {
908    my ($db_file) = @_;
909   
910    my $database = ();
911#    tie (%$database, 'GDBM_File', $db_file, GDBM_READER, 0400) ||
912#   die "Couldn't open database $db_file\n";
913
914    return $database;
915}
916
917sub close_database {
918    my ($database) = @_;
919    untie %$database;
920}
921sub isHorizontalClassifier {
922    my ($database, $name) = @_;
923
924    return 0; # can't get this to work for windows
925    my $record = $database->{$name};
926    my ($childtype) = $record =~ /<childtype>(\w*)/;
927    if ($childtype eq "HList") {
928    return 1;
929    }
930    return 0;
931}
932#$writer->startTag('');
933#$writer->endTag('');
934#$writer->characters();
935#$writer->emptyTag('');
936
9371;
Note: See TracBrowser for help on using the browser.