summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 7e55e271cbe6a403115fb38eff1f5655fbc626d6 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::rename;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.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. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 0,
  17. },
  18. } #}}}
  19. sub check_canrename ($$$$$$) { #{{{
  20. my $src=shift;
  21. my $srcfile=shift;
  22. my $dest=shift;
  23. my $destfile=shift;
  24. my $q=shift;
  25. my $session=shift;
  26. my $attachment=! defined pagetype($pagesources{$src});
  27. # Must be a known source file.
  28. if (! exists $pagesources{$src}) {
  29. error(sprintf(gettext("%s does not exist"),
  30. htmllink("", "", $src, noimageinline => 1)));
  31. }
  32. # Must exist on disk, and be a regular file.
  33. if (! -e "$config{srcdir}/$srcfile") {
  34. error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
  35. }
  36. elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
  37. error(sprintf(gettext("%s is not a file"), $srcfile));
  38. }
  39. # Must be editable.
  40. IkiWiki::check_canedit($src, $q, $session);
  41. if ($attachment) {
  42. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  43. IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
  44. }
  45. else {
  46. error("renaming of attachments is not allowed");
  47. }
  48. }
  49. # Dest checks can be omitted by passing undef.
  50. if (defined $dest) {
  51. if ($srcfile eq $destfile) {
  52. error(gettext("no change to the file name was specified"));
  53. }
  54. # Must be a legal filename, and not absolute.
  55. if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
  56. $destfile=~/^\//) {
  57. error(sprintf(gettext("illegal name")));
  58. }
  59. # Must not be a known source file.
  60. if ($src ne $dest && exists $pagesources{$dest}) {
  61. error(sprintf(gettext("%s already exists"),
  62. htmllink("", "", $dest, noimageinline => 1)));
  63. }
  64. # Must not exist on disk already.
  65. if (-l "$config{srcdir}/$destfile" || -e _) {
  66. error(sprintf(gettext("%s already exists on disk"), $destfile));
  67. }
  68. # Must be editable.
  69. IkiWiki::check_canedit($dest, $q, $session);
  70. if ($attachment) {
  71. # Note that $srcfile is used here, not $destfile,
  72. # because it wants the current file, to check it.
  73. IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
  74. }
  75. }
  76. } #}}}
  77. sub rename_form ($$$) { #{{{
  78. my $q=shift;
  79. my $session=shift;
  80. my $page=shift;
  81. eval q{use CGI::FormBuilder};
  82. error($@) if $@;
  83. my $f = CGI::FormBuilder->new(
  84. name => "rename",
  85. title => sprintf(gettext("rename %s"), pagetitle($page)),
  86. header => 0,
  87. charset => "utf-8",
  88. method => 'POST',
  89. javascript => 0,
  90. params => $q,
  91. action => $config{cgiurl},
  92. stylesheet => IkiWiki::baseurl()."style.css",
  93. fields => [qw{do page new_name attachment}],
  94. );
  95. $f->field(name => "do", type => "hidden", value => "rename", force => 1);
  96. $f->field(name => "page", type => "hidden", value => $page, force => 1);
  97. $f->field(name => "new_name", value => pagetitle($page), size => 60);
  98. if (!$q->param("attachment")) {
  99. # insert the standard extensions
  100. my @page_types;
  101. if (exists $IkiWiki::hooks{htmlize}) {
  102. @page_types=grep { !/^_/ }
  103. keys %{$IkiWiki::hooks{htmlize}};
  104. }
  105. # make sure the current extension is in the list
  106. my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
  107. if (! $IkiWiki::hooks{htmlize}{$ext}) {
  108. unshift(@page_types, $ext);
  109. }
  110. $f->field(name => "type", type => 'select',
  111. options => \@page_types,
  112. value => $ext, force => 1);
  113. foreach my $p (keys %pagesources) {
  114. if ($pagesources{$p}=~m/^\Q$page\E\//) {
  115. $f->field(name => "subpages",
  116. label => "",
  117. type => "checkbox",
  118. options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
  119. value => 1,
  120. force => 1);
  121. last;
  122. }
  123. }
  124. }
  125. $f->field(name => "attachment", type => "hidden");
  126. return $f, ["Rename", "Cancel"];
  127. } #}}}
  128. sub rename_start ($$$$) { #{{{
  129. my $q=shift;
  130. my $session=shift;
  131. my $attachment=shift;
  132. my $page=shift;
  133. check_canrename($page, $pagesources{$page}, undef, undef,
  134. $q, $session);
  135. # Save current form state to allow returning to it later
  136. # without losing any edits.
  137. # (But don't save what button was submitted, to avoid
  138. # looping back to here.)
  139. # Note: "_submit" is CGI::FormBuilder internals.
  140. $q->param(-name => "_submit", -value => "");
  141. $session->param(postrename => scalar $q->Vars);
  142. IkiWiki::cgi_savesession($session);
  143. if (defined $attachment) {
  144. $q->param(-name => "attachment", -value => $attachment);
  145. }
  146. my ($f, $buttons)=rename_form($q, $session, $page);
  147. IkiWiki::showform($f, $buttons, $session, $q);
  148. exit 0;
  149. } #}}}
  150. sub postrename ($;$$$) { #{{{
  151. my $session=shift;
  152. my $src=shift;
  153. my $dest=shift;
  154. my $attachment=shift;
  155. # Load saved form state and return to edit page.
  156. my $postrename=CGI->new($session->param("postrename"));
  157. $session->clear("postrename");
  158. IkiWiki::cgi_savesession($session);
  159. if (defined $dest) {
  160. if (! $attachment) {
  161. # They renamed the page they were editing. This requires
  162. # fixups to the edit form state.
  163. # Tweak the edit form to be editing the new page.
  164. $postrename->param("page", $dest);
  165. }
  166. # Update edit form content to fix any links present
  167. # on it.
  168. $postrename->param("editcontent",
  169. renamepage_hook($dest, $src, $dest,
  170. $postrename->param("editcontent")));
  171. # Get a new edit token; old was likely invalidated.
  172. $postrename->param("rcsinfo",
  173. IkiWiki::rcs_prepedit($pagesources{$dest}));
  174. }
  175. IkiWiki::cgi_editpage($postrename, $session);
  176. } #}}}
  177. sub formbuilder (@) { #{{{
  178. my %params=@_;
  179. my $form=$params{form};
  180. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  181. $form->field("do") eq "create")) {
  182. my $q=$params{cgi};
  183. my $session=$params{session};
  184. if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
  185. rename_start($q, $session, 0, $form->field("page"));
  186. }
  187. elsif ($form->submitted eq "Rename Attachment") {
  188. my @selected=$q->param("attachment_select");
  189. if (@selected > 1) {
  190. error(gettext("Only one attachment can be renamed at a time."));
  191. }
  192. elsif (! @selected) {
  193. error(gettext("Please select the attachment to rename."))
  194. }
  195. rename_start($q, $session, 1, $selected[0]);
  196. }
  197. }
  198. } #}}}
  199. my $renamesummary;
  200. sub formbuilder_setup (@) { #{{{
  201. my %params=@_;
  202. my $form=$params{form};
  203. my $q=$params{cgi};
  204. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  205. $form->field("do") eq "create")) {
  206. # Rename button for the page, and also for attachments.
  207. push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
  208. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  209. if (defined $renamesummary) {
  210. $form->tmpl_param(message => $renamesummary);
  211. }
  212. }
  213. } #}}}
  214. sub sessioncgi ($$) { #{{{
  215. my $q=shift;
  216. if ($q->param("do") eq 'rename') {
  217. my $session=shift;
  218. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  219. IkiWiki::decode_form_utf8($form);
  220. if ($form->submitted eq 'Cancel') {
  221. postrename($session);
  222. }
  223. elsif ($form->submitted eq 'Rename' && $form->validate) {
  224. # Queue of rename actions to perfom.
  225. my @torename;
  226. # These untaints are safe because of the checks
  227. # performed in check_canrename later.
  228. my $src=$q->param("page");
  229. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  230. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
  231. my $destfile=$dest;
  232. if (! $q->param("attachment")) {
  233. my $type=$q->param('type');
  234. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  235. $type=IkiWiki::possibly_foolish_untaint($type);
  236. }
  237. else {
  238. my ($ext)=$srcfile=~/\.([^.]+)$/;
  239. $type=$ext;
  240. }
  241. $destfile=newpagefile($dest, $type);
  242. }
  243. push @torename, {
  244. src => $src,
  245. srcfile => $srcfile,
  246. dest => $dest,
  247. destfile => $destfile,
  248. required => 1,
  249. };
  250. # See if any subpages need to be renamed.
  251. if ($q->param("subpages") && $src ne $dest) {
  252. foreach my $p (keys %pagesources) {
  253. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  254. # If indexpages is enabled, the
  255. # srcfile should not be confused
  256. # with a subpage.
  257. next if $pagesources{$p} eq $srcfile;
  258. my $d=$pagesources{$p};
  259. $d=~s/^\Q$src\E\//$dest\//;
  260. push @torename, {
  261. src => $p,
  262. srcfile => $pagesources{$p},
  263. dest => pagename($d),
  264. destfile => $d,
  265. required => 0,
  266. };
  267. }
  268. }
  269. require IkiWiki::Render;
  270. IkiWiki::disable_commit_hook() if $config{rcs};
  271. my %origpagesources=%pagesources;
  272. # First file renaming.
  273. foreach my $rename (@torename) {
  274. if ($rename->{required}) {
  275. do_rename($rename, $q, $session);
  276. }
  277. else {
  278. eval {do_rename($rename, $q, $session)};
  279. if ($@) {
  280. $rename->{error}=$@;
  281. next;
  282. }
  283. }
  284. # Temporarily tweak pagesources to point to
  285. # the renamed file, in case fixlinks needs
  286. # to edit it.
  287. $pagesources{$rename->{src}}=$rename->{destfile};
  288. }
  289. IkiWiki::rcs_commit_staged(
  290. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  291. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  292. # Then link fixups.
  293. foreach my $rename (@torename) {
  294. next if $rename->{src} eq $rename->{dest};
  295. next if $rename->{error};
  296. foreach my $p (fixlinks($rename, $session)) {
  297. # map old page names to new
  298. foreach my $r (@torename) {
  299. next if $rename->{error};
  300. if ($r->{src} eq $p) {
  301. $p=$r->{dest};
  302. last;
  303. }
  304. }
  305. push @{$rename->{fixedlinks}}, $p;
  306. }
  307. }
  308. # Then refresh.
  309. %pagesources=%origpagesources;
  310. if ($config{rcs}) {
  311. IkiWiki::enable_commit_hook();
  312. IkiWiki::rcs_update();
  313. }
  314. IkiWiki::refresh();
  315. IkiWiki::saveindex();
  316. # Find pages with remaining, broken links.
  317. foreach my $rename (@torename) {
  318. next if $rename->{src} eq $rename->{dest};
  319. foreach my $page (keys %links) {
  320. my $broken=0;
  321. foreach my $link (@{$links{$page}}) {
  322. my $bestlink=bestlink($page, $link);
  323. if ($bestlink eq $rename->{src}) {
  324. push @{$rename->{brokenlinks}}, $page;
  325. last;
  326. }
  327. }
  328. }
  329. }
  330. # Generate a summary, that will be shown at the top
  331. # of the edit template.
  332. $renamesummary="";
  333. foreach my $rename (@torename) {
  334. my $template=template("renamesummary.tmpl");
  335. $template->param(src => $rename->{srcfile});
  336. $template->param(dest => $rename->{destfile});
  337. $template->param(error => $rename->{error});
  338. if ($rename->{src} ne $rename->{dest}) {
  339. $template->param(brokenlinks_checked => 1);
  340. $template->param(brokenlinks => [
  341. map {
  342. {
  343. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  344. noimageinline => 1)
  345. }
  346. } @{$rename->{brokenlinks}}
  347. ]);
  348. $template->param(fixedlinks => [
  349. map {
  350. {
  351. page => htmllink($rename->{dest}, $rename->{dest}, $_,
  352. noimageinline => 1)
  353. }
  354. } @{$rename->{fixedlinks}}
  355. ]);
  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. sub renamepage_hook ($$$$) { #{{{
  368. my ($page, $src, $dest, $content)=@_;
  369. IkiWiki::run_hooks(renamepage => sub {
  370. $content=shift->(
  371. page => $page,
  372. oldpage => $src,
  373. newpage => $dest,
  374. content => $content,
  375. );
  376. });
  377. return $content;
  378. }# }}}
  379. sub do_rename ($$$) { #{{{
  380. my $rename=shift;
  381. my $q=shift;
  382. my $session=shift;
  383. # First, check if this rename is allowed.
  384. check_canrename($rename->{src},
  385. $rename->{srcfile},
  386. $rename->{dest},
  387. $rename->{destfile},
  388. $q, $session);
  389. # Ensure that the dest directory exists and is ok.
  390. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  391. if ($config{rcs}) {
  392. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  393. }
  394. else {
  395. if (! rename($config{srcdir}."/".$rename->{srcfile},
  396. $config{srcdir}."/".$rename->{destfile})) {
  397. error("rename: $!");
  398. }
  399. }
  400. } # }}}
  401. sub fixlinks ($$$) { #{{{
  402. my $rename=shift;
  403. my $session=shift;
  404. my @fixedlinks;
  405. foreach my $page (keys %links) {
  406. my $needfix=0;
  407. foreach my $link (@{$links{$page}}) {
  408. my $bestlink=bestlink($page, $link);
  409. if ($bestlink eq $rename->{src}) {
  410. $needfix=1;
  411. last;
  412. }
  413. }
  414. if ($needfix) {
  415. my $file=$pagesources{$page};
  416. my $oldcontent=readfile($config{srcdir}."/".$file);
  417. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  418. if ($oldcontent ne $content) {
  419. my $token=IkiWiki::rcs_prepedit($file);
  420. eval { writefile($file, $config{srcdir}, $content) };
  421. next if $@;
  422. my $conflict=IkiWiki::rcs_commit(
  423. $file,
  424. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  425. $token,
  426. $session->param("name"),
  427. $ENV{REMOTE_ADDR}
  428. );
  429. push @fixedlinks, $page if ! defined $conflict;
  430. }
  431. }
  432. }
  433. return @fixedlinks;
  434. } #}}}
  435. 1