]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/editpage.pm
po(isvalidpo): more meaningful error message
[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 check_cansave ($$$$) {
85         my $page=shift;
86         my $content=shift;
87         my $q=shift;
88         my $session=shift;
90         my $cansave;
91         run_hooks(cansave => sub {
92                 return if defined $cansave;
93                 my $ret=shift->($page, $content, $q, $session);
94                 if (defined $ret) {
95                         if ($ret eq "") {
96                                 $cansave=1;
97                         }
98                         elsif (ref $ret eq 'CODE') {
99                                 $ret->();
100                                 $cansave=0;
101                         }
102                         else {
103                                 error($ret);
104                                 $cansave=0;
105                         }
106                 }
107         });
108         return $cansave;
111 sub cgi_editpage ($$) {
112         my $q=shift;
113         my $session=shift;
114         
115         my $do=$q->param('do');
116         return unless $do eq 'create' || $do eq 'edit';
118         decode_cgi_utf8($q);
120         my @fields=qw(do rcsinfo subpage from page type editcontent comments);
121         my @buttons=("Save Page", "Preview", "Cancel");
122         eval q{use CGI::FormBuilder};
123         error($@) if $@;
124         my $form = CGI::FormBuilder->new(
125                 fields => \@fields,
126                 charset => "utf-8",
127                 method => 'POST',
128                 required => [qw{editcontent}],
129                 javascript => 0,
130                 params => $q,
131                 action => $config{cgiurl},
132                 header => 0,
133                 table => 0,
134                 template => scalar template_params("editpage.tmpl"),
135         );
136         
137         decode_form_utf8($form);
138         run_hooks(formbuilder_setup => sub {
139                 shift->(form => $form, cgi => $q, session => $session,
140                         buttons => \@buttons);
141         });
142         decode_form_utf8($form);
143         
144         # This untaint is safe because we check file_pruned and
145         # wiki_file_regexp.
146         my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
147         $page=possibly_foolish_untaint($page);
148         my $absolute=($page =~ s#^/+##);
149         if (! defined $page || ! length $page ||
150             file_pruned($page, $config{srcdir})) {
151                 error(gettext("bad page name"));
152         }
154         my $baseurl = urlto($page, undef, 1);
156         my $from;
157         if (defined $form->field('from')) {
158                 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
159         }
160         
161         my $file;
162         my $type;
163         if (exists $pagesources{$page} && $form->field("do") ne "create") {
164                 $file=$pagesources{$page};
165                 $type=pagetype($file);
166                 if (! defined $type || $type=~/^_/) {
167                         error(sprintf(gettext("%s is not an editable page"), $page));
168                 }
169                 if (! $form->submitted) {
170                         $form->field(name => "rcsinfo",
171                                 value => rcs_prepedit($file), force => 1);
172                 }
173                 $form->field(name => "editcontent", validate => '/.*/');
174         }
175         else {
176                 $type=$form->param('type');
177                 if (defined $type && length $type && $hooks{htmlize}{$type}) {
178                         $type=possibly_foolish_untaint($type);
179                 }
180                 elsif (defined $from && exists $pagesources{$from}) {
181                         # favor the type of linking page
182                         $type=pagetype($pagesources{$from});
183                 }
184                 $type=$config{default_pageext} unless defined $type;
185                 $file=newpagefile($page, $type);
186                 if (! $form->submitted) {
187                         $form->field(name => "rcsinfo", value => "", force => 1);
188                 }
189                 $form->field(name => "editcontent", validate => '/.+/');
190         }
192         $form->field(name => "do", type => 'hidden');
193         $form->field(name => "sid", type => "hidden", value => $session->id,
194                 force => 1);
195         $form->field(name => "from", type => 'hidden');
196         $form->field(name => "rcsinfo", type => 'hidden');
197         $form->field(name => "subpage", type => 'hidden');
198         $form->field(name => "page", value => $page, force => 1);
199         $form->field(name => "type", value => $type, force => 1);
200         $form->field(name => "comments", type => "text", size => 80);
201         $form->field(name => "editcontent", type => "textarea", rows => 20,
202                 cols => 80);
203         $form->tmpl_param("can_commit", $config{rcs});
204         $form->tmpl_param("indexlink", indexlink());
205         $form->tmpl_param("helponformattinglink",
206                 htmllink($page, $page, "ikiwiki/formatting",
207                         noimageinline => 1,
208                         linktext => "FormattingHelp"));
209         
210         if ($form->submitted eq "Cancel") {
211                 if ($form->field("do") eq "create" && defined $from) {
212                         redirect($q, urlto($from, undef, 1));
213                 }
214                 elsif ($form->field("do") eq "create") {
215                         redirect($q, $config{url});
216                 }
217                 else {
218                         redirect($q, urlto($page, undef, 1));
219                 }
220                 exit;
221         }
222         elsif ($form->submitted eq "Preview") {
223                 my $new=not exists $pagesources{$page};
224                 if ($new) {
225                         # temporarily record its type
226                         $pagesources{$page}=$page.".".$type;
227                 }
228                 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
230                 my $content=$form->field('editcontent');
232                 run_hooks(editcontent => sub {
233                         $content=shift->(
234                                 content => $content,
235                                 page => $page,
236                                 cgi => $q,
237                                 session => $session,
238                         );
239                 });
240                 my $preview=htmlize($page, $page, $type,
241                         linkify($page, $page,
242                         preprocess($page, $page,
243                         filter($page, $page, $content), 0, 1)));
244                 run_hooks(format => sub {
245                         $preview=shift->(
246                                 page => $page,
247                                 content => $preview,
248                         );
249                 });
250                 $form->tmpl_param("page_preview", $preview);
251                 
252                 if ($new) {
253                         delete $pagesources{$page};
254                 }
256                 # Previewing may have created files on disk.
257                 # Keep a list of these to be deleted later.
258                 my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
259                 foreach my $f (@{$renderedfiles{$page}}) {
260                         $previews{$f}=1 unless $wasrendered{$f};
261                 }
262                 @{$wikistate{editpage}{previews}} = keys %previews;
263                 $renderedfiles{$page}=[keys %wasrendered];
264                 saveindex();
265         }
266         elsif ($form->submitted eq "Save Page") {
267                 $form->tmpl_param("page_preview", "");
268         }
269         
270         if ($form->submitted ne "Save Page" || ! $form->validate) {
271                 if ($form->field("do") eq "create") {
272                         my @page_locs;
273                         my $best_loc;
274                         if (! defined $from || ! length $from ||
275                             $from ne $form->field('from') ||
276                             file_pruned($from, $config{srcdir}) ||
277                             $from=~/^\// || 
278                             $absolute ||
279                             $form->submitted) {
280                                 @page_locs=$best_loc=$page;
281                         }
282                         else {
283                                 my $dir=$from."/";
284                                 $dir=~s![^/]+/+$!!;
285                                 
286                                 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
287                                     $page eq gettext('discussion')) {
288                                         $best_loc="$from/$page";
289                                 }
290                                 else {
291                                         $best_loc=$dir.$page;
292                                 }
293                                 
294                                 push @page_locs, $dir.$page;
295                                 push @page_locs, "$from/$page";
296                                 while (length $dir) {
297                                         $dir=~s![^/]+/+$!!;
298                                         push @page_locs, $dir.$page;
299                                 }
300                         
301                                 push @page_locs, "$config{userdir}/$page"
302                                         if length $config{userdir};
303                         }
305                         @page_locs = grep {
306                                 ! exists $pagecase{lc $_}
307                         } @page_locs;
308                         if (! @page_locs) {
309                                 # hmm, someone else made the page in the
310                                 # meantime?
311                                 if ($form->submitted eq "Preview") {
312                                         # let them go ahead with the edit
313                                         # and resolve the conflict at save
314                                         # time
315                                         @page_locs=$page;
316                                 }
317                                 else {
318                                         redirect($q, urlto($page, undef, 1));
319                                         exit;
320                                 }
321                         }
323                         my @editable_locs = grep {
324                                 check_canedit($_, $q, $session, 1)
325                         } @page_locs;
326                         if (! @editable_locs) {
327                                 # let it throw an error this time
328                                 map { check_canedit($_, $q, $session) } @page_locs;
329                         }
330                         
331                         my @page_types;
332                         if (exists $hooks{htmlize}) {
333                                 @page_types=grep { !/^_/ }
334                                         keys %{$hooks{htmlize}};
335                         }
336                         
337                         $form->tmpl_param("page_select", 1);
338                         $form->field(name => "page", type => 'select',
339                                 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
340                                 value => $best_loc);
341                         $form->field(name => "type", type => 'select',
342                                 options => \@page_types);
343                         $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
344                         
345                 }
346                 elsif ($form->field("do") eq "edit") {
347                         check_canedit($page, $q, $session);
348                         if (! defined $form->field('editcontent') || 
349                             ! length $form->field('editcontent')) {
350                                 my $content="";
351                                 if (exists $pagesources{$page}) {
352                                         $content=readfile(srcfile($pagesources{$page}));
353                                         $content=~s/\n/\r\n/g;
354                                 }
355                                 $form->field(name => "editcontent", value => $content,
356                                         force => 1);
357                         }
358                         $form->tmpl_param("page_select", 0);
359                         $form->field(name => "page", type => 'hidden');
360                         $form->field(name => "type", type => 'hidden');
361                         $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
362                 }
363                 
364                 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
365         }
366         else {
367                 # save page
368                 check_canedit($page, $q, $session);
369                 checksessionexpiry($q, $session, $q->param('sid'));
371                 my $exists=-e "$config{srcdir}/$file";
373                 if ($form->field("do") ne "create" && ! $exists &&
374                     ! defined srcfile($file, 1)) {
375                         $form->tmpl_param("message", template("editpagegone.tmpl")->output);
376                         $form->field(name => "do", value => "create", force => 1);
377                         $form->tmpl_param("page_select", 0);
378                         $form->field(name => "page", type => 'hidden');
379                         $form->field(name => "type", type => 'hidden');
380                         $form->title(sprintf(gettext("editing %s"), $page));
381                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
382                         exit;
383                 }
384                 elsif ($form->field("do") eq "create" && $exists) {
385                         $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
386                         $form->field(name => "do", value => "edit", force => 1);
387                         $form->tmpl_param("page_select", 0);
388                         $form->field(name => "page", type => 'hidden');
389                         $form->field(name => "type", type => 'hidden');
390                         $form->title(sprintf(gettext("editing %s"), $page));
391                         $form->field("editcontent", 
392                                 value => readfile("$config{srcdir}/$file").
393                                          "\n\n\n".$form->field("editcontent"),
394                                 force => 1);
395                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
396                         exit;
397                 }
398                 
399                 my $content=$form->field('editcontent');
400                 check_cansave($page, $content, $q, $session);
401                 run_hooks(editcontent => sub {
402                         $content=shift->(
403                                 content => $content,
404                                 page => $page,
405                                 cgi => $q,
406                                 session => $session,
407                         );
408                 });
409                 $content=~s/\r\n/\n/g;
410                 $content=~s/\r/\n/g;
411                 $content.="\n" if $content !~ /\n$/;
413                 $config{cgi}=0; # avoid cgi error message
414                 eval { writefile($file, $config{srcdir}, $content) };
415                 $config{cgi}=1;
416                 if ($@) {
417                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
418                                 force => 1);
419                         my $mtemplate=template("editfailedsave.tmpl");
420                         $mtemplate->param(error_message => $@);
421                         $form->tmpl_param("message", $mtemplate->output);
422                         $form->field("editcontent", value => $content, force => 1);
423                         $form->tmpl_param("page_select", 0);
424                         $form->field(name => "page", type => 'hidden');
425                         $form->field(name => "type", type => 'hidden');
426                         $form->title(sprintf(gettext("editing %s"), $page));
427                         showform($form, \@buttons, $session, $q,
428                                 forcebaseurl => $baseurl);
429                         exit;
430                 }
431                 
432                 my $conflict;
433                 if ($config{rcs}) {
434                         my $message="";
435                         if (defined $form->field('comments') &&
436                             length $form->field('comments')) {
437                                 $message=$form->field('comments');
438                         }
439                         
440                         if (! $exists) {
441                                 rcs_add($file);
442                         }
444                         # Prevent deadlock with post-commit hook by
445                         # signaling to it that it should not try to
446                         # do anything.
447                         disable_commit_hook();
448                         $conflict=rcs_commit($file, $message,
449                                 $form->field("rcsinfo"),
450                                 $session->param("name"), $ENV{REMOTE_ADDR});
451                         enable_commit_hook();
452                         rcs_update();
453                 }
454                 
455                 # Refresh even if there was a conflict, since other changes
456                 # may have been committed while the post-commit hook was
457                 # disabled.
458                 require IkiWiki::Render;
459                 refresh();
460                 saveindex();
462                 if (defined $conflict) {
463                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
464                                 force => 1);
465                         $form->tmpl_param("message", template("editconflict.tmpl")->output);
466                         $form->field("editcontent", value => $conflict, force => 1);
467                         $form->field("do", "edit", force => 1);
468                         $form->tmpl_param("page_select", 0);
469                         $form->field(name => "page", type => 'hidden');
470                         $form->field(name => "type", type => 'hidden');
471                         $form->title(sprintf(gettext("editing %s"), $page));
472                         showform($form, \@buttons, $session, $q,
473                                 forcebaseurl => $baseurl);
474                 }
475                 else {
476                         # The trailing question mark tries to avoid broken
477                         # caches and get the most recent version of the page.
478                         redirect($q, urlto($page, undef, 1)."?updated");
479                 }
480         }
482         exit;