factor out do_rename
[ikiwiki] / IkiWiki / Plugin / rename.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rename;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
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);
13
14 } # }}}
15
16 sub getsetup () { #{{{
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22 } #}}}
23
24 sub check_canrename ($$$$$$$) { #{{{
25         my $src=shift;
26         my $srcfile=shift;
27         my $dest=shift;
28         my $destfile=shift;
29         my $q=shift;
30         my $session=shift;
31         my $attachment=shift;
32
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)));
37         }
38         
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));
42         }
43         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
44                 error(sprintf(gettext("%s is not a file"), $srcfile));
45         }
46
47         # Must be editable.
48         IkiWiki::check_canedit($src, $q, $session);
49         if ($attachment) {
50                 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
51         }
52         
53         # Dest checks can be omitted by passing undef.
54         if (defined $dest) {
55                 if ($srcfile eq $destfile) {
56                         error(gettext("no change to the file name was specified"));
57                 }
58
59                 # Must be a legal filename, and not absolute.
60                 if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
61                     $destfile=~/^\//) {
62                         error(sprintf(gettext("illegal name")));
63                 }
64
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)));
69                 }
70         
71                 # Must not exist on disk already.
72                 if (-l "$config{srcdir}/$destfile" || -e _) {
73                         error(sprintf(gettext("%s already exists on disk"), $destfile));
74                 }
75         
76                 # Must be editable.
77                 IkiWiki::check_canedit($dest, $q, $session);
78                 if ($attachment) {
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);
82                 }
83         }
84 } #}}}
85
86 sub rename_form ($$$) { #{{{ 
87         my $q=shift;
88         my $session=shift;
89         my $page=shift;
90
91         eval q{use CGI::FormBuilder};
92         error($@) if $@;
93         my $f = CGI::FormBuilder->new(
94                 name => "rename",
95                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
96                 header => 0,
97                 charset => "utf-8",
98                 method => 'POST',
99                 javascript => 0,
100                 params => $q,
101                 action => $config{cgiurl},
102                 stylesheet => IkiWiki::baseurl()."style.css",
103                 fields => [qw{do page new_name attachment}],
104         );
105         
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
111                 my @page_types;
112                 if (exists $IkiWiki::hooks{htmlize}) {
113                         @page_types=grep { !/^_/ }
114                                 keys %{$IkiWiki::hooks{htmlize}};
115                 }
116         
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);
121                 }
122         
123                 $f->field(name => "type", type => 'select',
124                         options => \@page_types,
125                         value => $ext, force => 1);
126                 
127                 $f->field(name => "subpages",
128                         label => "",
129                         type => "checkbox",
130                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
131                         value => 1,
132                         force => 1);
133         }
134         $f->field(name => "attachment", type => "hidden");
135
136         return $f, ["Rename", "Cancel"];
137 } #}}}
138
139 sub rename_start ($$$$) { #{{{
140         my $q=shift;
141         my $session=shift;
142         my $attachment=shift;
143         my $page=shift;
144
145         check_canrename($page, $pagesources{$page}, undef, undef,
146                 $q, $session, $attachment);
147
148         # Save current form state to allow returning to it later
149         # without losing any edits.
150         # (But don't save what button was submitted, to avoid
151         # looping back to here.)
152         # Note: "_submit" is CGI::FormBuilder internals.
153         $q->param(-name => "_submit", -value => "");
154         $session->param(postrename => scalar $q->Vars);
155         IkiWiki::cgi_savesession($session);
156         
157         my ($f, $buttons)=rename_form($q, $session, $page);
158         if (defined $attachment) {
159                 $f->field(name => "attachment", value => $attachment, force => 1);
160         }
161         
162         IkiWiki::showform($f, $buttons, $session, $q);
163         exit 0;
164 } #}}}
165
166 sub postrename ($;$$$) { #{{{
167         my $session=shift;
168         my $src=shift;
169         my $dest=shift;
170         my $attachment=shift;
171
172         # Load saved form state and return to edit page.
173         my $postrename=CGI->new($session->param("postrename"));
174         $session->clear("postrename");
175         IkiWiki::cgi_savesession($session);
176
177         if (defined $dest) {
178                 if (! $attachment) {
179                         # They renamed the page they were editing. This requires
180                         # fixups to the edit form state.
181                         # Tweak the edit form to be editing the new page.
182                         $postrename->param("page", $dest);
183                 }
184
185                 # Update edit form content to fix any links present
186                 # on it.
187                 $postrename->param("editcontent",
188                         renamepage_hook($dest, $src, $dest,
189                                  $postrename->param("editcontent")));
190
191                 # Get a new edit token; old was likely invalidated.
192                 $postrename->param("rcsinfo",
193                         IkiWiki::rcs_prepedit($pagesources{$dest}));
194         }
195
196         IkiWiki::cgi_editpage($postrename, $session);
197 } #}}}
198
199 sub formbuilder (@) { #{{{
200         my %params=@_;
201         my $form=$params{form};
202
203         if (defined $form->field("do") && $form->field("do") eq "edit") {
204                 my $q=$params{cgi};
205                 my $session=$params{session};
206
207                 if ($form->submitted eq "Rename") {
208                         rename_start($q, $session, 0, $form->field("page"));
209                 }
210                 elsif ($form->submitted eq "Rename Attachment") {
211                         my @selected=$q->param("attachment_select");
212                         if (@selected > 1) {
213                                 error(gettext("Only one attachment can be renamed at a time."));
214                         }
215                         elsif (! @selected) {
216                                 error(gettext("Please select the attachment to rename."))
217                         }
218                         rename_start($q, $session, 1, $selected[0]);
219                 }
220         }
221 } #}}}
222
223 my $renamesummary;
224
225 sub formbuilder_setup (@) { #{{{
226         my %params=@_;
227         my $form=$params{form};
228         my $q=$params{cgi};
229
230         if (defined $form->field("do") && $form->field("do") eq "edit") {
231                 # Rename button for the page, and also for attachments.
232                 push @{$params{buttons}}, "Rename";
233                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
234
235                 if (defined $renamesummary) {
236                         $form->tmpl_param(message => $renamesummary);
237                 }
238         }
239 } #}}}
240
241 sub sessioncgi ($$) { #{{{
242         my $q=shift;
243
244         if ($q->param("do") eq 'rename') {
245                 my $session=shift;
246                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
247                 IkiWiki::decode_form_utf8($form);
248
249                 if ($form->submitted eq 'Cancel') {
250                         postrename($session);
251                 }
252                 elsif ($form->submitted eq 'Rename' && $form->validate) {
253                         # These untaints are safe because of the checks
254                         # performed in check_canrename below.
255                         my $src=$q->param("page");
256                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
257                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
258
259                         my $destfile=$dest;
260                         if (! $q->param("attachment")) {
261                                 my $type=$q->param('type');
262                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
263                                         $type=IkiWiki::possibly_foolish_untaint($type);
264                                 }
265                                 else {
266                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
267                                         $type=$ext;
268                                 }
269                                 
270                                 $destfile.=".".$type;
271                         }
272
273                         check_canrename($src, $srcfile, $dest, $destfile,
274                                 $q, $session, $q->param("attachment"));
275
276                         # Begin renaming process, which will end with a
277                         # wiki refresh.
278                         require IkiWiki::Render;
279                         IkiWiki::disable_commit_hook() if $config{rcs};
280
281                         do_rename($srcfile, $destfile, $session);
282
283                         my @fixedlinks;
284                         if ($src ne $dest) {
285                                 foreach my $page (keys %links) {
286                                         my $needfix=0;
287                                         foreach my $link (@{$links{$page}}) {
288                                                 my $bestlink=bestlink($page, $link);
289                                                 if ($bestlink eq $src) {
290                                                         $needfix=1;
291                                                         last;
292                                                 }
293                                         }
294                                         if ($needfix) {
295                                                 my $file=$pagesources{$page};
296                                                 my $oldcontent=readfile($config{srcdir}."/".$file);
297                                                 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
298                                                 if ($oldcontent ne $content) {
299                                                         my $token=IkiWiki::rcs_prepedit($file);
300                                                         eval { writefile($file, $config{srcdir}, $content) };
301                                                         next if $@;
302                                                         my $conflict=IkiWiki::rcs_commit(
303                                                                 $file,
304                                                                 sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
305                                                                 $token,
306                                                                 $session->param("name"), 
307                                                                 $ENV{REMOTE_ADDR}
308                                                         );
309                                                         push @fixedlinks, $page if ! defined $conflict;
310                                                 }
311                                         }
312                                 }
313                         }
314
315                         # End renaming process and refresh wiki.
316                         if ($config{rcs}) {
317                                 IkiWiki::enable_commit_hook();
318                                 IkiWiki::rcs_update();
319                         }
320                         IkiWiki::refresh();
321                         IkiWiki::saveindex();
322
323                         # Scan for any remaining broken links to $src.
324                         my @brokenlinks;
325                         if ($src ne $dest) {
326                                 foreach my $page (keys %links) {
327                                         my $broken=0;
328                                         foreach my $link (@{$links{$page}}) {
329                                                 my $bestlink=bestlink($page, $link);
330                                                 if ($bestlink eq $src) {
331                                                         $broken=1;
332                                                         last;
333                                                 }
334                                         }
335                                         push @brokenlinks, $page if $broken;
336                                 }
337                         }
338
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);
344                         if ($src ne $dest) {
345                                 $template->param(brokenlinks_checked => 1);
346                                 $template->param(brokenlinks => [
347                                         map {
348                                                 {
349                                                         page => htmllink($dest, $dest, $_,
350                                                                         noimageinline => 1)
351                                                 }
352                                         } @brokenlinks
353                                 ]);
354                                 $template->param(fixedlinks => [
355                                         map {
356                                                 {
357                                                         page => htmllink($dest, $dest, $_,
358                                                                         noimageinline => 1)
359                                                 }
360                                         } @fixedlinks
361                                 ]);
362                         }
363                         $renamesummary=$template->output;
364
365                         postrename($session, $src, $dest, $q->param("attachment"));
366                 }
367                 else {
368                         IkiWiki::showform($form, $buttons, $session, $q);
369                 }
370
371                 exit 0;
372         }
373 } #}}}
374
375 sub renamepage_hook ($$$$) { #{{{
376         my ($page, $src, $dest, $content)=@_;
377
378         IkiWiki::run_hooks(renamepage => sub {
379                 $content=shift->(
380                         page => $page,
381                         oldpage => $src,
382                         newpage => $dest,
383                         content => $content,
384                 );
385         });
386
387         return $content;
388 }# }}}
389                         
390 sub do_rename ($$$) { #{{{
391         my $srcfile=shift;
392         my $destfile=shift;
393         my $session=shift;
394                         
395         # Actual file rename happens here.
396         # First, ensure that the dest directory exists and is ok.
397         IkiWiki::prep_writefile($destfile, $config{srcdir});
398         if ($config{rcs}) {
399                 IkiWiki::rcs_rename($srcfile, $destfile);
400                 IkiWiki::rcs_commit_staged(
401                         sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
402                         $session->param("name"), $ENV{REMOTE_ADDR});
403         }
404         else {
405                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
406                         error("rename: $!");
407                 }
408         }
409 } # }}}
410
411 1