]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/editpage.pm
remove deprecated googlecalendar plugin
[git.ikiwiki.info.git] / IkiWiki / Plugin / editpage.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::editpage;
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use open qw{:utf8 :std};
9 sub import {
10         hook(type => "getsetup", id => "editpage", call => \&getsetup);
11         hook(type => "refresh", id => "editpage", call => \&refresh);
12         hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
13 }
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 1,
20                 },
21 }
23 sub refresh () {
24         if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
25                 # Expire old preview files after one hour.
26                 my $expire=time - (60 * 60);
28                 my @previews;
29                 foreach my $file (@{$wikistate{editpage}{previews}}) {
30                         my $mtime=(stat("$config{destdir}/$file"))[9];
31                         if (defined $mtime && $mtime <= $expire) {
32                                 # Avoid deleting a preview that was later saved.
33                                 my $delete=1;
34                                 foreach my $page (keys %renderedfiles) {
35                                         if (grep { $_ eq $file } @{$renderedfiles{$page}}) {
36                                                 $delete=0;
37                                         }
38                                 }
39                                 if ($delete) {
40                                         debug(sprintf(gettext("removing old preview %s"), $file));
41                                         IkiWiki::prune("$config{destdir}/$file");
42                                 }
43                         }
44                         elsif (defined $mtime) {
45                                 push @previews, $file;
46                         }
47                 }
48                 $wikistate{editpage}{previews}=\@previews;
49         }
50 }
52 # Back to ikiwiki namespace for the rest, this code is very much
53 # internal to ikiwiki even though it's separated into a plugin,
54 # and other plugins use the functions below.
55 package IkiWiki;
57 sub check_canedit ($$$;$) {
58         my $page=shift;
59         my $q=shift;
60         my $session=shift;
61         my $nonfatal=shift;
62         
63         my $canedit;
64         run_hooks(canedit => sub {
65                 return if defined $canedit;
66                 my $ret=shift->($page, $q, $session);
67                 if (defined $ret) {
68                         if ($ret eq "") {
69                                 $canedit=1;
70                         }
71                         elsif (ref $ret eq 'CODE') {
72                                 $ret->() unless $nonfatal;
73                                 $canedit=0;
74                         }
75                         elsif (defined $ret) {
76                                 error($ret) unless $nonfatal;
77                                 $canedit=0;
78                         }
79                 }
80         });
81         return $canedit;
82 }
84 sub cgi_editpage ($$) {
85         my $q=shift;
86         my $session=shift;
87         
88         my $do=$q->param('do');
89         return unless $do eq 'create' || $do eq 'edit';
91         decode_cgi_utf8($q);
93         my @fields=qw(do rcsinfo subpage from page type editcontent comments);
94         my @buttons=("Save Page", "Preview", "Cancel");
95         eval q{use CGI::FormBuilder};
96         error($@) if $@;
97         my $form = CGI::FormBuilder->new(
98                 fields => \@fields,
99                 charset => "utf-8",
100                 method => 'POST',
101                 required => [qw{editcontent}],
102                 javascript => 0,
103                 params => $q,
104                 action => $config{cgiurl},
105                 header => 0,
106                 table => 0,
107                 template => scalar template_params("editpage.tmpl"),
108         );
109         
110         decode_form_utf8($form);
111         run_hooks(formbuilder_setup => sub {
112                 shift->(form => $form, cgi => $q, session => $session,
113                         buttons => \@buttons);
114         });
115         decode_form_utf8($form);
116         
117         # This untaint is safe because we check file_pruned and
118         # wiki_file_regexp.
119         my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
120         $page=possibly_foolish_untaint($page);
121         my $absolute=($page =~ s#^/+##);
122         if (! defined $page || ! length $page ||
123             file_pruned($page, $config{srcdir})) {
124                 error(gettext("bad page name"));
125         }
127         my $baseurl = urlto($page, undef, 1);
129         my $from;
130         if (defined $form->field('from')) {
131                 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
132         }
133         
134         my $file;
135         my $type;
136         if (exists $pagesources{$page} && $form->field("do") ne "create") {
137                 $file=$pagesources{$page};
138                 $type=pagetype($file);
139                 if (! defined $type || $type=~/^_/) {
140                         error(sprintf(gettext("%s is not an editable page"), $page));
141                 }
142                 if (! $form->submitted) {
143                         $form->field(name => "rcsinfo",
144                                 value => rcs_prepedit($file), force => 1);
145                 }
146                 $form->field(name => "editcontent", validate => '/.*/');
147         }
148         else {
149                 $type=$form->param('type');
150                 if (defined $type && length $type && $hooks{htmlize}{$type}) {
151                         $type=possibly_foolish_untaint($type);
152                 }
153                 elsif (defined $from && exists $pagesources{$from}) {
154                         # favor the type of linking page
155                         $type=pagetype($pagesources{$from});
156                 }
157                 $type=$config{default_pageext} unless defined $type;
158                 $file=newpagefile($page, $type);
159                 if (! $form->submitted) {
160                         $form->field(name => "rcsinfo", value => "", force => 1);
161                 }
162                 $form->field(name => "editcontent", validate => '/.+/');
163         }
165         $form->field(name => "do", type => 'hidden');
166         $form->field(name => "sid", type => "hidden", value => $session->id,
167                 force => 1);
168         $form->field(name => "from", type => 'hidden');
169         $form->field(name => "rcsinfo", type => 'hidden');
170         $form->field(name => "subpage", type => 'hidden');
171         $form->field(name => "page", value => $page, force => 1);
172         $form->field(name => "type", value => $type, force => 1);
173         $form->field(name => "comments", type => "text", size => 80);
174         $form->field(name => "editcontent", type => "textarea", rows => 20,
175                 cols => 80);
176         $form->tmpl_param("can_commit", $config{rcs});
177         $form->tmpl_param("indexlink", indexlink());
178         $form->tmpl_param("helponformattinglink",
179                 htmllink($page, $page, "ikiwiki/formatting",
180                         noimageinline => 1,
181                         linktext => "FormattingHelp"));
182         
183         if ($form->submitted eq "Cancel") {
184                 if ($form->field("do") eq "create" && defined $from) {
185                         redirect($q, urlto($from, undef, 1));
186                 }
187                 elsif ($form->field("do") eq "create") {
188                         redirect($q, $config{url});
189                 }
190                 else {
191                         redirect($q, urlto($page, undef, 1));
192                 }
193                 exit;
194         }
195         elsif ($form->submitted eq "Preview") {
196                 my $new=not exists $pagesources{$page};
197                 if ($new) {
198                         # temporarily record its type
199                         $pagesources{$page}=$page.".".$type;
200                 }
201                 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
203                 my $content=$form->field('editcontent');
205                 run_hooks(editcontent => sub {
206                         $content=shift->(
207                                 content => $content,
208                                 page => $page,
209                                 cgi => $q,
210                                 session => $session,
211                         );
212                 });
213                 my $preview=htmlize($page, $page, $type,
214                         linkify($page, $page,
215                         preprocess($page, $page,
216                         filter($page, $page, $content), 0, 1)));
217                 run_hooks(format => sub {
218                         $preview=shift->(
219                                 page => $page,
220                                 content => $preview,
221                         );
222                 });
223                 $form->tmpl_param("page_preview", $preview);
224                 
225                 if ($new) {
226                         delete $pagesources{$page};
227                 }
229                 # Previewing may have created files on disk.
230                 # Keep a list of these to be deleted later.
231                 my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
232                 foreach my $f (@{$renderedfiles{$page}}) {
233                         $previews{$f}=1 unless $wasrendered{$f};
234                 }
235                 @{$wikistate{editpage}{previews}} = keys %previews;
236                 $renderedfiles{$page}=[keys %wasrendered];
237                 saveindex();
238         }
239         elsif ($form->submitted eq "Save Page") {
240                 $form->tmpl_param("page_preview", "");
241         }
242         
243         if ($form->submitted ne "Save Page" || ! $form->validate) {
244                 if ($form->field("do") eq "create") {
245                         my @page_locs;
246                         my $best_loc;
247                         if (! defined $from || ! length $from ||
248                             $from ne $form->field('from') ||
249                             file_pruned($from, $config{srcdir}) ||
250                             $from=~/^\// || 
251                             $absolute ||
252                             $form->submitted) {
253                                 @page_locs=$best_loc=$page;
254                         }
255                         else {
256                                 my $dir=$from."/";
257                                 $dir=~s![^/]+/+$!!;
258                                 
259                                 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
260                                     $page eq gettext('discussion')) {
261                                         $best_loc="$from/$page";
262                                 }
263                                 else {
264                                         $best_loc=$dir.$page;
265                                 }
266                                 
267                                 push @page_locs, $dir.$page;
268                                 push @page_locs, "$from/$page";
269                                 while (length $dir) {
270                                         $dir=~s![^/]+/+$!!;
271                                         push @page_locs, $dir.$page;
272                                 }
273                         
274                                 push @page_locs, "$config{userdir}/$page"
275                                         if length $config{userdir};
276                         }
278                         @page_locs = grep {
279                                 ! exists $pagecase{lc $_}
280                         } @page_locs;
281                         if (! @page_locs) {
282                                 # hmm, someone else made the page in the
283                                 # meantime?
284                                 if ($form->submitted eq "Preview") {
285                                         # let them go ahead with the edit
286                                         # and resolve the conflict at save
287                                         # time
288                                         @page_locs=$page;
289                                 }
290                                 else {
291                                         redirect($q, urlto($page, undef, 1));
292                                         exit;
293                                 }
294                         }
296                         my @editable_locs = grep {
297                                 check_canedit($_, $q, $session, 1)
298                         } @page_locs;
299                         if (! @editable_locs) {
300                                 # let it throw an error this time
301                                 map { check_canedit($_, $q, $session) } @page_locs;
302                         }
303                         
304                         my @page_types;
305                         if (exists $hooks{htmlize}) {
306                                 @page_types=grep { !/^_/ }
307                                         keys %{$hooks{htmlize}};
308                         }
309                         
310                         $form->tmpl_param("page_select", 1);
311                         $form->field(name => "page", type => 'select',
312                                 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
313                                 value => $best_loc);
314                         $form->field(name => "type", type => 'select',
315                                 options => \@page_types);
316                         $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
317                         
318                 }
319                 elsif ($form->field("do") eq "edit") {
320                         check_canedit($page, $q, $session);
321                         if (! defined $form->field('editcontent') || 
322                             ! length $form->field('editcontent')) {
323                                 my $content="";
324                                 if (exists $pagesources{$page}) {
325                                         $content=readfile(srcfile($pagesources{$page}));
326                                         $content=~s/\n/\r\n/g;
327                                 }
328                                 $form->field(name => "editcontent", value => $content,
329                                         force => 1);
330                         }
331                         $form->tmpl_param("page_select", 0);
332                         $form->field(name => "page", type => 'hidden');
333                         $form->field(name => "type", type => 'hidden');
334                         $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
335                 }
336                 
337                 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
338         }
339         else {
340                 # save page
341                 check_canedit($page, $q, $session);
342                 checksessionexpiry($q, $session, $q->param('sid'));
344                 my $exists=-e "$config{srcdir}/$file";
346                 if ($form->field("do") ne "create" && ! $exists &&
347                     ! defined srcfile($file, 1)) {
348                         $form->tmpl_param("message", template("editpagegone.tmpl")->output);
349                         $form->field(name => "do", value => "create", force => 1);
350                         $form->tmpl_param("page_select", 0);
351                         $form->field(name => "page", type => 'hidden');
352                         $form->field(name => "type", type => 'hidden');
353                         $form->title(sprintf(gettext("editing %s"), $page));
354                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
355                         exit;
356                 }
357                 elsif ($form->field("do") eq "create" && $exists) {
358                         $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
359                         $form->field(name => "do", value => "edit", force => 1);
360                         $form->tmpl_param("page_select", 0);
361                         $form->field(name => "page", type => 'hidden');
362                         $form->field(name => "type", type => 'hidden');
363                         $form->title(sprintf(gettext("editing %s"), $page));
364                         $form->field("editcontent", 
365                                 value => readfile("$config{srcdir}/$file").
366                                          "\n\n\n".$form->field("editcontent"),
367                                 force => 1);
368                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
369                         exit;
370                 }
371                 
372                 my $content=$form->field('editcontent');
373                 run_hooks(editcontent => sub {
374                         $content=shift->(
375                                 content => $content,
376                                 page => $page,
377                                 cgi => $q,
378                                 session => $session,
379                         );
380                 });
381                 $content=~s/\r\n/\n/g;
382                 $content=~s/\r/\n/g;
383                 $content.="\n" if $content !~ /\n$/;
385                 $config{cgi}=0; # avoid cgi error message
386                 eval { writefile($file, $config{srcdir}, $content) };
387                 $config{cgi}=1;
388                 if ($@) {
389                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
390                                 force => 1);
391                         my $mtemplate=template("editfailedsave.tmpl");
392                         $mtemplate->param(error_message => $@);
393                         $form->tmpl_param("message", $mtemplate->output);
394                         $form->field("editcontent", value => $content, force => 1);
395                         $form->tmpl_param("page_select", 0);
396                         $form->field(name => "page", type => 'hidden');
397                         $form->field(name => "type", type => 'hidden');
398                         $form->title(sprintf(gettext("editing %s"), $page));
399                         showform($form, \@buttons, $session, $q,
400                                 forcebaseurl => $baseurl);
401                         exit;
402                 }
403                 
404                 my $conflict;
405                 if ($config{rcs}) {
406                         my $message="";
407                         if (defined $form->field('comments') &&
408                             length $form->field('comments')) {
409                                 $message=$form->field('comments');
410                         }
411                         
412                         if (! $exists) {
413                                 rcs_add($file);
414                         }
416                         # Prevent deadlock with post-commit hook by
417                         # signaling to it that it should not try to
418                         # do anything.
419                         disable_commit_hook();
420                         $conflict=rcs_commit($file, $message,
421                                 $form->field("rcsinfo"),
422                                 $session->param("name"), $ENV{REMOTE_ADDR});
423                         enable_commit_hook();
424                         rcs_update();
425                 }
426                 
427                 # Refresh even if there was a conflict, since other changes
428                 # may have been committed while the post-commit hook was
429                 # disabled.
430                 require IkiWiki::Render;
431                 refresh();
432                 saveindex();
434                 if (defined $conflict) {
435                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
436                                 force => 1);
437                         $form->tmpl_param("message", template("editconflict.tmpl")->output);
438                         $form->field("editcontent", value => $conflict, force => 1);
439                         $form->field("do", "edit", force => 1);
440                         $form->tmpl_param("page_select", 0);
441                         $form->field(name => "page", type => 'hidden');
442                         $form->field(name => "type", type => 'hidden');
443                         $form->title(sprintf(gettext("editing %s"), $page));
444                         showform($form, \@buttons, $session, $q,
445                                 forcebaseurl => $baseurl);
446                 }
447                 else {
448                         # The trailing question mark tries to avoid broken
449                         # caches and get the most recent version of the page.
450                         redirect($q, urlto($page, undef, 1)."?updated");
451                 }
452         }
454         exit;