summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: d0e5894dcac9e135f83794493c00cd75cea17527 (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. 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. my $q=$params{cgi};
  206. my $session=$params{session};
  207. if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
  208. rename_start($q, $session, 0, $form->field("page"));
  209. }
  210. elsif ($form->submitted eq "Rename Attachment") {
  211. my @selected=$q->param("attachment_select");
  212. if (@selected > 1) {
  213. error(gettext("Only one attachment can be renamed at a time."));
  214. }
  215. elsif (! @selected) {
  216. error(gettext("Please select the attachment to rename."))
  217. }
  218. rename_start($q, $session, 1, $selected[0]);
  219. }
  220. }
  221. }
  222. my $renamesummary;
  223. sub formbuilder_setup (@) {
  224. my %params=@_;
  225. my $form=$params{form};
  226. my $q=$params{cgi};
  227. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  228. $form->field("do") eq "create")) {
  229. # Rename button for the page, and also for attachments.
  230. push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
  231. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  232. if (defined $renamesummary) {
  233. $form->tmpl_param(message => $renamesummary);
  234. }
  235. }
  236. }
  237. sub sessioncgi ($$) {
  238. my $q=shift;
  239. if ($q->param("do") eq 'rename') {
  240. my $session=shift;
  241. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  242. IkiWiki::decode_form_utf8($form);
  243. if ($form->submitted eq 'Cancel') {
  244. postrename($session);
  245. }
  246. elsif ($form->submitted eq 'Rename' && $form->validate) {
  247. # Queue of rename actions to perfom.
  248. my @torename;
  249. # These untaints are safe because of the checks
  250. # performed in check_canrename later.
  251. my $src=$q->param("page");
  252. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  253. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
  254. my $destfile=$dest;
  255. if (! $q->param("attachment")) {
  256. my $type=$q->param('type');
  257. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  258. $type=IkiWiki::possibly_foolish_untaint($type);
  259. }
  260. else {
  261. my ($ext)=$srcfile=~/\.([^.]+)$/;
  262. $type=$ext;
  263. }
  264. $destfile=newpagefile($dest, $type);
  265. }
  266. push @torename, {
  267. src => $src,
  268. srcfile => $srcfile,
  269. dest => $dest,
  270. destfile => $destfile,
  271. required => 1,
  272. };
  273. @torename=rename_hook(
  274. torename => \@torename,
  275. done => {},
  276. cgi => $q,
  277. session => $session,
  278. );
  279. require IkiWiki::Render;
  280. IkiWiki::disable_commit_hook() if $config{rcs};
  281. my %origpagesources=%pagesources;
  282. # First file renaming.
  283. foreach my $rename (@torename) {
  284. if ($rename->{required}) {
  285. do_rename($rename, $q, $session);
  286. }
  287. else {
  288. eval {do_rename($rename, $q, $session)};
  289. if ($@) {
  290. $rename->{error}=$@;
  291. next;
  292. }
  293. }
  294. # Temporarily tweak pagesources to point to
  295. # the renamed file, in case fixlinks needs
  296. # to edit it.
  297. $pagesources{$rename->{src}}=$rename->{destfile};
  298. }
  299. IkiWiki::rcs_commit_staged(
  300. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  301. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  302. # Then link fixups.
  303. foreach my $rename (@torename) {
  304. next if $rename->{src} eq $rename->{dest};
  305. next if $rename->{error};
  306. foreach my $p (fixlinks($rename, $session)) {
  307. # map old page names to new
  308. foreach my $r (@torename) {
  309. next if $rename->{error};
  310. if ($r->{src} eq $p) {
  311. $p=$r->{dest};
  312. last;
  313. }
  314. }
  315. push @{$rename->{fixedlinks}}, $p;
  316. }
  317. }
  318. # Then refresh.
  319. %pagesources=%origpagesources;
  320. if ($config{rcs}) {
  321. IkiWiki::enable_commit_hook();
  322. IkiWiki::rcs_update();
  323. }
  324. IkiWiki::refresh();
  325. IkiWiki::saveindex();
  326. # Find pages with remaining, broken links.
  327. foreach my $rename (@torename) {
  328. next if $rename->{src} eq $rename->{dest};
  329. foreach my $page (keys %links) {
  330. my $broken=0;
  331. foreach my $link (@{$links{$page}}) {
  332. my $bestlink=bestlink($page, $link);
  333. if ($bestlink eq $rename->{src}) {
  334. push @{$rename->{brokenlinks}}, $page;
  335. last;
  336. }
  337. }
  338. }
  339. }
  340. # Generate a summary, that will be shown at the top
  341. # of the edit template.
  342. $renamesummary="";
  343. foreach my $rename (@torename) {
  344. my $template=template("renamesummary.tmpl");
  345. $template->param(src => $rename->{srcfile});
  346. $template->param(dest => $rename->{destfile});
  347. $template->param(error => $rename->{error});
  348. if ($rename->{src} ne $rename->{dest}) {
  349. $template->param(brokenlinks_checked => 1);
  350. $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
  351. $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
  352. }
  353. $renamesummary.=$template->output;
  354. }
  355. postrename($session, $src, $dest, $q->param("attachment"));
  356. }
  357. else {
  358. IkiWiki::showform($form, $buttons, $session, $q);
  359. }
  360. exit 0;
  361. }
  362. }
  363. # Add subpages to the list of pages to be renamed, if needed.
  364. sub rename_subpages (@) {
  365. my %params = @_;
  366. my %torename = %{$params{torename}};
  367. my $q = $params{cgi};
  368. my $src = $torename{src};
  369. my $srcfile = $torename{src};
  370. my $dest = $torename{dest};
  371. my $destfile = $torename{dest};
  372. return () unless ($q->param("subpages") && $src ne $dest);
  373. my @ret;
  374. foreach my $p (keys %pagesources) {
  375. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  376. # If indexpages is enabled, the srcfile should not be confused
  377. # with a subpage.
  378. next if $pagesources{$p} eq $srcfile;
  379. my $d=$pagesources{$p};
  380. $d=~s/^\Q$src\E\//$dest\//;
  381. push @ret, {
  382. src => $p,
  383. srcfile => $pagesources{$p},
  384. dest => pagename($d),
  385. destfile => $d,
  386. required => 0,
  387. };
  388. }
  389. return @ret;
  390. }
  391. sub linklist {
  392. # generates a list of links in a form suitable for FormBuilder
  393. my $dest=shift;
  394. my $list=shift;
  395. # converts a list of pages into a list of links
  396. # in a form suitable for FormBuilder.
  397. [map {
  398. {
  399. page => htmllink($dest, $dest, $_,
  400. noimageinline => 1,
  401. linktext => pagetitle($_),
  402. )
  403. }
  404. } @{$list}]
  405. }
  406. sub renamepage_hook ($$$$) {
  407. my ($page, $src, $dest, $content)=@_;
  408. IkiWiki::run_hooks(renamepage => sub {
  409. $content=shift->(
  410. page => $page,
  411. oldpage => $src,
  412. newpage => $dest,
  413. content => $content,
  414. );
  415. });
  416. return $content;
  417. }
  418. sub rename_hook (@) {
  419. my %params = @_;
  420. my @torename=@{$params{torename}};
  421. my %done=%{$params{done}};
  422. my $q=$params{cgi};
  423. my $session=$params{session};
  424. return () unless @torename;
  425. my @nextset;
  426. foreach my $torename (@torename) {
  427. unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
  428. IkiWiki::run_hooks(rename => sub {
  429. push @nextset, shift->(
  430. torename => $torename,
  431. cgi => $q,
  432. session => $session,
  433. );
  434. });
  435. $done{$torename->{src}}=1;
  436. }
  437. }
  438. push @torename, rename_hook(
  439. torename => \@nextset,
  440. done => \%done,
  441. cgi => $q,
  442. session => $session,
  443. );
  444. # dedup
  445. my %seen;
  446. return grep { ! $seen{$_->{src}}++ } @torename;
  447. }
  448. sub do_rename ($$$) {
  449. my $rename=shift;
  450. my $q=shift;
  451. my $session=shift;
  452. # First, check if this rename is allowed.
  453. check_canrename($rename->{src},
  454. $rename->{srcfile},
  455. $rename->{dest},
  456. $rename->{destfile},
  457. $q, $session);
  458. # Ensure that the dest directory exists and is ok.
  459. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  460. if ($config{rcs}) {
  461. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  462. }
  463. else {
  464. if (! rename($config{srcdir}."/".$rename->{srcfile},
  465. $config{srcdir}."/".$rename->{destfile})) {
  466. error("rename: $!");
  467. }
  468. }
  469. }
  470. sub fixlinks ($$$) {
  471. my $rename=shift;
  472. my $session=shift;
  473. my @fixedlinks;
  474. foreach my $page (keys %links) {
  475. my $needfix=0;
  476. foreach my $link (@{$links{$page}}) {
  477. my $bestlink=bestlink($page, $link);
  478. if ($bestlink eq $rename->{src}) {
  479. $needfix=1;
  480. last;
  481. }
  482. }
  483. if ($needfix) {
  484. my $file=$pagesources{$page};
  485. my $oldcontent=readfile($config{srcdir}."/".$file);
  486. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  487. if ($oldcontent ne $content) {
  488. my $token=IkiWiki::rcs_prepedit($file);
  489. eval { writefile($file, $config{srcdir}, $content) };
  490. next if $@;
  491. my $conflict=IkiWiki::rcs_commit(
  492. $file,
  493. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  494. $token,
  495. $session->param("name"),
  496. $ENV{REMOTE_ADDR}
  497. );
  498. push @fixedlinks, $page if ! defined $conflict;
  499. }
  500. }
  501. }
  502. return @fixedlinks;
  503. }
  504. 1