]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/edittemplate.pm
Avoid uninitialized warnings with comments+no CGI.
[git.ikiwiki.info.git] / IkiWiki / Plugin / edittemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::edittemplate;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use HTML::Template;
8 use Encode;
10 sub import {
11         hook(type => "getsetup", id => "edittemplate",
12                 call => \&getsetup);
13         hook(type => "needsbuild", id => "edittemplate",
14                 call => \&needsbuild);
15         hook(type => "preprocess", id => "edittemplate",
16                 call => \&preprocess);
17         hook(type => "formbuilder", id => "edittemplate",
18                 call => \&formbuilder);
19 }
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => undef,
26                         section => "web",
27                 },
28 }
30 sub needsbuild (@) {
31         my $needsbuild=shift;
33         foreach my $page (keys %pagestate) {
34                 if (exists $pagestate{$page}{edittemplate}) {
35                         if (exists $pagesources{$page} && 
36                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
37                                 # remove state, it will be re-added
38                                 # if the preprocessor directive is still
39                                 # there during the rebuild
40                                 delete $pagestate{$page}{edittemplate};
41                         }
42                 }
43         }
45         return $needsbuild;
46 }
48 sub preprocess (@) {
49         my %params=@_;
51         return "" if $params{page} ne $params{destpage};
53         if (! exists $params{template} || ! length($params{template})) {
54                 error gettext("template not specified")
55         }
56         if (! exists $params{match} || ! length($params{match})) {
57                 error gettext("match not specified")
58         }
60         my $link=linkpage($params{template});
61         add_depends($params{page}, $link, deptype("presence"));
62         my $bestlink=bestlink($params{page}, $link);
63         if (! length $bestlink) {
64                 add_depends($params{page}, "templates/$link", deptype("presence"));
65                 $link="/templates/".$link;
66                 $bestlink=bestlink($params{page}, $link);
67         }
68         $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
70         return "" if ($params{silent} && IkiWiki::yesno($params{silent})) &&
71                 length $bestlink;
72         return sprintf(gettext("edittemplate %s registered for %s"),
73                 htmllink($params{page}, $params{destpage}, $link),
74                 $params{match});
75 }
77 sub formbuilder (@) {
78         my %params=@_;
79         my $form=$params{form};
81         return if $form->field("do") ne "create" ||
82                 (defined $form->field("editcontent") && length $form->field("editcontent"));
83         
84         my $page=$form->field("page");
85         
86         # The tricky bit here is that $page is probably just the base
87         # page name, without any subdir, but the pagespec for a template
88         # probably does include the subdir (ie, "bugs/*"). We don't know
89         # what subdir the user will pick to put the page in. So, try them
90         # all, starting with the one that was made default.
91         my @page_locs=$page;
92         foreach my $field ($form->field) {
93                 if ($field eq 'page') {
94                         @page_locs=$field->def_value;
96                         # FormBuilder is on the bad crack. See #551499
97                         my @options=map { ref $_ ? @$_ : $_ } $field->options;
99                         push @page_locs, @options;
100                 }
101         }
102         foreach my $p (@page_locs) {
103                 foreach my $registering_page (keys %pagestate) {
104                         if (exists $pagestate{$registering_page}{edittemplate}) {
105                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
106                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
107                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
108                                                 $form->field(name => "editcontent",
109                                                          value =>  filltemplate($template, $page));
110                                                 my $type=pagetype($pagesources{$template})
111                                                                 if $pagesources{$template};
112                                                 $form->field(name => "type",
113                                                          value => $type)
114                                                                 if defined $type;
115                                                 return;
116                                         }
117                                 }
118                         }
119                 }
120         }
123 sub filltemplate ($$) {
124         my $template_page=shift;
125         my $page=shift;
127         my $template;
128         eval {
129                 # force page name absolute so it doesn't look in templates/
130                 $template=template("/".$template_page);
131         };
132         if ($@) {
133                 # gettext can clobber $@
134                 my $error = $@;
135                 # Indicate that the earlier preprocessor directive set 
136                 # up a template that doesn't work.
137                 return "[[!edittemplate ".gettext("failed to process template:")." $error]]";
138         }
140         $template->param(name => $page);
142         if ($template->query(name => 'uuid')) {
143                 my $uuid;
144                 if (open(my $fh, "<", "/proc/sys/kernel/random/uuid")) {
145                         $uuid = <$fh>;
146                         chomp $uuid;
147                         close $fh;
148                 }
149                 else {
150                         eval {
151                                 require UUID::Tiny;
152                                 $uuid = UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V4());
153                         };
154                 }
155                 $template->param(uuid => $uuid);
156         }
158         my $time = time();
159         $template->param(time => IkiWiki::date_3339($time));
161         return $template->output;