]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/editpage.pm
fix back-compat with old Net::OpenID
[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 => scalar template_params("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#^/+##);
96         if (! defined $page || ! length $page ||
97             file_pruned($page, $config{srcdir})) {
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("indexlink", indexlink());
152         $form->tmpl_param("helponformattinglink",
153                 htmllink($page, $page, "ikiwiki/formatting",
154                         noimageinline => 1,
155                         linktext => "FormattingHelp"));
156         
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                 my $new=not exists $pagesources{$page};
171                 if ($new) {
172                         # temporarily record its type
173                         $pagesources{$page}=$page.".".$type;
174                 }
175                 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
177                 my $content=$form->field('editcontent');
179                 run_hooks(editcontent => sub {
180                         $content=shift->(
181                                 content => $content,
182                                 page => $page,
183                                 cgi => $q,
184                                 session => $session,
185                         );
186                 });
187                 my $preview=htmlize($page, $page, $type,
188                         linkify($page, $page,
189                         preprocess($page, $page,
190                         filter($page, $page, $content), 0, 1)));
191                 run_hooks(format => sub {
192                         $preview=shift->(
193                                 page => $page,
194                                 content => $preview,
195                         );
196                 });
197                 $form->tmpl_param("page_preview", $preview);
198                 
199                 if ($new) {
200                         delete $pagesources{$page};
201                 }
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                 }
209                 @{$wikistate{editpage}{previews}} = keys %previews;
210                 $renderedfiles{$page}=[keys %wasrendered];
211                 saveindex();
212         }
213         elsif ($form->submitted eq "Save Page") {
214                 $form->tmpl_param("page_preview", "");
215         }
216         
217         if ($form->submitted ne "Save Page" || ! $form->validate) {
218                 if ($form->field("do") eq "create") {
219                         my @page_locs;
220                         my $best_loc;
221                         if (! defined $from || ! length $from ||
222                             $from ne $form->field('from') ||
223                             file_pruned($from, $config{srcdir}) ||
224                             $from=~/^\// || 
225                             $absolute ||
226                             $form->submitted) {
227                                 @page_locs=$best_loc=$page;
228                         }
229                         else {
230                                 my $dir=$from."/";
231                                 $dir=~s![^/]+/+$!!;
232                                 
233                                 if ((defined $form->field('subpage') &&
234                                      length $form->field('subpage')) ||
235                                     $page eq lc($config{discussionpage})) {
236                                         $best_loc="$from/$page";
237                                 }
238                                 else {
239                                         $best_loc=$dir.$page;
240                                 }
241                                 
242                                 push @page_locs, $dir.$page;
243                                 push @page_locs, "$from/$page";
244                                 while (length $dir) {
245                                         $dir=~s![^/]+/+$!!;
246                                         push @page_locs, $dir.$page;
247                                 }
248                         
249                                 my $userpage=IkiWiki::userpage($page);
250                                 push @page_locs, $userpage
251                                         if ! grep { $_ eq $userpage } @page_locs;
252                         }
254                         @page_locs = grep {
255                                 ! exists $pagecase{lc $_}
256                         } @page_locs;
257                         if (! @page_locs) {
258                                 # hmm, someone else made the page in the
259                                 # meantime?
260                                 if ($form->submitted eq "Preview") {
261                                         # let them go ahead with the edit
262                                         # and resolve the conflict at save
263                                         # time
264                                         @page_locs=$page;
265                                 }
266                                 else {
267                                         redirect($q, urlto($page, undef, 1));
268                                         exit;
269                                 }
270                         }
272                         my @editable_locs = grep {
273                                 check_canedit($_, $q, $session, 1)
274                         } @page_locs;
275                         if (! @editable_locs) {
276                                 # now let it throw an error, or prompt for
277                                 # login
278                                 map { check_canedit($_, $q, $session) }
279                                         ($best_loc, @page_locs);
280                         }
281                         
282                         my @page_types;
283                         if (exists $hooks{htmlize}) {
284                                 foreach my $key (grep { !/^_/ } keys %{$hooks{htmlize}}) {
285                                         push @page_types, [$key, $hooks{htmlize}{$key}{longname} || $key];
286                                 }
287                         }
288                         @page_types=sort @page_types;
289                         
290                         $form->tmpl_param("page_select", 1);
291                         $form->field(name => "page", type => 'select',
292                                 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
293                                 value => $best_loc);
294                         $form->field(name => "type", type => 'select',
295                                 options => \@page_types);
296                         $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
297                         
298                 }
299                 elsif ($form->field("do") eq "edit") {
300                         check_canedit($page, $q, $session);
301                         if (! defined $form->field('editcontent') || 
302                             ! length $form->field('editcontent')) {
303                                 my $content="";
304                                 if (exists $pagesources{$page}) {
305                                         $content=readfile(srcfile($pagesources{$page}));
306                                         $content=~s/\n/\r\n/g;
307                                 }
308                                 $form->field(name => "editcontent", value => $content,
309                                         force => 1);
310                         }
311                         $form->tmpl_param("page_select", 0);
312                         $form->field(name => "page", type => 'hidden');
313                         $form->field(name => "type", type => 'hidden');
314                         $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
315                 }
316                 
317                 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
318         }
319         else {
320                 # save page
321                 check_canedit($page, $q, $session);
322                 checksessionexpiry($q, $session, $q->param('sid'));
324                 my $exists=-e "$config{srcdir}/$file";
326                 if ($form->field("do") ne "create" && ! $exists &&
327                     ! defined srcfile($file, 1)) {
328                         $form->tmpl_param("message", template("editpagegone.tmpl")->output);
329                         $form->field(name => "do", value => "create", force => 1);
330                         $form->tmpl_param("page_select", 0);
331                         $form->field(name => "page", type => 'hidden');
332                         $form->field(name => "type", type => 'hidden');
333                         $form->title(sprintf(gettext("editing %s"), $page));
334                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
335                         exit;
336                 }
337                 elsif ($form->field("do") eq "create" && $exists) {
338                         $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
339                         $form->field(name => "do", value => "edit", force => 1);
340                         $form->tmpl_param("page_select", 0);
341                         $form->field(name => "page", type => 'hidden');
342                         $form->field(name => "type", type => 'hidden');
343                         $form->title(sprintf(gettext("editing %s"), $page));
344                         $form->field("editcontent", 
345                                 value => readfile("$config{srcdir}/$file").
346                                          "\n\n\n".$form->field("editcontent"),
347                                 force => 1);
348                         showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
349                         exit;
350                 }
351                         
352                 my $message="";
353                 if (defined $form->field('comments') &&
354                     length $form->field('comments')) {
355                         $message=$form->field('comments');
356                 }
357                 
358                 my $content=$form->field('editcontent');
359                 check_content(content => $content, page => $page,
360                         cgi => $q, session => $session,
361                         subject => $message);
362                 run_hooks(editcontent => sub {
363                         $content=shift->(
364                                 content => $content,
365                                 page => $page,
366                                 cgi => $q,
367                                 session => $session,
368                         );
369                 });
370                 $content=~s/\r\n/\n/g;
371                 $content=~s/\r/\n/g;
372                 $content.="\n" if $content !~ /\n$/;
374                 $config{cgi}=0; # avoid cgi error message
375                 eval { writefile($file, $config{srcdir}, $content) };
376                 $config{cgi}=1;
377                 if ($@) {
378                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
379                                 force => 1);
380                         my $mtemplate=template("editfailedsave.tmpl");
381                         $mtemplate->param(error_message => $@);
382                         $form->tmpl_param("message", $mtemplate->output);
383                         $form->field("editcontent", value => $content, force => 1);
384                         $form->tmpl_param("page_select", 0);
385                         $form->field(name => "page", type => 'hidden');
386                         $form->field(name => "type", type => 'hidden');
387                         $form->title(sprintf(gettext("editing %s"), $page));
388                         showform($form, \@buttons, $session, $q,
389                                 forcebaseurl => $baseurl);
390                         exit;
391                 }
392                 
393                 my $conflict;
394                 if ($config{rcs}) {
395                         if (! $exists) {
396                                 rcs_add($file);
397                         }
399                         # Prevent deadlock with post-commit hook by
400                         # signaling to it that it should not try to
401                         # do anything.
402                         disable_commit_hook();
403                         $conflict=rcs_commit($file, $message,
404                                 $form->field("rcsinfo"),
405                                 $session->param("name"), $ENV{REMOTE_ADDR});
406                         enable_commit_hook();
407                         rcs_update();
408                 }
409                 
410                 # Refresh even if there was a conflict, since other changes
411                 # may have been committed while the post-commit hook was
412                 # disabled.
413                 require IkiWiki::Render;
414                 refresh();
415                 saveindex();
417                 if (defined $conflict) {
418                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
419                                 force => 1);
420                         $form->tmpl_param("message", template("editconflict.tmpl")->output);
421                         $form->field("editcontent", value => $conflict, force => 1);
422                         $form->field("do", "edit", 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                 }
430                 else {
431                         # The trailing question mark tries to avoid broken
432                         # caches and get the most recent version of the page.
433                         redirect($q, urlto($page, undef, 1)."?updated");
434                 }
435         }
437         exit;