2 package IkiWiki::Plugin::editpage;
7 use open qw{:utf8 :std};
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);
24 if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
25 # Expire old preview files after one hour.
26 my $expire=time - (60 * 60);
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.
34 foreach my $page (keys %renderedfiles) {
35 if (grep { $_ eq $file } @{$renderedfiles{$page}}) {
40 debug(sprintf(gettext("removing old preview %s"), $file));
41 IkiWiki::prune("$config{destdir}/$file");
44 elsif (defined $mtime) {
45 push @previews, $file;
48 $wikistate{editpage}{previews}=\@previews;
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.
57 sub check_canedit ($$$;$) {
64 run_hooks(canedit => sub {
65 return if defined $canedit;
66 my $ret=shift->($page, $q, $session);
71 elsif (ref $ret eq 'CODE') {
72 $ret->() unless $nonfatal;
75 elsif (defined $ret) {
76 error($ret) unless $nonfatal;
84 sub check_cansave ($$$$) {
91 run_hooks(cansave => sub {
92 return if defined $cansave;
93 my $ret=shift->($page, $content, $q, $session);
98 elsif (ref $ret eq 'CODE') {
111 sub cgi_editpage ($$) {
115 my $do=$q->param('do');
116 return unless $do eq 'create' || $do eq 'edit';
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};
124 my $form = CGI::FormBuilder->new(
128 required => [qw{editcontent}],
131 action => $config{cgiurl},
134 template => scalar template_params("editpage.tmpl"),
137 decode_form_utf8($form);
138 run_hooks(formbuilder_setup => sub {
139 shift->(form => $form, cgi => $q, session => $session,
140 buttons => \@buttons);
142 decode_form_utf8($form);
144 # This untaint is safe because we check file_pruned and
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"));
154 my $baseurl = urlto($page, undef, 1);
157 if (defined $form->field('from')) {
158 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
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));
169 if (! $form->submitted) {
170 $form->field(name => "rcsinfo",
171 value => rcs_prepedit($file), force => 1);
173 $form->field(name => "editcontent", validate => '/.*/');
176 $type=$form->param('type');
177 if (defined $type && length $type && $hooks{htmlize}{$type}) {
178 $type=possibly_foolish_untaint($type);
180 elsif (defined $from && exists $pagesources{$from}) {
181 # favor the type of linking page
182 $type=pagetype($pagesources{$from});
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);
189 $form->field(name => "editcontent", validate => '/.+/');
192 $form->field(name => "do", type => 'hidden');
193 $form->field(name => "sid", type => "hidden", value => $session->id,
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,
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",
208 linktext => "FormattingHelp"));
210 if ($form->submitted eq "Cancel") {
211 if ($form->field("do") eq "create" && defined $from) {
212 redirect($q, urlto($from, undef, 1));
214 elsif ($form->field("do") eq "create") {
215 redirect($q, $config{url});
218 redirect($q, urlto($page, undef, 1));
222 elsif ($form->submitted eq "Preview") {
223 my $new=not exists $pagesources{$page};
225 # temporarily record its type
226 $pagesources{$page}=$page.".".$type;
228 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
230 my $content=$form->field('editcontent');
232 run_hooks(editcontent => sub {
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 {
250 $form->tmpl_param("page_preview", $preview);
253 delete $pagesources{$page};
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};
262 @{$wikistate{editpage}{previews}} = keys %previews;
263 $renderedfiles{$page}=[keys %wasrendered];
266 elsif ($form->submitted eq "Save Page") {
267 $form->tmpl_param("page_preview", "");
270 if ($form->submitted ne "Save Page" || ! $form->validate) {
271 if ($form->field("do") eq "create") {
274 if (! defined $from || ! length $from ||
275 $from ne $form->field('from') ||
276 file_pruned($from, $config{srcdir}) ||
280 @page_locs=$best_loc=$page;
286 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
287 $page eq gettext('discussion')) {
288 $best_loc="$from/$page";
291 $best_loc=$dir.$page;
294 push @page_locs, $dir.$page;
295 push @page_locs, "$from/$page";
296 while (length $dir) {
298 push @page_locs, $dir.$page;
301 push @page_locs, "$config{userdir}/$page"
302 if length $config{userdir};
306 ! exists $pagecase{lc $_}
309 # hmm, someone else made the page in the
311 if ($form->submitted eq "Preview") {
312 # let them go ahead with the edit
313 # and resolve the conflict at save
318 redirect($q, urlto($page, undef, 1));
323 my @editable_locs = grep {
324 check_canedit($_, $q, $session, 1)
326 if (! @editable_locs) {
327 # let it throw an error this time
328 map { check_canedit($_, $q, $session) } @page_locs;
332 if (exists $hooks{htmlize}) {
333 @page_types=grep { !/^_/ }
334 keys %{$hooks{htmlize}};
337 $form->tmpl_param("page_select", 1);
338 $form->field(name => "page", type => 'select',
339 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
341 $form->field(name => "type", type => 'select',
342 options => \@page_types);
343 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
346 elsif ($form->field("do") eq "edit") {
347 check_canedit($page, $q, $session);
348 if (! defined $form->field('editcontent') ||
349 ! length $form->field('editcontent')) {
351 if (exists $pagesources{$page}) {
352 $content=readfile(srcfile($pagesources{$page}));
353 $content=~s/\n/\r\n/g;
355 $form->field(name => "editcontent", value => $content,
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)));
364 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
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);
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"),
395 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
399 my $content=$form->field('editcontent');
400 check_cansave($page, $content, $q, $session);
401 run_hooks(editcontent => sub {
409 $content=~s/\r\n/\n/g;
411 $content.="\n" if $content !~ /\n$/;
413 $config{cgi}=0; # avoid cgi error message
414 eval { writefile($file, $config{srcdir}, $content) };
417 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
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);
435 if (defined $form->field('comments') &&
436 length $form->field('comments')) {
437 $message=$form->field('comments');
444 # Prevent deadlock with post-commit hook by
445 # signaling to it that it should not try to
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();
455 # Refresh even if there was a conflict, since other changes
456 # may have been committed while the post-commit hook was
458 require IkiWiki::Render;
462 if (defined $conflict) {
463 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
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);
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");