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 ($src eq $dest || $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 (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 $f->field(name => "attachment", type => "hidden");
111 return $f, ["Rename", "Cancel"];
114 sub rename_start ($$$$) { #{{{
117 my $attachment=shift;
120 check_canrename($page, $pagesources{$page}, undef, undef,
121 $q, $session, $attachment);
123 # Save current form state to allow returning to it later
124 # without losing any edits.
125 # (But don't save what button was submitted, to avoid
126 # looping back to here.)
127 # Note: "_submit" is CGI::FormBuilder internals.
128 $q->param(-name => "_submit", -value => "");
129 $session->param(postrename => scalar $q->Vars);
130 IkiWiki::cgi_savesession($session);
132 my ($f, $buttons)=rename_form($q, $session, $page);
133 if (defined $attachment) {
134 $f->field(name => "attachment", value => $attachment, force => 1);
137 IkiWiki::showform($f, $buttons, $session, $q);
141 sub postrename ($;$$$) { #{{{
145 my $attachment=shift;
147 # Load saved form state and return to edit page.
148 my $postrename=CGI->new($session->param("postrename"));
149 $session->clear("postrename");
150 IkiWiki::cgi_savesession($session);
154 # They renamed the page they were editing. This requires
155 # fixups to the edit form state.
156 # Tweak the edit form to be editing the new page.
157 $postrename->param("page", $dest);
160 # Update edit form content to fix any links present
162 $postrename->param("editcontent",
163 renamepage_hook($dest, $src, $dest,
164 $postrename->param("editcontent")));
166 # Get a new edit token; old was likely invalidated.
167 $postrename->param("rcsinfo",
168 IkiWiki::rcs_prepedit($pagesources{$dest}));
171 IkiWiki::cgi_editpage($postrename, $session);
174 sub formbuilder (@) { #{{{
176 my $form=$params{form};
178 if (defined $form->field("do") && $form->field("do") eq "edit") {
180 my $session=$params{session};
182 if ($form->submitted eq "Rename") {
183 rename_start($q, $session, 0, $form->field("page"));
185 elsif ($form->submitted eq "Rename Attachment") {
186 my @selected=$q->param("attachment_select");
188 error(gettext("Only one attachment can be renamed at a time."));
190 elsif (! @selected) {
191 error(gettext("Please select the attachment to rename."))
193 rename_start($q, $session, 1, $selected[0]);
200 sub formbuilder_setup (@) { #{{{
202 my $form=$params{form};
205 if (defined $form->field("do") && $form->field("do") eq "edit") {
206 # Rename button for the page, and also for attachments.
207 push @{$params{buttons}}, "Rename";
208 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
210 if (defined $renamesummary) {
211 $form->tmpl_param(message => $renamesummary);
216 sub sessioncgi ($$) { #{{{
219 if ($q->param("do") eq 'rename') {
221 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
222 IkiWiki::decode_form_utf8($form);
224 if ($form->submitted eq 'Cancel') {
225 postrename($session);
227 elsif ($form->submitted eq 'Rename' && $form->validate) {
228 # These untaints are safe because of the checks
229 # performed in check_canrename below.
230 my $src=$q->param("page");
231 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
232 my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
234 # The extension of dest is the same as src if it's
235 # a page. If it's an extension, the extension is
238 if (! $q->param("attachment")) {
239 my ($ext)=$srcfile=~/(\.[^.]+)$/;
243 check_canrename($src, $srcfile, $dest, $destfile,
244 $q, $session, $q->param("attachment"));
246 # Ensures that the dest directory exists and is ok.
247 IkiWiki::prep_writefile($destfile, $config{srcdir});
249 # Do rename, update other pages, and refresh site.
250 IkiWiki::disable_commit_hook() if $config{rcs};
251 require IkiWiki::Render;
253 IkiWiki::rcs_rename($srcfile, $destfile);
254 IkiWiki::rcs_commit_staged(
255 sprintf(gettext("rename %s to %s"), $src, $dest),
256 $session->param("name"), $ENV{REMOTE_ADDR});
259 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
264 foreach my $page (keys %links) {
266 foreach my $link (@{$links{$page}}) {
267 my $bestlink=bestlink($page, $link);
268 if ($bestlink eq $src) {
274 my $file=$pagesources{$page};
275 my $oldcontent=readfile($config{srcdir}."/".$file);
276 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
277 if ($oldcontent ne $content) {
278 my $token=IkiWiki::rcs_prepedit($file);
279 eval { writefile($file, $config{srcdir}, $content) };
281 my $conflict=IkiWiki::rcs_commit(
283 sprintf(gettext("update for rename of %s to %s"), $src, $dest),
285 $session->param("name"),
288 push @fixedlinks, $page if ! defined $conflict;
293 IkiWiki::enable_commit_hook();
294 IkiWiki::rcs_update();
297 IkiWiki::saveindex();
299 # Scan for any remaining broken links to $src.
301 foreach my $page (keys %links) {
303 foreach my $link (@{$links{$page}}) {
304 my $bestlink=bestlink($page, $link);
305 if ($bestlink eq $src) {
310 push @brokenlinks, $page if $broken;
313 # Generate a rename summary, that will be shown at the top
314 # of the edit template.
315 my $template=template("renamesummary.tmpl");
316 $template->param(src => $src);
317 $template->param(dest => $dest);
318 $template->param(brokenlinks => [
321 page => htmllink($dest, $dest, $_,
326 $template->param(fixedlinks => [
329 page => htmllink($dest, $dest, $_,
334 $renamesummary=$template->output;
336 postrename($session, $src, $dest, $q->param("attachment"));
339 IkiWiki::showform($form, $buttons, $session, $q);
346 sub renamepage_hook ($$$$) { #{{{
347 my ($page, $src, $dest, $content)=@_;
349 IkiWiki::run_hooks(renamepage => sub {