summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: d14be879d5f1aab1f432d46bd42f9d824e95c14a (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. my @plugins=grep { $_ ne $config{rcs} } sort(IkiWiki::listplugins());
  72. unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
  73. foreach my $plugin (@plugins) {
  74. eval { IkiWiki::loadplugin($plugin) };
  75. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  76. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  77. }
  78. }
  79. foreach my $plugin (@plugins) {
  80. if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
  81. # use an array rather than a hash, to preserve order
  82. my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
  83. next unless @s;
  84. push @ret, [ $plugin, \@s ],
  85. }
  86. }
  87. return @ret;
  88. } #}}}
  89. sub dump ($) { #{{{
  90. my $file=IkiWiki::possibly_foolish_untaint(shift);
  91. require IkiWiki::Setup::Standard;
  92. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  93. open (OUT, ">", $file) || die "$file: $!";
  94. print OUT "$_\n" foreach @dump;
  95. close OUT;
  96. }
  97. 1