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