2 package IkiWiki::Plugin::editpage;
7 use open qw{:utf8 :std};
10 hook(type => "getsetup", id => "editpage", call => \&getsetup);
11 hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
14 sub getsetup () { #{{{
22 # Back to ikiwiki namespace for the rest, this code is very much
23 # internal to ikiwiki even though it's separated into a plugin,
24 # and other plugins use the functions below.
27 sub check_canedit ($$$;$) { #{{{
34 run_hooks(canedit => sub {
35 return if defined $canedit;
36 my $ret=shift->($page, $q, $session);
41 elsif (ref $ret eq 'CODE') {
42 $ret->() unless $nonfatal;
45 elsif (defined $ret) {
46 error($ret) unless $nonfatal;
54 sub cgi_editpage ($$) { #{{{
58 my $do=$q->param('do');
59 return unless $do eq 'create' || $do eq 'edit';
63 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
64 my @buttons=("Save Page", "Preview", "Cancel");
65 eval q{use CGI::FormBuilder};
67 my $form = CGI::FormBuilder->new(
71 required => [qw{editcontent}],
74 action => $config{cgiurl},
77 template => scalar template_params("editpage.tmpl"),
78 wikiname => $config{wikiname},
81 decode_form_utf8($form);
82 run_hooks(formbuilder_setup => sub {
83 shift->(form => $form, cgi => $q, session => $session,
84 buttons => \@buttons);
86 decode_form_utf8($form);
88 # This untaint is safe because we check file_pruned and
90 my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
91 $page=possibly_foolish_untaint($page);
92 my $absolute=($page =~ s#^/+##);
93 if (! defined $page || ! length $page ||
94 file_pruned($page, $config{srcdir})) {
95 error("bad page name");
98 my $baseurl=$config{url}."/".htmlpage($page);
101 if (defined $form->field('from')) {
102 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
107 if (exists $pagesources{$page} && $form->field("do") ne "create") {
108 $file=$pagesources{$page};
109 $type=pagetype($file);
110 if (! defined $type || $type=~/^_/) {
111 error(sprintf(gettext("%s is not an editable page"), $page));
113 if (! $form->submitted) {
114 $form->field(name => "rcsinfo",
115 value => rcs_prepedit($file), force => 1);
117 $form->field(name => "editcontent", validate => '/.*/');
120 $type=$form->param('type');
121 if (defined $type && length $type && $hooks{htmlize}{$type}) {
122 $type=possibly_foolish_untaint($type);
124 elsif (defined $from && exists $pagesources{$from}) {
125 # favor the type of linking page
126 $type=pagetype($pagesources{$from});
128 $type=$config{default_pageext} unless defined $type;
129 $file=$page.".".$type;
130 if (! $form->submitted) {
131 $form->field(name => "rcsinfo", value => "", force => 1);
133 $form->field(name => "editcontent", validate => '/.+/');
136 $form->field(name => "do", type => 'hidden');
137 $form->field(name => "sid", type => "hidden", value => $session->id,
139 $form->field(name => "from", type => 'hidden');
140 $form->field(name => "rcsinfo", type => 'hidden');
141 $form->field(name => "subpage", type => 'hidden');
142 $form->field(name => "page", value => $page, force => 1);
143 $form->field(name => "type", value => $type, force => 1);
144 $form->field(name => "comments", type => "text", size => 80);
145 $form->field(name => "editcontent", type => "textarea", rows => 20,
147 $form->tmpl_param("can_commit", $config{rcs});
148 $form->tmpl_param("indexlink", indexlink());
149 $form->tmpl_param("helponformattinglink",
150 htmllink($page, $page, "ikiwiki/formatting",
152 linktext => "FormattingHelp"));
154 if ($form->submitted eq "Cancel") {
155 if ($form->field("do") eq "create" && defined $from) {
156 redirect($q, "$config{url}/".htmlpage($from));
158 elsif ($form->field("do") eq "create") {
159 redirect($q, $config{url});
162 redirect($q, "$config{url}/".htmlpage($page));
166 elsif ($form->submitted eq "Preview") {
167 my $new=not exists $pagesources{$page};
169 # temporarily record its type
170 $pagesources{$page}=$page.".".$type;
173 my $content=$form->field('editcontent');
175 run_hooks(editcontent => sub {
183 my $preview=htmlize($page, $page, $type,
184 linkify($page, $page,
185 preprocess($page, $page,
186 filter($page, $page, $content), 0, 1)));
187 run_hooks(format => sub {
193 $form->tmpl_param("page_preview", $preview);
196 delete $pagesources{$page};
198 # previewing may have created files on disk
201 elsif ($form->submitted eq "Save Page") {
202 $form->tmpl_param("page_preview", "");
205 if ($form->submitted ne "Save Page" || ! $form->validate) {
206 if ($form->field("do") eq "create") {
209 if (! defined $from || ! length $from ||
210 $from ne $form->field('from') ||
211 file_pruned($from, $config{srcdir}) ||
214 $form->submitted eq "Preview") {
215 @page_locs=$best_loc=$page;
221 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
222 $page eq gettext('discussion')) {
223 $best_loc="$from/$page";
226 $best_loc=$dir.$page;
229 push @page_locs, $dir.$page;
230 push @page_locs, "$from/$page";
231 while (length $dir) {
233 push @page_locs, $dir.$page;
236 push @page_locs, "$config{userdir}/$page"
237 if length $config{userdir};
241 ! exists $pagecase{lc $_}
244 # hmm, someone else made the page in the
246 if ($form->submitted eq "Preview") {
247 # let them go ahead with the edit
248 # and resolve the conflict at save
253 redirect($q, "$config{url}/".htmlpage($page));
258 my @editable_locs = grep {
259 check_canedit($_, $q, $session, 1)
261 if (! @editable_locs) {
262 # let it throw an error this time
263 map { check_canedit($_, $q, $session) } @page_locs;
267 if (exists $hooks{htmlize}) {
268 @page_types=grep { !/^_/ }
269 keys %{$hooks{htmlize}};
272 $form->tmpl_param("page_select", 1);
273 $form->field(name => "page", type => 'select',
274 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
276 $form->field(name => "type", type => 'select',
277 options => \@page_types);
278 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
281 elsif ($form->field("do") eq "edit") {
282 check_canedit($page, $q, $session);
283 if (! defined $form->field('editcontent') ||
284 ! length $form->field('editcontent')) {
286 if (exists $pagesources{$page}) {
287 $content=readfile(srcfile($pagesources{$page}));
288 $content=~s/\n/\r\n/g;
290 $form->field(name => "editcontent", value => $content,
293 $form->tmpl_param("page_select", 0);
294 $form->field(name => "page", type => 'hidden');
295 $form->field(name => "type", type => 'hidden');
296 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
299 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
303 check_canedit($page, $q, $session);
305 # The session id is stored on the form and checked to
306 # guard against CSRF. But only if the user is logged in,
307 # as anonok can allow anonymous edits.
308 if (defined $session->param("name")) {
309 my $sid=$q->param('sid');
310 if (! defined $sid || $sid ne $session->id) {
311 error(gettext("Your login session has expired."));
315 my $exists=-e "$config{srcdir}/$file";
317 if ($form->field("do") ne "create" && ! $exists &&
318 ! defined srcfile($file, 1)) {
319 $form->tmpl_param("message", template("editpagegone.tmpl")->output);
320 $form->field(name => "do", value => "create", force => 1);
321 $form->tmpl_param("page_select", 0);
322 $form->field(name => "page", type => 'hidden');
323 $form->field(name => "type", type => 'hidden');
324 $form->title(sprintf(gettext("editing %s"), $page));
325 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
328 elsif ($form->field("do") eq "create" && $exists) {
329 $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
330 $form->field(name => "do", value => "edit", force => 1);
331 $form->tmpl_param("page_select", 0);
332 $form->field(name => "page", type => 'hidden');
333 $form->field(name => "type", type => 'hidden');
334 $form->title(sprintf(gettext("editing %s"), $page));
335 $form->field("editcontent",
336 value => readfile("$config{srcdir}/$file").
337 "\n\n\n".$form->field("editcontent"),
339 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
343 my $content=$form->field('editcontent');
344 run_hooks(editcontent => sub {
352 $content=~s/\r\n/\n/g;
354 $content.="\n" if $content !~ /\n$/;
356 $config{cgi}=0; # avoid cgi error message
357 eval { writefile($file, $config{srcdir}, $content) };
360 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
362 my $mtemplate=template("editfailedsave.tmpl");
363 $mtemplate->param(error_message => $@);
364 $form->tmpl_param("message", $mtemplate->output);
365 $form->field("editcontent", value => $content, force => 1);
366 $form->tmpl_param("page_select", 0);
367 $form->field(name => "page", type => 'hidden');
368 $form->field(name => "type", type => 'hidden');
369 $form->title(sprintf(gettext("editing %s"), $page));
370 showform($form, \@buttons, $session, $q,
371 forcebaseurl => $baseurl);
378 if (defined $form->field('comments') &&
379 length $form->field('comments')) {
380 $message=$form->field('comments');
387 # Prevent deadlock with post-commit hook by
388 # signaling to it that it should not try to
390 disable_commit_hook();
391 $conflict=rcs_commit($file, $message,
392 $form->field("rcsinfo"),
393 $session->param("name"), $ENV{REMOTE_ADDR});
394 enable_commit_hook();
398 # Refresh even if there was a conflict, since other changes
399 # may have been committed while the post-commit hook was
401 require IkiWiki::Render;
405 if (defined $conflict) {
406 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
408 $form->tmpl_param("message", template("editconflict.tmpl")->output);
409 $form->field("editcontent", value => $conflict, force => 1);
410 $form->field("do", "edit", force => 1);
411 $form->tmpl_param("page_select", 0);
412 $form->field(name => "page", type => 'hidden');
413 $form->field(name => "type", type => 'hidden');
414 $form->title(sprintf(gettext("editing %s"), $page));
415 showform($form, \@buttons, $session, $q,
416 forcebaseurl => $baseurl);
419 # The trailing question mark tries to avoid broken
420 # caches and get the most recent version of the page.
421 redirect($q, "$config{url}/".htmlpage($page)."?updated");