-
Notifications
You must be signed in to change notification settings - Fork 2
/
CIAODOC.pm
executable file
·1865 lines (1588 loc) · 51.7 KB
/
CIAODOC.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Aim:
# Useful routines for the CIAO documentation system.
#
# Author:
# Doug Burke ([email protected])
#
#
# Global (ie in package main) variables used by this module
# $configfile
# $verbose
# $group
# $htmldoc
# $site
#
package CIAODOC;
use strict;
$|++;
use Carp;
use Cwd;
use IO::File;
use File::stat;
use File::Basename;
use File::Temp;
use XML::LibXML;
use XML::LibXSLT;
# Try to support using LaTeX (use_mathjax()=0) or MathJax
# (use_mathjax()=1) for displaying LaTeX equations on the
# web pages.
# For now treat as a hard-coded constant rather than something
# we can change by setting a flag in the publishing script.
#
# This is intended as a temporary measure; once MathJax can
# be installed onto the web site this can be removed, or
# it may become a per-site setting?
sub use_mathjax () { return 1; }
# Set up XML/XSLT processors
# (registration of functions happens later)
#
# NOTE: documentation seems to suggest that expand_entities is set to 1
# but this no-longer holds, so explicitly set it.
#
my $parser = XML::LibXML->new(expand_entities => 1)
or die "Error: Unable to create XML::LibXML parser instance.\n";
$parser->validation(0);
$parser->expand_xinclude(1); # NOTE: don't actually use XInclude at the moment
my $xslt = XML::LibXSLT->new()
or die "Error: Unable to create XML::LibXSLT instance.\n";
# Set up potentially-useful functions. Ideally we would use
# register_element but this is not available using the
# installed version of XML::LibXSLT
#
# TODO: should read-file-if-exists report this information
# as part of the dependency tracking?
XML::LibXSLT->register_function("http://hea-www.harvard.edu/~dburke/xsl/extfuncs",
"read-file-if-exists",
sub {
my $filename = shift;
# want to differentiate between 'file does not exist' and
# 'file is invalid'.
return XML::LibXML::NodeList->new()
unless -e $filename;
my $rval;
eval { $rval = $parser->parse_file($filename); };
die "ERROR: problem parsing XML file $filename\n $@\n"
if $@;
return $rval;
}
);
XML::LibXSLT->register_function("http://hea-www.harvard.edu/~dburke/xsl/extfuncs",
"delete-file-if-exists",
sub {
my $filename = shift;
eval { CIAODOC::myrm($filename); };
die "ERROR: unable to delete $filename (requested by stylesheet)\n $@\n"
if $@;
return 1;
}
);
# default depth is 250 but this causes problems with some style sheets
# (eg wavdetect, tg_create_mask), so increase randomly until everything
# compiles. Why did we not see this in the xsltproc command-line tool?
#
# XML::LibXSLT->max_depth(750);
XML::LibXSLT->max_depth(2000);
my @funcs_util =
qw(
fixme dbg check_dir mymkdir mycp myrm mysetmods
check_paths check_executables check_executable_runs
extract_filename get_ostype
list_ahelp_sites find_ahelp_site check_ahelp_site_valid
get_pygments_styles
);
my @funcs_xslt =
qw(
translate_file
read_xml_file read_xml_string read_html_string
find_math_pages
);
my @funcs_cfg =
qw(
parse_config find_site get_config_main get_config_main_type
get_config_site get_config_version get_config_type
check_config_exists check_type_known check_location get_group
get_storage_location
);
my @funcs_deps =
qw(
clear_dependencies get_dependencies have_dependencies
dump_dependencies write_dependencies
identify_files_to_republish
use_mathjax
);
use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
@ISA = qw ( Exporter );
@EXPORT = ();
@EXPORT_OK = ( @funcs_util, @funcs_xslt, @funcs_cfg, @funcs_deps );
%EXPORT_TAGS =
(
util => \@funcs_util, xslt => \@funcs_xslt, cfg => \@funcs_cfg,
deps => \@funcs_deps
);
## Subroutines (see end of file)
#
sub fixme ($);
sub dbg ($);
sub check_dir ($$);
sub mymkdir ($);
sub mycp ($$);
sub myrm ($);
sub mysetmods ($);
sub check_paths (@);
sub check_executables (@);
sub check_executable_runs ($$$);
sub extract_filename ($);
sub read_xml_file ($);
sub read_xml_string ($);
sub read_html_string ($);
sub translate_file ($$;$);
sub translate_file_lang ($$$;$);
sub parse_config ($);
sub find_site ($$);
sub get_config_main ($@);
sub get_config_site ($@);
sub get_config_version ($@);
sub get_config_type ($$$);
sub check_config_exists ($$);
sub check_type_known ($$);
sub get_storage_location ($$);
sub check_location ($$);
sub get_group ($);
sub get_ostype ();
sub list_ahelp_sites ();
sub find_ahelp_site ($$);
sub clear_dependencies ();
sub get_dependencies ();
## Subroutines
#
# fixme $msg
#
# can print out the message to screen if we want to
# - but mainly useful as a way of marking a problem area
#
##sub fixme ($) {}
sub fixme ($) { print STDERR "$_[0]\n"; }
# dbg $msg
#
# prints the message to STDOUT if $verbose != 0
# (adds a leading >>DBG: and trailing \n)
#
sub dbg ($) { print ">>DBG: $_[0]\n" if $main::verbose; }
# used to check input params
#
# note:
# we do not check for the existence of the directory
# just that the input string matches certain requirements
#
sub check_dir ($$) {
my $name = shift;
my $value = shift;
die "Error: $name not defined\n"
unless defined $value;
die "Error: $name ($value) not found\n"
unless -d $value;
die "Error: $name ($value) does not end in '/'\n"
unless $value =~ /\/$/;
} # sub: check_dir()
# Hide the OS location of these calls; should use POSIX module instead?
#
sub call_mkdir ($) {
my $dname = shift;
system "/bin/mkdir $dname" and die "Unable to mkdir $dname";
}
sub call_chmod ($$) {
my $opts = shift;
my $name = shift;
my $chmod = $^O eq "solaris" ? "/bin/chmod" : "/bin/chmod";
system "$chmod $opts $name" and die "Unable to chmod $opts $name";
}
sub call_chgrp ($$) {
my $opts = shift;
my $name = shift;
my $chgrp = $^O eq "linux" ? "/bin/chgrp" : "/usr/bin/chgrp";
system "$chgrp $opts $name" and die "Unable to chgrp $opts $name";
}
sub call_rm ($) {
my $name = shift;
my $rm = $^O eq "solaris" ? "/usr/bin/rm" : "/bin/rm";
system "$rm -f $name";
die "Error: been unable to delete $name\n"
if -e $name;
}
sub call_cp ($$) {
my $in = shift;
my $out = shift;
my $cp = $^O eq "solaris" ? "/usr/bin/cp" : "/bin/cp";
system "$cp $in $out" and die "Unable to cp $in to $out";
}
# mymkdir( $dname )
#
# Create the directory $dname, automatically creating any 'intermediate' directories
# as required. It uses /bin/mkdir rather then perl's mkdir() function because I
# can't get the umask value set properly.
#
# for this program we also ensure that each created directory has
# g+w
#
# For example:
# mymkdir( "bob/fred/john" ) and only bob/ already exists, creates bob/fred/ then
# bob/fred/john/.
#
sub mymkdir ($) {
my $dname = $_[0];
return if -d $dname;
die "ERROR: <$dname> is a file.\n" if -e $dname;
dbg "Creating directory: $dname";
# strip through the parts
my @dirs = split "/", $dname;
my $dhead = "";
foreach my $d ( @dirs ) {
$dhead .= $d;
unless ( $dhead eq "" or -d $dhead ) {
call_mkdir $dhead;
call_chmod "ug+w", $dhead;
call_chgrp $main::group, $dhead;
}
$dhead .= "/";
}
} # sub: mymkdir()
# mycp( $in, $out )
#
# Deletes $out (if it exists)
# Copies $in to $out
# Sets the permissions on $out to ugo-wx
# group of $out to $group (global parameter)
#
sub mycp ($$) {
my $in = shift;
my $out = shift;
my $name = $_[0];
myrm $out;
call_cp $in, $out;
mysetmods $out;
} # sub: mycp()
# myrm( $fname )
#
# Deletes the input file ($name) using '-f'.
# It uses /usr/bin/rm rather then perl's unlink() function because I
# couldn't be bothered to work out how to do the equivalent of 'rm -f $foo'
#
# does nothing if $fname doesn't exist or is not a file
# (eg if it's a directory)
#
sub myrm ($) {
my $name = $_[0];
return unless -e $name;
call_rm $name;
} # sub: myrm()
# mysetmods( $name )
#
# Sets the permissions of $name to ugo-wx
# group $group (global parameter)
#
# Uses system routines ratehr than perl ones because I couldn't be
# bothered to sort out the octal codes
#
# does nothing if $name doesn't exist or isn't a file (eg a directory)
#
sub mysetmods ($) {
my $name = $_[0];
return unless -e $name;
call_chmod "ugo-wx", $name;
call_chgrp $main::group, $name;
} # sub: mysetmods()
# check_paths( $path1, $path2, ... )
#
# checks that the input paths are directories and that they end in a /
# character. dies if they don't.
#
sub check_paths (@) {
foreach my $path ( @_ ) {
die "Error: unable to find the directory '$path'\n"
unless -d $path;
die "Error: path '$path' does not end in a / character.\n"
unless $path =~ /\/$/;
}
return;
} # sub: check_paths()
# check_executables( $bin1, $bin2, ... )
#
# checks that the input files are executable
# dies if they aren't.
#
sub check_executables (@) {
foreach my $exe ( @_ ) {
die "Error: unable to find the executable $exe\n"
unless -x $exe;
}
return;
} # sub: check_exectuables()
# check_executable_runs( $name, $bin, $arg )
#
# checks that you can run the command $bin with the supplied
# argument and you get back status=0. Note that we use the
# "simple" eval method (as a single string) so there are
# security risks here.
#
sub check_executable_runs ($$$) {
my $name = shift;
my $exe = shift;
my $arg = shift;
my $retval = `$exe $arg`;
die <<"EOE" unless $? == 0;
Error: Executable '$name' failed to run with argument
'$arg'
The return value was:
$retval
EOE
return;
} # sub: check_executable_runs()
# my $fname = extract_filename( $fullname );
#
# returns everything after the last '/' character
# should really use the filename-parsing routines
#
sub extract_filename ($) { return (split( "/", $_[0] ))[-1]; }
# run the processor and return the screen output
#
# We now (as of CIAO 4 beta 3) use XML::LibXSLT to process the file,
# rather than call an external process (xsltproc)
#
{
# add parsed stylesheets to an internal repository
# in case they are needed again.
#
my %xslt_store;
# Returns the parsed stylesheet, loading and storing
# it if it hasn't already been loaded.
#
sub _get_stylesheet ($) {
my $filename = shift;
return $xslt_store{$filename} if exists $xslt_store{$filename};
my $style = $xslt->parse_stylesheet_file ($filename) ||
die "ERROR: unable to parse stylesheet '$filename'\n";
$xslt_store{$filename} = $style;
return $xslt_store{$filename};
}
# Returns the DOM for the file or dies, although it may be that this
# method already dies and I need to improve my error handling here.
#
# This routine need not be within this closure, but left here for now.
#
# We add the .xml suffix if it does not exist.
#
sub read_xml_file ($) {
my $filename = shift;
$filename .= ".xml" unless $filename =~ /\.xml$/;
dbg " - about to read XML file '$filename'";
$parser->parse_file($filename)
or die "ERROR: unable to parse XML file '$filename'\n";
}
# As read_xml_file but use the input string as the file
# contents.
#
sub read_xml_string ($) {
my $str = shift;
my $firstline = (split(/\n/,$str))[0];
dbg " - about to parse XML chunk, first line='$firstline'";
$parser->parse_string($str)
or die "ERROR: unable to parse XML string, start='$firstline'\n";
}
sub read_html_string ($) {
my $str = shift;
my $firstline = (split(/\n/,$str))[0];
dbg " - about to parse HTML chunk, first line='$firstline'";
$parser->parse_html_string($str)
or die "ERROR: unable to parse HTML string, start='$firstline'\n";
}
# TODO:
# allow the return value to be XML and not assume plain text
#
# The XML file can be given by name, in which case the suffix
# ".xml" is added if it doesn't exist, otherwise it can be
# an XML::LibXML::Document
#
sub translate_file ($$;$) {
my $stylesheet = shift;
my $xml_arg = shift;
my $params = shift || {};
dbg "*** XSLT (start) ***";
# Should do this properly (allow for sub-classes etc) but go
# for the simple route
#
my $xml;
if (ref $xml_arg eq "") {
dbg " reading XML from $xml_arg";
$xml = read_xml_file $xml_arg;
} elsif (ref $xml_arg eq "XML::LibXML::Document") {
dbg " XML is from a DOM";
$xml = $xml_arg;
} else {
die "Expected xml_file argument to translate_file to be a string or XML::LibXML::Document, found " .
ref $xml_arg . " instead!\n";
}
dbg " xslt=$stylesheet";
my $sheet = _get_stylesheet $stylesheet;
# hard code the MathJax setting
$$params{'use-mathjax'} = use_mathjax;
dbg " *** params (start) ***";
my %newparams = XML::LibXSLT::xpath_to_string(%$params);
while (my ($parname, $parval) = each %newparams) {
dbg " $parname=$parval";
}
dbg " *** params (end) ***";
# How do find out if there has been an error in the
# transformation (e.g. xsl:message with terminate="yes"
# called)? No error is thrown, so no point in using
# an eval block (although left in just in case).
# Is this a module version thing, ie only seen on
# Sun with old code? Seems to be.
#
# my ($results, $retval);
# eval {
my $results = $sheet->transform($xml, %newparams);
my $retval = $sheet->output_string($results);
# };
# die "ERROR from transformation: $@\n" if $@;
dbg "*** results = [$results]";
dbg "*** as string =\n$retval";
dbg "*** XSLT (end) ***";
return $retval;
} # sub: translate_file()
}
# Returns an array of all the name nodes within math tags
# in the file. If there are no math tags then returns an
# empty list.
#
# Should be sent a DOM
#
# NOTE:
# This routine is not needed for MathJax, so we return
# an empty list in this case.
#
sub find_math_pages ($) {
return () if use_mathjax;
my $dom = shift;
# Should use a proper OO check here to allow sub-classes to match.
#
my $r = ref $dom;
die "Error: expected a XML::LibXML::Document, but sent " . $r eq "" ? "scalar" : $r . "\n"
unless $r eq "XML::LibXML::Document";
my @out;
foreach my $node ( $dom->findnodes("//math/name") ) {
push @out, $node->textContent;
}
return @out;
} # find_math_pages
#---------------------------------------------------------------------
#
# Configuration:
#
# my $config = parse_config $filename
#
# parse the config file
# returns a reference to an associative array
#
sub parse_config ($) {
my $infile = shift;
# check the options
die "Error: the config option can not be blank\n"
unless defined $infile and $infile ne "";
dbg "Parsing config file: $infile";
my $fh = IO::File->new( "< $infile" )
or die "Error: unable to open the config file ($infile) for reading.\n";
my %config = ( sites => {} );
my $site;
my $version;
# the array we're currently adding to
# can be the top level, site, or one of the version sections
my $array = \%config;
while ( <$fh> ) {
# assume a simple format
# - # or blank lines are ignored
# - no continuation lines (ie everything on one line)
#
next if /^#/ or /^\s*$/;
chomp;
# are we exiting a site block?
if ( /^-site/ ) {
die "Exiting a site block when not in one.\n"
unless defined $site;
die "Exiting a site block ($site) when version block ($version) is still open.\n"
if defined $version;
$site = undef;
$array = \%config;
next;
}
# are we exiting a version block?
if ( /^-version/ ) {
die "Exiting a version block when not in one.\n"
unless defined $version;
$version = undef;
$array = $config{sites}{$site}; # drop back to the site
next;
}
# are we entering a site block?
if ( /^\+site/ ) {
die "Entering a site block ('$_') when already in one for $site\n"
if defined $site;
my @words = split;
die "Error: expected '+site name', found '$_'\n"
unless $#words == 1;
$site = $words[1];
die "Error: site $site is already defined\n"
if exists $$array{sites}{$site};
$$array{sites}{$site} = { versions => {} };
$array = $$array{sites}{$site};
next;
}
# are we entering a version block?
if ( /^\+version/ ) {
die "Entering a version block ('$_') when not in a site\n"
unless defined $site;
die "Entering a version block ('$_') when already in one for $version\n"
if defined $version;
my @words = split;
die "Error: expected '+version name', found '$_'\n"
unless $#words == 1;
$version = $words[1];
die "Error: version $version (site $site) is already defined\n"
if exists $$array{versions}{$version};
$$array{versions}{$version} = {};
$array = $$array{versions}{$version};
next;
}
# safety checks
die "Error: valid line must be of the form '[\@\%]name=data' not '$_'\n"
unless /=/;
die "Error: version ('$_') is not an allowed parameter\n"
if /^version=/;
# now parse the input line
# scalar, array, hash array
#
my ( $key, $data ) = split "=", $_, 2;
# array
if ( substr($key,0,1) eq "@" ) {
$key = substr($key,1);
# check for overlap: unlike perl we don't allow scalar/array/hash
# values to have the same name, everything must be distinct
if ( exists $$array{$key} ) {
my $ref = ref( $$array{$key} ) || "SCALAR";
die "Error: defining an array element for $key, but it's previously\n" .
"been defined as a $ref\n"
unless $ref eq "ARRAY";
} else {
$$array{$key} = [];
}
push @{ $$array{$key} }, $data;
next;
}
# associative array
if ( substr($key,0,1) eq "%" ) {
$key = substr($key,1);
# check for overlap: unlike perl we don't allow scalar/array/hash
# values to have the same name, everything must be distinct
if ( exists $$array{$key} ) {
my $ref = ref( $$array{$key} ) || "SCALAR";
die "Error: defining an associative array element for $key, but it's previously\n" .
"been defined as a $ref\n"
unless $ref eq "HASH";
} else {
$$array{$key} = {};
}
my ( $left, $right ) = split " ", $data, 2;
$$array{$key}{$left} = $right;
next;
}
# must be a scalar
# check for overlap
die "Error: multiple definitions for $key\n"
if exists $$array{$key};
$$array{$key} = $data;
}
$fh->close;
# check that that the required fields exist
# note we do not check anything to do with the version info
#
my %required =
(
group => "SCALAR", prefix => "SCALAR",
types => "ARRAY", stylesheets => "ARRAY",
);
foreach my $site ( keys %{ $config{sites} } ) {
my $s = $config{sites}{$site};
while ( my ( $key, $kref ) = each %required ) {
die "Error: the config file ($infile) did not contain a $key element for site $site\n"
unless exists $$s{$key};
my $ref = ref( $$s{$key} ) || "SCALAR";
die "Error: the config file ($infile) has $key as a $ref when it should be a $kref\n"
unless $ref eq $kref;
}
}
return \%config;
} # sub: parse_config
# ( $site, $site_config ) = find_site $config, $dname;
#
# given the config 'object' and a directory name, find the
# corresponding site and its config options
#
sub find_site ($$) {
my $config = shift;
my $dname = shift;
dbg "Finding site for directory $dname";
my @matches;
while ( my ( $site, $href ) = each %{ $$config{sites} } ) {
my $prefix = $$href{prefix};
push @matches, $site
if substr($dname,0,length($prefix)) eq $prefix;
}
die "Error: unable to find a site matching the directory $dname\n"
if $#matches == -1;
die "Error: found multiple sites [" . join(",",@matches) . "] matching the directory $dname\n"
if $#matches == -1;
return ( $matches[0], $$config{sites}{$matches[0]} );
} # sub: find_site
# $val = get_config_main( $config, $name );
# ( $val1, $val2 ) = get_config_main( $config, $name1, $name2 );
#
# returns the value of the config variables from the "main" part
# of the config file - ie the part that is not dependent upon the
# site you are in
#
# Dies with an error if the requested value doesn't exist
#
sub get_config_main ($@) {
my $config = shift;
my @out;
foreach my $name ( @_ ) {
die "Error: $name not defined in config file ($main::configfile).\n"
unless exists $$config{$name};
push @out, $$config{$name};
}
return wantarray ? @out : $out[0];
} # sub: get_config_main()
# $val = get_config_main_type( $config, $name, $type );
# ( $val1, $val2 ) = get_config_main( $config, $name1, $name2, $type );
#
# returns the value of the config variables from the "main" part
# of the config file - ie the part that is not dependent upon the
# site you are in - for the given type. This is for fields like
# %foo=typea vala
# %foo=typeb valb
# and get_config_main_type($config, "foo", "typeb") will return
# valb.
#
# Dies with an error if the requested value doesn't exist, or
# isn't an associative array, or the type does not exist.
#
sub get_config_main_type ($@) {
my $config = shift;
my $type = pop;
my @out;
foreach my $name ( @_ ) {
die "Error: $name not defined in config file ($main::configfile).\n"
unless exists $$config{$name};
my $obj = $$config{$name};
my $ref = ref ($obj) || "SCALAR";
die "Error: the config file has $name as a $ref when it should be a HASH\n"
unless $ref eq "HASH";
die "Error: $name option does not contain a value for type=$type\n"
unless exists $$obj{$type};
push @out, $$obj{$type};
}
return wantarray ? @out : $out[0];
} # sub: get_config_main_type()
# $val = get_config_site( $site_config, $name );
# ( $val1, $val2 ) = get_config_site( $site_config, $name1, $name2 );
#
# returns the value of the config variables from the "site" part
# of the config file - ie the value returned by get_config_site
#
# Dies with an error if the requested value doesn't exist
#
sub get_config_site ($@) {
my $config = shift;
my @out;
foreach my $name ( @_ ) {
die "Error: $name not defined in site part of config file (site=$main::site, file=$main::configfile).\n"
unless exists $$config{$name};
push @out, $$config{$name};
}
return wantarray ? @out : $out[0];
} # sub: get_config_site()
# $val = get_config_version( $version_config, $name );
# ( $val1, $val2 ) = get_config_version( $version_config, $name1, $name2 );
#
# returns the value of the config variables from the "version" part
# of the config file.
#
# Dies with an error if the requested value doesn't exist
#
sub get_config_version ($@) {
my $config = shift;
my @out;
foreach my $name ( @_ ) {
die "Error: $name not defined in version part of config file ($main::configfile).\n"
unless exists $$config{$name};
push @out, $$config{$name};
}
return wantarray ? @out : $out[0];
} # sub: get_config_version()
# $val = get_config_type $config, $name, $type;
#
# gets the value for the given variable ($name) with the
# publishing type ($type)
# dies if $name doesnt' exist, isn't a hash reference, or
# if the type isn't defined for this variable
#
sub get_config_type ($$$) {
my $config = shift;
my $name = shift;
my $type = shift;
die "Error: the config file did not contain a $name element\n"
unless exists $$config{$name};
my $obj = $$config{$name};
my $ref = ref( $obj ) || "SCALAR";
die "Error: the config file has $name as a $ref when it should be a HASH\n"
unless $ref eq "HASH";
die "Error: $name option does not contain a value for type=$type\n"
unless exists $$obj{$type};
return $$obj{$type};
} # sub: get_config_type
# $boolean = check_config_exists( $config, $name );
#
# returns 1 if $name exists in the config variable, 0 otherwise.
#
# Note: got tired of having different methods in case things changes so using one for
# the moment
#
sub check_config_exists ($$) {
my $config = shift;
my $name = shift;
return exists $$config{$name};
} # sub: check_config_exists()
# check_type_known $site_config, $type
#
# dies unless the supplied type is allowed for this site
#
sub check_type_known ($$) {
my $site_config = shift;
my $type = shift;
my %_types = map { ($_,1) } @{ get_config_site( $site_config, "types" ) };
die "Error: unknown type ($type) for this site ($main::site)\n"
unless exists $_types{$type};
return;
} # sub: check_type_known()
# ( $version, $config_version, $dhead, $depth ) = check_location $site_config, $dname;
#
# checks we're in the right place and sets up some necessary variables
#
sub check_location ($$) {
my $site_config = shift;
my $dname = shift;
my ( $site_prefix, $depth_offset, $vinfo ) = get_config_site $site_config, "prefix", "depth_offset", "versions";
my $lp = length( $site_prefix );
# find out what version we are in
# $dname has previously been set to cwd()
#
die "Error: expected to be running within the $site_prefix directory structure\n"
unless substr($dname,0,$lp) eq $site_prefix;
die "Error: expected to be running in a sub-directory of $site_prefix\n"
if $dname eq $site_prefix;
my @dnames = split "/", substr($dname,$lp+1);
my $version = shift @dnames;
# do we know this version?
# - note: we are cheating since, at this point, $vinfo isn't technically
# the version config variable. However, since we are just dealing with
# hash references we can use the same bit of code.
# which isn't exactly brilliant.
#
$vinfo = get_config_version $vinfo, $version;
# are we locked?
die "Error:\n The pages for version=$version are locked from further publishing!\n"
if check_config_exists $vinfo, "locked";
my $dhead = join "/", @dnames;
$dhead .= "/" unless $dhead eq "";
# calculate the depth, including any offset from the config file
my $depth = 2 + $#dnames + $depth_offset;
return ( $version, $vinfo, $dhead, $depth );
} # sub: check_location
# $group = get_group $site_config;
#
# returns the group to use for the created pages (and
# checks that the user is a member of the group).
# dies on failuire
#
sub get_group ($) {
my $site_config = shift;
my $group = get_config_site $site_config, "group";
# check the group
my $allowed_groups = `groups`;
chomp $allowed_groups;
my %allowed_groups = map { ($_,1); } split " ", $allowed_groups;
die "Error: group was set to $group but you are only a member of:\n " .
join( " ", keys %allowed_groups ) . "\n"
unless exists $allowed_groups{$group};
return $group;
} # sub: get_group()
# Returns the location of the storage area (ie where we store the
# published version of a page + metadata) for a given site.
#
# $storageloc - location of the storage file
# $site - site name
#
sub get_storage_location ($$) {
my $storageloc = shift;
my $site = shift;
my $dom = $parser->parse_file($storageloc);
my $root = $dom->getDocumentElement();
my $loc = $root->findvalue('/storage/dir[@site="' . $site . '"]');
die "Unable to find site='$site' in $storageloc\n"
if $loc eq "";
return $loc;
} # sub: get_storage_location
# $ostype = get_ostype;
#
# returns a string representing the OS that the system
# is running:
# Return Value System