responses
[ikiwiki.git] / 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 => "attachment", type => "hidden");
128
129         return $f, ["Rename", "Cancel"];
130 } #}}}
131
132 sub rename_start ($$$$) { #{{{
133         my $q=shift;
134         my $session=shift;
135         my $attachment=shift;
136         my $page=shift;
137
138         check_canrename($page, $pagesources{$page}, undef, undef,
139                 $q, $session, $attachment);
140
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);
149         
150         my ($f, $buttons)=rename_form($q, $session, $page);
151         if (defined $attachment) {
152                 $f->field(name => "attachment", value => $attachment, force => 1);
153         }
154         
155         IkiWiki::showform($f, $buttons, $session, $q);
156         exit 0;
157 } #}}}
158
159 sub postrename ($;$$$) { #{{{
160         my $session=shift;
161         my $src=shift;
162         my $dest=shift;
163         my $attachment=shift;
164
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);
169
170         if (defined $dest) {
171                 if (! $attachment) {
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);
176                 }
177
178                 # Update edit form content to fix any links present
179                 # on it.
180                 $postrename->param("editcontent",
181                         renamepage_hook($dest, $src, $dest,
182                                  $postrename->param("editcontent")));
183
184                 # Get a new edit token; old was likely invalidated.
185                 $postrename->param("rcsinfo",
186                         IkiWiki::rcs_prepedit($pagesources{$dest}));
187         }
188
189         IkiWiki::cgi_editpage($postrename, $session);
190 } #}}}
191
192 sub formbuilder (@) { #{{{
193         my %params=@_;
194         my $form=$params{form};
195
196         if (defined $form->field("do") && $form->field("do") eq "edit") {
197                 my $q=$params{cgi};
198                 my $session=$params{session};
199
200                 if ($form->submitted eq "Rename") {
201                         rename_start($q, $session, 0, $form->field("page"));
202                 }
203                 elsif ($form->submitted eq "Rename Attachment") {
204                         my @selected=$q->param("attachment_select");
205                         if (@selected > 1) {
206                                 error(gettext("Only one attachment can be renamed at a time."));
207                         }
208                         elsif (! @selected) {
209                                 error(gettext("Please select the attachment to rename."))
210                         }
211                         rename_start($q, $session, 1, $selected[0]);
212                 }
213         }
214 } #}}}
215
216 my $renamesummary;
217
218 sub formbuilder_setup (@) { #{{{
219         my %params=@_;
220         my $form=$params{form};
221         my $q=$params{cgi};
222
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" />');
227
228                 if (defined $renamesummary) {
229                         $form->tmpl_param(message => $renamesummary);
230                 }
231         }
232 } #}}}
233
234 sub sessioncgi ($$) { #{{{
235         my $q=shift;
236
237         if ($q->param("do") eq 'rename') {
238                 my $session=shift;
239                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
240                 IkiWiki::decode_form_utf8($form);
241
242                 if ($form->submitted eq 'Cancel') {
243                         postrename($session);
244                 }
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")));
251
252                         my $destfile=$dest;
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);
257                                 }
258                                 else {
259                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
260                                         $type=$ext;
261                                 }
262                                 
263                                 $destfile.=".".$type;
264                         }
265
266                         check_canrename($src, $srcfile, $dest, $destfile,
267                                 $q, $session, $q->param("attachment"));
268
269                         # Ensures that the dest directory exists and is ok.
270                         IkiWiki::prep_writefile($destfile, $config{srcdir});
271
272                         # Do rename, update other pages, and refresh site.
273                         IkiWiki::disable_commit_hook() if $config{rcs};
274                         require IkiWiki::Render;
275                         if ($config{rcs}) {
276                                 IkiWiki::rcs_rename($srcfile, $destfile);
277                                 IkiWiki::rcs_commit_staged(
278                                         sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
279                                         $session->param("name"), $ENV{REMOTE_ADDR});
280                         }
281                         else {
282                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
283                                         error("rename: $!");
284                                 }
285                         }
286                         my @fixedlinks;
287                         if ($src ne $dest) {
288                                 foreach my $page (keys %links) {
289                                         my $needfix=0;
290                                         foreach my $link (@{$links{$page}}) {
291                                                 my $bestlink=bestlink($page, $link);
292                                                 if ($bestlink eq $src) {
293                                                         $needfix=1;
294                                                         last;
295                                                 }
296                                         }
297                                         if ($needfix) {
298                                                 my $file=$pagesources{$page};
299                                                 my $oldcontent=readfile($config{srcdir}."/".$file);
300                                                 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
301                                                 if ($oldcontent ne $content) {
302                                                         my $token=IkiWiki::rcs_prepedit($file);
303                                                         eval { writefile($file, $config{srcdir}, $content) };
304                                                         next if $@;
305                                                         my $conflict=IkiWiki::rcs_commit(
306                                                                 $file,
307                                                                 sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
308                                                                 $token,
309                                                                 $session->param("name"), 
310                                                                 $ENV{REMOTE_ADDR}
311                                                         );
312                                                         push @fixedlinks, $page if ! defined $conflict;
313                                                 }
314                                         }
315                                 }
316                         }
317                         if ($config{rcs}) {
318                                 IkiWiki::enable_commit_hook();
319                                 IkiWiki::rcs_update();
320                         }
321                         IkiWiki::refresh();
322                         IkiWiki::saveindex();
323
324                         # Scan for any remaining broken links to $src.
325                         my @brokenlinks;
326                         if ($src ne $dest) {
327                                 foreach my $page (keys %links) {
328                                         my $broken=0;
329                                         foreach my $link (@{$links{$page}}) {
330                                                 my $bestlink=bestlink($page, $link);
331                                                 if ($bestlink eq $src) {
332                                                         $broken=1;
333                                                         last;
334                                                 }
335                                         }
336                                         push @brokenlinks, $page if $broken;
337                                 }
338                         }
339
340                         # Generate a rename summary, that will be shown at the top
341                         # of the edit template.
342                         my $template=template("renamesummary.tmpl");
343                         $template->param(src => $srcfile);
344                         $template->param(dest => $destfile);
345                         if ($src ne $dest) {
346                                 $template->param(brokenlinks_checked => 1);
347                                 $template->param(brokenlinks => [
348                                         map {
349                                                 {
350                                                         page => htmllink($dest, $dest, $_,
351                                                                         noimageinline => 1)
352                                                 }
353                                         } @brokenlinks
354                                 ]);
355                                 $template->param(fixedlinks => [
356                                         map {
357                                                 {
358                                                         page => htmllink($dest, $dest, $_,
359                                                                         noimageinline => 1)
360                                                 }
361                                         } @fixedlinks
362                                 ]);
363                         }
364                         $renamesummary=$template->output;
365
366                         postrename($session, $src, $dest, $q->param("attachment"));
367                 }
368                 else {
369                         IkiWiki::showform($form, $buttons, $session, $q);
370                 }
371
372                 exit 0;
373         }
374 } #}}}
375
376 sub renamepage_hook ($$$$) { #{{{
377         my ($page, $src, $dest, $content)=@_;
378
379         IkiWiki::run_hooks(renamepage => sub {
380                 $content=shift->(
381                         page => $page,
382                         oldpage => $src,
383                         newpage => $dest,
384                         content => $content,
385                 );
386         });
387
388         return $content;
389 }# }}}
390
391 1