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