summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/editpage.pm
blob: e4f0cdac09de50f2e3d312e3b9ad97089464d702 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::editpage;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use open qw{:utf8 :std};
  7. sub import { #{{{
  8. hook(type => "getsetup", id => "editpage", call => \&getsetup);
  9. hook(type => "refresh", id => "editpage", call => \&refresh);
  10. hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
  11. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. },
  18. } #}}}
  19. sub refresh () {
  20. if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
  21. # Expire old preview files after one hour.
  22. my $expire=time - (60 * 60);
  23. my @previews;
  24. foreach my $file (@{$wikistate{editpage}{previews}}) {
  25. my $mtime=(stat("$config{destdir}/$file"))[9];
  26. if (defined $mtime && $mtime <= $expire) {
  27. # Avoid deleting a preview that was later saved.
  28. my $delete=1;
  29. foreach my $page (keys %renderedfiles) {
  30. if (grep { $_ eq $file } @{$renderedfiles{$page}}) {
  31. $delete=0;
  32. }
  33. }
  34. if ($delete) {
  35. debug(sprintf(gettext("removing old preview %s"), $file));
  36. IkiWiki::prune("$config{destdir}/$file");
  37. }
  38. }
  39. elsif (defined $mtime) {
  40. push @previews, $file;
  41. }
  42. }
  43. $wikistate{editpage}{previews}=\@previews;
  44. }
  45. }
  46. # Back to ikiwiki namespace for the rest, this code is very much
  47. # internal to ikiwiki even though it's separated into a plugin,
  48. # and other plugins use the functions below.
  49. package IkiWiki;
  50. sub check_canedit ($$$;$) { #{{{
  51. my $page=shift;
  52. my $q=shift;
  53. my $session=shift;
  54. my $nonfatal=shift;
  55. my $canedit;
  56. run_hooks(canedit => sub {
  57. return if defined $canedit;
  58. my $ret=shift->($page, $q, $session);
  59. if (defined $ret) {
  60. if ($ret eq "") {
  61. $canedit=1;
  62. }
  63. elsif (ref $ret eq 'CODE') {
  64. $ret->() unless $nonfatal;
  65. $canedit=0;
  66. }
  67. elsif (defined $ret) {
  68. error($ret) unless $nonfatal;
  69. $canedit=0;
  70. }
  71. }
  72. });
  73. return $canedit;
  74. } #}}}
  75. sub cgi_editpage ($$) { #{{{
  76. my $q=shift;
  77. my $session=shift;
  78. my $do=$q->param('do');
  79. return unless $do eq 'create' || $do eq 'edit';
  80. decode_cgi_utf8($q);
  81. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  82. my @buttons=("Save Page", "Preview", "Cancel");
  83. eval q{use CGI::FormBuilder};
  84. error($@) if $@;
  85. my $form = CGI::FormBuilder->new(
  86. fields => \@fields,
  87. charset => "utf-8",
  88. method => 'POST',
  89. required => [qw{editcontent}],
  90. javascript => 0,
  91. params => $q,
  92. action => $config{cgiurl},
  93. header => 0,
  94. table => 0,
  95. template => scalar template_params("editpage.tmpl"),
  96. wikiname => $config{wikiname},
  97. );
  98. decode_form_utf8($form);
  99. run_hooks(formbuilder_setup => sub {
  100. shift->(form => $form, cgi => $q, session => $session,
  101. buttons => \@buttons);
  102. });
  103. decode_form_utf8($form);
  104. # This untaint is safe because we check file_pruned and
  105. # wiki_file_regexp.
  106. my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
  107. $page=possibly_foolish_untaint($page);
  108. my $absolute=($page =~ s#^/+##);
  109. if (! defined $page || ! length $page ||
  110. file_pruned($page, $config{srcdir})) {
  111. error(gettext("bad page name"));
  112. }
  113. my $baseurl = urlto($page, undef, 1);
  114. my $from;
  115. if (defined $form->field('from')) {
  116. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  117. }
  118. my $file;
  119. my $type;
  120. if (exists $pagesources{$page} && $form->field("do") ne "create") {
  121. $file=$pagesources{$page};
  122. $type=pagetype($file);
  123. if (! defined $type || $type=~/^_/) {
  124. error(sprintf(gettext("%s is not an editable page"), $page));
  125. }
  126. if (! $form->submitted) {
  127. $form->field(name => "rcsinfo",
  128. value => rcs_prepedit($file), force => 1);
  129. }
  130. $form->field(name => "editcontent", validate => '/.*/');
  131. }
  132. else {
  133. $type=$form->param('type');
  134. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  135. $type=possibly_foolish_untaint($type);
  136. }
  137. elsif (defined $from && exists $pagesources{$from}) {
  138. # favor the type of linking page
  139. $type=pagetype($pagesources{$from});
  140. }
  141. $type=$config{default_pageext} unless defined $type;
  142. $file=newpagefile($page, $type);
  143. if (! $form->submitted) {
  144. $form->field(name => "rcsinfo", value => "", force => 1);
  145. }
  146. $form->field(name => "editcontent", validate => '/.+/');
  147. }
  148. $form->field(name => "do", type => 'hidden');
  149. $form->field(name => "sid", type => "hidden", value => $session->id,
  150. force => 1);
  151. $form->field(name => "from", type => 'hidden');
  152. $form->field(name => "rcsinfo", type => 'hidden');
  153. $form->field(name => "subpage", type => 'hidden');
  154. $form->field(name => "page", value => $page, force => 1);
  155. $form->field(name => "type", value => $type, force => 1);
  156. $form->field(name => "comments", type => "text", size => 80);
  157. $form->field(name => "editcontent", type => "textarea", rows => 20,
  158. cols => 80);
  159. $form->tmpl_param("can_commit", $config{rcs});
  160. $form->tmpl_param("indexlink", indexlink());
  161. $form->tmpl_param("helponformattinglink",
  162. htmllink($page, $page, "ikiwiki/formatting",
  163. noimageinline => 1,
  164. linktext => "FormattingHelp"));
  165. if ($form->submitted eq "Cancel") {
  166. if ($form->field("do") eq "create" && defined $from) {
  167. redirect($q, urlto($from, undef, 1));
  168. }
  169. elsif ($form->field("do") eq "create") {
  170. redirect($q, $config{url});
  171. }
  172. else {
  173. redirect($q, urlto($page, undef, 1));
  174. }
  175. exit;
  176. }
  177. elsif ($form->submitted eq "Preview") {
  178. my $new=not exists $pagesources{$page};
  179. if ($new) {
  180. # temporarily record its type
  181. $pagesources{$page}=$page.".".$type;
  182. }
  183. my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
  184. my $content=$form->field('editcontent');
  185. run_hooks(editcontent => sub {
  186. $content=shift->(
  187. content => $content,
  188. page => $page,
  189. cgi => $q,
  190. session => $session,
  191. );
  192. });
  193. my $preview=htmlize($page, $page, $type,
  194. linkify($page, $page,
  195. preprocess($page, $page,
  196. filter($page, $page, $content), 0, 1)));
  197. run_hooks(format => sub {
  198. $preview=shift->(
  199. page => $page,
  200. content => $preview,
  201. );
  202. });
  203. $form->tmpl_param("page_preview", $preview);
  204. if ($new) {
  205. delete $pagesources{$page};
  206. }
  207. # Previewing may have created files on disk.
  208. # Keep a list of these to be deleted later.
  209. my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
  210. foreach my $f (@{$renderedfiles{$page}}) {
  211. $previews{$f}=1 unless $wasrendered{$f};
  212. }
  213. @{$wikistate{editpage}{previews}} = keys %previews;
  214. $renderedfiles{$page}=[keys %wasrendered];
  215. saveindex();
  216. }
  217. elsif ($form->submitted eq "Save Page") {
  218. $form->tmpl_param("page_preview", "");
  219. }
  220. if ($form->submitted ne "Save Page" || ! $form->validate) {
  221. if ($form->field("do") eq "create") {
  222. my @page_locs;
  223. my $best_loc;
  224. if (! defined $from || ! length $from ||
  225. $from ne $form->field('from') ||
  226. file_pruned($from, $config{srcdir}) ||
  227. $from=~/^\// ||
  228. $absolute ||
  229. $form->submitted) {
  230. @page_locs=$best_loc=$page;
  231. }
  232. else {
  233. my $dir=$from."/";
  234. $dir=~s![^/]+/+$!!;
  235. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  236. $page eq gettext('discussion')) {
  237. $best_loc="$from/$page";
  238. }
  239. else {
  240. $best_loc=$dir.$page;
  241. }
  242. push @page_locs, $dir.$page;
  243. push @page_locs, "$from/$page";
  244. while (length $dir) {
  245. $dir=~s![^/]+/+$!!;
  246. push @page_locs, $dir.$page;
  247. }
  248. push @page_locs, "$config{userdir}/$page"
  249. if length $config{userdir};
  250. }
  251. @page_locs = grep {
  252. ! exists $pagecase{lc $_}
  253. } @page_locs;
  254. if (! @page_locs) {
  255. # hmm, someone else made the page in the
  256. # meantime?
  257. if ($form->submitted eq "Preview") {
  258. # let them go ahead with the edit
  259. # and resolve the conflict at save
  260. # time
  261. @page_locs=$page;
  262. }
  263. else {
  264. redirect($q, urlto($page, undef, 1));
  265. exit;
  266. }
  267. }
  268. my @editable_locs = grep {
  269. check_canedit($_, $q, $session, 1)
  270. } @page_locs;
  271. if (! @editable_locs) {
  272. # let it throw an error this time
  273. map { check_canedit($_, $q, $session) } @page_locs;
  274. }
  275. my @page_types;
  276. if (exists $hooks{htmlize}) {
  277. @page_types=grep { !/^_/ }
  278. keys %{$hooks{htmlize}};
  279. }
  280. $form->tmpl_param("page_select", 1);
  281. $form->field(name => "page", type => 'select',
  282. options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
  283. value => $best_loc);
  284. $form->field(name => "type", type => 'select',
  285. options => \@page_types);
  286. $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
  287. }
  288. elsif ($form->field("do") eq "edit") {
  289. check_canedit($page, $q, $session);
  290. if (! defined $form->field('editcontent') ||
  291. ! length $form->field('editcontent')) {
  292. my $content="";
  293. if (exists $pagesources{$page}) {
  294. $content=readfile(srcfile($pagesources{$page}));
  295. $content=~s/\n/\r\n/g;
  296. }
  297. $form->field(name => "editcontent", value => $content,
  298. force => 1);
  299. }
  300. $form->tmpl_param("page_select", 0);
  301. $form->field(name => "page", type => 'hidden');
  302. $form->field(name => "type", type => 'hidden');
  303. $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
  304. }
  305. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  306. }
  307. else {
  308. # save page
  309. check_canedit($page, $q, $session);
  310. checksessionexpiry($session, $q->param('sid'));
  311. my $exists=-e "$config{srcdir}/$file";
  312. if ($form->field("do") ne "create" && ! $exists &&
  313. ! defined srcfile($file, 1)) {
  314. $form->tmpl_param("message", template("editpagegone.tmpl")->output);
  315. $form->field(name => "do", value => "create", force => 1);
  316. $form->tmpl_param("page_select", 0);
  317. $form->field(name => "page", type => 'hidden');
  318. $form->field(name => "type", type => 'hidden');
  319. $form->title(sprintf(gettext("editing %s"), $page));
  320. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  321. exit;
  322. }
  323. elsif ($form->field("do") eq "create" && $exists) {
  324. $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
  325. $form->field(name => "do", value => "edit", force => 1);
  326. $form->tmpl_param("page_select", 0);
  327. $form->field(name => "page", type => 'hidden');
  328. $form->field(name => "type", type => 'hidden');
  329. $form->title(sprintf(gettext("editing %s"), $page));
  330. $form->field("editcontent",
  331. value => readfile("$config{srcdir}/$file").
  332. "\n\n\n".$form->field("editcontent"),
  333. force => 1);
  334. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  335. exit;
  336. }
  337. my $content=$form->field('editcontent');
  338. run_hooks(editcontent => sub {
  339. $content=shift->(
  340. content => $content,
  341. page => $page,
  342. cgi => $q,
  343. session => $session,
  344. );
  345. });
  346. $content=~s/\r\n/\n/g;
  347. $content=~s/\r/\n/g;
  348. $content.="\n" if $content !~ /\n$/;
  349. $config{cgi}=0; # avoid cgi error message
  350. eval { writefile($file, $config{srcdir}, $content) };
  351. $config{cgi}=1;
  352. if ($@) {
  353. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  354. force => 1);
  355. my $mtemplate=template("editfailedsave.tmpl");
  356. $mtemplate->param(error_message => $@);
  357. $form->tmpl_param("message", $mtemplate->output);
  358. $form->field("editcontent", value => $content, force => 1);
  359. $form->tmpl_param("page_select", 0);
  360. $form->field(name => "page", type => 'hidden');
  361. $form->field(name => "type", type => 'hidden');
  362. $form->title(sprintf(gettext("editing %s"), $page));
  363. showform($form, \@buttons, $session, $q,
  364. forcebaseurl => $baseurl);
  365. exit;
  366. }
  367. my $conflict;
  368. if ($config{rcs}) {
  369. my $message="";
  370. if (defined $form->field('comments') &&
  371. length $form->field('comments')) {
  372. $message=$form->field('comments');
  373. }
  374. if (! $exists) {
  375. rcs_add($file);
  376. }
  377. # Prevent deadlock with post-commit hook by
  378. # signaling to it that it should not try to
  379. # do anything.
  380. disable_commit_hook();
  381. $conflict=rcs_commit($file, $message,
  382. $form->field("rcsinfo"),
  383. $session->param("name"), $ENV{REMOTE_ADDR});
  384. enable_commit_hook();
  385. rcs_update();
  386. }
  387. # Refresh even if there was a conflict, since other changes
  388. # may have been committed while the post-commit hook was
  389. # disabled.
  390. require IkiWiki::Render;
  391. refresh();
  392. saveindex();
  393. if (defined $conflict) {
  394. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  395. force => 1);
  396. $form->tmpl_param("message", template("editconflict.tmpl")->output);
  397. $form->field("editcontent", value => $conflict, force => 1);
  398. $form->field("do", "edit", force => 1);
  399. $form->tmpl_param("page_select", 0);
  400. $form->field(name => "page", type => 'hidden');
  401. $form->field(name => "type", type => 'hidden');
  402. $form->title(sprintf(gettext("editing %s"), $page));
  403. showform($form, \@buttons, $session, $q,
  404. forcebaseurl => $baseurl);
  405. }
  406. else {
  407. # The trailing question mark tries to avoid broken
  408. # caches and get the most recent version of the page.
  409. redirect($q, urlto($page, undef, 1)."?updated");
  410. }
  411. }
  412. exit;
  413. } #}}}
  414. 1