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