]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
allow account_creation_password to not be defined
[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 => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
10         hook(type => "formbuilder", id => "rename", call => \&formbuilder);
11         hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
12
13 } # }}}
14
15 sub check_canrename ($$$$$$$) { #{{{
16         my $src=shift;
17         my $srcfile=shift;
18         my $dest=shift;
19         my $destfile=shift;
20         my $q=shift;
21         my $session=shift;
22         my $attachment=shift;
23
24         # Must be a known source file.
25         if (! exists $pagesources{$src}) {
26                 error(sprintf(gettext("%s does not exist"),
27                         htmllink("", "", $src, noimageinline => 1)));
28         }
29         
30         # Must exist on disk, and be a regular file.
31         if (! -e "$config{srcdir}/$srcfile") {
32                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
33         }
34         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
35                 error(sprintf(gettext("%s is not a file"), $srcfile));
36         }
37
38         # Must be editable.
39         IkiWiki::check_canedit($src, $q, $session);
40         if ($attachment) {
41                 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
42         }
43         
44         # Dest checks can be omitted by passing undef.
45         if (defined $dest) {
46                 if ($src eq $dest || $srcfile eq $destfile) {
47                         error(gettext("no change to the file name was specified"));
48                 }
49
50                 # Must be a legal filename, and not absolute.
51                 if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
52                     $destfile=~/^\//) {
53                         error(sprintf(gettext("illegal name")));
54                 }
55
56                 # Must not be a known source file.
57                 if (exists $pagesources{$dest}) {
58                         error(sprintf(gettext("%s already exists"),
59                                 htmllink("", "", $dest, noimageinline => 1)));
60                 }
61         
62                 # Must not exist on disk already.
63                 if (-l "$config{srcdir}/$destfile" || -e _) {
64                         error(sprintf(gettext("%s already exists on disk"), $destfile));
65                 }
66         
67                 # Must be editable.
68                 IkiWiki::check_canedit($dest, $q, $session);
69                 if ($attachment) {
70                         # Note that $srcfile is used here, not $destfile,
71                         # because it wants the current file, to check it.
72                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
73                 }
74         }
75 } #}}}
76
77 sub rename_form ($$$) { #{{{ 
78         my $q=shift;
79         my $session=shift;
80         my $page=shift;
81
82         eval q{use CGI::FormBuilder};
83         error($@) if $@;
84         my $f = CGI::FormBuilder->new(
85                 name => "rename",
86                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
87                 header => 0,
88                 charset => "utf-8",
89                 method => 'POST',
90                 javascript => 0,
91                 params => $q,
92                 action => $config{cgiurl},
93                 stylesheet => IkiWiki::baseurl()."style.css",
94                 fields => [qw{do page new_name attachment}],
95         );
96         
97         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
98         $f->field(name => "page", type => "hidden", value => $page, force => 1);
99         $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
100         $f->field(name => "attachment", type => "hidden");
101
102         return $f, ["Rename", "Cancel"];
103 } #}}}
104
105 sub rename_start ($$$$) { #{{{
106         my $q=shift;
107         my $session=shift;
108         my $attachment=shift;
109         my $page=shift;
110
111         check_canrename($page, $pagesources{$page}, undef, undef,
112                 $q, $session, $attachment);
113
114         # Save current form state to allow returning to it later
115         # without losing any edits.
116         # (But don't save what button was submitted, to avoid
117         # looping back to here.)
118         # Note: "_submit" is CGI::FormBuilder internals.
119         $q->param(-name => "_submit", -value => "");
120         $session->param(postrename => scalar $q->Vars);
121         IkiWiki::cgi_savesession($session);
122         
123         my ($f, $buttons)=rename_form($q, $session, $page);
124         if (defined $attachment) {
125                 $f->field(name => "attachment", value => $attachment, force => 1);
126         }
127         
128         IkiWiki::showform($f, $buttons, $session, $q);
129         exit 0;
130 } #}}}
131
132 sub postrename ($;$$$) { #{{{
133         my $session=shift;
134         my $src=shift;
135         my $dest=shift;
136         my $attachment=shift;
137
138         # Load saved form state and return to edit page.
139         my $postrename=CGI->new($session->param("postrename"));
140         $session->clear("postrename");
141         IkiWiki::cgi_savesession($session);
142
143         if (defined $dest) {
144                 if (! $attachment) {
145                         # They renamed the page they were editing. This requires
146                         # fixups to the edit form state.
147                         # Tweak the edit form to be editing the new page.
148                         $postrename->param("page", $dest);
149                 }
150
151                 # Update edit form content to fix any links present
152                 # on it.
153                 $postrename->param("editcontent",
154                         renamepage_hook($dest, $src, $dest,
155                                  $postrename->param("editcontent")));
156
157                 # Get a new edit token; old was likely invalidated.
158                 $postrename->param("rcsinfo",
159                         IkiWiki::rcs_prepedit($pagesources{$dest}));
160         }
161
162         IkiWiki::cgi_editpage($postrename, $session);
163 } #}}}
164
165 sub formbuilder (@) { #{{{
166         my %params=@_;
167         my $form=$params{form};
168
169         if (defined $form->field("do") && $form->field("do") eq "edit") {
170                 my $q=$params{cgi};
171                 my $session=$params{session};
172
173                 if ($form->submitted eq "Rename") {
174                         rename_start($q, $session, 0, $form->field("page"));
175                 }
176                 elsif ($form->submitted eq "Rename Attachment") {
177                         my @selected=$q->param("attachment_select");
178                         if (@selected > 1) {
179                                 error(gettext("Only one attachment can be renamed at a time."));
180                         }
181                         elsif (! @selected) {
182                                 error(gettext("Please select the attachment to rename."))
183                         }
184                         rename_start($q, $session, 1, $selected[0]);
185                 }
186         }
187 } #}}}
188
189 my $renamesummary;
190
191 sub formbuilder_setup (@) { #{{{
192         my %params=@_;
193         my $form=$params{form};
194         my $q=$params{cgi};
195
196         if (defined $form->field("do") && $form->field("do") eq "edit") {
197                 # Rename button for the page, and also for attachments.
198                 push @{$params{buttons}}, "Rename";
199                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
200
201                 if (defined $renamesummary) {
202                         $form->tmpl_param(message => $renamesummary);
203                 }
204         }
205 } #}}}
206
207 sub sessioncgi ($$) { #{{{
208         my $q=shift;
209
210         if ($q->param("do") eq 'rename') {
211                 my $session=shift;
212                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
213                 IkiWiki::decode_form_utf8($form);
214
215                 if ($form->submitted eq 'Cancel') {
216                         postrename($session);
217                 }
218                 elsif ($form->submitted eq 'Rename' && $form->validate) {
219                         # These untaints are safe because of the checks
220                         # performed in check_canrename below.
221                         my $src=$q->param("page");
222                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
223                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
224
225                         # The extension of dest is the same as src if it's
226                         # a page. If it's an extension, the extension is
227                         # already included.
228                         my $destfile=$dest;
229                         if (! $q->param("attachment")) {
230                                 my ($ext)=$srcfile=~/(\.[^.]+)$/;
231                                 $destfile.=$ext;
232                         }
233
234                         check_canrename($src, $srcfile, $dest, $destfile,
235                                 $q, $session, $q->param("attachment"));
236
237                         # Ensures that the dest directory exists and is ok.
238                         IkiWiki::prep_writefile($destfile, $config{srcdir});
239
240                         # Do rename, update other pages, and refresh site.
241                         IkiWiki::disable_commit_hook() if $config{rcs};
242                         require IkiWiki::Render;
243                         if ($config{rcs}) {
244                                 IkiWiki::rcs_rename($srcfile, $destfile);
245                                 IkiWiki::rcs_commit_staged(
246                                         sprintf(gettext("rename %s to %s"), $src, $dest),
247                                         $session->param("name"), $ENV{REMOTE_ADDR});
248                         }
249                         else {
250                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
251                                         error("rename: $!");
252                                 }
253                         }
254                         my @fixedlinks;
255                         foreach my $page (keys %links) {
256                                 my $needfix=0;
257                                 foreach my $link (@{$links{$page}}) {
258                                         my $bestlink=bestlink($page, $link);
259                                         if ($bestlink eq $src) {
260                                                 $needfix=1;
261                                                 last;
262                                         }
263                                 }
264                                 if ($needfix) {
265                                         my $file=$pagesources{$page};
266                                         my $oldcontent=readfile($config{srcdir}."/".$file);
267                                         my $content=renamepage_hook($page, $src, $dest, $oldcontent);
268                                         if ($oldcontent ne $content) {
269                                                 my $token=IkiWiki::rcs_prepedit($file);
270                                                 eval { writefile($file, $config{srcdir}, $content) };
271                                                 next if $@;
272                                                 my $conflict=IkiWiki::rcs_commit(
273                                                         $file,
274                                                         sprintf(gettext("update for rename of %s to %s"), $src, $dest),
275                                                         $token,
276                                                         $session->param("name"), 
277                                                         $ENV{REMOTE_ADDR}
278                                                 );
279                                                 push @fixedlinks, $page if ! defined $conflict;
280                                         }
281                                 }
282                         }
283                         if ($config{rcs}) {
284                                 IkiWiki::enable_commit_hook();
285                                 IkiWiki::rcs_update();
286                         }
287                         IkiWiki::refresh();
288                         IkiWiki::saveindex();
289
290                         # Scan for any remaining broken links to $src.
291                         my @brokenlinks;
292                         foreach my $page (keys %links) {
293                                 my $broken=0;
294                                 foreach my $link (@{$links{$page}}) {
295                                         my $bestlink=bestlink($page, $link);
296                                         if ($bestlink eq $src) {
297                                                 $broken=1;
298                                                 last;
299                                         }
300                                 }
301                                 push @brokenlinks, $page if $broken;
302                         }
303
304                         # Generate a rename summary, that will be shown at the top
305                         # of the edit template.
306                         my $template=template("renamesummary.tmpl");
307                         $template->param(src => $src);
308                         $template->param(dest => $dest);
309                         $template->param(brokenlinks => [
310                                 map {
311                                         {
312                                                 page => htmllink($dest, $dest, $_,
313                                                                 noimageinline => 1)
314                                         }
315                                 } @brokenlinks
316                         ]);
317                         $template->param(fixedlinks => [
318                                 map {
319                                         {
320                                                 page => htmllink($dest, $dest, $_,
321                                                                 noimageinline => 1)
322                                         }
323                                 } @fixedlinks
324                         ]);
325                         $renamesummary=$template->output;
326
327                         postrename($session, $src, $dest, $q->param("attachment"));
328                 }
329                 else {
330                         IkiWiki::showform($form, $buttons, $session, $q);
331                 }
332
333                 exit 0;
334         }
335 } #}}}
336
337 sub renamepage_hook ($$$$) { #{{{
338         my ($page, $src, $dest, $content)=@_;
339
340         IkiWiki::run_hooks(renamepage => sub {
341                 $content=shift->(
342                         page => $page,
343                         oldpage => $src,
344                         newpage => $dest,
345                         content => $content,
346                 );
347         });
348
349         return $content;
350 }# }}}
351
352 1