summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: fd0bd3ae848b7399053726e8d28e316e772c9197 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::rename;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
  8. hook(type => "formbuilder", id => "rename", call => \&formbuilder);
  9. hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
  10. } # }}}
  11. sub check_canrename ($$$$$$$) { #{{{
  12. my $src=shift;
  13. my $srcfile=shift;
  14. my $dest=shift;
  15. my $destfile=shift;
  16. my $q=shift;
  17. my $session=shift;
  18. my $attachment=shift;
  19. # Must be a known source file.
  20. if (! exists $pagesources{$src}) {
  21. error(sprintf(gettext("%s does not exist"),
  22. htmllink("", "", $src, noimageinline => 1)));
  23. }
  24. # Must exist on disk, and be a regular file.
  25. if (! -e "$config{srcdir}/$srcfile") {
  26. error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
  27. }
  28. elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
  29. error(sprintf(gettext("%s is not a file"), $srcfile));
  30. }
  31. # Must be editable.
  32. IkiWiki::check_canedit($src, $q, $session);
  33. if ($attachment) {
  34. IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
  35. }
  36. # Dest checks can be omitted by passing undef.
  37. if (defined $dest) {
  38. if ($src eq $dest || $srcfile eq $destfile) {
  39. error(gettext("no change to the file name was specified"));
  40. }
  41. # Must be a legal filename.
  42. if (IkiWiki::file_pruned($destfile, $config{srcdir})) {
  43. error(sprintf(gettext("illegal name")));
  44. }
  45. # Must not be a known source file.
  46. if (exists $pagesources{$dest}) {
  47. error(sprintf(gettext("%s already exists"),
  48. htmllink("", "", $dest, noimageinline => 1)));
  49. }
  50. # Must not exist on disk already.
  51. if (-l "$config{srcdir}/$destfile" || -e _) {
  52. error(sprintf(gettext("%s already exists on disk"), $destfile));
  53. }
  54. # Must be editable.
  55. IkiWiki::check_canedit($dest, $q, $session);
  56. if ($attachment) {
  57. # Note that $srcfile is used here, not $destfile,
  58. # because it wants the current file, to check it.
  59. IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
  60. }
  61. }
  62. } #}}}
  63. sub formbuilder_setup (@) { #{{{
  64. my %params=@_;
  65. my $form=$params{form};
  66. my $q=$params{cgi};
  67. if (defined $form->field("do") && $form->field("do") eq "edit") {
  68. # Rename button for the page, and also for attachments.
  69. push @{$params{buttons}}, "Rename";
  70. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  71. }
  72. } #}}}
  73. sub rename_form ($$$) { #{{{
  74. my $q=shift;
  75. my $session=shift;
  76. my $page=shift;
  77. eval q{use CGI::FormBuilder};
  78. error($@) if $@;
  79. my $f = CGI::FormBuilder->new(
  80. name => "rename",
  81. title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
  82. header => 0,
  83. charset => "utf-8",
  84. method => 'POST',
  85. javascript => 0,
  86. params => $q,
  87. action => $config{cgiurl},
  88. stylesheet => IkiWiki::baseurl()."style.css",
  89. fields => [qw{do page new_name attachment}],
  90. );
  91. $f->field(name => "do", type => "hidden", value => "rename", force => 1);
  92. $f->field(name => "page", type => "hidden", value => $page, force => 1);
  93. $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
  94. $f->field(name => "attachment", type => "hidden");
  95. return $f, ["Rename", "Cancel"];
  96. } #}}}
  97. sub rename_start ($$$$) {
  98. my $q=shift;
  99. my $session=shift;
  100. my $attachment=shift;
  101. my $page=shift;
  102. check_canrename($page, $pagesources{$page}, undef, undef,
  103. $q, $session, $attachment);
  104. # Save current form state to allow returning to it later
  105. # without losing any edits.
  106. # (But don't save what button was submitted, to avoid
  107. # looping back to here.)
  108. # Note: "_submit" is CGI::FormBuilder internals.
  109. $q->param(-name => "_submit", -value => "");
  110. $session->param(postrename => scalar $q->Vars);
  111. IkiWiki::cgi_savesession($session);
  112. my ($f, $buttons)=rename_form($q, $session, $page);
  113. if (defined $attachment) {
  114. $f->field(name => "attachment", value => $attachment, force => 1);
  115. }
  116. IkiWiki::showform($f, $buttons, $session, $q);
  117. exit 0;
  118. }
  119. sub postrename ($;$) {
  120. my $session=shift;
  121. my $newname=shift;
  122. # Load saved form state and return to edit form.
  123. my $postrename=CGI->new($session->param("postrename"));
  124. if (defined $newname) {
  125. # They renamed the page they were editing.
  126. # Tweak the edit form to be editing the new
  127. # page name, and redirect back to it.
  128. # (Deep evil here.)
  129. error("don't know how to redir back!"); ## FIXME
  130. }
  131. $session->clear("postrename");
  132. IkiWiki::cgi_savesession($session);
  133. IkiWiki::cgi($postrename, $session);
  134. }
  135. sub formbuilder (@) { #{{{
  136. my %params=@_;
  137. my $form=$params{form};
  138. if (defined $form->field("do") && $form->field("do") eq "edit") {
  139. my $q=$params{cgi};
  140. my $session=$params{session};
  141. if ($form->submitted eq "Rename") {
  142. rename_start($q, $session, 0, $form->field("page"));
  143. }
  144. elsif ($form->submitted eq "Rename Attachment") {
  145. my @selected=$q->param("attachment_select");
  146. if (@selected > 1) {
  147. error(gettext("Only one attachment can be renamed at a time."));
  148. }
  149. elsif (! @selected) {
  150. error(gettext("Please select the attachment to rename."))
  151. }
  152. rename_start($q, $session, 1, $selected[0]);
  153. }
  154. }
  155. } #}}}
  156. sub sessioncgi ($$) { #{{{
  157. my $q=shift;
  158. if ($q->param("do") eq 'rename') {
  159. my $session=shift;
  160. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  161. IkiWiki::decode_form_utf8($form);
  162. if ($form->submitted eq 'Cancel') {
  163. postrename($session);
  164. }
  165. elsif ($form->submitted eq 'Rename' && $form->validate) {
  166. # These untaints are safe because of the checks
  167. # performed in check_canrename below.
  168. my $src=$q->param("page");
  169. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  170. my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
  171. # The extension of dest is the same as src if it's
  172. # a page. If it's an extension, the extension is
  173. # already included.
  174. my $destfile=$dest;
  175. if (! $q->param("attachment")) {
  176. my ($ext)=$srcfile=~/(\.[^.]+)$/;
  177. $destfile.=$ext;
  178. }
  179. check_canrename($src, $srcfile, $dest, $destfile,
  180. $q, $session, $q->param("attachment"));
  181. # Ensures that the dest directory exists and is ok.
  182. IkiWiki::prep_writefile($destfile, $config{srcdir});
  183. # Do rename, and update the wiki.
  184. require IkiWiki::Render;
  185. if ($config{rcs}) {
  186. IkiWiki::disable_commit_hook();
  187. IkiWiki::rcs_rename($srcfile, $destfile);
  188. IkiWiki::rcs_commit_staged(gettext("rename $srcfile to $destfile"),
  189. $session->param("name"), $ENV{REMOTE_ADDR});
  190. IkiWiki::enable_commit_hook();
  191. IkiWiki::rcs_update();
  192. }
  193. else {
  194. if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
  195. error("rename: $!");
  196. }
  197. }
  198. IkiWiki::refresh();
  199. IkiWiki::saveindex();
  200. if ($q->param("attachment")) {
  201. postrename($session);
  202. }
  203. else {
  204. postrename($session, $dest);
  205. }
  206. }
  207. else {
  208. IkiWiki::showform($form, $buttons, $session, $q);
  209. }
  210. exit 0;
  211. }
  212. }
  213. 1