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