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