summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rename.pm
blob: 0b6e74705b29e86330395344140d7b6390db7cf2 (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. foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
  103. push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
  104. }
  105. }
  106. @page_types=sort @page_types;
  107. # make sure the current extension is in the list
  108. my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
  109. if (! $IkiWiki::hooks{htmlize}{$ext}) {
  110. unshift(@page_types, [$ext, $ext]);
  111. }
  112. $f->field(name => "type", type => 'select',
  113. options => \@page_types,
  114. value => $ext, force => 1);
  115. foreach my $p (keys %pagesources) {
  116. if ($pagesources{$p}=~m/^\Q$page\E\//) {
  117. $f->field(name => "subpages",
  118. label => "",
  119. type => "checkbox",
  120. options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
  121. value => 1,
  122. force => 1);
  123. last;
  124. }
  125. }
  126. }
  127. $f->field(name => "attachment", type => "hidden");
  128. return $f, ["Rename", "Cancel"];
  129. }
  130. sub rename_start ($$$$) {
  131. my $q=shift;
  132. my $session=shift;
  133. my $attachment=shift;
  134. my $page=shift;
  135. check_canrename($page, $pagesources{$page}, undef, undef,
  136. $q, $session);
  137. # Save current form state to allow returning to it later
  138. # without losing any edits.
  139. # (But don't save what button was submitted, to avoid
  140. # looping back to here.)
  141. # Note: "_submit" is CGI::FormBuilder internals.
  142. $q->param(-name => "_submit", -value => "");
  143. $session->param(postrename => scalar $q->Vars);
  144. IkiWiki::cgi_savesession($session);
  145. if (defined $attachment) {
  146. $q->param(-name => "attachment", -value => $attachment);
  147. }
  148. my ($f, $buttons)=rename_form($q, $session, $page);
  149. IkiWiki::showform($f, $buttons, $session, $q);
  150. exit 0;
  151. }
  152. sub postrename ($;$$$) {
  153. my $session=shift;
  154. my $src=shift;
  155. my $dest=shift;
  156. my $attachment=shift;
  157. # Load saved form state and return to edit page.
  158. my $postrename=CGI->new($session->param("postrename"));
  159. $session->clear("postrename");
  160. IkiWiki::cgi_savesession($session);
  161. if (defined $dest) {
  162. if (! $attachment) {
  163. # They renamed the page they were editing. This requires
  164. # fixups to the edit form state.
  165. # Tweak the edit form to be editing the new page.
  166. $postrename->param("page", $dest);
  167. }
  168. # Update edit form content to fix any links present
  169. # on it.
  170. $postrename->param("editcontent",
  171. renamepage_hook($dest, $src, $dest,
  172. $postrename->param("editcontent")));
  173. # Get a new edit token; old was likely invalidated.
  174. $postrename->param("rcsinfo",
  175. IkiWiki::rcs_prepedit($pagesources{$dest}));
  176. }
  177. IkiWiki::cgi_editpage($postrename, $session);
  178. }
  179. sub formbuilder (@) {
  180. my %params=@_;
  181. my $form=$params{form};
  182. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  183. $form->field("do") eq "create")) {
  184. my $q=$params{cgi};
  185. my $session=$params{session};
  186. if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
  187. rename_start($q, $session, 0, $form->field("page"));
  188. }
  189. elsif ($form->submitted eq "Rename Attachment") {
  190. my @selected=$q->param("attachment_select");
  191. if (@selected > 1) {
  192. error(gettext("Only one attachment can be renamed at a time."));
  193. }
  194. elsif (! @selected) {
  195. error(gettext("Please select the attachment to rename."))
  196. }
  197. rename_start($q, $session, 1, $selected[0]);
  198. }
  199. }
  200. }
  201. my $renamesummary;
  202. sub formbuilder_setup (@) {
  203. my %params=@_;
  204. my $form=$params{form};
  205. my $q=$params{cgi};
  206. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  207. $form->field("do") eq "create")) {
  208. # Rename button for the page, and also for attachments.
  209. push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
  210. $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
  211. if (defined $renamesummary) {
  212. $form->tmpl_param(message => $renamesummary);
  213. }
  214. }
  215. }
  216. sub sessioncgi ($$) {
  217. my $q=shift;
  218. if ($q->param("do") eq 'rename') {
  219. my $session=shift;
  220. my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
  221. IkiWiki::decode_form_utf8($form);
  222. if ($form->submitted eq 'Cancel') {
  223. postrename($session);
  224. }
  225. elsif ($form->submitted eq 'Rename' && $form->validate) {
  226. # Queue of rename actions to perfom.
  227. my @torename;
  228. # These untaints are safe because of the checks
  229. # performed in check_canrename later.
  230. my $src=$q->param("page");
  231. my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
  232. my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
  233. my $destfile=$dest;
  234. if (! $q->param("attachment")) {
  235. my $type=$q->param('type');
  236. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  237. $type=IkiWiki::possibly_foolish_untaint($type);
  238. }
  239. else {
  240. my ($ext)=$srcfile=~/\.([^.]+)$/;
  241. $type=$ext;
  242. }
  243. $destfile=newpagefile($dest, $type);
  244. }
  245. push @torename, {
  246. src => $src,
  247. srcfile => $srcfile,
  248. dest => $dest,
  249. destfile => $destfile,
  250. required => 1,
  251. };
  252. # See if any subpages need to be renamed.
  253. if ($q->param("subpages") && $src ne $dest) {
  254. foreach my $p (keys %pagesources) {
  255. next unless $pagesources{$p}=~m/^\Q$src\E\//;
  256. # If indexpages is enabled, the
  257. # srcfile should not be confused
  258. # with a subpage.
  259. next if $pagesources{$p} eq $srcfile;
  260. my $d=$pagesources{$p};
  261. $d=~s/^\Q$src\E\//$dest\//;
  262. push @torename, {
  263. src => $p,
  264. srcfile => $pagesources{$p},
  265. dest => pagename($d),
  266. destfile => $d,
  267. required => 0,
  268. };
  269. }
  270. }
  271. require IkiWiki::Render;
  272. IkiWiki::disable_commit_hook() if $config{rcs};
  273. my %origpagesources=%pagesources;
  274. # First file renaming.
  275. foreach my $rename (@torename) {
  276. if ($rename->{required}) {
  277. do_rename($rename, $q, $session);
  278. }
  279. else {
  280. eval {do_rename($rename, $q, $session)};
  281. if ($@) {
  282. $rename->{error}=$@;
  283. next;
  284. }
  285. }
  286. # Temporarily tweak pagesources to point to
  287. # the renamed file, in case fixlinks needs
  288. # to edit it.
  289. $pagesources{$rename->{src}}=$rename->{destfile};
  290. }
  291. IkiWiki::rcs_commit_staged(
  292. sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
  293. $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
  294. # Then link fixups.
  295. foreach my $rename (@torename) {
  296. next if $rename->{src} eq $rename->{dest};
  297. next if $rename->{error};
  298. foreach my $p (fixlinks($rename, $session)) {
  299. # map old page names to new
  300. foreach my $r (@torename) {
  301. next if $rename->{error};
  302. if ($r->{src} eq $p) {
  303. $p=$r->{dest};
  304. last;
  305. }
  306. }
  307. push @{$rename->{fixedlinks}}, $p;
  308. }
  309. }
  310. # Then refresh.
  311. %pagesources=%origpagesources;
  312. if ($config{rcs}) {
  313. IkiWiki::enable_commit_hook();
  314. IkiWiki::rcs_update();
  315. }
  316. IkiWiki::refresh();
  317. IkiWiki::saveindex();
  318. # Find pages with remaining, broken links.
  319. foreach my $rename (@torename) {
  320. next if $rename->{src} eq $rename->{dest};
  321. foreach my $page (keys %links) {
  322. my $broken=0;
  323. foreach my $link (@{$links{$page}}) {
  324. my $bestlink=bestlink($page, $link);
  325. if ($bestlink eq $rename->{src}) {
  326. push @{$rename->{brokenlinks}}, $page;
  327. last;
  328. }
  329. }
  330. }
  331. }
  332. # Generate a summary, that will be shown at the top
  333. # of the edit template.
  334. $renamesummary="";
  335. foreach my $rename (@torename) {
  336. my $template=template("renamesummary.tmpl");
  337. $template->param(src => $rename->{srcfile});
  338. $template->param(dest => $rename->{destfile});
  339. $template->param(error => $rename->{error});
  340. if ($rename->{src} ne $rename->{dest}) {
  341. $template->param(brokenlinks_checked => 1);
  342. $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
  343. $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
  344. }
  345. $renamesummary.=$template->output;
  346. }
  347. postrename($session, $src, $dest, $q->param("attachment"));
  348. }
  349. else {
  350. IkiWiki::showform($form, $buttons, $session, $q);
  351. }
  352. exit 0;
  353. }
  354. }
  355. sub linklist {
  356. # generates a list of links in a form suitable for FormBuilder
  357. my $dest=shift;
  358. my $list=shift;
  359. # converts a list of pages into a list of links
  360. # in a form suitable for FormBuilder.
  361. [map {
  362. {
  363. page => htmllink($dest, $dest, $_,
  364. noimageinline => 1,
  365. linktext => pagetitle($_),
  366. )
  367. }
  368. } @{$list}]
  369. }
  370. sub renamepage_hook ($$$$) {
  371. my ($page, $src, $dest, $content)=@_;
  372. IkiWiki::run_hooks(renamepage => sub {
  373. $content=shift->(
  374. page => $page,
  375. oldpage => $src,
  376. newpage => $dest,
  377. content => $content,
  378. );
  379. });
  380. return $content;
  381. }
  382. sub do_rename ($$$) {
  383. my $rename=shift;
  384. my $q=shift;
  385. my $session=shift;
  386. # First, check if this rename is allowed.
  387. check_canrename($rename->{src},
  388. $rename->{srcfile},
  389. $rename->{dest},
  390. $rename->{destfile},
  391. $q, $session);
  392. # Ensure that the dest directory exists and is ok.
  393. IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
  394. if ($config{rcs}) {
  395. IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
  396. }
  397. else {
  398. if (! rename($config{srcdir}."/".$rename->{srcfile},
  399. $config{srcdir}."/".$rename->{destfile})) {
  400. error("rename: $!");
  401. }
  402. }
  403. }
  404. sub fixlinks ($$$) {
  405. my $rename=shift;
  406. my $session=shift;
  407. my @fixedlinks;
  408. foreach my $page (keys %links) {
  409. my $needfix=0;
  410. foreach my $link (@{$links{$page}}) {
  411. my $bestlink=bestlink($page, $link);
  412. if ($bestlink eq $rename->{src}) {
  413. $needfix=1;
  414. last;
  415. }
  416. }
  417. if ($needfix) {
  418. my $file=$pagesources{$page};
  419. my $oldcontent=readfile($config{srcdir}."/".$file);
  420. my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
  421. if ($oldcontent ne $content) {
  422. my $token=IkiWiki::rcs_prepedit($file);
  423. eval { writefile($file, $config{srcdir}, $content) };
  424. next if $@;
  425. my $conflict=IkiWiki::rcs_commit(
  426. $file,
  427. sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
  428. $token,
  429. $session->param("name"),
  430. $ENV{REMOTE_ADDR}
  431. );
  432. push @fixedlinks, $page if ! defined $conflict;
  433. }
  434. }
  435. }
  436. return @fixedlinks;
  437. }
  438. 1