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