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