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