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