summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 77aed9556902d9344c85e0f5c7b0eedf180b0476 (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 ($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 ($src ne $dest && 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. if (!$q->param("attachment")) {
  94. # insert the standard extensions
  95. my @page_types;
  96. if (exists $IkiWiki::hooks{htmlize}) {
  97. @page_types=grep { !/^_/ }
  98. keys %{$IkiWiki::hooks{htmlize}};
  99. }
  100. # make sure the current extension is in the list
  101. my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
  102. if (! $IkiWiki::hooks{htmlize}{$ext}) {
  103. unshift(@page_types, $ext);
  104. }
  105. $f->field(name => "type", type => 'select',
  106. options => \@page_types,
  107. value => $ext, force => 1);
  108. }
  109. $f->field(name => "attachment", type => "hidden");
  110. return $f, ["Rename", "Cancel"];
  111. } #}}}
  112. sub rename_start ($$$$) { #{{{
  113. my $q=shift;
  114. my $session=shift;
  115. my $attachment=shift;
  116. my $page=shift;
  117. check_canrename($page, $pagesources{$page}, undef, undef,
  118. $q, $session, $attachment);
  119. # Save current form state to allow returning to it later
  120. # without losing any edits.
  121. # (But don't save what button was submitted, to avoid
  122. # looping back to here.)
  123. # Note: "_submit" is CGI::FormBuilder internals.
  124. $q->param(-name => "_submit", -value => "");
  125. $session->param(postrename => scalar $q->Vars);
  126. IkiWiki::cgi_savesession($session);
  127. my ($f, $buttons)=rename_form($q, $session, $page);
  128. if (defined $attachment) {
  129. $f->field(name => "attachment", value => $attachment, force => 1);
  130. }
  131. IkiWiki::showform($f, $buttons, $session, $q);
  132. exit 0;
  133. } #}}}
  134. sub postrename ($;$$$) { #{{{
  135. my $session=shift;
  136. my $src=shift;
  137. my $dest=shift;
  138. my $attachment=shift;
  139. # Load saved form state and return to edit page.
  140. my $postrename=CGI->new($session->param("postrename"));
  141. $session->clear("postrename");
  142. IkiWiki::cgi_savesession($session);
  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. # Update edit form content to fix any links present
  151. # on it.
  152. $postrename->param("editcontent",
  153. renamepage_hook($dest, $src, $dest,
  154. $postrename->param("editcontent")));
  155. # Get a new edit token; old was likely invalidated.
  156. $postrename->param("rcsinfo",
  157. IkiWiki::rcs_prepedit($pagesources{$dest}));
  158. }
  159. IkiWiki::cgi_editpage($postrename, $session);
  160. } #}}}
  161. sub formbuilder (@) { #{{{
  162. my %params=@_;
  163. my $form=$params{form};
  164. if (defined $form->field("do") && $form->field("do") eq "edit") {
  165. my $q=$params{cgi};
  166. my $session=$params{session};
  167. if ($form->submitted eq "Rename") {
  168. rename_start($q, $session, 0, $form->field("page"));
  169. }
  170. elsif ($form->submitted eq "Rename Attachment") {
  171. my @selected=$q->param("attachment_select");
  172. if (@selected > 1) {
  173. error(gettext("Only one attachment can be renamed at a time."));
  174. }
  175. elsif (! @selected) {
  176. error(gettext("Please select the attachment to rename."))
  177. }
  178. rename_start($q, $session, 1, $selected[0]);
  179. }
  180. }
  181. } #}}}
  182. my $renamesummary;
  183. sub formbuilder_setup (@) { #{{{
  184. my %params=@_;
  185. my $form=$params{form};
  186. my $q=$params{cgi};
  187. if (defined $form->field("do") && $form->field("do") eq "edit") {
  188. # Rename button for the page, and also for attachments.
  189. push @{$params{buttons}}, "Rename";
  190. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  191. if (defined $renamesummary) {
  192. $form->tmpl_param(message => $renamesummary);
  193. }
  194. }
  195. } #}}}
  196. sub sessioncgi ($$) { #{{{
  197. my $q=shift;
  198. if ($q->param("do") eq 'rename') {
  199. my $session=shift;
  200. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  201. IkiWiki::decode_form_utf8($form);
  202. if ($form->submitted eq 'Cancel') {
  203. postrename($session);
  204. }
  205. elsif ($form->submitted eq 'Rename' && $form->validate) {
  206. # These untaints are safe because of the checks
  207. # performed in check_canrename below.
  208. my $src=$q->param("page");
  209. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  210. my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
  211. my $destfile=$dest;
  212. if (! $q->param("attachment")) {
  213. my $type=$q->param('type');
  214. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  215. $type=IkiWiki::possibly_foolish_untaint($type);
  216. } else {
  217. my ($ext)=$srcfile=~/\.([^.]+)$/;
  218. $type=$ext;
  219. }
  220. $destfile.=".".$type;
  221. }
  222. check_canrename($src, $srcfile, $dest, $destfile,
  223. $q, $session, $q->param("attachment"));
  224. # Ensures that the dest directory exists and is ok.
  225. IkiWiki::prep_writefile($destfile, $config{srcdir});
  226. # Do rename, update other pages, and refresh site.
  227. IkiWiki::disable_commit_hook() if $config{rcs};
  228. require IkiWiki::Render;
  229. if ($config{rcs}) {
  230. IkiWiki::rcs_rename($srcfile, $destfile);
  231. IkiWiki::rcs_commit_staged(
  232. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  233. $session->param("name"), $ENV{REMOTE_ADDR});
  234. }
  235. else {
  236. if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
  237. error("rename: $!");
  238. }
  239. }
  240. my @fixedlinks;
  241. if ($src ne $dest) {
  242. foreach my $page (keys %links) {
  243. my $needfix=0;
  244. foreach my $link (@{$links{$page}}) {
  245. my $bestlink=bestlink($page, $link);
  246. if ($bestlink eq $src) {
  247. $needfix=1;
  248. last;
  249. }
  250. }
  251. if ($needfix) {
  252. my $file=$pagesources{$page};
  253. my $oldcontent=readfile($config{srcdir}."/".$file);
  254. my $content=renamepage_hook($page, $src, $dest, $oldcontent);
  255. if ($oldcontent ne $content) {
  256. my $token=IkiWiki::rcs_prepedit($file);
  257. eval { writefile($file, $config{srcdir}, $content) };
  258. next if $@;
  259. my $conflict=IkiWiki::rcs_commit(
  260. $file,
  261. sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
  262. $token,
  263. $session->param("name"),
  264. $ENV{REMOTE_ADDR}
  265. );
  266. push @fixedlinks, $page if ! defined $conflict;
  267. }
  268. }
  269. }
  270. }
  271. if ($config{rcs}) {
  272. IkiWiki::enable_commit_hook();
  273. IkiWiki::rcs_update();
  274. }
  275. IkiWiki::refresh();
  276. IkiWiki::saveindex();
  277. # Scan for any remaining broken links to $src.
  278. my @brokenlinks;
  279. if ($src ne $dest) {
  280. foreach my $page (keys %links) {
  281. my $broken=0;
  282. foreach my $link (@{$links{$page}}) {
  283. my $bestlink=bestlink($page, $link);
  284. if ($bestlink eq $src) {
  285. $broken=1;
  286. last;
  287. }
  288. }
  289. push @brokenlinks, $page if $broken;
  290. }
  291. }
  292. # Generate a rename summary, that will be shown at the top
  293. # of the edit template.
  294. my $template=template("renamesummary.tmpl");
  295. $template->param(src => $srcfile);
  296. $template->param(dest => $destfile);
  297. if ($src ne $dest) {
  298. $template->param(brokenlinks_checked => 1);
  299. $template->param(brokenlinks => [
  300. map {
  301. {
  302. page => htmllink($dest, $dest, $_,
  303. noimageinline => 1)
  304. }
  305. } @brokenlinks
  306. ]);
  307. $template->param(fixedlinks => [
  308. map {
  309. {
  310. page => htmllink($dest, $dest, $_,
  311. noimageinline => 1)
  312. }
  313. } @fixedlinks
  314. ]);
  315. }
  316. $renamesummary=$template->output;
  317. postrename($session, $src, $dest, $q->param("attachment"));
  318. }
  319. else {
  320. IkiWiki::showform($form, $buttons, $session, $q);
  321. }
  322. exit 0;
  323. }
  324. } #}}}
  325. sub renamepage_hook ($$$$) { #{{{
  326. my ($page, $src, $dest, $content)=@_;
  327. IkiWiki::run_hooks(renamepage => sub {
  328. $content=shift->(
  329. page => $page,
  330. oldpage => $src,
  331. newpage => $dest,
  332. content => $content,
  333. );
  334. });
  335. return $content;
  336. }# }}}
  337. 1