]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
rename, remove: Don't rely on a form parameter to tell whether the page should be...
[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 IkiWiki::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"), 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 => "attachment", type => "hidden");
134
135         return $f, ["Rename", "Cancel"];
136 } #}}}
137
138 sub rename_start ($$$$) { #{{{
139         my $q=shift;
140         my $session=shift;
141         my $attachment=shift;
142         my $page=shift;
143
144         check_canrename($page, $pagesources{$page}, undef, undef,
145                 $q, $session);
146
147         # Save current form state to allow returning to it later
148         # without losing any edits.
149         # (But don't save what button was submitted, to avoid
150         # looping back to here.)
151         # Note: "_submit" is CGI::FormBuilder internals.
152         $q->param(-name => "_submit", -value => "");
153         $session->param(postrename => scalar $q->Vars);
154         IkiWiki::cgi_savesession($session);
155         
156         my ($f, $buttons)=rename_form($q, $session, $page);
157         if (defined $attachment) {
158                 $f->field(name => "attachment", value => $attachment, force => 1);
159         }
160         
161         IkiWiki::showform($f, $buttons, $session, $q);
162         exit 0;
163 } #}}}
164
165 sub postrename ($;$$$) { #{{{
166         my $session=shift;
167         my $src=shift;
168         my $dest=shift;
169         my $attachment=shift;
170
171         # Load saved form state and return to edit page.
172         my $postrename=CGI->new($session->param("postrename"));
173         $session->clear("postrename");
174         IkiWiki::cgi_savesession($session);
175
176         if (defined $dest) {
177                 if (! $attachment) {
178                         # They renamed the page they were editing. This requires
179                         # fixups to the edit form state.
180                         # Tweak the edit form to be editing the new page.
181                         $postrename->param("page", $dest);
182                 }
183
184                 # Update edit form content to fix any links present
185                 # on it.
186                 $postrename->param("editcontent",
187                         renamepage_hook($dest, $src, $dest,
188                                  $postrename->param("editcontent")));
189
190                 # Get a new edit token; old was likely invalidated.
191                 $postrename->param("rcsinfo",
192                         IkiWiki::rcs_prepedit($pagesources{$dest}));
193         }
194
195         IkiWiki::cgi_editpage($postrename, $session);
196 } #}}}
197
198 sub formbuilder (@) { #{{{
199         my %params=@_;
200         my $form=$params{form};
201
202         if (defined $form->field("do") && $form->field("do") eq "edit") {
203                 my $q=$params{cgi};
204                 my $session=$params{session};
205
206                 if ($form->submitted eq "Rename") {
207                         rename_start($q, $session, 0, $form->field("page"));
208                 }
209                 elsif ($form->submitted eq "Rename Attachment") {
210                         my @selected=$q->param("attachment_select");
211                         if (@selected > 1) {
212                                 error(gettext("Only one attachment can be renamed at a time."));
213                         }
214                         elsif (! @selected) {
215                                 error(gettext("Please select the attachment to rename."))
216                         }
217                         rename_start($q, $session, 1, $selected[0]);
218                 }
219         }
220 } #}}}
221
222 my $renamesummary;
223
224 sub formbuilder_setup (@) { #{{{
225         my %params=@_;
226         my $form=$params{form};
227         my $q=$params{cgi};
228
229         if (defined $form->field("do") && $form->field("do") eq "edit") {
230                 # Rename button for the page, and also for attachments.
231                 push @{$params{buttons}}, "Rename";
232                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
233
234                 if (defined $renamesummary) {
235                         $form->tmpl_param(message => $renamesummary);
236                 }
237         }
238 } #}}}
239
240 sub sessioncgi ($$) { #{{{
241         my $q=shift;
242
243         if ($q->param("do") eq 'rename') {
244                 my $session=shift;
245                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
246                 IkiWiki::decode_form_utf8($form);
247
248                 if ($form->submitted eq 'Cancel') {
249                         postrename($session);
250                 }
251                 elsif ($form->submitted eq 'Rename' && $form->validate) {
252                         # These untaints are safe because of the checks
253                         # performed in check_canrename below.
254                         my $src=$q->param("page");
255                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
256                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
257
258                         my $destfile=$dest;
259                         if (! $q->param("attachment")) {
260                                 my $type=$q->param('type');
261                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
262                                         $type=IkiWiki::possibly_foolish_untaint($type);
263                                 }
264                                 else {
265                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
266                                         $type=$ext;
267                                 }
268                                 
269                                 $destfile.=".".$type;
270                         }
271
272                         check_canrename($src, $srcfile, $dest, $destfile,
273                                 $q, $session);
274
275                         # Ensures that the dest directory exists and is ok.
276                         IkiWiki::prep_writefile($destfile, $config{srcdir});
277
278                         # Do rename, update other pages, and refresh site.
279                         IkiWiki::disable_commit_hook() if $config{rcs};
280                         require IkiWiki::Render;
281                         if ($config{rcs}) {
282                                 IkiWiki::rcs_rename($srcfile, $destfile);
283                                 IkiWiki::rcs_commit_staged(
284                                         sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
285                                         $session->param("name"), $ENV{REMOTE_ADDR});
286                         }
287                         else {
288                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
289                                         error("rename: $!");
290                                 }
291                         }
292                         my @fixedlinks;
293                         if ($src ne $dest) {
294                                 foreach my $page (keys %links) {
295                                         my $needfix=0;
296                                         foreach my $link (@{$links{$page}}) {
297                                                 my $bestlink=bestlink($page, $link);
298                                                 if ($bestlink eq $src) {
299                                                         $needfix=1;
300                                                         last;
301                                                 }
302                                         }
303                                         if ($needfix) {
304                                                 my $file=$pagesources{$page};
305                                                 my $oldcontent=readfile($config{srcdir}."/".$file);
306                                                 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
307                                                 if ($oldcontent ne $content) {
308                                                         my $token=IkiWiki::rcs_prepedit($file);
309                                                         eval { writefile($file, $config{srcdir}, $content) };
310                                                         next if $@;
311                                                         my $conflict=IkiWiki::rcs_commit(
312                                                                 $file,
313                                                                 sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
314                                                                 $token,
315                                                                 $session->param("name"), 
316                                                                 $ENV{REMOTE_ADDR}
317                                                         );
318                                                         push @fixedlinks, $page if ! defined $conflict;
319                                                 }
320                                         }
321                                 }
322                         }
323                         if ($config{rcs}) {
324                                 IkiWiki::enable_commit_hook();
325                                 IkiWiki::rcs_update();
326                         }
327                         IkiWiki::refresh();
328                         IkiWiki::saveindex();
329
330                         # Scan for any remaining broken links to $src.
331                         my @brokenlinks;
332                         if ($src ne $dest) {
333                                 foreach my $page (keys %links) {
334                                         my $broken=0;
335                                         foreach my $link (@{$links{$page}}) {
336                                                 my $bestlink=bestlink($page, $link);
337                                                 if ($bestlink eq $src) {
338                                                         $broken=1;
339                                                         last;
340                                                 }
341                                         }
342                                         push @brokenlinks, $page if $broken;
343                                 }
344                         }
345
346                         # Generate a rename summary, that will be shown at the top
347                         # of the edit template.
348                         my $template=template("renamesummary.tmpl");
349                         $template->param(src => $srcfile);
350                         $template->param(dest => $destfile);
351                         if ($src ne $dest) {
352                                 $template->param(brokenlinks_checked => 1);
353                                 $template->param(brokenlinks => [
354                                         map {
355                                                 {
356                                                         page => htmllink($dest, $dest, $_,
357                                                                         noimageinline => 1)
358                                                 }
359                                         } @brokenlinks
360                                 ]);
361                                 $template->param(fixedlinks => [
362                                         map {
363                                                 {
364                                                         page => htmllink($dest, $dest, $_,
365                                                                         noimageinline => 1)
366                                                 }
367                                         } @fixedlinks
368                                 ]);
369                         }
370                         $renamesummary=$template->output;
371
372                         postrename($session, $src, $dest, $q->param("attachment"));
373                 }
374                 else {
375                         IkiWiki::showform($form, $buttons, $session, $q);
376                 }
377
378                 exit 0;
379         }
380 } #}}}
381
382 sub renamepage_hook ($$$$) { #{{{
383         my ($page, $src, $dest, $content)=@_;
384
385         IkiWiki::run_hooks(renamepage => sub {
386                 $content=shift->(
387                         page => $page,
388                         oldpage => $src,
389                         newpage => $dest,
390                         content => $content,
391                 );
392         });
393
394         return $content;
395 }# }}}
396
397 1