fix uninitilized value warning on bad page name
[ikiwiki.git] / IkiWiki / Plugin / editpage.pm
blob8f71dafa89df49a3983f0ae185114a484608c994
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);
15 sub getsetup () {
16 return
17 plugin => {
18 safe => 1,
19 rebuild => 1,
20 section => "core",
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;
40 if ($delete) {
41 debug(sprintf(gettext("removing old preview %s"), $file));
42 IkiWiki::prune("$config{destdir}/$file");
45 elsif (defined $mtime) {
46 push @previews, $file;
49 $wikistate{editpage}{previews}=\@previews;
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;
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") },
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);
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"));
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"));
104 my $baseurl = urlto($page);
106 my $from;
107 if (defined $form->field('from')) {
108 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
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));
119 if (! $form->submitted) {
120 $form->field(name => "rcsinfo",
121 value => rcs_prepedit($file), force => 1);
123 $form->field(name => "editcontent", validate => '/.*/');
125 else {
126 $type=$form->param('type');
127 if (defined $type && length $type && $hooks{htmlize}{$type}) {
128 $type=possibly_foolish_untaint($type);
130 elsif (defined $from && exists $pagesources{$from}) {
131 # favor the type of linking page
132 $type=pagetype($pagesources{$from});
134 $type=$config{default_pageext} unless defined $type;
135 $file=newpagefile($page, $type);
136 if (! $form->submitted) {
137 $form->field(name => "rcsinfo", value => "", force => 1);
139 $form->field(name => "editcontent", validate => '/.+/');
142 $form->field(name => "do", type => 'hidden');
143 $form->field(name => "sid", type => "hidden", value => $session->id,
144 force => 1);
145 $form->field(name => "from", type => 'hidden');
146 $form->field(name => "rcsinfo", type => 'hidden');
147 $form->field(name => "subpage", type => 'hidden');
148 $form->field(name => "page", value => $page, force => 1);
149 $form->field(name => "type", value => $type, force => 1);
150 $form->field(name => "editmessage", type => "text", size => 80);
151 $form->field(name => "editcontent", type => "textarea", rows => 20,
152 cols => 80);
153 $form->tmpl_param("can_commit", $config{rcs});
154 $form->tmpl_param("helponformattinglink",
155 htmllink($page, $page, "ikiwiki/formatting",
156 noimageinline => 1,
157 linktext => "FormattingHelp"));
159 my $previewing=0;
160 if ($form->submitted eq "Cancel") {
161 if ($form->field("do") eq "create" && defined $from) {
162 redirect($q, urlto($from));
164 elsif ($form->field("do") eq "create") {
165 redirect($q, baseurl(undef));
167 else {
168 redirect($q, $baseurl);
170 exit;
172 elsif ($form->submitted eq "Preview") {
173 $previewing=1;
175 my $new=not exists $pagesources{$page};
176 # temporarily record its type
177 $pagesources{$page}=$page.".".$type if $new;
178 my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
180 my $content=$form->field('editcontent');
182 run_hooks(editcontent => sub {
183 $content=shift->(
184 content => $content,
185 page => $page,
186 cgi => $q,
187 session => $session,
190 my $preview=htmlize($page, $page, $type,
191 linkify($page, $page,
192 preprocess($page, $page,
193 filter($page, $page, $content), 0, 1)));
194 run_hooks(format => sub {
195 $preview=shift->(
196 page => $page,
197 content => $preview,
200 $form->tmpl_param("page_preview", $preview);
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};
209 # Throw out any other state changes made during previewing,
210 # and save the previews list.
211 loadindex();
212 @{$wikistate{editpage}{previews}} = keys %previews;
213 saveindex();
215 elsif ($form->submitted eq "Save Page") {
216 $form->tmpl_param("page_preview", "");
219 if ($form->submitted ne "Save Page" || ! $form->validate) {
220 if ($form->field("do") eq "create") {
221 my @page_locs;
222 my $best_loc;
223 if (! defined $from || ! length $from ||
224 $from ne $form->field('from') ||
225 file_pruned($from) ||
226 $absolute ||
227 $form->submitted) {
228 @page_locs=$best_loc=$page;
230 else {
231 my $dir=$from."/";
232 $dir=~s![^/]+/+$!!;
234 if ((defined $form->field('subpage') &&
235 length $form->field('subpage')) ||
236 $page eq lc($config{discussionpage})) {
237 $best_loc="$from/$page";
239 else {
240 $best_loc=$dir.$page;
243 push @page_locs, $dir.$page;
244 push @page_locs, "$from/$page";
245 while (length $dir) {
246 $dir=~s![^/]+/+$!!;
247 push @page_locs, $dir.$page;
250 my $userpage=IkiWiki::userpage($page);
251 push @page_locs, $userpage
252 if ! grep { $_ eq $userpage } @page_locs;
255 @page_locs = grep {
256 ! exists $pagecase{lc $_}
257 } @page_locs;
258 if (! @page_locs) {
259 # hmm, someone else made the page in the
260 # meantime?
261 if ($previewing) {
262 # let them go ahead with the edit
263 # and resolve the conflict at save
264 # time
265 @page_locs=$page;
267 else {
268 redirect($q, $baseurl);
269 exit;
273 my @editable_locs = grep {
274 check_canedit($_, $q, $session, 1)
275 } @page_locs;
276 if (! @editable_locs) {
277 # now let it throw an error, or prompt for
278 # login
279 map { check_canedit($_, $q, $session) }
280 ($best_loc, @page_locs);
283 my @page_types;
284 if (exists $hooks{htmlize}) {
285 foreach my $key (grep { !/^_/ } keys %{$hooks{htmlize}}) {
286 push @page_types, [$key, $hooks{htmlize}{$key}{longname} || $key];
289 @page_types=sort @page_types;
291 $form->tmpl_param("page_select", 1);
292 $form->field(name => "page", type => 'select',
293 options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
294 value => $best_loc);
295 $form->field(name => "type", type => 'select',
296 options => \@page_types);
297 $form->title(sprintf(gettext("creating %s"), pagetitle(basename($page))));
300 elsif ($form->field("do") eq "edit") {
301 check_canedit($page, $q, $session);
302 if (! defined $form->field('editcontent') ||
303 ! length $form->field('editcontent')) {
304 my $content="";
305 if (exists $pagesources{$page}) {
306 $content=readfile(srcfile($pagesources{$page}));
307 $content=~s/\n/\r\n/g;
309 $form->field(name => "editcontent", value => $content,
310 force => 1);
312 $form->tmpl_param("page_select", 0);
313 $form->field(name => "page", type => 'hidden');
314 $form->field(name => "type", type => 'hidden');
315 $form->title(sprintf(gettext("editing %s"), pagetitle(basename($page))));
318 showform($form, \@buttons, $session, $q, page => $page);
320 else {
321 # save page
322 check_canedit($page, $q, $session);
323 checksessionexpiry($q, $session, $q->param('sid'));
325 my $exists=-e "$config{srcdir}/$file";
327 if ($form->field("do") ne "create" && ! $exists &&
328 ! defined srcfile($file, 1)) {
329 $form->tmpl_param("message", template("editpagegone.tmpl")->output);
330 $form->field(name => "do", value => "create", 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 showform($form, \@buttons, $session, $q,
336 page => $page);
337 exit;
339 elsif ($form->field("do") eq "create" && $exists) {
340 $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
341 $form->field(name => "do", value => "edit", force => 1);
342 $form->tmpl_param("page_select", 0);
343 $form->field(name => "page", type => 'hidden');
344 $form->field(name => "type", type => 'hidden');
345 $form->title(sprintf(gettext("editing %s"), $page));
346 $form->field("editcontent",
347 value => readfile("$config{srcdir}/$file").
348 "\n\n\n".$form->field("editcontent"),
349 force => 1);
350 showform($form, \@buttons, $session, $q,
351 page => $page);
352 exit;
355 my $message="";
356 if (defined $form->field('editmessage') &&
357 length $form->field('editmessage')) {
358 $message=$form->field('editmessage');
361 my $content=$form->field('editcontent');
362 check_content(content => $content, page => $page,
363 cgi => $q, session => $session,
364 subject => $message);
365 run_hooks(editcontent => sub {
366 $content=shift->(
367 content => $content,
368 page => $page,
369 cgi => $q,
370 session => $session,
373 $content=~s/\r\n/\n/g;
374 $content=~s/\r/\n/g;
375 $content.="\n" if $content !~ /\n$/;
377 $config{cgi}=0; # avoid cgi error message
378 eval { writefile($file, $config{srcdir}, $content) };
379 $config{cgi}=1;
380 if ($@) {
381 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
382 force => 1);
383 my $mtemplate=template("editfailedsave.tmpl");
384 $mtemplate->param(error_message => $@);
385 $form->tmpl_param("message", $mtemplate->output);
386 $form->field("editcontent", value => $content, 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 showform($form, \@buttons, $session, $q,
392 page => $page);
393 exit;
396 my $conflict;
397 if ($config{rcs}) {
398 if (! $exists) {
399 rcs_add($file);
402 # Prevent deadlock with post-commit hook by
403 # signaling to it that it should not try to
404 # do anything.
405 disable_commit_hook();
406 $conflict=rcs_commit(
407 file => $file,
408 message => $message,
409 token => $form->field("rcsinfo"),
410 session => $session,
412 enable_commit_hook();
413 rcs_update();
416 # Refresh even if there was a conflict, since other changes
417 # may have been committed while the post-commit hook was
418 # disabled.
419 require IkiWiki::Render;
420 refresh();
421 saveindex();
423 if (defined $conflict) {
424 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
425 force => 1);
426 $form->tmpl_param("message", template("editconflict.tmpl")->output);
427 $form->field("editcontent", value => $conflict, force => 1);
428 $form->field("do", "edit", force => 1);
429 $form->tmpl_param("page_select", 0);
430 $form->field(name => "page", type => 'hidden');
431 $form->field(name => "type", type => 'hidden');
432 $form->title(sprintf(gettext("editing %s"), $page));
433 showform($form, \@buttons, $session, $q,
434 page => $page);
436 else {
437 # The trailing question mark tries to avoid broken
438 # caches and get the most recent version of the page.
439 redirect($q, $baseurl."?updated");
443 exit;