]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/editpage.pm
Add underlay plugin
[git.ikiwiki.info.git] / IkiWiki / Plugin / editpage.pm
index 242624d77fb89ae03bed3b6970498673a6137613..bba52e4fd322e8bbad5f63a13198ff5f491cdef5 100644 (file)
@@ -6,19 +6,19 @@ use strict;
 use IkiWiki;
 use open qw{:utf8 :std};
 
 use IkiWiki;
 use open qw{:utf8 :std};
 
-sub import { #{{{
+sub import {
        hook(type => "getsetup", id => "editpage", call => \&getsetup);
        hook(type => "refresh", id => "editpage", call => \&refresh);
         hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
        hook(type => "getsetup", id => "editpage", call => \&getsetup);
        hook(type => "refresh", id => "editpage", call => \&refresh);
         hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
-} # }}}
+}
 
 
-sub getsetup () { #{{{
+sub getsetup () {
        return
                plugin => {
                        safe => 1,
                        rebuild => 1,
                },
        return
                plugin => {
                        safe => 1,
                        rebuild => 1,
                },
-} #}}}
+}
 
 sub refresh () {
        if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
 
 sub refresh () {
        if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
@@ -54,7 +54,7 @@ sub refresh () {
 # and other plugins use the functions below.
 package IkiWiki;
 
 # and other plugins use the functions below.
 package IkiWiki;
 
-sub check_canedit ($$$;$) { #{{{
+sub check_canedit ($$$;$) {
        my $page=shift;
        my $q=shift;
        my $session=shift;
        my $page=shift;
        my $q=shift;
        my $session=shift;
@@ -78,10 +78,46 @@ sub check_canedit ($$$;$) { #{{{
                        }
                }
        });
                        }
                }
        });
-       return $canedit;
-} #}}}
+       return defined $canedit ? $canedit : 1;
+}
 
 
-sub cgi_editpage ($$) { #{{{
+sub check_content (@) {
+       my %params=@_;
+       
+       return 1 if ! exists $hooks{checkcontent}; # optimisation
+
+       if (exists $pagesources{$params{page}}) {
+               my @diff;
+               my %old=map { $_ => 1 }
+                       split("\n", readfile(srcfile($pagesources{$params{page}})));
+               foreach my $line (split("\n", $params{content})) {
+                       push @diff, $line if ! exists $old{$_};
+               }
+               $params{content}=join("\n", @diff);
+       }
+
+       my $ok;
+       run_hooks(checkcontent => sub {
+               return if defined $ok;
+               my $ret=shift->(%params);
+               if (defined $ret) {
+                       if ($ret eq "") {
+                               $ok=1;
+                       }
+                       elsif (ref $ret eq 'CODE') {
+                               $ret->();
+                               $ok=0;
+                       }
+                       elsif (defined $ret) {
+                               error($ret);
+                       }
+               }
+
+       });
+       return defined $ok ? $ok : 1;
+}
+
+sub cgi_editpage ($$) {
        my $q=shift;
        my $session=shift;
        
        my $q=shift;
        my $session=shift;
        
@@ -105,7 +141,6 @@ sub cgi_editpage ($$) { #{{{
                header => 0,
                table => 0,
                template => scalar template_params("editpage.tmpl"),
                header => 0,
                table => 0,
                template => scalar template_params("editpage.tmpl"),
-               wikiname => $config{wikiname},
        );
        
        decode_form_utf8($form);
        );
        
        decode_form_utf8($form);
@@ -369,8 +404,17 @@ sub cgi_editpage ($$) { #{{{
                        showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
                        exit;
                }
                        showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
                        exit;
                }
+                       
+               my $message="";
+               if (defined $form->field('comments') &&
+                   length $form->field('comments')) {
+                       $message=$form->field('comments');
+               }
                
                my $content=$form->field('editcontent');
                
                my $content=$form->field('editcontent');
+               check_content(content => $content, page => $page,
+                       cgi => $q, session => $session,
+                       subject => $message);
                run_hooks(editcontent => sub {
                        $content=shift->(
                                content => $content,
                run_hooks(editcontent => sub {
                        $content=shift->(
                                content => $content,
@@ -404,12 +448,6 @@ sub cgi_editpage ($$) { #{{{
                
                my $conflict;
                if ($config{rcs}) {
                
                my $conflict;
                if ($config{rcs}) {
-                       my $message="";
-                       if (defined $form->field('comments') &&
-                           length $form->field('comments')) {
-                               $message=$form->field('comments');
-                       }
-                       
                        if (! $exists) {
                                rcs_add($file);
                        }
                        if (! $exists) {
                                rcs_add($file);
                        }
@@ -453,6 +491,6 @@ sub cgi_editpage ($$) { #{{{
        }
 
        exit;
        }
 
        exit;
-} #}}}
+}
 
 1
 
 1