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