summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 8dad92be3e8794c2ef09ed4a518f814fb49be2a5 (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. },
  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. require IkiWiki::Render;
  278. IkiWiki::disable_commit_hook() if $config{rcs};
  279. my %origpagesources=%pagesources;
  280. # First file renaming.
  281. foreach my $rename (@torename) {
  282. if ($rename->{required}) {
  283. do_rename($rename, $q, $session);
  284. }
  285. else {
  286. eval {do_rename($rename, $q, $session)};
  287. if ($@) {
  288. $rename->{error}=$@;
  289. next;
  290. }
  291. }
  292. # Temporarily tweak pagesources to point to
  293. # the renamed file, in case fixlinks needs
  294. # to edit it.
  295. $pagesources{$rename->{src}}=$rename->{destfile};
  296. }
  297. IkiWiki::rcs_commit_staged(
  298. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  299. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  300. # Then link fixups.
  301. foreach my $rename (@torename) {
  302. next if $rename->{src} eq $rename->{dest};
  303. next if $rename->{error};
  304. foreach my $p (fixlinks($rename, $session)) {
  305. # map old page names to new
  306. foreach my $r (@torename) {
  307. next if $rename->{error};
  308. if ($r->{src} eq $p) {
  309. $p=$r->{dest};
  310. last;
  311. }
  312. }
  313. push @{$rename->{fixedlinks}}, $p;
  314. }
  315. }
  316. # Then refresh.
  317. %pagesources=%origpagesources;
  318. if ($config{rcs}) {
  319. IkiWiki::enable_commit_hook();
  320. IkiWiki::rcs_update();
  321. }
  322. IkiWiki::refresh();
  323. IkiWiki::saveindex();
  324. # Find pages with remaining, broken links.
  325. foreach my $rename (@torename) {
  326. next if $rename->{src} eq $rename->{dest};
  327. foreach my $page (keys %links) {
  328. my $broken=0;
  329. foreach my $link (@{$links{$page}}) {
  330. my $bestlink=bestlink($page, $link);
  331. if ($bestlink eq $rename->{src}) {
  332. push @{$rename->{brokenlinks}}, $page;
  333. last;
  334. }
  335. }
  336. }
  337. }
  338. # Generate a summary, that will be shown at the top
  339. # of the edit template.
  340. $renamesummary="";
  341. foreach my $rename (@torename) {
  342. my $template=template("renamesummary.tmpl");
  343. $template->param(src => $rename->{srcfile});
  344. $template->param(dest => $rename->{destfile});
  345. $template->param(error => $rename->{error});
  346. if ($rename->{src} ne $rename->{dest}) {
  347. $template->param(brokenlinks_checked => 1);
  348. $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
  349. $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
  350. }
  351. $renamesummary.=$template->output;
  352. }
  353. postrename($session, $src, $dest, $q->param("attachment"));
  354. }
  355. else {
  356. IkiWiki::showform($form, $buttons, $session, $q);
  357. }
  358. exit 0;
  359. }
  360. }
  361. # Add subpages to the list of pages to be renamed, if needed.
  362. sub rename_subpages (@) {
  363. my %params = @_;
  364. my %torename = %{$params{torename}};
  365. my $q = $params{cgi};
  366. my $src = $torename{src};
  367. my $srcfile = $torename{src};
  368. my $dest = $torename{dest};
  369. my $destfile = $torename{dest};
  370. return () unless ($q->param("subpages") && $src ne $dest);
  371. my @ret;
  372. foreach my $p (keys %pagesources) {
  373. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  374. # If indexpages is enabled, the srcfile should not be confused
  375. # with a subpage.
  376. next if $pagesources{$p} eq $srcfile;
  377. my $d=$pagesources{$p};
  378. $d=~s/^\Q$src\E\//$dest\//;
  379. push @ret, {
  380. src => $p,
  381. srcfile => $pagesources{$p},
  382. dest => pagename($d),
  383. destfile => $d,
  384. required => 0,
  385. };
  386. }
  387. return @ret;
  388. }
  389. sub linklist {
  390. # generates a list of links in a form suitable for FormBuilder
  391. my $dest=shift;
  392. my $list=shift;
  393. # converts a list of pages into a list of links
  394. # in a form suitable for FormBuilder.
  395. [map {
  396. {
  397. page => htmllink($dest, $dest, $_,
  398. noimageinline => 1,
  399. linktext => pagetitle($_),
  400. )
  401. }
  402. } @{$list}]
  403. }
  404. sub renamepage_hook ($$$$) {
  405. my ($page, $src, $dest, $content)=@_;
  406. IkiWiki::run_hooks(renamepage => sub {
  407. $content=shift->(
  408. page => $page,
  409. oldpage => $src,
  410. newpage => $dest,
  411. content => $content,
  412. );
  413. });
  414. return $content;
  415. }
  416. sub rename_hook (@) {
  417. my %params = @_;
  418. my @torename=@{$params{torename}};
  419. my %done=%{$params{done}};
  420. my $q=$params{cgi};
  421. my $session=$params{session};
  422. return () unless @torename;
  423. my @nextset;
  424. foreach my $torename (@torename) {
  425. unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
  426. IkiWiki::run_hooks(rename => sub {
  427. push @nextset, shift->(
  428. torename => $torename,
  429. cgi => $q,
  430. session => $session,
  431. );
  432. });
  433. $done{$torename->{src}}=1;
  434. }
  435. }
  436. push @torename, rename_hook(
  437. torename => \@nextset,
  438. done => \%done,
  439. cgi => $q,
  440. session => $session,
  441. );
  442. # dedup
  443. my %seen;
  444. return grep { ! $seen{$_->{src}}++ } @torename;
  445. }
  446. sub do_rename ($$$) {
  447. my $rename=shift;
  448. my $q=shift;
  449. my $session=shift;
  450. # First, check if this rename is allowed.
  451. check_canrename($rename->{src},
  452. $rename->{srcfile},
  453. $rename->{dest},
  454. $rename->{destfile},
  455. $q, $session);
  456. # Ensure that the dest directory exists and is ok.
  457. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  458. if ($config{rcs}) {
  459. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  460. }
  461. else {
  462. if (! rename($config{srcdir}."/".$rename->{srcfile},
  463. $config{srcdir}."/".$rename->{destfile})) {
  464. error("rename: $!");
  465. }
  466. }
  467. }
  468. sub fixlinks ($$$) {
  469. my $rename=shift;
  470. my $session=shift;
  471. my @fixedlinks;
  472. foreach my $page (keys %links) {
  473. my $needfix=0;
  474. foreach my $link (@{$links{$page}}) {
  475. my $bestlink=bestlink($page, $link);
  476. if ($bestlink eq $rename->{src}) {
  477. $needfix=1;
  478. last;
  479. }
  480. }
  481. if ($needfix) {
  482. my $file=$pagesources{$page};
  483. my $oldcontent=readfile($config{srcdir}."/".$file);
  484. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  485. if ($oldcontent ne $content) {
  486. my $token=IkiWiki::rcs_prepedit($file);
  487. eval { writefile($file, $config{srcdir}, $content) };
  488. next if $@;
  489. my $conflict=IkiWiki::rcs_commit(
  490. $file,
  491. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  492. $token,
  493. $session->param("name"),
  494. $ENV{REMOTE_ADDR}
  495. );
  496. push @fixedlinks, $page if ! defined $conflict;
  497. }
  498. }
  499. }
  500. return @fixedlinks;
  501. }
  502. 1