summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: 38b7152029a851d0651943abd6ff772f76e6f364 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki setup files are perl files that 'use IkiWiki::Setup::foo',
  3. # passing it some sort of configuration data.
  4. package IkiWiki::Setup;
  5. use warnings;
  6. use strict;
  7. use IkiWiki;
  8. use open qw{:utf8 :std};
  9. # There can be multiple modules, with different configuration styles.
  10. # The setup modules each convert the data into the hashes used by ikiwiki
  11. # internally (if it's not already in that format), and store it in
  12. # IkiWiki::Setup::$raw_setup, to pass it back to this module.
  13. our $raw_setup;
  14. sub load ($) { # {{{
  15. my $setup=IkiWiki::possibly_foolish_untaint(shift);
  16. delete $config{setup};
  17. #translators: The first parameter is a filename, and the second
  18. #translators: is a (probably not translated) error message.
  19. open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
  20. my $code;
  21. {
  22. local $/=undef;
  23. $code=<IN>;
  24. }
  25. ($code)=$code=~/(.*)/s;
  26. close IN;
  27. eval $code;
  28. error("$setup: ".$@) if $@;
  29. my %setup=%{$raw_setup};
  30. $raw_setup=undef;
  31. # Merge setup into existing config and untaint.
  32. if (exists $setup{add_plugins}) {
  33. push @{$setup{add_plugins}}, @{$config{add_plugins}};
  34. }
  35. if (exists $setup{exclude}) {
  36. push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
  37. }
  38. foreach my $c (keys %setup) {
  39. if (defined $setup{$c}) {
  40. if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
  41. $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
  42. }
  43. elsif (ref $setup{$c} eq 'ARRAY') {
  44. if ($c eq 'wrappers') {
  45. # backwards compatability code
  46. $config{$c}=$setup{$c};
  47. }
  48. else {
  49. $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
  50. }
  51. }
  52. elsif (ref $setup{$c} eq 'HASH') {
  53. foreach my $key (keys %{$setup{$c}}) {
  54. $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
  55. }
  56. }
  57. }
  58. else {
  59. $config{$c}=undef;
  60. }
  61. }
  62. if (length $config{cgi_wrapper}) {
  63. push @{$config{wrappers}}, {
  64. cgi => 1,
  65. wrapper => $config{cgi_wrapper},
  66. wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
  67. };
  68. }
  69. } #}}}
  70. sub dump ($) { #{{{
  71. my $file=IkiWiki::possibly_foolish_untaint(shift);
  72. require IkiWiki::Setup::Standard;
  73. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  74. open (OUT, ">", $file) || die "$file: $!";
  75. print OUT "$_\n" foreach @dump;
  76. close OUT;
  77. }
  78. 1