]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/edittemplate.pm
Provide the current time to edittemplate.
[ikiwiki.git] / IkiWiki / Plugin / edittemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::edittemplate;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use HTML::Template;
8 use Encode;
9
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 }
20
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => undef,
26                         section => "web",
27                 },
28 }
29
30 sub needsbuild (@) {
31         my $needsbuild=shift;
32
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         }
44
45         return $needsbuild;
46 }
47
48 sub preprocess (@) {
49         my %params=@_;
50
51         return "" if $params{page} ne $params{destpage};
52
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         }
59
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;
69
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 }
76
77 sub formbuilder (@) {
78         my %params=@_;
79         my $form=$params{form};
80
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;
95
96                         # FormBuilder is on the bad crack. See #551499
97                         my @options=map { ref $_ ? @$_ : $_ } $field->options;
98
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         }
121 }
122
123 sub filltemplate ($$) {
124         my $template_page=shift;
125         my $page=shift;
126
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         }
139
140         $template->param(name => $page);
141
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         }
157
158         my $time = time();
159         $template->param(time => IkiWiki::formattime($time, "%Y-%m-%d %H:%M:%S"));
160         $template->param(formatted_time => IkiWiki::formattime($time));
161
162         return $template->output;
163 }
164
165 1