summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/editpage.pm
blob: ed994306f55251b561acf61459fcdc06fef8b994 (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. );
  97. decode_form_utf8($form);
  98. run_hooks(formbuilder_setup => sub {
  99. shift->(form => $form, cgi => $q, session => $session,
  100. buttons => \@buttons);
  101. });
  102. decode_form_utf8($form);
  103. # This untaint is safe because we check file_pruned and
  104. # wiki_file_regexp.
  105. my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
  106. $page=possibly_foolish_untaint($page);
  107. my $absolute=($page =~ s#^/+##);
  108. if (! defined $page || ! length $page ||
  109. file_pruned($page, $config{srcdir})) {
  110. error(gettext("bad page name"));
  111. }
  112. my $baseurl = urlto($page, undef, 1);
  113. my $from;
  114. if (defined $form->field('from')) {
  115. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  116. }
  117. my $file;
  118. my $type;
  119. if (exists $pagesources{$page} && $form->field("do") ne "create") {
  120. $file=$pagesources{$page};
  121. $type=pagetype($file);
  122. if (! defined $type || $type=~/^_/) {
  123. error(sprintf(gettext("%s is not an editable page"), $page));
  124. }
  125. if (! $form->submitted) {
  126. $form->field(name => "rcsinfo",
  127. value => rcs_prepedit($file), force => 1);
  128. }
  129. $form->field(name => "editcontent", validate => '/.*/');
  130. }
  131. else {
  132. $type=$form->param('type');
  133. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  134. $type=possibly_foolish_untaint($type);
  135. }
  136. elsif (defined $from && exists $pagesources{$from}) {
  137. # favor the type of linking page
  138. $type=pagetype($pagesources{$from});
  139. }
  140. $type=$config{default_pageext} unless defined $type;
  141. $file=newpagefile($page, $type);
  142. if (! $form->submitted) {
  143. $form->field(name => "rcsinfo", value => "", force => 1);
  144. }
  145. $form->field(name => "editcontent", validate => '/.+/');
  146. }
  147. $form->field(name => "do", type => 'hidden');
  148. $form->field(name => "sid", type => "hidden", value => $session->id,
  149. force => 1);
  150. $form->field(name => "from", type => 'hidden');
  151. $form->field(name => "rcsinfo", type => 'hidden');
  152. $form->field(name => "subpage", type => 'hidden');
  153. $form->field(name => "page", value => $page, force => 1);
  154. $form->field(name => "type", value => $type, force => 1);
  155. $form->field(name => "comments", type => "text", size => 80);
  156. $form->field(name => "editcontent", type => "textarea", rows => 20,
  157. cols => 80);
  158. $form->tmpl_param("can_commit", $config{rcs});
  159. $form->tmpl_param("indexlink", indexlink());
  160. $form->tmpl_param("helponformattinglink",
  161. htmllink($page, $page, "ikiwiki/formatting",
  162. noimageinline => 1,
  163. linktext => "FormattingHelp"));
  164. if ($form->submitted eq "Cancel") {
  165. if ($form->field("do") eq "create" && defined $from) {
  166. redirect($q, urlto($from, undef, 1));
  167. }
  168. elsif ($form->field("do") eq "create") {
  169. redirect($q, $config{url});
  170. }
  171. else {
  172. redirect($q, urlto($page, undef, 1));
  173. }
  174. exit;
  175. }
  176. elsif ($form->submitted eq "Preview") {
  177. my $new=not exists $pagesources{$page};
  178. if ($new) {
  179. # temporarily record its type
  180. $pagesources{$page}=$page.".".$type;
  181. }
  182. my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
  183. my $content=$form->field('editcontent');
  184. run_hooks(editcontent => sub {
  185. $content=shift->(
  186. content => $content,
  187. page => $page,
  188. cgi => $q,
  189. session => $session,
  190. );
  191. });
  192. my $preview=htmlize($page, $page, $type,
  193. linkify($page, $page,
  194. preprocess($page, $page,
  195. filter($page, $page, $content), 0, 1)));
  196. run_hooks(format => sub {
  197. $preview=shift->(
  198. page => $page,
  199. content => $preview,
  200. );
  201. });
  202. $form->tmpl_param("page_preview", $preview);
  203. if ($new) {
  204. delete $pagesources{$page};
  205. }
  206. # Previewing may have created files on disk.
  207. # Keep a list of these to be deleted later.
  208. my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
  209. foreach my $f (@{$renderedfiles{$page}}) {
  210. $previews{$f}=1 unless $wasrendered{$f};
  211. }
  212. @{$wikistate{editpage}{previews}} = keys %previews;
  213. $renderedfiles{$page}=[keys %wasrendered];
  214. saveindex();
  215. }
  216. elsif ($form->submitted eq "Save Page") {
  217. $form->tmpl_param("page_preview", "");
  218. }
  219. if ($form->submitted ne "Save Page" || ! $form->validate) {
  220. if ($form->field("do") eq "create") {
  221. my @page_locs;
  222. my $best_loc;
  223. if (! defined $from || ! length $from ||
  224. $from ne $form->field('from') ||
  225. file_pruned($from, $config{srcdir}) ||
  226. $from=~/^\// ||
  227. $absolute ||
  228. $form->submitted) {
  229. @page_locs=$best_loc=$page;
  230. }
  231. else {
  232. my $dir=$from."/";
  233. $dir=~s![^/]+/+$!!;
  234. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  235. $page eq gettext('discussion')) {
  236. $best_loc="$from/$page";
  237. }
  238. else {
  239. $best_loc=$dir.$page;
  240. }
  241. push @page_locs, $dir.$page;
  242. push @page_locs, "$from/$page";
  243. while (length $dir) {
  244. $dir=~s![^/]+/+$!!;
  245. push @page_locs, $dir.$page;
  246. }
  247. push @page_locs, "$config{userdir}/$page"
  248. if length $config{userdir};
  249. }
  250. @page_locs = grep {
  251. ! exists $pagecase{lc $_}
  252. } @page_locs;
  253. if (! @page_locs) {
  254. # hmm, someone else made the page in the
  255. # meantime?
  256. if ($form->submitted eq "Preview") {
  257. # let them go ahead with the edit
  258. # and resolve the conflict at save
  259. # time
  260. @page_locs=$page;
  261. }
  262. else {
  263. redirect($q, urlto($page, undef, 1));
  264. exit;
  265. }
  266. }
  267. my @editable_locs = grep {
  268. check_canedit($_, $q, $session, 1)
  269. } @page_locs;
  270. if (! @editable_locs) {
  271. # let it throw an error this time
  272. map { check_canedit($_, $q, $session) } @page_locs;
  273. }
  274. my @page_types;
  275. if (exists $hooks{htmlize}) {
  276. @page_types=grep { !/^_/ }
  277. keys %{$hooks{htmlize}};
  278. }
  279. $form->tmpl_param("page_select", 1);
  280. $form->field(name => "page", type => 'select',
  281. options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
  282. value => $best_loc);
  283. $form->field(name => "type", type => 'select',
  284. options => \@page_types);
  285. $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
  286. }
  287. elsif ($form->field("do") eq "edit") {
  288. check_canedit($page, $q, $session);
  289. if (! defined $form->field('editcontent') ||
  290. ! length $form->field('editcontent')) {
  291. my $content="";
  292. if (exists $pagesources{$page}) {
  293. $content=readfile(srcfile($pagesources{$page}));
  294. $content=~s/\n/\r\n/g;
  295. }
  296. $form->field(name => "editcontent", value => $content,
  297. force => 1);
  298. }
  299. $form->tmpl_param("page_select", 0);
  300. $form->field(name => "page", type => 'hidden');
  301. $form->field(name => "type", type => 'hidden');
  302. $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
  303. }
  304. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  305. }
  306. else {
  307. # save page
  308. check_canedit($page, $q, $session);
  309. checksessionexpiry($q, $session, $q->param('sid'));
  310. my $exists=-e "$config{srcdir}/$file";
  311. if ($form->field("do") ne "create" && ! $exists &&
  312. ! defined srcfile($file, 1)) {
  313. $form->tmpl_param("message", template("editpagegone.tmpl")->output);
  314. $form->field(name => "do", value => "create", force => 1);
  315. $form->tmpl_param("page_select", 0);
  316. $form->field(name => "page", type => 'hidden');
  317. $form->field(name => "type", type => 'hidden');
  318. $form->title(sprintf(gettext("editing %s"), $page));
  319. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  320. exit;
  321. }
  322. elsif ($form->field("do") eq "create" && $exists) {
  323. $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
  324. $form->field(name => "do", value => "edit", force => 1);
  325. $form->tmpl_param("page_select", 0);
  326. $form->field(name => "page", type => 'hidden');
  327. $form->field(name => "type", type => 'hidden');
  328. $form->title(sprintf(gettext("editing %s"), $page));
  329. $form->field("editcontent",
  330. value => readfile("$config{srcdir}/$file").
  331. "\n\n\n".$form->field("editcontent"),
  332. force => 1);
  333. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  334. exit;
  335. }
  336. my $content=$form->field('editcontent');
  337. run_hooks(editcontent => sub {
  338. $content=shift->(
  339. content => $content,
  340. page => $page,
  341. cgi => $q,
  342. session => $session,
  343. );
  344. });
  345. $content=~s/\r\n/\n/g;
  346. $content=~s/\r/\n/g;
  347. $content.="\n" if $content !~ /\n$/;
  348. $config{cgi}=0; # avoid cgi error message
  349. eval { writefile($file, $config{srcdir}, $content) };
  350. $config{cgi}=1;
  351. if ($@) {
  352. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  353. force => 1);
  354. my $mtemplate=template("editfailedsave.tmpl");
  355. $mtemplate->param(error_message => $@);
  356. $form->tmpl_param("message", $mtemplate->output);
  357. $form->field("editcontent", value => $content, force => 1);
  358. $form->tmpl_param("page_select", 0);
  359. $form->field(name => "page", type => 'hidden');
  360. $form->field(name => "type", type => 'hidden');
  361. $form->title(sprintf(gettext("editing %s"), $page));
  362. showform($form, \@buttons, $session, $q,
  363. forcebaseurl => $baseurl);
  364. exit;
  365. }
  366. my $conflict;
  367. if ($config{rcs}) {
  368. my $message="";
  369. if (defined $form->field('comments') &&
  370. length $form->field('comments')) {
  371. $message=$form->field('comments');
  372. }
  373. if (! $exists) {
  374. rcs_add($file);
  375. }
  376. # Prevent deadlock with post-commit hook by
  377. # signaling to it that it should not try to
  378. # do anything.
  379. disable_commit_hook();
  380. $conflict=rcs_commit($file, $message,
  381. $form->field("rcsinfo"),
  382. $session->param("name"), $ENV{REMOTE_ADDR});
  383. enable_commit_hook();
  384. rcs_update();
  385. }
  386. # Refresh even if there was a conflict, since other changes
  387. # may have been committed while the post-commit hook was
  388. # disabled.
  389. require IkiWiki::Render;
  390. refresh();
  391. saveindex();
  392. if (defined $conflict) {
  393. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  394. force => 1);
  395. $form->tmpl_param("message", template("editconflict.tmpl")->output);
  396. $form->field("editcontent", value => $conflict, force => 1);
  397. $form->field("do", "edit", force => 1);
  398. $form->tmpl_param("page_select", 0);
  399. $form->field(name => "page", type => 'hidden');
  400. $form->field(name => "type", type => 'hidden');
  401. $form->title(sprintf(gettext("editing %s"), $page));
  402. showform($form, \@buttons, $session, $q,
  403. forcebaseurl => $baseurl);
  404. }
  405. else {
  406. # The trailing question mark tries to avoid broken
  407. # caches and get the most recent version of the page.
  408. redirect($q, urlto($page, undef, 1)."?updated");
  409. }
  410. }
  411. exit;
  412. }
  413. 1