summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: 73e32447c05c651eead1617ddcaaa39d650ceb96 (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. sub load ($) { # {{{
  10. my $setup=IkiWiki::possibly_foolish_untaint(shift);
  11. delete $config{setup};
  12. #translators: The first parameter is a filename, and the second
  13. #translators: is a (probably not translated) error message.
  14. open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
  15. my $code;
  16. {
  17. local $/=undef;
  18. $code=<IN>;
  19. }
  20. ($code)=$code=~/(.*)/s;
  21. close IN;
  22. eval $code;
  23. error("$setup: ".$@) if $@;
  24. } #}}}
  25. sub merge ($) {
  26. # Merge setup into existing config and untaint.
  27. my %setup=%{shift()};
  28. if (exists $setup{add_plugins}) {
  29. push @{$setup{add_plugins}}, @{$config{add_plugins}};
  30. }
  31. if (exists $setup{exclude}) {
  32. push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
  33. }
  34. foreach my $c (keys %setup) {
  35. if (defined $setup{$c}) {
  36. if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
  37. $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
  38. }
  39. elsif (ref $setup{$c} eq 'ARRAY') {
  40. if ($c eq 'wrappers') {
  41. # backwards compatability code
  42. $config{$c}=$setup{$c};
  43. }
  44. else {
  45. $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
  46. }
  47. }
  48. elsif (ref $setup{$c} eq 'HASH') {
  49. foreach my $key (keys %{$setup{$c}}) {
  50. $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
  51. }
  52. }
  53. }
  54. else {
  55. $config{$c}=undef;
  56. }
  57. }
  58. if (length $config{cgi_wrapper}) {
  59. push @{$config{wrappers}}, {
  60. cgi => 1,
  61. wrapper => $config{cgi_wrapper},
  62. wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
  63. };
  64. }
  65. } #}}}
  66. sub getsetup () { #{{{
  67. # Gets all available setup data from all plugins. Returns an ordered list of
  68. # [plugin, setup] pairs.
  69. my @ret;
  70. # Load all plugins, so that all setup options are available.
  71. # (But skip a few problematic external demo plugins.)
  72. my @plugins=grep { ! /^(externaldemo|pythondemo|\Q$config{rcs}\E)$/ }
  73. sort(IkiWiki::listplugins());
  74. unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
  75. foreach my $plugin (@plugins) {
  76. eval { IkiWiki::loadplugin($plugin) };
  77. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  78. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  79. }
  80. }
  81. foreach my $plugin (@plugins) {
  82. if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
  83. # use an array rather than a hash, to preserve order
  84. my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
  85. next unless @s;
  86. push @ret, [ $plugin, \@s ],
  87. }
  88. }
  89. return @ret;
  90. } #}}}
  91. sub dump ($) { #{{{
  92. my $file=IkiWiki::possibly_foolish_untaint(shift);
  93. require IkiWiki::Setup::Standard;
  94. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  95. open (OUT, ">", $file) || die "$file: $!";
  96. print OUT "$_\n" foreach @dump;
  97. close OUT;
  98. }
  99. 1