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