summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 61d39d4b568d74b3766984f585862ae698b9957b (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. }
  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 => 1,
  114. fields => [qw{do page new_name attachment}],
  115. );
  116. $f->field(name => "do", type => "hidden", value => "rename", force => 1);
  117. $f->field(name => "sid", type => "hidden", value => $session->id,
  118. force => 1);
  119. $f->field(name => "page", type => "hidden", value => $page, force => 1);
  120. $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
  121. if (!$q->param("attachment")) {
  122. # insert the standard extensions
  123. my @page_types;
  124. if (exists $IkiWiki::hooks{htmlize}) {
  125. foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
  126. push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
  127. }
  128. }
  129. @page_types=sort @page_types;
  130. # make sure the current extension is in the list
  131. my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
  132. if (! $IkiWiki::hooks{htmlize}{$ext}) {
  133. unshift(@page_types, [$ext, $ext]);
  134. }
  135. $f->field(name => "type", type => 'select',
  136. options => \@page_types,
  137. value => $ext, force => 1);
  138. foreach my $p (keys %pagesources) {
  139. if ($pagesources{$p}=~m/^\Q$page\E\//) {
  140. $f->field(name => "subpages",
  141. label => "",
  142. type => "checkbox",
  143. options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
  144. value => 1,
  145. force => 1);
  146. last;
  147. }
  148. }
  149. }
  150. $f->field(name => "attachment", type => "hidden");
  151. return $f, ["Rename", "Cancel"];
  152. }
  153. sub rename_start ($$$$) {
  154. my $q=shift;
  155. my $session=shift;
  156. my $attachment=shift;
  157. my $page=shift;
  158. check_canrename($page, $pagesources{$page}, undef, undef,
  159. $q, $session);
  160. # Save current form state to allow returning to it later
  161. # without losing any edits.
  162. # (But don't save what button was submitted, to avoid
  163. # looping back to here.)
  164. # Note: "_submit" is CGI::FormBuilder internals.
  165. $q->param(-name => "_submit", -value => "");
  166. $session->param(postrename => scalar $q->Vars);
  167. IkiWiki::cgi_savesession($session);
  168. if (defined $attachment) {
  169. $q->param(-name => "attachment", -value => $attachment);
  170. }
  171. my ($f, $buttons)=rename_form($q, $session, $page);
  172. IkiWiki::showform($f, $buttons, $session, $q);
  173. exit 0;
  174. }
  175. sub postrename ($;$$$) {
  176. my $session=shift;
  177. my $src=shift;
  178. my $dest=shift;
  179. my $attachment=shift;
  180. # Load saved form state and return to edit page.
  181. my $postrename=CGI->new($session->param("postrename"));
  182. $session->clear("postrename");
  183. IkiWiki::cgi_savesession($session);
  184. if (defined $dest) {
  185. if (! $attachment) {
  186. # They renamed the page they were editing. This requires
  187. # fixups to the edit form state.
  188. # Tweak the edit form to be editing the new page.
  189. $postrename->param("page", $dest);
  190. }
  191. # Update edit form content to fix any links present
  192. # on it.
  193. $postrename->param("editcontent",
  194. renamepage_hook($dest, $src, $dest,
  195. $postrename->param("editcontent")));
  196. # Get a new edit token; old was likely invalidated.
  197. $postrename->param("rcsinfo",
  198. IkiWiki::rcs_prepedit($pagesources{$dest}));
  199. }
  200. IkiWiki::cgi_editpage($postrename, $session);
  201. }
  202. sub formbuilder (@) {
  203. my %params=@_;
  204. my $form=$params{form};
  205. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  206. $form->field("do") eq "create")) {
  207. IkiWiki::decode_form_utf8($form);
  208. my $q=$params{cgi};
  209. my $session=$params{session};
  210. if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
  211. rename_start($q, $session, 0, $form->field("page"));
  212. }
  213. elsif ($form->submitted eq "Rename Attachment") {
  214. my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
  215. if (@selected > 1) {
  216. error(gettext("Only one attachment can be renamed at a time."));
  217. }
  218. elsif (! @selected) {
  219. error(gettext("Please select the attachment to rename."))
  220. }
  221. rename_start($q, $session, 1, $selected[0]);
  222. }
  223. }
  224. }
  225. my $renamesummary;
  226. sub formbuilder_setup (@) {
  227. my %params=@_;
  228. my $form=$params{form};
  229. my $q=$params{cgi};
  230. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  231. $form->field("do") eq "create")) {
  232. # Rename button for the page, and also for attachments.
  233. push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
  234. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  235. if (defined $renamesummary) {
  236. $form->tmpl_param(message => $renamesummary);
  237. }
  238. }
  239. }
  240. sub sessioncgi ($$) {
  241. my $q=shift;
  242. if ($q->param("do") eq 'rename') {
  243. my $session=shift;
  244. my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
  245. IkiWiki::decode_form_utf8($form);
  246. if ($form->submitted eq 'Cancel') {
  247. postrename($session);
  248. }
  249. elsif ($form->submitted eq 'Rename' && $form->validate) {
  250. IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
  251. # Queue of rename actions to perfom.
  252. my @torename;
  253. # These untaints are safe because of the checks
  254. # performed in check_canrename later.
  255. my $src=$form->field("page");
  256. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  257. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
  258. my $destfile=$dest;
  259. if (! $q->param("attachment")) {
  260. my $type=$q->param('type');
  261. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  262. $type=IkiWiki::possibly_foolish_untaint($type);
  263. }
  264. else {
  265. my ($ext)=$srcfile=~/\.([^.]+)$/;
  266. $type=$ext;
  267. }
  268. $destfile=newpagefile($dest, $type);
  269. }
  270. push @torename, {
  271. src => $src,
  272. srcfile => $srcfile,
  273. dest => $dest,
  274. destfile => $destfile,
  275. required => 1,
  276. };
  277. @torename=rename_hook(
  278. torename => \@torename,
  279. done => {},
  280. cgi => $q,
  281. session => $session,
  282. );
  283. require IkiWiki::Render;
  284. IkiWiki::disable_commit_hook() if $config{rcs};
  285. my %origpagesources=%pagesources;
  286. # First file renaming.
  287. foreach my $rename (@torename) {
  288. if ($rename->{required}) {
  289. do_rename($rename, $q, $session);
  290. }
  291. else {
  292. eval {do_rename($rename, $q, $session)};
  293. if ($@) {
  294. $rename->{error}=$@;
  295. next;
  296. }
  297. }
  298. # Temporarily tweak pagesources to point to
  299. # the renamed file, in case fixlinks needs
  300. # to edit it.
  301. $pagesources{$rename->{src}}=$rename->{destfile};
  302. }
  303. IkiWiki::rcs_commit_staged(
  304. message => sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  305. session => $session,
  306. ) 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_subpages (@) {
  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. return () unless @torename;
  430. my @nextset;
  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. # dedup
  450. my %seen;
  451. return grep { ! $seen{$_->{src}}++ } @torename;
  452. }
  453. sub do_rename ($$$) {
  454. my $rename=shift;
  455. my $q=shift;
  456. my $session=shift;
  457. # First, check if this rename is allowed.
  458. check_canrename($rename->{src},
  459. $rename->{srcfile},
  460. $rename->{dest},
  461. $rename->{destfile},
  462. $q, $session);
  463. # Ensure that the dest directory exists and is ok.
  464. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  465. if ($config{rcs}) {
  466. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  467. }
  468. else {
  469. if (! rename($config{srcdir}."/".$rename->{srcfile},
  470. $config{srcdir}."/".$rename->{destfile})) {
  471. error("rename: $!");
  472. }
  473. }
  474. }
  475. sub fixlinks ($$$) {
  476. my $rename=shift;
  477. my $session=shift;
  478. my @fixedlinks;
  479. foreach my $page (keys %links) {
  480. my $needfix=0;
  481. foreach my $link (@{$links{$page}}) {
  482. my $bestlink=bestlink($page, $link);
  483. if ($bestlink eq $rename->{src}) {
  484. $needfix=1;
  485. last;
  486. }
  487. }
  488. if ($needfix) {
  489. my $file=$pagesources{$page};
  490. my $oldcontent=readfile($config{srcdir}."/".$file);
  491. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  492. if ($oldcontent ne $content) {
  493. my $token=IkiWiki::rcs_prepedit($file);
  494. eval { writefile($file, $config{srcdir}, $content) };
  495. next if $@;
  496. my $conflict=IkiWiki::rcs_commit(
  497. $file,
  498. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  499. $token,
  500. $session->param("name"),
  501. $session->remote_addr(),
  502. );
  503. push @fixedlinks, $page if ! defined $conflict;
  504. }
  505. }
  506. }
  507. return @fixedlinks;
  508. }
  509. 1