2 package IkiWiki::Plugin::rename;
9 hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
10 hook(type => "formbuilder", id => "rename", call => \&formbuilder);
11 hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
15 sub check_canrename ($$$$$$$) { #{{{
24 # Must be a known source file.
25 if (! exists $pagesources{$src}) {
26 error(sprintf(gettext("%s does not exist"),
27 htmllink("", "", $src, noimageinline => 1)));
30 # Must exist on disk, and be a regular file.
31 if (! -e "$config{srcdir}/$srcfile") {
32 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
34 elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
35 error(sprintf(gettext("%s is not a file"), $srcfile));
39 IkiWiki::check_canedit($src, $q, $session);
41 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
44 # Dest checks can be omitted by passing undef.
46 if ($src eq $dest || $srcfile eq $destfile) {
47 error(gettext("no change to the file name was specified"));
50 # Must be a legal filename, and not absolute.
51 if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
53 error(sprintf(gettext("illegal name")));
56 # Must not be a known source file.
57 if (exists $pagesources{$dest}) {
58 error(sprintf(gettext("%s already exists"),
59 htmllink("", "", $dest, noimageinline => 1)));
62 # Must not exist on disk already.
63 if (-l "$config{srcdir}/$destfile" || -e _) {
64 error(sprintf(gettext("%s already exists on disk"), $destfile));
68 IkiWiki::check_canedit($dest, $q, $session);
70 # Note that $srcfile is used here, not $destfile,
71 # because it wants the current file, to check it.
72 IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
77 sub rename_form ($$$) { #{{{
82 eval q{use CGI::FormBuilder};
84 my $f = CGI::FormBuilder->new(
86 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
92 action => $config{cgiurl},
93 stylesheet => IkiWiki::baseurl()."style.css",
94 fields => [qw{do page new_name attachment}],
97 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
98 $f->field(name => "page", type => "hidden", value => $page, force => 1);
99 $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
100 $f->field(name => "attachment", type => "hidden");
102 return $f, ["Rename", "Cancel"];
105 sub rename_start ($$$$) { #{{{
108 my $attachment=shift;
111 check_canrename($page, $pagesources{$page}, undef, undef,
112 $q, $session, $attachment);
114 # Save current form state to allow returning to it later
115 # without losing any edits.
116 # (But don't save what button was submitted, to avoid
117 # looping back to here.)
118 # Note: "_submit" is CGI::FormBuilder internals.
119 $q->param(-name => "_submit", -value => "");
120 $session->param(postrename => scalar $q->Vars);
121 IkiWiki::cgi_savesession($session);
123 my ($f, $buttons)=rename_form($q, $session, $page);
124 if (defined $attachment) {
125 $f->field(name => "attachment", value => $attachment, force => 1);
128 IkiWiki::showform($f, $buttons, $session, $q);
132 sub postrename ($;$$$) { #{{{
136 my $attachment=shift;
138 # Load saved form state and return to edit page.
139 my $postrename=CGI->new($session->param("postrename"));
140 $session->clear("postrename");
141 IkiWiki::cgi_savesession($session);
145 # They renamed the page they were editing. This requires
146 # fixups to the edit form state.
147 # Tweak the edit form to be editing the new page.
148 $postrename->param("page", $dest);
151 # Update edit form content to fix any links present
153 $postrename->param("editcontent",
154 renamepage_hook($dest, $src, $dest,
155 $postrename->param("editcontent")));
157 # Get a new edit token; old was likely invalidated.
158 $postrename->param("rcsinfo",
159 IkiWiki::rcs_prepedit($pagesources{$dest}));
162 IkiWiki::cgi_editpage($postrename, $session);
165 sub formbuilder (@) { #{{{
167 my $form=$params{form};
169 if (defined $form->field("do") && $form->field("do") eq "edit") {
171 my $session=$params{session};
173 if ($form->submitted eq "Rename") {
174 rename_start($q, $session, 0, $form->field("page"));
176 elsif ($form->submitted eq "Rename Attachment") {
177 my @selected=$q->param("attachment_select");
179 error(gettext("Only one attachment can be renamed at a time."));
181 elsif (! @selected) {
182 error(gettext("Please select the attachment to rename."))
184 rename_start($q, $session, 1, $selected[0]);
191 sub formbuilder_setup (@) { #{{{
193 my $form=$params{form};
196 if (defined $form->field("do") && $form->field("do") eq "edit") {
197 # Rename button for the page, and also for attachments.
198 push @{$params{buttons}}, "Rename";
199 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
201 if (defined $renamesummary) {
202 $form->tmpl_param(message => $renamesummary);
207 sub sessioncgi ($$) { #{{{
210 if ($q->param("do") eq 'rename') {
212 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
213 IkiWiki::decode_form_utf8($form);
215 if ($form->submitted eq 'Cancel') {
216 postrename($session);
218 elsif ($form->submitted eq 'Rename' && $form->validate) {
219 # These untaints are safe because of the checks
220 # performed in check_canrename below.
221 my $src=$q->param("page");
222 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
223 my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
225 # The extension of dest is the same as src if it's
226 # a page. If it's an extension, the extension is
229 if (! $q->param("attachment")) {
230 my ($ext)=$srcfile=~/(\.[^.]+)$/;
234 check_canrename($src, $srcfile, $dest, $destfile,
235 $q, $session, $q->param("attachment"));
237 # Ensures that the dest directory exists and is ok.
238 IkiWiki::prep_writefile($destfile, $config{srcdir});
240 # Do rename, update other pages, and refresh site.
241 IkiWiki::disable_commit_hook() if $config{rcs};
242 require IkiWiki::Render;
244 IkiWiki::rcs_rename($srcfile, $destfile);
245 IkiWiki::rcs_commit_staged(
246 sprintf(gettext("rename %s to %s"), $src, $dest),
247 $session->param("name"), $ENV{REMOTE_ADDR});
250 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
255 foreach my $page (keys %links) {
257 foreach my $link (@{$links{$page}}) {
258 my $bestlink=bestlink($page, $link);
259 if ($bestlink eq $src) {
265 my $file=$pagesources{$page};
266 my $oldcontent=readfile($config{srcdir}."/".$file);
267 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
268 if ($oldcontent ne $content) {
269 my $token=IkiWiki::rcs_prepedit($file);
270 eval { writefile($file, $config{srcdir}, $content) };
272 my $conflict=IkiWiki::rcs_commit(
274 sprintf(gettext("update for rename of %s to %s"), $src, $dest),
276 $session->param("name"),
279 push @fixedlinks, $page if ! defined $conflict;
284 IkiWiki::enable_commit_hook();
285 IkiWiki::rcs_update();
288 IkiWiki::saveindex();
290 # Scan for any remaining broken links to $src.
292 foreach my $page (keys %links) {
294 foreach my $link (@{$links{$page}}) {
295 my $bestlink=bestlink($page, $link);
296 if ($bestlink eq $src) {
301 push @brokenlinks, $page if $broken;
304 # Generate a rename summary, that will be shown at the top
305 # of the edit template.
306 my $template=template("renamesummary.tmpl");
307 $template->param(src => $src);
308 $template->param(dest => $dest);
309 $template->param(brokenlinks => [
312 page => htmllink($dest, $dest, $_,
317 $template->param(fixedlinks => [
320 page => htmllink($dest, $dest, $_,
325 $renamesummary=$template->output;
327 postrename($session, $src, $dest, $q->param("attachment"));
330 IkiWiki::showform($form, $buttons, $session, $q);
337 sub renamepage_hook ($$$$) { #{{{
338 my ($page, $src, $dest, $content)=@_;
340 IkiWiki::run_hooks(renamepage => sub {