summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: d2d1e4b8e7fd44b88c0ed64f110e609f78785ea5 (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
  68. # ordered list of [plugin, setup] pairs.
  69. my @ret;
  70. # disable logging to syslog while dumping, broken plugins may
  71. # whine when loaded
  72. my $syslog=$config{syslog};
  73. $config{syslog}=0;
  74. # Load all plugins, so that all setup options are available.
  75. my @plugins=grep { $_ ne $config{rcs} } sort(IkiWiki::listplugins());
  76. unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
  77. foreach my $plugin (@plugins) {
  78. eval { IkiWiki::loadplugin($plugin) };
  79. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  80. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  81. }
  82. }
  83. foreach my $plugin (@plugins) {
  84. if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
  85. # use an array rather than a hash, to preserve order
  86. my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
  87. next unless @s;
  88. push @ret, [ $plugin, \@s ],
  89. }
  90. }
  91. $config{syslog}=$syslog;
  92. return @ret;
  93. } #}}}
  94. sub dump ($) { #{{{
  95. my $file=IkiWiki::possibly_foolish_untaint(shift);
  96. require IkiWiki::Setup::Standard;
  97. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  98. open (OUT, ">", $file) || die "$file: $!";
  99. print OUT "$_\n" foreach @dump;
  100. close OUT;
  101. }
  102. 1