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