source: greenstone3/branches/customizingGreenstone3/bin/script/convert_coll_from_gs2.pl@ 15191

Last change on this file since 15191 was 15191, checked in by dmn, 13 years ago

updating branch from trunk

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