]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/editpage.pm
Point to the live versions of the plugins.
[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 editmessage);
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 => IkiWiki::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         if (! defined $page) {
95                 error(gettext("bad page name"));
96         }
97         $page=possibly_foolish_untaint($page);
98         my $absolute=($page =~ s#^/+##); # absolute name used to force location
99         if (! defined $page || ! length $page ||
100             file_pruned($page)) {
101                 error(gettext("bad page name"));
102         }
104         my $baseurl = urlto($page);
106         my $from;
107         if (defined $form->field('from')) {
108                 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
109         }
110         
111         my $file;
112         my $type;
113         if (exists $pagesources{$page} && $form->field("do") ne "create") {
114                 $file=$pagesources{$page};
115                 $type=pagetype($file);
116                 if (! defined $type || $type=~/^_/) {
117                         error(sprintf(gettext("%s is not an editable page"), $page));
118                 }
119                 if (! $form->submitted) {
120                         $form->field(name => "rcsinfo",
121                                 value => rcs_prepedit($file), force => 1);
122                 }
123                 $form->field(name => "editcontent", validate => '/.*/');
124         }
125         else {
126                 $type=$form->param('type');
127                 if (defined $type && length $type && $hooks{htmlize}{$type}) {
128                         $type=possibly_foolish_untaint($type);
129                 }
130                 elsif (defined $from && exists $pagesources{$from}) {
131                         # favor the type of linking page
132                         $type=pagetype($pagesources{$from});
133                 }
134                 $type=$config{default_pageext}
135                         if ! defined $type || $type=~/^_/; # not internal type
136                 $file=newpagefile($page, $type);
137                 if (! $form->submitted) {
138                         $form->field(name => "rcsinfo", value => "", force => 1);
139                 }
140                 $form->field(name => "editcontent", validate => '/.+/');
141         }
143         $form->field(name => "do", type => 'hidden');
144         $form->field(name => "sid", type => "hidden", value => $session->id,
145                 force => 1);
146         $form->field(name => "from", type => 'hidden');
147         $form->field(name => "rcsinfo", type => 'hidden');
148         $form->field(name => "subpage", type => 'hidden');
149         $form->field(name => "page", value => $page, force => 1);
150         $form->field(name => "type", value => $type, force => 1);
151         $form->field(name => "editmessage", type => "text", size => 80);
152         $form->field(name => "editcontent", type => "textarea", rows => 20,
153                 cols => 80);
154         $form->tmpl_param("can_commit", $config{rcs});
155         $form->tmpl_param("helponformattinglink",
156                 htmllink($page, $page, "ikiwiki/formatting",
157                         noimageinline => 1,
158                         linktext => "FormattingHelp"));
159         
160         my $previewing=0;
161         if ($form->submitted eq "Cancel") {
162                 if ($form->field("do") eq "create" && defined $from) {
163                         redirect($q, urlto($from));
164                 }
165                 elsif ($form->field("do") eq "create") {
166                         redirect($q, baseurl(undef));
167                 }
168                 else {
169                         redirect($q, $baseurl);
170                 }
171                 exit;
172         }
173         elsif ($form->submitted eq "Preview") {
174                 $previewing=1;
176                 my $new=not exists $pagesources{$page};
177                 # temporarily record its type
178                 $pagesources{$page}=$page.".".$type if $new;
179                 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
181                 my $content=$form->field('editcontent');
183                 run_hooks(editcontent => sub {
184                         $content=shift->(
185                                 content => $content,
186                                 page => $page,
187                                 cgi => $q,
188                                 session => $session,
189                         );
190                 });
191                 my $preview=htmlize($page, $page, $type,
192                         linkify($page, $page,
193                         preprocess($page, $page,
194                         filter($page, $page, $content), 0, 1)));
195                 run_hooks(format => sub {
196                         $preview=shift->(
197                                 page => $page,
198                                 content => $preview,
199                         );
200                 });
201                 $form->tmpl_param("page_preview", $preview);
202                 
203                 # Previewing may have created files on disk.
204                 # Keep a list of these to be deleted later.
205                 my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
206                 foreach my $f (@{$renderedfiles{$page}}) {
207                         $previews{$f}=1 unless $wasrendered{$f};
208                 }
210                 # Throw out any other state changes made during previewing,
211                 # and save the previews list.
212                 loadindex();
213                 @{$wikistate{editpage}{previews}} = keys %previews;
214                 saveindex();
215         }
216         elsif ($form->submitted eq "Save Page") {
217                 $form->tmpl_param("page_preview", "");
218         }
219         
220         if ($form->submitted ne "Save Page" || ! $form->validate) {
221                 if ($form->field("do") eq "create") {
222                         my @page_locs;
223                         my $best_loc;
224                         if (! defined $from || ! length $from ||
225                             $from ne $form->field('from') ||
226                             file_pruned($from) ||
227                             $absolute ||
228                             $form->submitted) {
229                                 @page_locs=$best_loc=$page;
230                                 unshift @page_locs, lc($page)
231                                         if ! $form->submitted && lc($page) ne $page;
232                         }
233                         else {
234                                 my $dir=$from."/";
235                                 $dir=~s![^/]+/+$!!;
236                                 
237                                 if ((defined $form->field('subpage') &&
238                                      length $form->field('subpage')) ||
239                                     $page eq lc($config{discussionpage})) {
240                                         $best_loc="$from/$page";
241                                 }
242                                 else {
243                                         $best_loc=$dir.$page;
244                                 }
245                                 
246                                 my $mixedcase=lc($page) ne $page;
248                                 push @page_locs, $dir.lc($page) if $mixedcase;
249                                 push @page_locs, $dir.$page;
250                                 push @page_locs, $from."/".lc($page) if $mixedcase;
251                                 push @page_locs, $from."/".$page;
252                                 while (length $dir) {
253                                         $dir=~s![^/]+/+$!!;
254                                         push @page_locs, $dir.lc($page) if $mixedcase;
255                                         push @page_locs, $dir.$page;
256                                 }
258                                 my $userpage=IkiWiki::userpage($page);
259                                 push @page_locs, $userpage
260                                         if ! grep { $_ eq $userpage } @page_locs;
261                         }
263                         @page_locs = grep {
264                                 ! exists $pagecase{lc $_}
265                         } @page_locs;
266                         if (! @page_locs) {
267                                 # hmm, someone else made the page in the
268                                 # meantime?
269                                 if ($previewing) {
270                                         # let them go ahead with the edit
271                                         # and resolve the conflict at save
272                                         # time
273                                         @page_locs=$page;
274                                 }
275                                 else {
276                                         redirect($q, $baseurl);
277                                         exit;
278                                 }
279                         }
281                         my @editable_locs = grep {
282                                 check_canedit($_, $q, $session, 1)
283                         } @page_locs;
284                         if (! @editable_locs) {
285                                 # now let it throw an error, or prompt for
286                                 # login
287                                 map { check_canedit($_, $q, $session) }
288                                         ($best_loc, @page_locs);
289                         }
290                         
291                         my @page_types;
292                         if (exists $hooks{htmlize}) {
293                                 foreach my $key (grep { !/^_/ } keys %{$hooks{htmlize}}) {
294                                         push @page_types, [$key, $hooks{htmlize}{$key}{longname} || $key];
295                                 }
296                         }
297                         @page_types=sort @page_types;
298                         
299                         $form->tmpl_param("page_select", 1);
300                         $form->field(name => "page", type => 'select',
301                                 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
302                                 value => $best_loc);
303                         $form->field(name => "type", type => 'select',
304                                 options => \@page_types);
305                         $form->title(sprintf(gettext("creating %s"), pagetitle(basename($page))));
306                         
307                 }
308                 elsif ($form->field("do") eq "edit") {
309                         check_canedit($page, $q, $session);
310                         if (! defined $form->field('editcontent') || 
311                             ! length $form->field('editcontent')) {
312                                 my $content="";
313                                 if (exists $pagesources{$page}) {
314                                         $content=readfile(srcfile($pagesources{$page}));
315                                         $content=~s/\n/\r\n/g;
316                                 }
317                                 $form->field(name => "editcontent", value => $content,
318                                         force => 1);
319                         }
320                         $form->tmpl_param("page_select", 0);
321                         $form->field(name => "page", type => 'hidden');
322                         $form->field(name => "type", type => 'hidden');
323                         $form->title(sprintf(gettext("editing %s"), pagetitle(basename($page))));
324                 }
325                 
326                 showform($form, \@buttons, $session, $q, page => $page);
327         }
328         else {
329                 # save page
330                 check_canedit($page, $q, $session);
331                 checksessionexpiry($q, $session, $q->param('sid'));
333                 my $exists=-e "$config{srcdir}/$file";
335                 if ($form->field("do") ne "create" && ! $exists &&
336                     ! defined srcfile($file, 1)) {
337                         $form->tmpl_param("message", template("editpagegone.tmpl")->output);
338                         $form->field(name => "do", value => "create", force => 1);
339                         $form->tmpl_param("page_select", 0);
340                         $form->field(name => "page", type => 'hidden');
341                         $form->field(name => "type", type => 'hidden');
342                         $form->title(sprintf(gettext("editing %s"), $page));
343                         showform($form, \@buttons, $session, $q,
344                                 page => $page);
345                         exit;
346                 }
347                 elsif ($form->field("do") eq "create" && $exists) {
348                         $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
349                         $form->field(name => "do", value => "edit", 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                         $form->field("editcontent", 
355                                 value => readfile("$config{srcdir}/$file").
356                                          "\n\n\n".$form->field("editcontent"),
357                                 force => 1);
358                         showform($form, \@buttons, $session, $q,
359                                 page => $page);
360                         exit;
361                 }
362                         
363                 my $message="";
364                 if (defined $form->field('editmessage') &&
365                     length $form->field('editmessage')) {
366                         $message=$form->field('editmessage');
367                 }
368                 
369                 my $content=$form->field('editcontent');
370                 check_content(content => $content, page => $page,
371                         cgi => $q, session => $session,
372                         subject => $message);
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                                 page => $page);
401                         exit;
402                 }
403                 
404                 my $conflict;
405                 if ($config{rcs}) {
406                         if (! $exists) {
407                                 rcs_add($file);
408                         }
410                         # Prevent deadlock with post-commit hook by
411                         # signaling to it that it should not try to
412                         # do anything.
413                         disable_commit_hook();
414                         $conflict=rcs_commit(
415                                 file => $file,
416                                 message => $message,
417                                 token => $form->field("rcsinfo"),
418                                 session => $session,
419                         );
420                         enable_commit_hook();
421                         rcs_update();
422                 }
423                 
424                 # Refresh even if there was a conflict, since other changes
425                 # may have been committed while the post-commit hook was
426                 # disabled.
427                 require IkiWiki::Render;
428                 refresh();
429                 saveindex();
431                 if (defined $conflict) {
432                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
433                                 force => 1);
434                         $form->tmpl_param("message", template("editconflict.tmpl")->output);
435                         $form->field("editcontent", value => $conflict, force => 1);
436                         $form->field("do", "edit", force => 1);
437                         $form->tmpl_param("page_select", 0);
438                         $form->field(name => "page", type => 'hidden');
439                         $form->field(name => "type", type => 'hidden');
440                         $form->title(sprintf(gettext("editing %s"), $page));
441                         showform($form, \@buttons, $session, $q,
442                                 page => $page);
443                 }
444                 else {
445                         # The trailing question mark tries to avoid broken
446                         # caches and get the most recent version of the page.
447                         redirect($q, $baseurl."?updated");
448                 }
449         }
451         exit;