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.
51 if (IkiWiki::file_pruned($destfile, $config{srcdir})) {
52 error(sprintf(gettext("illegal name")));
55 # Must not be a known source file.
56 if (exists $pagesources{$dest}) {
57 error(sprintf(gettext("%s already exists"),
58 htmllink("", "", $dest, noimageinline => 1)));
61 # Must not exist on disk already.
62 if (-l "$config{srcdir}/$destfile" || -e _) {
63 error(sprintf(gettext("%s already exists on disk"), $destfile));
67 IkiWiki::check_canedit($dest, $q, $session);
69 # Note that $srcfile is used here, not $destfile,
70 # because it wants the current file, to check it.
71 IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
76 sub formbuilder_setup (@) { #{{{
78 my $form=$params{form};
81 if (defined $form->field("do") && $form->field("do") eq "edit") {
82 # Rename button for the page, and also for attachments.
83 push @{$params{buttons}}, "Rename";
84 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
88 sub rename_form ($$$) { #{{{
93 eval q{use CGI::FormBuilder};
95 my $f = CGI::FormBuilder->new(
97 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
103 action => $config{cgiurl},
104 stylesheet => IkiWiki::baseurl()."style.css",
105 fields => [qw{do page new_name attachment}],
108 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
109 $f->field(name => "page", type => "hidden", value => $page, force => 1);
110 $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
111 $f->field(name => "attachment", type => "hidden");
113 return $f, ["Rename", "Cancel"];
116 sub rename_start ($$$$) {
119 my $attachment=shift;
122 check_canrename($page, $pagesources{$page}, undef, undef,
123 $q, $session, $attachment);
125 # Save current form state to allow returning to it later
126 # without losing any edits.
127 # (But don't save what button was submitted, to avoid
128 # looping back to here.)
129 # Note: "_submit" is CGI::FormBuilder internals.
130 $q->param(-name => "_submit", -value => "");
131 $session->param(postrename => scalar $q->Vars);
132 IkiWiki::cgi_savesession($session);
134 my ($f, $buttons)=rename_form($q, $session, $page);
135 if (defined $attachment) {
136 $f->field(name => "attachment", value => $attachment, force => 1);
139 IkiWiki::showform($f, $buttons, $session, $q);
143 sub postrename ($;$) {
147 # Load saved form state and return to edit form.
148 my $postrename=CGI->new($session->param("postrename"));
149 if (defined $newname) {
150 # They renamed the page they were editing.
151 # Tweak the edit form to be editing the new
152 # page name, and redirect back to it.
154 error("don't know how to redir back!"); ## FIXME
156 $session->clear("postrename");
157 IkiWiki::cgi_savesession($session);
158 IkiWiki::cgi($postrename, $session);
161 sub formbuilder (@) { #{{{
163 my $form=$params{form};
165 if (defined $form->field("do") && $form->field("do") eq "edit") {
167 my $session=$params{session};
169 if ($form->submitted eq "Rename") {
170 rename_start($q, $session, 0, $form->field("page"));
172 elsif ($form->submitted eq "Rename Attachment") {
173 my @selected=$q->param("attachment_select");
175 error(gettext("Only one attachment can be renamed at a time."));
177 elsif (! @selected) {
178 error(gettext("Please select the attachment to rename."))
180 rename_start($q, $session, 1, $selected[0]);
185 sub sessioncgi ($$) { #{{{
188 if ($q->param("do") eq 'rename') {
190 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
191 IkiWiki::decode_form_utf8($form);
193 if ($form->submitted eq 'Cancel') {
194 postrename($session);
196 elsif ($form->submitted eq 'Rename' && $form->validate) {
197 # These untaints are safe because of the checks
198 # performed in check_canrename below.
199 my $src=$q->param("page");
200 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
201 my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
203 # The extension of dest is the same as src if it's
204 # a page. If it's an extension, the extension is
207 if (! $q->param("attachment")) {
208 my ($ext)=$srcfile=~/(\.[^.]+)$/;
212 check_canrename($src, $srcfile, $dest, $destfile,
213 $q, $session, $q->param("attachment"));
215 # Ensures that the dest directory exists and is ok.
216 IkiWiki::prep_writefile($destfile, $config{srcdir});
218 # Do rename, and update the wiki.
219 require IkiWiki::Render;
221 IkiWiki::disable_commit_hook();
222 IkiWiki::rcs_rename($srcfile, $destfile);
223 IkiWiki::rcs_commit_staged(gettext("rename $srcfile to $destfile"),
224 $session->param("name"), $ENV{REMOTE_ADDR});
225 IkiWiki::enable_commit_hook();
226 IkiWiki::rcs_update();
229 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
234 IkiWiki::saveindex();
236 if ($q->param("attachment")) {
237 postrename($session);
240 postrename($session, $dest);
244 IkiWiki::showform($form, $buttons, $session, $q);