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