summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 41af3ca4d61b2495dc0e69e77da6513a2de07373 (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. }
  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, 1), 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 => linklist($rename->{dest}, $rename->{brokenlinks}));
  341. $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
  342. }
  343. $renamesummary.=$template->output;
  344. }
  345. postrename($session, $src, $dest, $q->param("attachment"));
  346. }
  347. else {
  348. IkiWiki::showform($form, $buttons, $session, $q);
  349. }
  350. exit 0;
  351. }
  352. }
  353. sub linklist {
  354. # generates a list of links in a form suitable for FormBuilder
  355. my $dest=shift;
  356. my $list=shift;
  357. # converts a list of pages into a list of links
  358. # in a form suitable for FormBuilder.
  359. [map {
  360. {
  361. page => htmllink($dest, $dest, $_,
  362. noimageinline => 1,
  363. linktext => pagetitle($_),
  364. )
  365. }
  366. } @{$list}]
  367. }
  368. sub renamepage_hook ($$$$) {
  369. my ($page, $src, $dest, $content)=@_;
  370. IkiWiki::run_hooks(renamepage => sub {
  371. $content=shift->(
  372. page => $page,
  373. oldpage => $src,
  374. newpage => $dest,
  375. content => $content,
  376. );
  377. });
  378. return $content;
  379. }
  380. sub do_rename ($$$) {
  381. my $rename=shift;
  382. my $q=shift;
  383. my $session=shift;
  384. # First, check if this rename is allowed.
  385. check_canrename($rename->{src},
  386. $rename->{srcfile},
  387. $rename->{dest},
  388. $rename->{destfile},
  389. $q, $session);
  390. # Ensure that the dest directory exists and is ok.
  391. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  392. if ($config{rcs}) {
  393. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  394. }
  395. else {
  396. if (! rename($config{srcdir}."/".$rename->{srcfile},
  397. $config{srcdir}."/".$rename->{destfile})) {
  398. error("rename: $!");
  399. }
  400. }
  401. }
  402. sub fixlinks ($$$) {
  403. my $rename=shift;
  404. my $session=shift;
  405. my @fixedlinks;
  406. foreach my $page (keys %links) {
  407. my $needfix=0;
  408. foreach my $link (@{$links{$page}}) {
  409. my $bestlink=bestlink($page, $link);
  410. if ($bestlink eq $rename->{src}) {
  411. $needfix=1;
  412. last;
  413. }
  414. }
  415. if ($needfix) {
  416. my $file=$pagesources{$page};
  417. my $oldcontent=readfile($config{srcdir}."/".$file);
  418. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  419. if ($oldcontent ne $content) {
  420. my $token=IkiWiki::rcs_prepedit($file);
  421. eval { writefile($file, $config{srcdir}, $content) };
  422. next if $@;
  423. my $conflict=IkiWiki::rcs_commit(
  424. $file,
  425. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  426. $token,
  427. $session->param("name"),
  428. $ENV{REMOTE_ADDR}
  429. );
  430. push @fixedlinks, $page if ! defined $conflict;
  431. }
  432. }
  433. }
  434. return @fixedlinks;
  435. }
  436. 1