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 function below.
57 sub cgi_editpage ($$) {
61 my $do=$q->param('do');
62 return unless $do eq 'create' || $do eq 'edit';
66 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
67 my @buttons=("Save Page", "Preview", "Cancel");
68 eval q{use CGI::FormBuilder};
70 my $form = CGI::FormBuilder->new(
74 required => [qw{editcontent}],
77 action => $config{cgiurl},
80 template => scalar template_params("editpage.tmpl"),
83 decode_form_utf8($form);
84 run_hooks(formbuilder_setup => sub {
85 shift->(form => $form, cgi => $q, session => $session,
86 buttons => \@buttons);
88 decode_form_utf8($form);
90 # This untaint is safe because we check file_pruned and
92 my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
93 $page=possibly_foolish_untaint($page);
94 my $absolute=($page =~ s#^/+##);
95 if (! defined $page || ! length $page ||
96 file_pruned($page, $config{srcdir})) {
97 error(gettext("bad page name"));
100 my $baseurl = urlto($page, undef, 1);
103 if (defined $form->field('from')) {
104 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
109 if (exists $pagesources{$page} && $form->field("do") ne "create") {
110 $file=$pagesources{$page};
111 $type=pagetype($file);
112 if (! defined $type || $type=~/^_/) {
113 error(sprintf(gettext("%s is not an editable page"), $page));
115 if (! $form->submitted) {
116 $form->field(name => "rcsinfo",
117 value => rcs_prepedit($file), force => 1);
119 $form->field(name => "editcontent", validate => '/.*/');
122 $type=$form->param('type');
123 if (defined $type && length $type && $hooks{htmlize}{$type}) {
124 $type=possibly_foolish_untaint($type);
126 elsif (defined $from && exists $pagesources{$from}) {
127 # favor the type of linking page
128 $type=pagetype($pagesources{$from});
130 $type=$config{default_pageext} unless defined $type;
131 $file=newpagefile($page, $type);
132 if (! $form->submitted) {
133 $form->field(name => "rcsinfo", value => "", force => 1);
135 $form->field(name => "editcontent", validate => '/.+/');
138 $form->field(name => "do", type => 'hidden');
139 $form->field(name => "sid", type => "hidden", value => $session->id,
141 $form->field(name => "from", type => 'hidden');
142 $form->field(name => "rcsinfo", type => 'hidden');
143 $form->field(name => "subpage", type => 'hidden');
144 $form->field(name => "page", value => $page, force => 1);
145 $form->field(name => "type", value => $type, force => 1);
146 $form->field(name => "comments", type => "text", size => 80);
147 $form->field(name => "editcontent", type => "textarea", rows => 20,
149 $form->tmpl_param("can_commit", $config{rcs});
150 $form->tmpl_param("indexlink", indexlink());
151 $form->tmpl_param("helponformattinglink",
152 htmllink($page, $page, "ikiwiki/formatting",
154 linktext => "FormattingHelp"));
156 if ($form->submitted eq "Cancel") {
157 if ($form->field("do") eq "create" && defined $from) {
158 redirect($q, urlto($from, undef, 1));
160 elsif ($form->field("do") eq "create") {
161 redirect($q, $config{url});
164 redirect($q, urlto($page, undef, 1));
168 elsif ($form->submitted eq "Preview") {
169 my $new=not exists $pagesources{$page};
171 # temporarily record its type
172 $pagesources{$page}=$page.".".$type;
174 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
176 my $content=$form->field('editcontent');
178 run_hooks(editcontent => sub {
186 my $preview=htmlize($page, $page, $type,
187 linkify($page, $page,
188 preprocess($page, $page,
189 filter($page, $page, $content), 0, 1)));
190 run_hooks(format => sub {
196 $form->tmpl_param("page_preview", $preview);
199 delete $pagesources{$page};
202 # Previewing may have created files on disk.
203 # Keep a list of these to be deleted later.
204 my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
205 foreach my $f (@{$renderedfiles{$page}}) {
206 $previews{$f}=1 unless $wasrendered{$f};
208 @{$wikistate{editpage}{previews}} = keys %previews;
209 $renderedfiles{$page}=[keys %wasrendered];
212 elsif ($form->submitted eq "Save Page") {
213 $form->tmpl_param("page_preview", "");
216 if ($form->submitted ne "Save Page" || ! $form->validate) {
217 if ($form->field("do") eq "create") {
220 if (! defined $from || ! length $from ||
221 $from ne $form->field('from') ||
222 file_pruned($from, $config{srcdir}) ||
226 @page_locs=$best_loc=$page;
232 if ((defined $form->field('subpage') &&
233 length $form->field('subpage')) ||
234 $page eq lc($config{discussionpage})) {
235 $best_loc="$from/$page";
238 $best_loc=$dir.$page;
241 push @page_locs, $dir.$page;
242 push @page_locs, "$from/$page";
243 while (length $dir) {
245 push @page_locs, $dir.$page;
248 my $userpage=IkiWiki::userpage($page);
249 push @page_locs, $userpage
250 if ! grep { $_ eq $userpage } @page_locs;
254 ! exists $pagecase{lc $_}
257 # hmm, someone else made the page in the
259 if ($form->submitted eq "Preview") {
260 # let them go ahead with the edit
261 # and resolve the conflict at save
266 redirect($q, urlto($page, undef, 1));
271 my @editable_locs = grep {
272 check_canedit($_, $q, $session, 1)
274 if (! @editable_locs) {
275 # now let it throw an error, or prompt for
277 map { check_canedit($_, $q, $session) }
278 ($best_loc, @page_locs);
282 if (exists $hooks{htmlize}) {
283 foreach my $key (grep { !/^_/ } keys %{$hooks{htmlize}}) {
284 push @page_types, [$key, $hooks{htmlize}{$key}{longname} || $key];
287 @page_types=sort @page_types;
289 $form->tmpl_param("page_select", 1);
290 $form->field(name => "page", type => 'select',
291 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
293 $form->field(name => "type", type => 'select',
294 options => \@page_types);
295 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
298 elsif ($form->field("do") eq "edit") {
299 check_canedit($page, $q, $session);
300 if (! defined $form->field('editcontent') ||
301 ! length $form->field('editcontent')) {
303 if (exists $pagesources{$page}) {
304 $content=readfile(srcfile($pagesources{$page}));
305 $content=~s/\n/\r\n/g;
307 $form->field(name => "editcontent", value => $content,
310 $form->tmpl_param("page_select", 0);
311 $form->field(name => "page", type => 'hidden');
312 $form->field(name => "type", type => 'hidden');
313 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
316 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
320 check_canedit($page, $q, $session);
321 checksessionexpiry($q, $session, $q->param('sid'));
323 my $exists=-e "$config{srcdir}/$file";
325 if ($form->field("do") ne "create" && ! $exists &&
326 ! defined srcfile($file, 1)) {
327 $form->tmpl_param("message", template("editpagegone.tmpl")->output);
328 $form->field(name => "do", value => "create", force => 1);
329 $form->tmpl_param("page_select", 0);
330 $form->field(name => "page", type => 'hidden');
331 $form->field(name => "type", type => 'hidden');
332 $form->title(sprintf(gettext("editing %s"), $page));
333 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
336 elsif ($form->field("do") eq "create" && $exists) {
337 $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
338 $form->field(name => "do", value => "edit", 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 $form->field("editcontent",
344 value => readfile("$config{srcdir}/$file").
345 "\n\n\n".$form->field("editcontent"),
347 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
352 if (defined $form->field('comments') &&
353 length $form->field('comments')) {
354 $message=$form->field('comments');
357 my $content=$form->field('editcontent');
358 check_content(content => $content, page => $page,
359 cgi => $q, session => $session,
360 subject => $message);
361 run_hooks(editcontent => sub {
369 $content=~s/\r\n/\n/g;
371 $content.="\n" if $content !~ /\n$/;
373 $config{cgi}=0; # avoid cgi error message
374 eval { writefile($file, $config{srcdir}, $content) };
377 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
379 my $mtemplate=template("editfailedsave.tmpl");
380 $mtemplate->param(error_message => $@);
381 $form->tmpl_param("message", $mtemplate->output);
382 $form->field("editcontent", value => $content, force => 1);
383 $form->tmpl_param("page_select", 0);
384 $form->field(name => "page", type => 'hidden');
385 $form->field(name => "type", type => 'hidden');
386 $form->title(sprintf(gettext("editing %s"), $page));
387 showform($form, \@buttons, $session, $q,
388 forcebaseurl => $baseurl);
398 # Prevent deadlock with post-commit hook by
399 # signaling to it that it should not try to
401 disable_commit_hook();
402 $conflict=rcs_commit($file, $message,
403 $form->field("rcsinfo"),
404 $session->param("name"), $ENV{REMOTE_ADDR});
405 enable_commit_hook();
409 # Refresh even if there was a conflict, since other changes
410 # may have been committed while the post-commit hook was
412 require IkiWiki::Render;
416 if (defined $conflict) {
417 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
419 $form->tmpl_param("message", template("editconflict.tmpl")->output);
420 $form->field("editcontent", value => $conflict, force => 1);
421 $form->field("do", "edit", force => 1);
422 $form->tmpl_param("page_select", 0);
423 $form->field(name => "page", type => 'hidden');
424 $form->field(name => "type", type => 'hidden');
425 $form->title(sprintf(gettext("editing %s"), $page));
426 showform($form, \@buttons, $session, $q,
427 forcebaseurl => $baseurl);
430 # The trailing question mark tries to avoid broken
431 # caches and get the most recent version of the page.
432 redirect($q, urlto($page, undef, 1)."?updated");