]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
fix subpage rename bug with indexpages
[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
32         my $attachment=! defined pagetype($pagesources{$src});
33
34         # Must be a known source file.
35         if (! exists $pagesources{$src}) {
36                 error(sprintf(gettext("%s does not exist"),
37                         htmllink("", "", $src, noimageinline => 1)));
38         }
39         
40         # Must exist on disk, and be a regular file.
41         if (! -e "$config{srcdir}/$srcfile") {
42                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
43         }
44         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
45                 error(sprintf(gettext("%s is not a file"), $srcfile));
46         }
47
48         # Must be editable.
49         IkiWiki::check_canedit($src, $q, $session);
50         if ($attachment) {
51                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
52                         IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
53                 }
54                 else {
55                         error("renaming of attachments is not allowed");
56                 }
57         }
58         
59         # Dest checks can be omitted by passing undef.
60         if (defined $dest) {
61                 if ($srcfile eq $destfile) {
62                         error(gettext("no change to the file name was specified"));
63                 }
64
65                 # Must be a legal filename, and not absolute.
66                 if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
67                     $destfile=~/^\//) {
68                         error(sprintf(gettext("illegal name")));
69                 }
70
71                 # Must not be a known source file.
72                 if ($src ne $dest && exists $pagesources{$dest}) {
73                         error(sprintf(gettext("%s already exists"),
74                                 htmllink("", "", $dest, noimageinline => 1)));
75                 }
76         
77                 # Must not exist on disk already.
78                 if (-l "$config{srcdir}/$destfile" || -e _) {
79                         error(sprintf(gettext("%s already exists on disk"), $destfile));
80                 }
81         
82                 # Must be editable.
83                 IkiWiki::check_canedit($dest, $q, $session);
84                 if ($attachment) {
85                         # Note that $srcfile is used here, not $destfile,
86                         # because it wants the current file, to check it.
87                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
88                 }
89         }
90 } #}}}
91
92 sub rename_form ($$$) { #{{{ 
93         my $q=shift;
94         my $session=shift;
95         my $page=shift;
96
97         eval q{use CGI::FormBuilder};
98         error($@) if $@;
99         my $f = CGI::FormBuilder->new(
100                 name => "rename",
101                 title => sprintf(gettext("rename %s"), pagetitle($page)),
102                 header => 0,
103                 charset => "utf-8",
104                 method => 'POST',
105                 javascript => 0,
106                 params => $q,
107                 action => $config{cgiurl},
108                 stylesheet => IkiWiki::baseurl()."style.css",
109                 fields => [qw{do page new_name attachment}],
110         );
111         
112         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
113         $f->field(name => "page", type => "hidden", value => $page, force => 1);
114         $f->field(name => "new_name", value => pagetitle($page), size => 60);
115         if (!$q->param("attachment")) {
116                 # insert the standard extensions
117                 my @page_types;
118                 if (exists $IkiWiki::hooks{htmlize}) {
119                         @page_types=grep { !/^_/ }
120                                 keys %{$IkiWiki::hooks{htmlize}};
121                 }
122         
123                 # make sure the current extension is in the list
124                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
125                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
126                         unshift(@page_types, $ext);
127                 }
128         
129                 $f->field(name => "type", type => 'select',
130                         options => \@page_types,
131                         value => $ext, force => 1);
132                 
133                 foreach my $p (keys %pagesources) {
134                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
135                                 $f->field(name => "subpages",
136                                         label => "",
137                                         type => "checkbox",
138                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
139                                         value => 1,
140                                         force => 1);
141                                 last;
142                         }
143                 }
144         }
145         $f->field(name => "attachment", type => "hidden");
146
147         return $f, ["Rename", "Cancel"];
148 } #}}}
149
150 sub rename_start ($$$$) { #{{{
151         my $q=shift;
152         my $session=shift;
153         my $attachment=shift;
154         my $page=shift;
155
156         check_canrename($page, $pagesources{$page}, undef, undef,
157                 $q, $session);
158
159         # Save current form state to allow returning to it later
160         # without losing any edits.
161         # (But don't save what button was submitted, to avoid
162         # looping back to here.)
163         # Note: "_submit" is CGI::FormBuilder internals.
164         $q->param(-name => "_submit", -value => "");
165         $session->param(postrename => scalar $q->Vars);
166         IkiWiki::cgi_savesession($session);
167         
168         if (defined $attachment) {
169                 $q->param(-name => "attachment", -value => $attachment);
170         }
171         my ($f, $buttons)=rename_form($q, $session, $page);
172         IkiWiki::showform($f, $buttons, $session, $q);
173         exit 0;
174 } #}}}
175
176 sub postrename ($;$$$) { #{{{
177         my $session=shift;
178         my $src=shift;
179         my $dest=shift;
180         my $attachment=shift;
181
182         # Load saved form state and return to edit page.
183         my $postrename=CGI->new($session->param("postrename"));
184         $session->clear("postrename");
185         IkiWiki::cgi_savesession($session);
186
187         if (defined $dest) {
188                 if (! $attachment) {
189                         # They renamed the page they were editing. This requires
190                         # fixups to the edit form state.
191                         # Tweak the edit form to be editing the new page.
192                         $postrename->param("page", $dest);
193                 }
194
195                 # Update edit form content to fix any links present
196                 # on it.
197                 $postrename->param("editcontent",
198                         renamepage_hook($dest, $src, $dest,
199                                  $postrename->param("editcontent")));
200
201                 # Get a new edit token; old was likely invalidated.
202                 $postrename->param("rcsinfo",
203                         IkiWiki::rcs_prepedit($pagesources{$dest}));
204         }
205
206         IkiWiki::cgi_editpage($postrename, $session);
207 } #}}}
208
209 sub formbuilder (@) { #{{{
210         my %params=@_;
211         my $form=$params{form};
212
213         if (defined $form->field("do") && $form->field("do") eq "edit") {
214                 my $q=$params{cgi};
215                 my $session=$params{session};
216
217                 if ($form->submitted eq "Rename") {
218                         rename_start($q, $session, 0, $form->field("page"));
219                 }
220                 elsif ($form->submitted eq "Rename Attachment") {
221                         my @selected=$q->param("attachment_select");
222                         if (@selected > 1) {
223                                 error(gettext("Only one attachment can be renamed at a time."));
224                         }
225                         elsif (! @selected) {
226                                 error(gettext("Please select the attachment to rename."))
227                         }
228                         rename_start($q, $session, 1, $selected[0]);
229                 }
230         }
231 } #}}}
232
233 my $renamesummary;
234
235 sub formbuilder_setup (@) { #{{{
236         my %params=@_;
237         my $form=$params{form};
238         my $q=$params{cgi};
239
240         if (defined $form->field("do") && $form->field("do") eq "edit") {
241                 # Rename button for the page, and also for attachments.
242                 push @{$params{buttons}}, "Rename";
243                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
244
245                 if (defined $renamesummary) {
246                         $form->tmpl_param(message => $renamesummary);
247                 }
248         }
249 } #}}}
250
251 sub sessioncgi ($$) { #{{{
252         my $q=shift;
253
254         if ($q->param("do") eq 'rename') {
255                 my $session=shift;
256                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
257                 IkiWiki::decode_form_utf8($form);
258
259                 if ($form->submitted eq 'Cancel') {
260                         postrename($session);
261                 }
262                 elsif ($form->submitted eq 'Rename' && $form->validate) {
263                         # Queue of rename actions to perfom.
264                         my @torename;
265
266                         # These untaints are safe because of the checks
267                         # performed in check_canrename later.
268                         my $src=$q->param("page");
269                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
270                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
271                         my $destfile=$dest;
272                         if (! $q->param("attachment")) {
273                                 my $type=$q->param('type');
274                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
275                                         $type=IkiWiki::possibly_foolish_untaint($type);
276                                 }
277                                 else {
278                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
279                                         $type=$ext;
280                                 }
281                                 
282                                 $destfile=newpagefile($dest, $type);
283                         }
284                         push @torename, {
285                                 src => $src,
286                                 srcfile => $srcfile,
287                                 dest => $dest,
288                                 destfile => $destfile,
289                                 required => 1,
290                         };
291
292                         # See if any subpages need to be renamed.
293                         if ($q->param("subpages") && $src ne $dest) {
294                                 foreach my $p (keys %pagesources) {
295                                         next unless $pagesources{$p}=~m/^\Q$src\E\//;
296                                         # If indexpages is enabled, the
297                                         # srcfile should not be confused
298                                         # with a subpage.
299                                         next if $pagesources{$p} eq $srcfile;
300
301                                         my $d=$pagesources{$p};
302                                         $d=~s/^\Q$src\E\//$dest\//;
303                                         push @torename, {
304                                                 src => $p,
305                                                 srcfile => $pagesources{$p},
306                                                 dest => pagename($d),
307                                                 destfile => $d,
308                                                 required => 0,
309                                         };
310                                 }
311                         }
312                         
313                         require IkiWiki::Render;
314                         IkiWiki::disable_commit_hook() if $config{rcs};
315                         my %origpagesources=%pagesources;
316
317                         # First file renaming.
318                         foreach my $rename (@torename) {
319                                 if ($rename->{required}) {
320                                         do_rename($rename, $q, $session);
321                                 }
322                                 else {
323                                         eval {do_rename($rename, $q, $session)};
324                                         if ($@) {
325                                                 $rename->{error}=$@;
326                                                 next;
327                                         }
328                                 }
329
330                                 # Temporarily tweak pagesources to point to
331                                 # the renamed file, in case fixlinks needs
332                                 # to edit it.
333                                 $pagesources{$rename->{src}}=$rename->{destfile};
334                         }
335                         IkiWiki::rcs_commit_staged(
336                                 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
337                                 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
338
339                         # Then link fixups.
340                         foreach my $rename (@torename) {
341                                 next if $rename->{src} eq $rename->{dest};
342                                 next if $rename->{error};
343                                 foreach my $p (fixlinks($rename, $session)) {
344                                         # map old page names to new
345                                         foreach my $r (@torename) {
346                                                 next if $rename->{error};
347                                                 if ($r->{src} eq $p) {
348                                                         $p=$r->{dest};
349                                                         last;
350                                                 }
351                                         }
352                                         push @{$rename->{fixedlinks}}, $p;
353                                 }
354                         }
355
356                         # Then refresh.
357                         %pagesources=%origpagesources;
358                         if ($config{rcs}) {
359                                 IkiWiki::enable_commit_hook();
360                                 IkiWiki::rcs_update();
361                         }
362                         IkiWiki::refresh();
363                         IkiWiki::saveindex();
364
365                         # Find pages with remaining, broken links.
366                         foreach my $rename (@torename) {
367                                 next if $rename->{src} eq $rename->{dest};
368                                 
369                                 foreach my $page (keys %links) {
370                                         my $broken=0;
371                                         foreach my $link (@{$links{$page}}) {
372                                                 my $bestlink=bestlink($page, $link);
373                                                 if ($bestlink eq $rename->{src}) {
374                                                         push @{$rename->{brokenlinks}}, $page;
375                                                         last;
376                                                 }
377                                         }
378                                 }
379                         }
380
381                         # Generate a summary, that will be shown at the top
382                         # of the edit template.
383                         $renamesummary="";
384                         foreach my $rename (@torename) {
385                                 my $template=template("renamesummary.tmpl");
386                                 $template->param(src => $rename->{srcfile});
387                                 $template->param(dest => $rename->{destfile});
388                                 $template->param(error => $rename->{error});
389                                 if ($rename->{src} ne $rename->{dest}) {
390                                         $template->param(brokenlinks_checked => 1);
391                                         $template->param(brokenlinks => [
392                                                 map {
393                                                         {
394                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
395                                                                                 noimageinline => 1)
396                                                         }
397                                                 } @{$rename->{brokenlinks}}
398                                         ]);
399                                         $template->param(fixedlinks => [
400                                                 map {
401                                                         {
402                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
403                                                                                 noimageinline => 1)
404                                                         }
405                                                 } @{$rename->{fixedlinks}}
406                                         ]);
407                                 }
408                                 $renamesummary.=$template->output;
409                         }
410
411                         postrename($session, $src, $dest, $q->param("attachment"));
412                 }
413                 else {
414                         IkiWiki::showform($form, $buttons, $session, $q);
415                 }
416
417                 exit 0;
418         }
419 } #}}}
420
421 sub renamepage_hook ($$$$) { #{{{
422         my ($page, $src, $dest, $content)=@_;
423
424         IkiWiki::run_hooks(renamepage => sub {
425                 $content=shift->(
426                         page => $page,
427                         oldpage => $src,
428                         newpage => $dest,
429                         content => $content,
430                 );
431         });
432
433         return $content;
434 }# }}}
435                         
436 sub do_rename ($$$) { #{{{
437         my $rename=shift;
438         my $q=shift;
439         my $session=shift;
440
441         # First, check if this rename is allowed.
442         check_canrename($rename->{src},
443                 $rename->{srcfile},
444                 $rename->{dest},
445                 $rename->{destfile},
446                 $q, $session);
447
448         # Ensure that the dest directory exists and is ok.
449         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
450
451         if ($config{rcs}) {
452                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
453         }
454         else {
455                 if (! rename($config{srcdir}."/".$rename->{srcfile},
456                              $config{srcdir}."/".$rename->{destfile})) {
457                         error("rename: $!");
458                 }
459         }
460
461 } # }}}
462
463 sub fixlinks ($$$) { #{{{
464         my $rename=shift;
465         my $session=shift;
466
467         my @fixedlinks;
468
469         foreach my $page (keys %links) {
470                 my $needfix=0;
471                 foreach my $link (@{$links{$page}}) {
472                         my $bestlink=bestlink($page, $link);
473                         if ($bestlink eq $rename->{src}) {
474                                 $needfix=1;
475                                 last;
476                         }
477                 }
478                 if ($needfix) {
479                         my $file=$pagesources{$page};
480                         my $oldcontent=readfile($config{srcdir}."/".$file);
481                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
482                         if ($oldcontent ne $content) {
483                                 my $token=IkiWiki::rcs_prepedit($file);
484                                 eval { writefile($file, $config{srcdir}, $content) };
485                                 next if $@;
486                                 my $conflict=IkiWiki::rcs_commit(
487                                         $file,
488                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
489                                         $token,
490                                         $session->param("name"), 
491                                         $ENV{REMOTE_ADDR}
492                                 );
493                                 push @fixedlinks, $page if ! defined $conflict;
494                         }
495                 }
496         }
497
498         return @fixedlinks;
499 } #}}}
500
501 1