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