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