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