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