summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 3ee457ff09e0d8bc7ff4b24e21cc8e6077839294 (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 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"), 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 => 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. foreach my $p (keys %pagesources) {
  114. if ($pagesources{$p}=~m/^\Q$page\E\//) {
  115. $f->field(name => "subpages",
  116. label => "",
  117. type => "checkbox",
  118. options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
  119. value => 1,
  120. force => 1);
  121. last;
  122. }
  123. }
  124. }
  125. $f->field(name => "attachment", type => "hidden");
  126. return $f, ["Rename", "Cancel"];
  127. } #}}}
  128. sub rename_start ($$$$) { #{{{
  129. my $q=shift;
  130. my $session=shift;
  131. my $attachment=shift;
  132. my $page=shift;
  133. check_canrename($page, $pagesources{$page}, undef, undef,
  134. $q, $session);
  135. # Save current form state to allow returning to it later
  136. # without losing any edits.
  137. # (But don't save what button was submitted, to avoid
  138. # looping back to here.)
  139. # Note: "_submit" is CGI::FormBuilder internals.
  140. $q->param(-name => "_submit", -value => "");
  141. $session->param(postrename => scalar $q->Vars);
  142. IkiWiki::cgi_savesession($session);
  143. if (defined $attachment) {
  144. $q->param(-name => "attachment", -value => $attachment);
  145. }
  146. my ($f, $buttons)=rename_form($q, $session, $page);
  147. IkiWiki::showform($f, $buttons, $session, $q);
  148. exit 0;
  149. } #}}}
  150. sub postrename ($;$$$) { #{{{
  151. my $session=shift;
  152. my $src=shift;
  153. my $dest=shift;
  154. my $attachment=shift;
  155. # Load saved form state and return to edit page.
  156. my $postrename=CGI->new($session->param("postrename"));
  157. $session->clear("postrename");
  158. IkiWiki::cgi_savesession($session);
  159. if (defined $dest) {
  160. if (! $attachment) {
  161. # They renamed the page they were editing. This requires
  162. # fixups to the edit form state.
  163. # Tweak the edit form to be editing the new page.
  164. $postrename->param("page", $dest);
  165. }
  166. # Update edit form content to fix any links present
  167. # on it.
  168. $postrename->param("editcontent",
  169. renamepage_hook($dest, $src, $dest,
  170. $postrename->param("editcontent")));
  171. # Get a new edit token; old was likely invalidated.
  172. $postrename->param("rcsinfo",
  173. IkiWiki::rcs_prepedit($pagesources{$dest}));
  174. }
  175. IkiWiki::cgi_editpage($postrename, $session);
  176. } #}}}
  177. sub formbuilder (@) { #{{{
  178. my %params=@_;
  179. my $form=$params{form};
  180. if (defined $form->field("do") && $form->field("do") eq "edit") {
  181. my $q=$params{cgi};
  182. my $session=$params{session};
  183. if ($form->submitted eq "Rename") {
  184. rename_start($q, $session, 0, $form->field("page"));
  185. }
  186. elsif ($form->submitted eq "Rename Attachment") {
  187. my @selected=$q->param("attachment_select");
  188. if (@selected > 1) {
  189. error(gettext("Only one attachment can be renamed at a time."));
  190. }
  191. elsif (! @selected) {
  192. error(gettext("Please select the attachment to rename."))
  193. }
  194. rename_start($q, $session, 1, $selected[0]);
  195. }
  196. }
  197. } #}}}
  198. my $renamesummary;
  199. sub formbuilder_setup (@) { #{{{
  200. my %params=@_;
  201. my $form=$params{form};
  202. my $q=$params{cgi};
  203. if (defined $form->field("do") && $form->field("do") eq "edit") {
  204. # Rename button for the page, and also for attachments.
  205. push @{$params{buttons}}, "Rename";
  206. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  207. if (defined $renamesummary) {
  208. $form->tmpl_param(message => $renamesummary);
  209. }
  210. }
  211. } #}}}
  212. sub sessioncgi ($$) { #{{{
  213. my $q=shift;
  214. if ($q->param("do") eq 'rename') {
  215. my $session=shift;
  216. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  217. IkiWiki::decode_form_utf8($form);
  218. if ($form->submitted eq 'Cancel') {
  219. postrename($session);
  220. }
  221. elsif ($form->submitted eq 'Rename' && $form->validate) {
  222. # Queue of rename actions to perfom.
  223. my @torename;
  224. # These untaints are safe because of the checks
  225. # performed in check_canrename later.
  226. my $src=$q->param("page");
  227. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  228. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
  229. my $destfile=$dest;
  230. if (! $q->param("attachment")) {
  231. my $type=$q->param('type');
  232. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  233. $type=IkiWiki::possibly_foolish_untaint($type);
  234. }
  235. else {
  236. my ($ext)=$srcfile=~/\.([^.]+)$/;
  237. $type=$ext;
  238. }
  239. $destfile.=".".$type;
  240. }
  241. push @torename, {
  242. src => $src,
  243. srcfile => $srcfile,
  244. dest => $dest,
  245. destfile => $destfile,
  246. required => 1,
  247. };
  248. # See if any subpages need to be renamed.
  249. if ($q->param("subpages") && $src ne $dest) {
  250. foreach my $p (keys %pagesources) {
  251. if ($pagesources{$p}=~m/^\Q$src\E\//) {
  252. my $d=$pagesources{$p};
  253. $d=~s/^\Q$src\E\//$dest\//;
  254. push @torename, {
  255. src => $p,
  256. srcfile => $pagesources{$p},
  257. dest => pagename($d),
  258. destfile => $d,
  259. required => 0,
  260. };
  261. }
  262. }
  263. }
  264. require IkiWiki::Render;
  265. IkiWiki::disable_commit_hook() if $config{rcs};
  266. my %origpagesources=%pagesources;
  267. # First file renaming.
  268. foreach my $rename (@torename) {
  269. if ($rename->{required}) {
  270. do_rename($rename, $q, $session);
  271. }
  272. else {
  273. eval {do_rename($rename, $q, $session)};
  274. if ($@) {
  275. $rename->{error}=$@;
  276. next;
  277. }
  278. }
  279. # Temporarily tweak pagesources to point to
  280. # the renamed file, in case fixlinks needs
  281. # to edit it.
  282. $pagesources{$rename->{src}}=$rename->{destfile};
  283. }
  284. IkiWiki::rcs_commit_staged(
  285. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  286. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  287. # Then link fixups.
  288. foreach my $rename (@torename) {
  289. next if $rename->{src} eq $rename->{dest};
  290. next if $rename->{error};
  291. foreach my $p (fixlinks($rename, $session)) {
  292. # map old page names to new
  293. foreach my $r (@torename) {
  294. next if $rename->{error};
  295. if ($r->{src} eq $p) {
  296. $p=$r->{dest};
  297. last;
  298. }
  299. }
  300. push @{$rename->{fixedlinks}}, $p;
  301. }
  302. }
  303. # Then refresh.
  304. %pagesources=%origpagesources;
  305. if ($config{rcs}) {
  306. IkiWiki::enable_commit_hook();
  307. IkiWiki::rcs_update();
  308. }
  309. IkiWiki::refresh();
  310. IkiWiki::saveindex();
  311. # Find pages with remaining, broken links.
  312. foreach my $rename (@torename) {
  313. next if $rename->{src} eq $rename->{dest};
  314. foreach my $page (keys %links) {
  315. my $broken=0;
  316. foreach my $link (@{$links{$page}}) {
  317. my $bestlink=bestlink($page, $link);
  318. if ($bestlink eq $rename->{src}) {
  319. push @{$rename->{brokenlinks}}, $page;
  320. last;
  321. }
  322. }
  323. }
  324. }
  325. # Generate a summary, that will be shown at the top
  326. # of the edit template.
  327. $renamesummary="";
  328. foreach my $rename (@torename) {
  329. my $template=template("renamesummary.tmpl");
  330. $template->param(src => $rename->{srcfile});
  331. $template->param(dest => $rename->{destfile});
  332. $template->param(error => $rename->{error});
  333. if ($rename->{src} ne $rename->{dest}) {
  334. $template->param(brokenlinks_checked => 1);
  335. $template->param(brokenlinks => [
  336. map {
  337. {
  338. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  339. noimageinline => 1)
  340. }
  341. } @{$rename->{brokenlinks}}
  342. ]);
  343. $template->param(fixedlinks => [
  344. map {
  345. {
  346. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  347. noimageinline => 1)
  348. }
  349. } @{$rename->{fixedlinks}}
  350. ]);
  351. }
  352. $renamesummary.=$template->output;
  353. }
  354. postrename($session, $src, $dest, $q->param("attachment"));
  355. }
  356. else {
  357. IkiWiki::showform($form, $buttons, $session, $q);
  358. }
  359. exit 0;
  360. }
  361. } #}}}
  362. sub renamepage_hook ($$$$) { #{{{
  363. my ($page, $src, $dest, $content)=@_;
  364. IkiWiki::run_hooks(renamepage => sub {
  365. $content=shift->(
  366. page => $page,
  367. oldpage => $src,
  368. newpage => $dest,
  369. content => $content,
  370. );
  371. });
  372. return $content;
  373. }# }}}
  374. sub do_rename ($$$) { #{{{
  375. my $rename=shift;
  376. my $q=shift;
  377. my $session=shift;
  378. # First, check if this rename is allowed.
  379. check_canrename($rename->{src},
  380. $rename->{srcfile},
  381. $rename->{dest},
  382. $rename->{destfile},
  383. $q, $session);
  384. # Ensure that the dest directory exists and is ok.
  385. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  386. if ($config{rcs}) {
  387. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  388. }
  389. else {
  390. if (! rename($config{srcdir}."/".$rename->{srcfile},
  391. $config{srcdir}."/".$rename->{destfile})) {
  392. error("rename: $!");
  393. }
  394. }
  395. } # }}}
  396. sub fixlinks ($$$) { #{{{
  397. my $rename=shift;
  398. my $session=shift;
  399. my @fixedlinks;
  400. foreach my $page (keys %links) {
  401. my $needfix=0;
  402. foreach my $link (@{$links{$page}}) {
  403. my $bestlink=bestlink($page, $link);
  404. if ($bestlink eq $rename->{src}) {
  405. $needfix=1;
  406. last;
  407. }
  408. }
  409. if ($needfix) {
  410. my $file=$pagesources{$page};
  411. my $oldcontent=readfile($config{srcdir}."/".$file);
  412. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  413. if ($oldcontent ne $content) {
  414. my $token=IkiWiki::rcs_prepedit($file);
  415. eval { writefile($file, $config{srcdir}, $content) };
  416. next if $@;
  417. my $conflict=IkiWiki::rcs_commit(
  418. $file,
  419. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  420. $token,
  421. $session->param("name"),
  422. $ENV{REMOTE_ADDR}
  423. );
  424. push @fixedlinks, $page if ! defined $conflict;
  425. }
  426. }
  427. }
  428. return @fixedlinks;
  429. } #}}}
  430. 1