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