summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: d3ca8bd3a4b585d67b6792207bc1a89f76ade3e0 (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. hook(type => "rename", id => "rename", call => \&rename);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => 0,
  18. },
  19. }
  20. sub check_canrename ($$$$$$) {
  21. my $src=shift;
  22. my $srcfile=shift;
  23. my $dest=shift;
  24. my $destfile=shift;
  25. my $q=shift;
  26. my $session=shift;
  27. my $attachment=! defined pagetype($pagesources{$src});
  28. # Must be a known source file.
  29. if (! exists $pagesources{$src}) {
  30. error(sprintf(gettext("%s does not exist"),
  31. htmllink("", "", $src, noimageinline => 1)));
  32. }
  33. # Must exist on disk, and be a regular file.
  34. if (! -e "$config{srcdir}/$srcfile") {
  35. error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
  36. }
  37. elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
  38. error(sprintf(gettext("%s is not a file"), $srcfile));
  39. }
  40. # Must be editable.
  41. IkiWiki::check_canedit($src, $q, $session);
  42. if ($attachment) {
  43. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  44. IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
  45. }
  46. else {
  47. error("renaming of attachments is not allowed");
  48. }
  49. }
  50. # Dest checks can be omitted by passing undef.
  51. if (defined $dest) {
  52. if ($srcfile eq $destfile) {
  53. error(gettext("no change to the file name was specified"));
  54. }
  55. # Must be a legal filename, and not absolute.
  56. if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
  57. $destfile=~/^\//) {
  58. error(sprintf(gettext("illegal name")));
  59. }
  60. # Must not be a known source file.
  61. if ($src ne $dest && exists $pagesources{$dest}) {
  62. error(sprintf(gettext("%s already exists"),
  63. htmllink("", "", $dest, noimageinline => 1)));
  64. }
  65. # Must not exist on disk already.
  66. if (-l "$config{srcdir}/$destfile" || -e _) {
  67. error(sprintf(gettext("%s already exists on disk"), $destfile));
  68. }
  69. # Must be editable.
  70. IkiWiki::check_canedit($dest, $q, $session);
  71. if ($attachment) {
  72. # Note that $srcfile is used here, not $destfile,
  73. # because it wants the current file, to check it.
  74. IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
  75. }
  76. }
  77. my $canrename;
  78. IkiWiki::run_hooks(canrename => sub {
  79. return if defined $canrename;
  80. my $ret=shift->(cgi => $q, session => $session,
  81. src => $src, srcfile => $srcfile,
  82. dest => $dest, destfile => $destfile);
  83. if (defined $ret) {
  84. if ($ret eq "") {
  85. $canrename=1;
  86. }
  87. elsif (ref $ret eq 'CODE') {
  88. $ret->();
  89. $canrename=0;
  90. }
  91. elsif (defined $ret) {
  92. error($ret);
  93. $canrename=0;
  94. }
  95. }
  96. });
  97. }
  98. sub rename_form ($$$) {
  99. my $q=shift;
  100. my $session=shift;
  101. my $page=shift;
  102. eval q{use CGI::FormBuilder};
  103. error($@) if $@;
  104. my $f = CGI::FormBuilder->new(
  105. name => "rename",
  106. title => sprintf(gettext("rename %s"), pagetitle($page)),
  107. header => 0,
  108. charset => "utf-8",
  109. method => 'POST',
  110. javascript => 0,
  111. params => $q,
  112. action => $config{cgiurl},
  113. stylesheet => IkiWiki::baseurl()."style.css",
  114. fields => [qw{do page new_name attachment}],
  115. );
  116. $f->field(name => "do", type => "hidden", value => "rename", force => 1);
  117. $f->field(name => "page", type => "hidden", value => $page, force => 1);
  118. $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
  119. if (!$q->param("attachment")) {
  120. # insert the standard extensions
  121. my @page_types;
  122. if (exists $IkiWiki::hooks{htmlize}) {
  123. @page_types=grep { !/^_/ }
  124. keys %{$IkiWiki::hooks{htmlize}};
  125. }
  126. # make sure the current extension is in the list
  127. my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
  128. if (! $IkiWiki::hooks{htmlize}{$ext}) {
  129. unshift(@page_types, $ext);
  130. }
  131. $f->field(name => "type", type => 'select',
  132. options => \@page_types,
  133. value => $ext, force => 1);
  134. foreach my $p (keys %pagesources) {
  135. if ($pagesources{$p}=~m/^\Q$page\E\//) {
  136. $f->field(name => "subpages",
  137. label => "",
  138. type => "checkbox",
  139. options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
  140. value => 1,
  141. force => 1);
  142. last;
  143. }
  144. }
  145. }
  146. $f->field(name => "attachment", type => "hidden");
  147. return $f, ["Rename", "Cancel"];
  148. }
  149. sub rename_start ($$$$) {
  150. my $q=shift;
  151. my $session=shift;
  152. my $attachment=shift;
  153. my $page=shift;
  154. check_canrename($page, $pagesources{$page}, undef, undef,
  155. $q, $session);
  156. # Save current form state to allow returning to it later
  157. # without losing any edits.
  158. # (But don't save what button was submitted, to avoid
  159. # looping back to here.)
  160. # Note: "_submit" is CGI::FormBuilder internals.
  161. $q->param(-name => "_submit", -value => "");
  162. $session->param(postrename => scalar $q->Vars);
  163. IkiWiki::cgi_savesession($session);
  164. if (defined $attachment) {
  165. $q->param(-name => "attachment", -value => $attachment);
  166. }
  167. my ($f, $buttons)=rename_form($q, $session, $page);
  168. IkiWiki::showform($f, $buttons, $session, $q);
  169. exit 0;
  170. }
  171. sub postrename ($;$$$) {
  172. my $session=shift;
  173. my $src=shift;
  174. my $dest=shift;
  175. my $attachment=shift;
  176. # Load saved form state and return to edit page.
  177. my $postrename=CGI->new($session->param("postrename"));
  178. $session->clear("postrename");
  179. IkiWiki::cgi_savesession($session);
  180. if (defined $dest) {
  181. if (! $attachment) {
  182. # They renamed the page they were editing. This requires
  183. # fixups to the edit form state.
  184. # Tweak the edit form to be editing the new page.
  185. $postrename->param("page", $dest);
  186. }
  187. # Update edit form content to fix any links present
  188. # on it.
  189. $postrename->param("editcontent",
  190. renamepage_hook($dest, $src, $dest,
  191. $postrename->param("editcontent")));
  192. # Get a new edit token; old was likely invalidated.
  193. $postrename->param("rcsinfo",
  194. IkiWiki::rcs_prepedit($pagesources{$dest}));
  195. }
  196. IkiWiki::cgi_editpage($postrename, $session);
  197. }
  198. sub formbuilder (@) {
  199. my %params=@_;
  200. my $form=$params{form};
  201. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  202. $form->field("do") eq "create")) {
  203. my $q=$params{cgi};
  204. my $session=$params{session};
  205. if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
  206. rename_start($q, $session, 0, $form->field("page"));
  207. }
  208. elsif ($form->submitted eq "Rename Attachment") {
  209. my @selected=$q->param("attachment_select");
  210. if (@selected > 1) {
  211. error(gettext("Only one attachment can be renamed at a time."));
  212. }
  213. elsif (! @selected) {
  214. error(gettext("Please select the attachment to rename."))
  215. }
  216. rename_start($q, $session, 1, $selected[0]);
  217. }
  218. }
  219. }
  220. my $renamesummary;
  221. sub formbuilder_setup (@) {
  222. my %params=@_;
  223. my $form=$params{form};
  224. my $q=$params{cgi};
  225. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  226. $form->field("do") eq "create")) {
  227. # Rename button for the page, and also for attachments.
  228. push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
  229. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  230. if (defined $renamesummary) {
  231. $form->tmpl_param(message => $renamesummary);
  232. }
  233. }
  234. }
  235. sub sessioncgi ($$) {
  236. my $q=shift;
  237. if ($q->param("do") eq 'rename') {
  238. my $session=shift;
  239. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  240. IkiWiki::decode_form_utf8($form);
  241. if ($form->submitted eq 'Cancel') {
  242. postrename($session);
  243. }
  244. elsif ($form->submitted eq 'Rename' && $form->validate) {
  245. # Queue of rename actions to perfom.
  246. my @torename;
  247. # These untaints are safe because of the checks
  248. # performed in check_canrename later.
  249. my $src=$q->param("page");
  250. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  251. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
  252. my $destfile=$dest;
  253. if (! $q->param("attachment")) {
  254. my $type=$q->param('type');
  255. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  256. $type=IkiWiki::possibly_foolish_untaint($type);
  257. }
  258. else {
  259. my ($ext)=$srcfile=~/\.([^.]+)$/;
  260. $type=$ext;
  261. }
  262. $destfile=newpagefile($dest, $type);
  263. }
  264. push @torename, {
  265. src => $src,
  266. srcfile => $srcfile,
  267. dest => $dest,
  268. destfile => $destfile,
  269. required => 1,
  270. };
  271. @torename=rename_hook(
  272. torename => \@torename,
  273. done => {},
  274. cgi => $q,
  275. session => $session,
  276. );
  277. # remove duplicates from @torename
  278. my %seen=();
  279. my @uniq_torename;
  280. foreach my $item (@torename) {
  281. push(@uniq_torename, $item) unless $seen{$item->{src}}++;
  282. }
  283. @torename=@uniq_torename;
  284. require IkiWiki::Render;
  285. IkiWiki::disable_commit_hook() if $config{rcs};
  286. my %origpagesources=%pagesources;
  287. # First file renaming.
  288. foreach my $rename (@torename) {
  289. if ($rename->{required}) {
  290. do_rename($rename, $q, $session);
  291. }
  292. else {
  293. eval {do_rename($rename, $q, $session)};
  294. if ($@) {
  295. $rename->{error}=$@;
  296. next;
  297. }
  298. }
  299. # Temporarily tweak pagesources to point to
  300. # the renamed file, in case fixlinks needs
  301. # to edit it.
  302. $pagesources{$rename->{src}}=$rename->{destfile};
  303. }
  304. IkiWiki::rcs_commit_staged(
  305. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  306. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  307. # Then link fixups.
  308. foreach my $rename (@torename) {
  309. next if $rename->{src} eq $rename->{dest};
  310. next if $rename->{error};
  311. foreach my $p (fixlinks($rename, $session)) {
  312. # map old page names to new
  313. foreach my $r (@torename) {
  314. next if $rename->{error};
  315. if ($r->{src} eq $p) {
  316. $p=$r->{dest};
  317. last;
  318. }
  319. }
  320. push @{$rename->{fixedlinks}}, $p;
  321. }
  322. }
  323. # Then refresh.
  324. %pagesources=%origpagesources;
  325. if ($config{rcs}) {
  326. IkiWiki::enable_commit_hook();
  327. IkiWiki::rcs_update();
  328. }
  329. IkiWiki::refresh();
  330. IkiWiki::saveindex();
  331. # Find pages with remaining, broken links.
  332. foreach my $rename (@torename) {
  333. next if $rename->{src} eq $rename->{dest};
  334. foreach my $page (keys %links) {
  335. my $broken=0;
  336. foreach my $link (@{$links{$page}}) {
  337. my $bestlink=bestlink($page, $link);
  338. if ($bestlink eq $rename->{src}) {
  339. push @{$rename->{brokenlinks}}, $page;
  340. last;
  341. }
  342. }
  343. }
  344. }
  345. # Generate a summary, that will be shown at the top
  346. # of the edit template.
  347. $renamesummary="";
  348. foreach my $rename (@torename) {
  349. my $template=template("renamesummary.tmpl");
  350. $template->param(src => $rename->{srcfile});
  351. $template->param(dest => $rename->{destfile});
  352. $template->param(error => $rename->{error});
  353. if ($rename->{src} ne $rename->{dest}) {
  354. $template->param(brokenlinks_checked => 1);
  355. $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
  356. $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
  357. }
  358. $renamesummary.=$template->output;
  359. }
  360. postrename($session, $src, $dest, $q->param("attachment"));
  361. }
  362. else {
  363. IkiWiki::showform($form, $buttons, $session, $q);
  364. }
  365. exit 0;
  366. }
  367. }
  368. # Add subpages to the list of pages to be renamed, if needed.
  369. sub rename(@) {
  370. my %params = @_;
  371. my %torename = %{$params{torename}};
  372. my $q = $params{cgi};
  373. my $src = $torename{src};
  374. my $srcfile = $torename{src};
  375. my $dest = $torename{dest};
  376. my $destfile = $torename{dest};
  377. return () unless ($q->param("subpages") && $src ne $dest);
  378. my @ret;
  379. foreach my $p (keys %pagesources) {
  380. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  381. # If indexpages is enabled, the srcfile should not be confused
  382. # with a subpage.
  383. next if $pagesources{$p} eq $srcfile;
  384. my $d=$pagesources{$p};
  385. $d=~s/^\Q$src\E\//$dest\//;
  386. push @ret, {
  387. src => $p,
  388. srcfile => $pagesources{$p},
  389. dest => pagename($d),
  390. destfile => $d,
  391. required => 0,
  392. };
  393. }
  394. return @ret;
  395. }
  396. sub linklist {
  397. # generates a list of links in a form suitable for FormBuilder
  398. my $dest=shift;
  399. my $list=shift;
  400. # converts a list of pages into a list of links
  401. # in a form suitable for FormBuilder.
  402. [map {
  403. {
  404. page => htmllink($dest, $dest, $_,
  405. noimageinline => 1,
  406. linktext => pagetitle($_),
  407. )
  408. }
  409. } @{$list}]
  410. }
  411. sub renamepage_hook ($$$$) {
  412. my ($page, $src, $dest, $content)=@_;
  413. IkiWiki::run_hooks(renamepage => sub {
  414. $content=shift->(
  415. page => $page,
  416. oldpage => $src,
  417. newpage => $dest,
  418. content => $content,
  419. );
  420. });
  421. return $content;
  422. }
  423. sub rename_hook (@) {
  424. my %params = @_;
  425. my @torename=@{$params{torename}};
  426. my %done=%{$params{done}};
  427. my $q=$params{cgi};
  428. my $session=$params{session};
  429. my @nextset;
  430. if (@torename) {
  431. foreach my $torename (@torename) {
  432. unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
  433. IkiWiki::run_hooks(rename => sub {
  434. push @nextset, shift->(
  435. torename => $torename,
  436. cgi => $q,
  437. session => $session,
  438. );
  439. });
  440. $done{$torename->{src}}=1;
  441. }
  442. }
  443. push @torename, rename_hook(
  444. torename => \@nextset,
  445. done => \%done,
  446. cgi => $q,
  447. session => $session,
  448. );
  449. return @torename;
  450. }
  451. else {
  452. return ();
  453. }
  454. }
  455. sub do_rename ($$$) {
  456. my $rename=shift;
  457. my $q=shift;
  458. my $session=shift;
  459. # First, check if this rename is allowed.
  460. check_canrename($rename->{src},
  461. $rename->{srcfile},
  462. $rename->{dest},
  463. $rename->{destfile},
  464. $q, $session);
  465. # Ensure that the dest directory exists and is ok.
  466. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  467. if ($config{rcs}) {
  468. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  469. }
  470. else {
  471. if (! CORE::rename($config{srcdir}."/".$rename->{srcfile},
  472. $config{srcdir}."/".$rename->{destfile})) {
  473. error("rename: $!");
  474. }
  475. }
  476. }
  477. sub fixlinks ($$$) {
  478. my $rename=shift;
  479. my $session=shift;
  480. my @fixedlinks;
  481. foreach my $page (keys %links) {
  482. my $needfix=0;
  483. foreach my $link (@{$links{$page}}) {
  484. my $bestlink=bestlink($page, $link);
  485. if ($bestlink eq $rename->{src}) {
  486. $needfix=1;
  487. last;
  488. }
  489. }
  490. if ($needfix) {
  491. my $file=$pagesources{$page};
  492. my $oldcontent=readfile($config{srcdir}."/".$file);
  493. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  494. if ($oldcontent ne $content) {
  495. my $token=IkiWiki::rcs_prepedit($file);
  496. eval { writefile($file, $config{srcdir}, $content) };
  497. next if $@;
  498. my $conflict=IkiWiki::rcs_commit(
  499. $file,
  500. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  501. $token,
  502. $session->param("name"),
  503. $ENV{REMOTE_ADDR}
  504. );
  505. push @fixedlinks, $page if ! defined $conflict;
  506. }
  507. }
  508. }
  509. return @fixedlinks;
  510. }
  511. 1