]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
f0dfb2413856c5dcc2bbd1eae2930cc86ec5bac3
[git.ikiwiki.info.git] / IkiWiki / Plugin / rename.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rename;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
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);
14 } # }}}
16 sub getsetup () { #{{{
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22 } #}}}
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;
32         my $attachment=! defined IkiWiki::pagetype($pagesources{$src});
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         }
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                 }
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                 }
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 } #}}}
92 sub rename_form ($$$) { #{{{ 
93         my $q=shift;
94         my $session=shift;
95         my $page=shift;
97         eval q{use CGI::FormBuilder};
98         error($@) if $@;
99         my $f = CGI::FormBuilder->new(
100                 name => "rename",
101                 title => sprintf(gettext("rename %s"), IkiWiki::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 => IkiWiki::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                 $f->field(name => "subpages",
134                         label => "",
135                         type => "checkbox",
136                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
137                         value => 1,
138                         force => 1);
139         }
140         $f->field(name => "attachment", type => "hidden");
142         return $f, ["Rename", "Cancel"];
143 } #}}}
145 sub rename_start ($$$$) { #{{{
146         my $q=shift;
147         my $session=shift;
148         my $attachment=shift;
149         my $page=shift;
151         check_canrename($page, $pagesources{$page}, undef, undef,
152                 $q, $session);
154         # Save current form state to allow returning to it later
155         # without losing any edits.
156         # (But don't save what button was submitted, to avoid
157         # looping back to here.)
158         # Note: "_submit" is CGI::FormBuilder internals.
159         $q->param(-name => "_submit", -value => "");
160         $session->param(postrename => scalar $q->Vars);
161         IkiWiki::cgi_savesession($session);
162         
163         my ($f, $buttons)=rename_form($q, $session, $page);
164         if (defined $attachment) {
165                 $f->field(name => "attachment", value => $attachment, force => 1);
166         }
167         
168         IkiWiki::showform($f, $buttons, $session, $q);
169         exit 0;
170 } #}}}
172 sub postrename ($;$$$) { #{{{
173         my $session=shift;
174         my $src=shift;
175         my $dest=shift;
176         my $attachment=shift;
178         # Load saved form state and return to edit page.
179         my $postrename=CGI->new($session->param("postrename"));
180         $session->clear("postrename");
181         IkiWiki::cgi_savesession($session);
183         if (defined $dest) {
184                 if (! $attachment) {
185                         # They renamed the page they were editing. This requires
186                         # fixups to the edit form state.
187                         # Tweak the edit form to be editing the new page.
188                         $postrename->param("page", $dest);
189                 }
191                 # Update edit form content to fix any links present
192                 # on it.
193                 $postrename->param("editcontent",
194                         renamepage_hook($dest, $src, $dest,
195                                  $postrename->param("editcontent")));
197                 # Get a new edit token; old was likely invalidated.
198                 $postrename->param("rcsinfo",
199                         IkiWiki::rcs_prepedit($pagesources{$dest}));
200         }
202         IkiWiki::cgi_editpage($postrename, $session);
203 } #}}}
205 sub formbuilder (@) { #{{{
206         my %params=@_;
207         my $form=$params{form};
209         if (defined $form->field("do") && $form->field("do") eq "edit") {
210                 my $q=$params{cgi};
211                 my $session=$params{session};
213                 if ($form->submitted eq "Rename") {
214                         rename_start($q, $session, 0, $form->field("page"));
215                 }
216                 elsif ($form->submitted eq "Rename Attachment") {
217                         my @selected=$q->param("attachment_select");
218                         if (@selected > 1) {
219                                 error(gettext("Only one attachment can be renamed at a time."));
220                         }
221                         elsif (! @selected) {
222                                 error(gettext("Please select the attachment to rename."))
223                         }
224                         rename_start($q, $session, 1, $selected[0]);
225                 }
226         }
227 } #}}}
229 my $renamesummary;
231 sub formbuilder_setup (@) { #{{{
232         my %params=@_;
233         my $form=$params{form};
234         my $q=$params{cgi};
236         if (defined $form->field("do") && $form->field("do") eq "edit") {
237                 # Rename button for the page, and also for attachments.
238                 push @{$params{buttons}}, "Rename";
239                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
241                 if (defined $renamesummary) {
242                         $form->tmpl_param(message => $renamesummary);
243                 }
244         }
245 } #}}}
247 sub sessioncgi ($$) { #{{{
248         my $q=shift;
250         if ($q->param("do") eq 'rename') {
251                 my $session=shift;
252                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
253                 IkiWiki::decode_form_utf8($form);
255                 if ($form->submitted eq 'Cancel') {
256                         postrename($session);
257                 }
258                 elsif ($form->submitted eq 'Rename' && $form->validate) {
259                         # These untaints are safe because of the checks
260                         # performed in check_canrename below.
261                         my $src=$q->param("page");
262                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
263                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
265                         my $destfile=$dest;
266                         if (! $q->param("attachment")) {
267                                 my $type=$q->param('type');
268                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
269                                         $type=IkiWiki::possibly_foolish_untaint($type);
270                                 }
271                                 else {
272                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
273                                         $type=$ext;
274                                 }
275                                 
276                                 $destfile.=".".$type;
277                         }
279                         check_canrename($src, $srcfile, $dest, $destfile,
280                                 $q, $session);
282                         # See if subpages need to be renamed.
283                         my @subpages;
284                         if ($q->param("subpages") && $src ne $dest) {
285                                 foreach my $p (keys %pagesources) {
286                                         if ($pagesources{$p}=~m/^\Q$src\E\/$/) {
287                                                 push @subpages, $p;
288                                         }
289                                 }
290                         }
292                         # Begin renaming process, which will end with a
293                         # wiki refresh.
294                         require IkiWiki::Render;
295                         IkiWiki::disable_commit_hook() if $config{rcs};
297                         do_rename($srcfile, $destfile, $session);
299                         foreach my $subpage (@subpages) {
300                                 my $subsrc=$pagesources{$subpage};
301                                 my $subdest=$subsrc;
302                                 $subdest=~s/^\Q$src\E\//$dest/;
303                                 eval {
304                                         do_rename($subsrc, $subdest, $session)
305                                 };
306                         }
308                         my @fixedlinks;
309                         if ($src ne $dest) {
310                                 push @fixedlinks, fixlinks($src, $dest, $session);
311                         }
313                         # End renaming process and refresh wiki.
314                         if ($config{rcs}) {
315                                 IkiWiki::enable_commit_hook();
316                                 IkiWiki::rcs_update();
317                         }
318                         IkiWiki::refresh();
319                         IkiWiki::saveindex();
321                         # Scan for any remaining broken links to $src.
322                         my @brokenlinks;
323                         if ($src ne $dest) {
324                                 foreach my $page (keys %links) {
325                                         my $broken=0;
326                                         foreach my $link (@{$links{$page}}) {
327                                                 my $bestlink=bestlink($page, $link);
328                                                 if ($bestlink eq $src) {
329                                                         $broken=1;
330                                                         last;
331                                                 }
332                                         }
333                                         push @brokenlinks, $page if $broken;
334                                 }
335                         }
337                         # Generate a rename summary, that will be shown at the top
338                         # of the edit template.
339                         my $template=template("renamesummary.tmpl");
340                         $template->param(src => $srcfile);
341                         $template->param(dest => $destfile);
342                         if ($src ne $dest) {
343                                 $template->param(brokenlinks_checked => 1);
344                                 $template->param(brokenlinks => [
345                                         map {
346                                                 {
347                                                         page => htmllink($dest, $dest, $_,
348                                                                         noimageinline => 1)
349                                                 }
350                                         } @brokenlinks
351                                 ]);
352                                 $template->param(fixedlinks => [
353                                         map {
354                                                 {
355                                                         page => htmllink($dest, $dest, $_,
356                                                                         noimageinline => 1)
357                                                 }
358                                         } @fixedlinks
359                                 ]);
360                         }
361                         $renamesummary=$template->output;
363                         postrename($session, $src, $dest, $q->param("attachment"));
364                 }
365                 else {
366                         IkiWiki::showform($form, $buttons, $session, $q);
367                 }
369                 exit 0;
370         }
371 } #}}}
373 sub renamepage_hook ($$$$) { #{{{
374         my ($page, $src, $dest, $content)=@_;
376         IkiWiki::run_hooks(renamepage => sub {
377                 $content=shift->(
378                         page => $page,
379                         oldpage => $src,
380                         newpage => $dest,
381                         content => $content,
382                 );
383         });
385         return $content;
386 }# }}}
387                         
388 sub do_rename ($$$) { #{{{
389         my $srcfile=shift;
390         my $destfile=shift;
391         my $session=shift;
392                         
393         # Actual file rename happens here.
394         # First, ensure that the dest directory exists and is ok.
395         IkiWiki::prep_writefile($destfile, $config{srcdir});
396         if ($config{rcs}) {
397                 IkiWiki::rcs_rename($srcfile, $destfile);
398                 IkiWiki::rcs_commit_staged(
399                         sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
400                         $session->param("name"), $ENV{REMOTE_ADDR});
401         }
402         else {
403                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
404                         error("rename: $!");
405                 }
406         }
407 } # }}}
409 sub fixlinks ($$$) { #{{{
410         my $src=shift;
411         my $dest=shift;
412         my $session=shift;
414         my @fixedlinks;
416         foreach my $page (keys %links) {
417                 my $needfix=0;
418                 foreach my $link (@{$links{$page}}) {
419                         my $bestlink=bestlink($page, $link);
420                         if ($bestlink eq $src) {
421                                 $needfix=1;
422                                 last;
423                         }
424                 }
425                 if ($needfix) {
426                         my $file=$pagesources{$page};
427                         my $oldcontent=readfile($config{srcdir}."/".$file);
428                         my $content=renamepage_hook($page, $src, $dest, $oldcontent);
429                         if ($oldcontent ne $content) {
430                                 my $token=IkiWiki::rcs_prepedit($file);
431                                 eval { writefile($file, $config{srcdir}, $content) };
432                                 next if $@;
433                                 my $conflict=IkiWiki::rcs_commit(
434                                         $file,
435                                         sprintf(gettext("update for rename of %s to %s"), $src, $dest),
436                                         $token,
437                                         $session->param("name"), 
438                                         $ENV{REMOTE_ADDR}
439                                 );
440                                 push @fixedlinks, $page if ! defined $conflict;
441                         }
442                 }
443         }
445         return @fixedlinks;
446 } #}}}