summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 6c131487a3527c46c00b1ce57ca0a15936f04f49 (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=newpagefile($dest, $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. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  252. # If indexpages is enabled, the
  253. # srcfile should not be confused
  254. # with a subpage.
  255. next if $pagesources{$p} eq $srcfile;
  256. my $d=$pagesources{$p};
  257. $d=~s/^\Q$src\E\//$dest\//;
  258. push @torename, {
  259. src => $p,
  260. srcfile => $pagesources{$p},
  261. dest => pagename($d),
  262. destfile => $d,
  263. required => 0,
  264. };
  265. }
  266. }
  267. require IkiWiki::Render;
  268. IkiWiki::disable_commit_hook() if $config{rcs};
  269. my %origpagesources=%pagesources;
  270. # First file renaming.
  271. foreach my $rename (@torename) {
  272. if ($rename->{required}) {
  273. do_rename($rename, $q, $session);
  274. }
  275. else {
  276. eval {do_rename($rename, $q, $session)};
  277. if ($@) {
  278. $rename->{error}=$@;
  279. next;
  280. }
  281. }
  282. # Temporarily tweak pagesources to point to
  283. # the renamed file, in case fixlinks needs
  284. # to edit it.
  285. $pagesources{$rename->{src}}=$rename->{destfile};
  286. }
  287. IkiWiki::rcs_commit_staged(
  288. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  289. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  290. # Then link fixups.
  291. foreach my $rename (@torename) {
  292. next if $rename->{src} eq $rename->{dest};
  293. next if $rename->{error};
  294. foreach my $p (fixlinks($rename, $session)) {
  295. # map old page names to new
  296. foreach my $r (@torename) {
  297. next if $rename->{error};
  298. if ($r->{src} eq $p) {
  299. $p=$r->{dest};
  300. last;
  301. }
  302. }
  303. push @{$rename->{fixedlinks}}, $p;
  304. }
  305. }
  306. # Then refresh.
  307. %pagesources=%origpagesources;
  308. if ($config{rcs}) {
  309. IkiWiki::enable_commit_hook();
  310. IkiWiki::rcs_update();
  311. }
  312. IkiWiki::refresh();
  313. IkiWiki::saveindex();
  314. # Find pages with remaining, broken links.
  315. foreach my $rename (@torename) {
  316. next if $rename->{src} eq $rename->{dest};
  317. foreach my $page (keys %links) {
  318. my $broken=0;
  319. foreach my $link (@{$links{$page}}) {
  320. my $bestlink=bestlink($page, $link);
  321. if ($bestlink eq $rename->{src}) {
  322. push @{$rename->{brokenlinks}}, $page;
  323. last;
  324. }
  325. }
  326. }
  327. }
  328. # Generate a summary, that will be shown at the top
  329. # of the edit template.
  330. $renamesummary="";
  331. foreach my $rename (@torename) {
  332. my $template=template("renamesummary.tmpl");
  333. $template->param(src => $rename->{srcfile});
  334. $template->param(dest => $rename->{destfile});
  335. $template->param(error => $rename->{error});
  336. if ($rename->{src} ne $rename->{dest}) {
  337. $template->param(brokenlinks_checked => 1);
  338. $template->param(brokenlinks => [
  339. map {
  340. {
  341. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  342. noimageinline => 1)
  343. }
  344. } @{$rename->{brokenlinks}}
  345. ]);
  346. $template->param(fixedlinks => [
  347. map {
  348. {
  349. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  350. noimageinline => 1)
  351. }
  352. } @{$rename->{fixedlinks}}
  353. ]);
  354. }
  355. $renamesummary.=$template->output;
  356. }
  357. postrename($session, $src, $dest, $q->param("attachment"));
  358. }
  359. else {
  360. IkiWiki::showform($form, $buttons, $session, $q);
  361. }
  362. exit 0;
  363. }
  364. } #}}}
  365. sub renamepage_hook ($$$$) { #{{{
  366. my ($page, $src, $dest, $content)=@_;
  367. IkiWiki::run_hooks(renamepage => sub {
  368. $content=shift->(
  369. page => $page,
  370. oldpage => $src,
  371. newpage => $dest,
  372. content => $content,
  373. );
  374. });
  375. return $content;
  376. }# }}}
  377. sub do_rename ($$$) { #{{{
  378. my $rename=shift;
  379. my $q=shift;
  380. my $session=shift;
  381. # First, check if this rename is allowed.
  382. check_canrename($rename->{src},
  383. $rename->{srcfile},
  384. $rename->{dest},
  385. $rename->{destfile},
  386. $q, $session);
  387. # Ensure that the dest directory exists and is ok.
  388. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  389. if ($config{rcs}) {
  390. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  391. }
  392. else {
  393. if (! rename($config{srcdir}."/".$rename->{srcfile},
  394. $config{srcdir}."/".$rename->{destfile})) {
  395. error("rename: $!");
  396. }
  397. }
  398. } # }}}
  399. sub fixlinks ($$$) { #{{{
  400. my $rename=shift;
  401. my $session=shift;
  402. my @fixedlinks;
  403. foreach my $page (keys %links) {
  404. my $needfix=0;
  405. foreach my $link (@{$links{$page}}) {
  406. my $bestlink=bestlink($page, $link);
  407. if ($bestlink eq $rename->{src}) {
  408. $needfix=1;
  409. last;
  410. }
  411. }
  412. if ($needfix) {
  413. my $file=$pagesources{$page};
  414. my $oldcontent=readfile($config{srcdir}."/".$file);
  415. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  416. if ($oldcontent ne $content) {
  417. my $token=IkiWiki::rcs_prepedit($file);
  418. eval { writefile($file, $config{srcdir}, $content) };
  419. next if $@;
  420. my $conflict=IkiWiki::rcs_commit(
  421. $file,
  422. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  423. $token,
  424. $session->param("name"),
  425. $ENV{REMOTE_ADDR}
  426. );
  427. push @fixedlinks, $page if ! defined $conflict;
  428. }
  429. }
  430. }
  431. return @fixedlinks;
  432. } #}}}
  433. 1