]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
checkboxes back
[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;
31         my $attachment=shift;
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         }
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 ($src eq $dest || $srcfile eq $destfile) {
56                         error(gettext("no change to the file name was specified"));
57                 }
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                 }
65                 # Must not be a known source file.
66                 if (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 } #}}}
86 sub rename_form ($$$) { #{{{ 
87         my $q=shift;
88         my $session=shift;
89         my $page=shift;
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         $f->field(name => "attachment", type => "hidden");
111         return $f, ["Rename", "Cancel"];
112 } #}}}
114 sub rename_start ($$$$) { #{{{
115         my $q=shift;
116         my $session=shift;
117         my $attachment=shift;
118         my $page=shift;
120         check_canrename($page, $pagesources{$page}, undef, undef,
121                 $q, $session, $attachment);
123         # Save current form state to allow returning to it later
124         # without losing any edits.
125         # (But don't save what button was submitted, to avoid
126         # looping back to here.)
127         # Note: "_submit" is CGI::FormBuilder internals.
128         $q->param(-name => "_submit", -value => "");
129         $session->param(postrename => scalar $q->Vars);
130         IkiWiki::cgi_savesession($session);
131         
132         my ($f, $buttons)=rename_form($q, $session, $page);
133         if (defined $attachment) {
134                 $f->field(name => "attachment", value => $attachment, force => 1);
135         }
136         
137         IkiWiki::showform($f, $buttons, $session, $q);
138         exit 0;
139 } #}}}
141 sub postrename ($;$$$) { #{{{
142         my $session=shift;
143         my $src=shift;
144         my $dest=shift;
145         my $attachment=shift;
147         # Load saved form state and return to edit page.
148         my $postrename=CGI->new($session->param("postrename"));
149         $session->clear("postrename");
150         IkiWiki::cgi_savesession($session);
152         if (defined $dest) {
153                 if (! $attachment) {
154                         # They renamed the page they were editing. This requires
155                         # fixups to the edit form state.
156                         # Tweak the edit form to be editing the new page.
157                         $postrename->param("page", $dest);
158                 }
160                 # Update edit form content to fix any links present
161                 # on it.
162                 $postrename->param("editcontent",
163                         renamepage_hook($dest, $src, $dest,
164                                  $postrename->param("editcontent")));
166                 # Get a new edit token; old was likely invalidated.
167                 $postrename->param("rcsinfo",
168                         IkiWiki::rcs_prepedit($pagesources{$dest}));
169         }
171         IkiWiki::cgi_editpage($postrename, $session);
172 } #}}}
174 sub formbuilder (@) { #{{{
175         my %params=@_;
176         my $form=$params{form};
178         if (defined $form->field("do") && $form->field("do") eq "edit") {
179                 my $q=$params{cgi};
180                 my $session=$params{session};
182                 if ($form->submitted eq "Rename") {
183                         rename_start($q, $session, 0, $form->field("page"));
184                 }
185                 elsif ($form->submitted eq "Rename Attachment") {
186                         my @selected=$q->param("attachment_select");
187                         if (@selected > 1) {
188                                 error(gettext("Only one attachment can be renamed at a time."));
189                         }
190                         elsif (! @selected) {
191                                 error(gettext("Please select the attachment to rename."))
192                         }
193                         rename_start($q, $session, 1, $selected[0]);
194                 }
195         }
196 } #}}}
198 my $renamesummary;
200 sub formbuilder_setup (@) { #{{{
201         my %params=@_;
202         my $form=$params{form};
203         my $q=$params{cgi};
205         if (defined $form->field("do") && $form->field("do") eq "edit") {
206                 # Rename button for the page, and also for attachments.
207                 push @{$params{buttons}}, "Rename";
208                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
210                 if (defined $renamesummary) {
211                         $form->tmpl_param(message => $renamesummary);
212                 }
213         }
214 } #}}}
216 sub sessioncgi ($$) { #{{{
217         my $q=shift;
219         if ($q->param("do") eq 'rename') {
220                 my $session=shift;
221                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
222                 IkiWiki::decode_form_utf8($form);
224                 if ($form->submitted eq 'Cancel') {
225                         postrename($session);
226                 }
227                 elsif ($form->submitted eq 'Rename' && $form->validate) {
228                         # These untaints are safe because of the checks
229                         # performed in check_canrename below.
230                         my $src=$q->param("page");
231                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
232                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
234                         # The extension of dest is the same as src if it's
235                         # a page. If it's an extension, the extension is
236                         # already included.
237                         my $destfile=$dest;
238                         if (! $q->param("attachment")) {
239                                 my ($ext)=$srcfile=~/(\.[^.]+)$/;
240                                 $destfile.=$ext;
241                         }
243                         check_canrename($src, $srcfile, $dest, $destfile,
244                                 $q, $session, $q->param("attachment"));
246                         # Ensures that the dest directory exists and is ok.
247                         IkiWiki::prep_writefile($destfile, $config{srcdir});
249                         # Do rename, update other pages, and refresh site.
250                         IkiWiki::disable_commit_hook() if $config{rcs};
251                         require IkiWiki::Render;
252                         if ($config{rcs}) {
253                                 IkiWiki::rcs_rename($srcfile, $destfile);
254                                 IkiWiki::rcs_commit_staged(
255                                         sprintf(gettext("rename %s to %s"), $src, $dest),
256                                         $session->param("name"), $ENV{REMOTE_ADDR});
257                         }
258                         else {
259                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
260                                         error("rename: $!");
261                                 }
262                         }
263                         my @fixedlinks;
264                         foreach my $page (keys %links) {
265                                 my $needfix=0;
266                                 foreach my $link (@{$links{$page}}) {
267                                         my $bestlink=bestlink($page, $link);
268                                         if ($bestlink eq $src) {
269                                                 $needfix=1;
270                                                 last;
271                                         }
272                                 }
273                                 if ($needfix) {
274                                         my $file=$pagesources{$page};
275                                         my $oldcontent=readfile($config{srcdir}."/".$file);
276                                         my $content=renamepage_hook($page, $src, $dest, $oldcontent);
277                                         if ($oldcontent ne $content) {
278                                                 my $token=IkiWiki::rcs_prepedit($file);
279                                                 eval { writefile($file, $config{srcdir}, $content) };
280                                                 next if $@;
281                                                 my $conflict=IkiWiki::rcs_commit(
282                                                         $file,
283                                                         sprintf(gettext("update for rename of %s to %s"), $src, $dest),
284                                                         $token,
285                                                         $session->param("name"), 
286                                                         $ENV{REMOTE_ADDR}
287                                                 );
288                                                 push @fixedlinks, $page if ! defined $conflict;
289                                         }
290                                 }
291                         }
292                         if ($config{rcs}) {
293                                 IkiWiki::enable_commit_hook();
294                                 IkiWiki::rcs_update();
295                         }
296                         IkiWiki::refresh();
297                         IkiWiki::saveindex();
299                         # Scan for any remaining broken links to $src.
300                         my @brokenlinks;
301                         foreach my $page (keys %links) {
302                                 my $broken=0;
303                                 foreach my $link (@{$links{$page}}) {
304                                         my $bestlink=bestlink($page, $link);
305                                         if ($bestlink eq $src) {
306                                                 $broken=1;
307                                                 last;
308                                         }
309                                 }
310                                 push @brokenlinks, $page if $broken;
311                         }
313                         # Generate a rename summary, that will be shown at the top
314                         # of the edit template.
315                         my $template=template("renamesummary.tmpl");
316                         $template->param(src => $src);
317                         $template->param(dest => $dest);
318                         $template->param(brokenlinks => [
319                                 map {
320                                         {
321                                                 page => htmllink($dest, $dest, $_,
322                                                                 noimageinline => 1)
323                                         }
324                                 } @brokenlinks
325                         ]);
326                         $template->param(fixedlinks => [
327                                 map {
328                                         {
329                                                 page => htmllink($dest, $dest, $_,
330                                                                 noimageinline => 1)
331                                         }
332                                 } @fixedlinks
333                         ]);
334                         $renamesummary=$template->output;
336                         postrename($session, $src, $dest, $q->param("attachment"));
337                 }
338                 else {
339                         IkiWiki::showform($form, $buttons, $session, $q);
340                 }
342                 exit 0;
343         }
344 } #}}}
346 sub renamepage_hook ($$$$) { #{{{
347         my ($page, $src, $dest, $content)=@_;
349         IkiWiki::run_hooks(renamepage => sub {
350                 $content=shift->(
351                         page => $page,
352                         oldpage => $src,
353                         newpage => $dest,
354                         content => $content,
355                 );
356         });
358         return $content;
359 }# }}}