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