summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: a3fd5ce66466f45e1480776e31be39ece90f592b (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. # disable logging to syslog while dumping, broken plugins may
  71. # whine when loaded
  72. my $syslog=$config{syslog};
  73. $config{syslog}=undef;
  74. # Load all plugins, so that all setup options are available.
  75. my @plugins=IkiWiki::listplugins();
  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. my %sections;
  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. # set default section value (note use of shared
  89. # hashref between array and hash)
  90. my %s=@s;
  91. if (! exists $s{plugin} || ! $s{plugin}->{section}) {
  92. $s{plugin}->{section}="other";
  93. }
  94. # only the selected rcs plugin is included
  95. if ($config{rcs} && $plugin eq $config{rcs}) {
  96. $s{plugin}->{section}="core";
  97. }
  98. elsif ($s{plugin}->{section} eq "rcs") {
  99. next;
  100. }
  101. push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
  102. }
  103. }
  104. $config{syslog}=$syslog;
  105. return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
  106. sort { # core first, other last, otherwise alphabetical
  107. ($b eq "core") <=> ($a eq "core")
  108. ||
  109. ($a eq "other") <=> ($b eq "other")
  110. ||
  111. $a cmp $b
  112. } keys %sections;
  113. }
  114. sub dump ($) {
  115. my $file=IkiWiki::possibly_foolish_untaint(shift);
  116. require IkiWiki::Setup::Standard;
  117. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  118. open (OUT, ">", $file) || die "$file: $!";
  119. print OUT "$_\n" foreach @dump;
  120. close OUT;
  121. }
  122. 1