2 package IkiWiki::Plugin::rename;
9 hook(type => "getsetup", id => "rename", call => \&getsetup);
10 hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
11 hook(type => "formbuilder", id => "rename", call => \&formbuilder);
12 hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
16 sub getsetup () { #{{{
24 sub check_canrename ($$$$$$$) { #{{{
33 # Must be a known source file.
34 if (! exists $pagesources{$src}) {
35 error(sprintf(gettext("%s does not exist"),
36 htmllink("", "", $src, noimageinline => 1)));
39 # Must exist on disk, and be a regular file.
40 if (! -e "$config{srcdir}/$srcfile") {
41 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
43 elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
44 error(sprintf(gettext("%s is not a file"), $srcfile));
48 IkiWiki::check_canedit($src, $q, $session);
50 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
53 # Dest checks can be omitted by passing undef.
55 if ($srcfile eq $destfile) {
56 error(gettext("no change to the file name was specified"));
59 # Must be a legal filename, and not absolute.
60 if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
62 error(sprintf(gettext("illegal name")));
65 # Must not be a known source file.
66 if ($src ne $dest && exists $pagesources{$dest}) {
67 error(sprintf(gettext("%s already exists"),
68 htmllink("", "", $dest, noimageinline => 1)));
71 # Must not exist on disk already.
72 if (-l "$config{srcdir}/$destfile" || -e _) {
73 error(sprintf(gettext("%s already exists on disk"), $destfile));
77 IkiWiki::check_canedit($dest, $q, $session);
79 # Note that $srcfile is used here, not $destfile,
80 # because it wants the current file, to check it.
81 IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
86 sub rename_form ($$$) { #{{{
91 eval q{use CGI::FormBuilder};
93 my $f = CGI::FormBuilder->new(
95 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
101 action => $config{cgiurl},
102 stylesheet => IkiWiki::baseurl()."style.css",
103 fields => [qw{do page new_name attachment}],
106 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
107 $f->field(name => "page", type => "hidden", value => $page, force => 1);
108 $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
109 if (!$q->param("attachment")) {
110 # insert the standard extensions
112 if (exists $IkiWiki::hooks{htmlize}) {
113 @page_types=grep { !/^_/ }
114 keys %{$IkiWiki::hooks{htmlize}};
117 # make sure the current extension is in the list
118 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
119 if (! $IkiWiki::hooks{htmlize}{$ext}) {
120 unshift(@page_types, $ext);
123 $f->field(name => "type", type => 'select',
124 options => \@page_types,
125 value => $ext, force => 1);
127 $f->field(name => "attachment", type => "hidden");
129 return $f, ["Rename", "Cancel"];
132 sub rename_start ($$$$) { #{{{
135 my $attachment=shift;
138 check_canrename($page, $pagesources{$page}, undef, undef,
139 $q, $session, $attachment);
141 # Save current form state to allow returning to it later
142 # without losing any edits.
143 # (But don't save what button was submitted, to avoid
144 # looping back to here.)
145 # Note: "_submit" is CGI::FormBuilder internals.
146 $q->param(-name => "_submit", -value => "");
147 $session->param(postrename => scalar $q->Vars);
148 IkiWiki::cgi_savesession($session);
150 my ($f, $buttons)=rename_form($q, $session, $page);
151 if (defined $attachment) {
152 $f->field(name => "attachment", value => $attachment, force => 1);
155 IkiWiki::showform($f, $buttons, $session, $q);
159 sub postrename ($;$$$) { #{{{
163 my $attachment=shift;
165 # Load saved form state and return to edit page.
166 my $postrename=CGI->new($session->param("postrename"));
167 $session->clear("postrename");
168 IkiWiki::cgi_savesession($session);
172 # They renamed the page they were editing. This requires
173 # fixups to the edit form state.
174 # Tweak the edit form to be editing the new page.
175 $postrename->param("page", $dest);
178 # Update edit form content to fix any links present
180 $postrename->param("editcontent",
181 renamepage_hook($dest, $src, $dest,
182 $postrename->param("editcontent")));
184 # Get a new edit token; old was likely invalidated.
185 $postrename->param("rcsinfo",
186 IkiWiki::rcs_prepedit($pagesources{$dest}));
189 IkiWiki::cgi_editpage($postrename, $session);
192 sub formbuilder (@) { #{{{
194 my $form=$params{form};
196 if (defined $form->field("do") && $form->field("do") eq "edit") {
198 my $session=$params{session};
200 if ($form->submitted eq "Rename") {
201 rename_start($q, $session, 0, $form->field("page"));
203 elsif ($form->submitted eq "Rename Attachment") {
204 my @selected=$q->param("attachment_select");
206 error(gettext("Only one attachment can be renamed at a time."));
208 elsif (! @selected) {
209 error(gettext("Please select the attachment to rename."))
211 rename_start($q, $session, 1, $selected[0]);
218 sub formbuilder_setup (@) { #{{{
220 my $form=$params{form};
223 if (defined $form->field("do") && $form->field("do") eq "edit") {
224 # Rename button for the page, and also for attachments.
225 push @{$params{buttons}}, "Rename";
226 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
228 if (defined $renamesummary) {
229 $form->tmpl_param(message => $renamesummary);
234 sub sessioncgi ($$) { #{{{
237 if ($q->param("do") eq 'rename') {
239 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
240 IkiWiki::decode_form_utf8($form);
242 if ($form->submitted eq 'Cancel') {
243 postrename($session);
245 elsif ($form->submitted eq 'Rename' && $form->validate) {
246 # These untaints are safe because of the checks
247 # performed in check_canrename below.
248 my $src=$q->param("page");
249 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
250 my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
253 if (! $q->param("attachment")) {
254 my $type=$q->param('type');
255 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
256 $type=IkiWiki::possibly_foolish_untaint($type);
258 my ($ext)=$srcfile=~/\.([^.]+)$/;
262 $destfile.=".".$type;
265 check_canrename($src, $srcfile, $dest, $destfile,
266 $q, $session, $q->param("attachment"));
268 # Ensures that the dest directory exists and is ok.
269 IkiWiki::prep_writefile($destfile, $config{srcdir});
271 # Do rename, update other pages, and refresh site.
272 IkiWiki::disable_commit_hook() if $config{rcs};
273 require IkiWiki::Render;
275 IkiWiki::rcs_rename($srcfile, $destfile);
276 IkiWiki::rcs_commit_staged(
277 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
278 $session->param("name"), $ENV{REMOTE_ADDR});
281 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
287 foreach my $page (keys %links) {
289 foreach my $link (@{$links{$page}}) {
290 my $bestlink=bestlink($page, $link);
291 if ($bestlink eq $src) {
297 my $file=$pagesources{$page};
298 my $oldcontent=readfile($config{srcdir}."/".$file);
299 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
300 if ($oldcontent ne $content) {
301 my $token=IkiWiki::rcs_prepedit($file);
302 eval { writefile($file, $config{srcdir}, $content) };
304 my $conflict=IkiWiki::rcs_commit(
306 sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
308 $session->param("name"),
311 push @fixedlinks, $page if ! defined $conflict;
317 IkiWiki::enable_commit_hook();
318 IkiWiki::rcs_update();
321 IkiWiki::saveindex();
323 # Scan for any remaining broken links to $src.
326 foreach my $page (keys %links) {
328 foreach my $link (@{$links{$page}}) {
329 my $bestlink=bestlink($page, $link);
330 if ($bestlink eq $src) {
335 push @brokenlinks, $page if $broken;
339 # Generate a rename summary, that will be shown at the top
340 # of the edit template.
341 my $template=template("renamesummary.tmpl");
342 $template->param(src => $srcfile);
343 $template->param(dest => $destfile);
345 $template->param(brokenlinks_checked => 1);
346 $template->param(brokenlinks => [
349 page => htmllink($dest, $dest, $_,
354 $template->param(fixedlinks => [
357 page => htmllink($dest, $dest, $_,
363 $renamesummary=$template->output;
365 postrename($session, $src, $dest, $q->param("attachment"));
368 IkiWiki::showform($form, $buttons, $session, $q);
375 sub renamepage_hook ($$$$) { #{{{
376 my ($page, $src, $dest, $content)=@_;
378 IkiWiki::run_hooks(renamepage => sub {