]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
improve handling of typoed or problem rcs
[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                                 } else {
258                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
259                                         $type=$ext;
260                                 }
261                                 
262                                 $destfile.=".".$type;
263                         }
264
265                         check_canrename($src, $srcfile, $dest, $destfile,
266                                 $q, $session, $q->param("attachment"));
267
268                         # Ensures that the dest directory exists and is ok.
269                         IkiWiki::prep_writefile($destfile, $config{srcdir});
270
271                         # Do rename, update other pages, and refresh site.
272                         IkiWiki::disable_commit_hook() if $config{rcs};
273                         require IkiWiki::Render;
274                         if ($config{rcs}) {
275                                 IkiWiki::rcs_rename($srcfile, $destfile);
276                                 IkiWiki::rcs_commit_staged(
277                                         sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
278                                         $session->param("name"), $ENV{REMOTE_ADDR});
279                         }
280                         else {
281                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
282                                         error("rename: $!");
283                                 }
284                         }
285                         my @fixedlinks;
286                         if ($src ne $dest) {
287                                 foreach my $page (keys %links) {
288                                         my $needfix=0;
289                                         foreach my $link (@{$links{$page}}) {
290                                                 my $bestlink=bestlink($page, $link);
291                                                 if ($bestlink eq $src) {
292                                                         $needfix=1;
293                                                         last;
294                                                 }
295                                         }
296                                         if ($needfix) {
297                                                 my $file=$pagesources{$page};
298                                                 my $oldcontent=readfile($config{srcdir}."/".$file);
299                                                 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
300                                                 if ($oldcontent ne $content) {
301                                                         my $token=IkiWiki::rcs_prepedit($file);
302                                                         eval { writefile($file, $config{srcdir}, $content) };
303                                                         next if $@;
304                                                         my $conflict=IkiWiki::rcs_commit(
305                                                                 $file,
306                                                                 sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
307                                                                 $token,
308                                                                 $session->param("name"), 
309                                                                 $ENV{REMOTE_ADDR}
310                                                         );
311                                                         push @fixedlinks, $page if ! defined $conflict;
312                                                 }
313                                         }
314                                 }
315                         }
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 1