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