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
);
25 if (exists $wikistate{editpage
} && exists $wikistate{editpage
}{previews
}) {
26 # Expire old preview files after one hour.
27 my $expire=time - (60 * 60);
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.
35 foreach my $page (keys %renderedfiles) {
36 if (grep { $_ eq $file } @
{$renderedfiles{$page}}) {
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.
58 sub cgi_editpage
($$) {
62 my $do=$q->param('do');
63 return unless $do eq 'create' || $do eq 'edit';
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};
71 my $form = CGI
::FormBuilder
->new(
75 required
=> [qw{editcontent
}],
78 action
=> IkiWiki
::cgiurl
(),
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);
89 decode_form_utf8
($form);
91 # This untaint is safe because we check file_pruned and
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);
107 if (defined $form->field('from')) {
108 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
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
=> '/.*/');
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
}
135 if ! defined $type || $type=~/^_/; # not internal type
136 $file=newpagefile
($page, $type);
137 if (! $form->submitted) {
138 $form->field(name
=> "rcsinfo", value
=> "", force
=> 1);
140 $form->field(name
=> "editcontent", validate
=> '/.+/');
143 $form->field(name
=> "do", type
=> 'hidden');
144 $form->field(name
=> "sid", type
=> "hidden", value
=> $session->id,
146 $form->field(name
=> "from", type
=> 'hidden');
147 $form->field(name
=> "rcsinfo", type
=> 'hidden');
148 $form->field(name
=> "subpage", type
=> 'hidden');
149 $form->field(name
=> "page", value
=> $page, force
=> 1);
150 $form->field(name
=> "type", value
=> $type, force
=> 1);
151 $form->field(name
=> "editmessage", type
=> "text", size
=> 80);
152 $form->field(name
=> "editcontent", type
=> "textarea", rows
=> 20,
154 $form->tmpl_param("can_commit", $config{rcs
});
155 $form->tmpl_param("helponformattinglink",
156 htmllink
($page, $page, "ikiwiki/formatting",
158 linktext
=> "FormattingHelp"));
161 if ($form->submitted eq "Cancel") {
162 if ($form->field("do") eq "create" && defined $from) {
163 redirect
($q, urlto
($from));
165 elsif ($form->field("do") eq "create") {
166 redirect
($q, baseurl
(undef));
169 redirect
($q, $baseurl);
173 elsif ($form->submitted eq "Preview") {
176 my $new=not exists $pagesources{$page};
177 # temporarily record its type
178 $pagesources{$page}=$page.".".$type if $new;
179 my %wasrendered=map { $_ => 1 } @
{$renderedfiles{$page}};
181 my $content=$form->field('editcontent');
183 run_hooks
(editcontent
=> sub {
191 my $preview=htmlize
($page, $page, $type,
192 linkify
($page, $page,
193 preprocess
($page, $page,
194 filter
($page, $page, $content), 0, 1)));
195 run_hooks
(format
=> sub {
201 $form->tmpl_param("page_preview", $preview);
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};
210 # Throw out any other state changes made during previewing,
211 # and save the previews list.
213 @
{$wikistate{editpage
}{previews
}} = keys %previews;
216 elsif ($form->submitted eq "Save Page") {
217 $form->tmpl_param("page_preview", "");
220 if ($form->submitted ne "Save Page" || ! $form->validate) {
221 if ($form->field("do") eq "create") {
224 if (! defined $from || ! length $from ||
225 $from ne $form->field('from') ||
226 file_pruned
($from) ||
229 @page_locs=$best_loc=$page;
235 if ((defined $form->field('subpage') &&
236 length $form->field('subpage')) ||
237 $page eq lc($config{discussionpage
})) {
238 $best_loc="$from/$page";
241 $best_loc=$dir.$page;
244 push @page_locs, $dir.$page;
245 push @page_locs, "$from/$page";
246 while (length $dir) {
248 push @page_locs, $dir.$page;
251 my $userpage=IkiWiki
::userpage
($page);
252 push @page_locs, $userpage
253 if ! grep { $_ eq $userpage } @page_locs;
257 ! exists $pagecase{lc $_}
260 # hmm, someone else made the page in the
263 # let them go ahead with the edit
264 # and resolve the conflict at save
269 redirect
($q, $baseurl);
274 my @editable_locs = grep {
275 check_canedit
($_, $q, $session, 1)
277 if (! @editable_locs) {
278 # now let it throw an error, or prompt for
280 map { check_canedit
($_, $q, $session) }
281 ($best_loc, @page_locs);
285 if (exists $hooks{htmlize
}) {
286 foreach my $key (grep { !/^_/ } keys %{$hooks{htmlize
}}) {
287 push @page_types, [$key, $hooks{htmlize
}{$key}{longname
} || $key];
290 @page_types=sort @page_types;
292 $form->tmpl_param("page_select", 1);
293 $form->field(name
=> "page", type
=> 'select',
294 options
=> [ map { [ $_, pagetitle
($_, 1) ] } @editable_locs ],
296 $form->field(name
=> "type", type
=> 'select',
297 options
=> \
@page_types);
298 $form->title(sprintf(gettext
("creating %s"), pagetitle
(basename
($page))));
301 elsif ($form->field("do") eq "edit") {
302 check_canedit
($page, $q, $session);
303 if (! defined $form->field('editcontent') ||
304 ! length $form->field('editcontent')) {
306 if (exists $pagesources{$page}) {
307 $content=readfile
(srcfile
($pagesources{$page}));
308 $content=~s/\n/\r\n/g;
310 $form->field(name
=> "editcontent", value
=> $content,
313 $form->tmpl_param("page_select", 0);
314 $form->field(name
=> "page", type
=> 'hidden');
315 $form->field(name
=> "type", type
=> 'hidden');
316 $form->title(sprintf(gettext
("editing %s"), pagetitle
(basename
($page))));
319 showform
($form, \
@buttons, $session, $q, page
=> $page);
323 check_canedit
($page, $q, $session);
324 checksessionexpiry
($q, $session, $q->param('sid'));
326 my $exists=-e
"$config{srcdir}/$file";
328 if ($form->field("do") ne "create" && ! $exists &&
329 ! defined srcfile
($file, 1)) {
330 $form->tmpl_param("message", template
("editpagegone.tmpl")->output);
331 $form->field(name
=> "do", value
=> "create", force
=> 1);
332 $form->tmpl_param("page_select", 0);
333 $form->field(name
=> "page", type
=> 'hidden');
334 $form->field(name
=> "type", type
=> 'hidden');
335 $form->title(sprintf(gettext
("editing %s"), $page));
336 showform
($form, \
@buttons, $session, $q,
340 elsif ($form->field("do") eq "create" && $exists) {
341 $form->tmpl_param("message", template
("editcreationconflict.tmpl")->output);
342 $form->field(name
=> "do", value
=> "edit", force
=> 1);
343 $form->tmpl_param("page_select", 0);
344 $form->field(name
=> "page", type
=> 'hidden');
345 $form->field(name
=> "type", type
=> 'hidden');
346 $form->title(sprintf(gettext
("editing %s"), $page));
347 $form->field("editcontent",
348 value
=> readfile
("$config{srcdir}/$file").
349 "\n\n\n".$form->field("editcontent"),
351 showform
($form, \
@buttons, $session, $q,
357 if (defined $form->field('editmessage') &&
358 length $form->field('editmessage')) {
359 $message=$form->field('editmessage');
362 my $content=$form->field('editcontent');
363 check_content
(content
=> $content, page
=> $page,
364 cgi
=> $q, session
=> $session,
365 subject
=> $message);
366 run_hooks
(editcontent
=> sub {
374 $content=~s/\r\n/\n/g;
376 $content.="\n" if $content !~ /\n$/;
378 $config{cgi
}=0; # avoid cgi error message
379 eval { writefile
($file, $config{srcdir
}, $content) };
382 $form->field(name
=> "rcsinfo", value
=> rcs_prepedit
($file),
384 my $mtemplate=template
("editfailedsave.tmpl");
385 $mtemplate->param(error_message
=> $@
);
386 $form->tmpl_param("message", $mtemplate->output);
387 $form->field("editcontent", value
=> $content, force
=> 1);
388 $form->tmpl_param("page_select", 0);
389 $form->field(name
=> "page", type
=> 'hidden');
390 $form->field(name
=> "type", type
=> 'hidden');
391 $form->title(sprintf(gettext
("editing %s"), $page));
392 showform
($form, \
@buttons, $session, $q,
403 # Prevent deadlock with post-commit hook by
404 # signaling to it that it should not try to
406 disable_commit_hook
();
407 $conflict=rcs_commit
(
410 token
=> $form->field("rcsinfo"),
413 enable_commit_hook
();
417 # Refresh even if there was a conflict, since other changes
418 # may have been committed while the post-commit hook was
420 require IkiWiki
::Render
;
424 if (defined $conflict) {
425 $form->field(name
=> "rcsinfo", value
=> rcs_prepedit
($file),
427 $form->tmpl_param("message", template
("editconflict.tmpl")->output);
428 $form->field("editcontent", value
=> $conflict, force
=> 1);
429 $form->field("do", "edit", force
=> 1);
430 $form->tmpl_param("page_select", 0);
431 $form->field(name
=> "page", type
=> 'hidden');
432 $form->field(name
=> "type", type
=> 'hidden');
433 $form->title(sprintf(gettext
("editing %s"), $page));
434 showform
($form, \
@buttons, $session, $q,
438 # The trailing question mark tries to avoid broken
439 # caches and get the most recent version of the page.
440 redirect
($q, $baseurl."?updated");