]> sipb.mit.edu Git - ikiwiki.git/commitdiff
Add new disable hook, allowing plugins to perform cleanup after they have been disabled.
authorJoey Hess <joey@kitenet.net>
Mon, 26 Jul 2010 20:24:17 +0000 (16:24 -0400)
committerJoey Hess <joey@kitenet.net>
Mon, 26 Jul 2010 20:33:42 +0000 (16:33 -0400)
IkiWiki.pm
IkiWiki/Plugin/skeleton.pm.example
IkiWiki/Setup.pm
debian/changelog
doc/plugins/write.mdwn

index cfa4f5f039781205d404819388c6700e1d2a90a3..e08d02922dca78a96f18b633fc4d27e0dda4d3be 100644 (file)
@@ -1596,6 +1596,12 @@ sub loadindex () {
        if (exists $index->{version} && ! ref $index->{version}) {
                $pages=$index->{page};
                %wikistate=%{$index->{state}};
+               # Handle plugins that got disabled by loading a new setup.
+               if (exists $config{setupfile}) {
+                       require IkiWiki::Setup;
+                       IkiWiki::Setup::disabled_plugins(
+                               grep { ! $loaded_plugins{$_} } keys %wikistate);
+               }
        }
        else {
                $pages=$index;
@@ -1663,11 +1669,7 @@ sub loadindex () {
 sub saveindex () {
        run_hooks(savestate => sub { shift->() });
 
-       my %hookids;
-       foreach my $type (keys %hooks) {
-               $hookids{$_}=1 foreach keys %{$hooks{$type}};
-       }
-       my @hookids=keys %hookids;
+       my @plugins=keys %loaded_plugins;
 
        if (! -d $config{wikistatedir}) {
                mkdir($config{wikistatedir});
@@ -1701,7 +1703,7 @@ sub saveindex () {
                }
 
                if (exists $pagestate{$page}) {
-                       foreach my $id (@hookids) {
+                       foreach my $id (@plugins) {
                                foreach my $key (keys %{$pagestate{$page}{$id}}) {
                                        $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
                                }
@@ -1710,7 +1712,8 @@ sub saveindex () {
        }
 
        $index{state}={};
-       foreach my $id (@hookids) {
+       foreach my $id (@plugins) {
+               $index{state}{$id}={}; # used to detect disabled plugins
                foreach my $key (keys %{$wikistate{$id}}) {
                        $index{state}{$id}{$key}=$wikistate{$id}{$key};
                }
index c3a3b0c01eabcb1001257a6f2b4e407fe184db3b..a57a2c8fe575626c7144256185f94e05bdfe8f2c 100644 (file)
@@ -41,6 +41,7 @@ sub import {
        hook(type => "rename", id => "skeleton", call => \&rename);
        hook(type => "savestate", id => "skeleton", call => \&savestate);
        hook(type => "genwrapper", id => "skeleton", call => \&genwrapper);
+       hook(type => "disable", id => "skeleton", call => \&disable);
 }
 
 sub getopt () {
@@ -254,4 +255,8 @@ sub genwrapper () {
        debug("skeleton plugin running in genwrapper");
 }
 
+sub savestate () {
+       debug("skeleton plugin running in disable");
+}
+
 1
index 7af744f6ab18fe2bc0132891ce7c4fc3a461040f..f34571bcfdcedb845a84bf19d8430ff1c650256c 100644 (file)
@@ -124,6 +124,28 @@ sub merge ($) {
        }
 }
 
+sub disabled_plugins (@) {
+       # Handles running disable hooks of plugins that were enabled
+       # previously, but got disabled when a new setup file was loaded.
+       if (exists $config{setupfile} && @_) {
+               # Fork a child to load the disabled plugins.
+               my $pid=fork();
+               if ($pid == 0) {
+                       foreach my $plugin (@_) {
+                               print STDERR "** plugin $plugin disabled\n";
+                               eval { IkiWiki::loadplugin($plugin, 1) };
+                               if (exists $IkiWiki::hooks{disable}{$plugin}{call}) {
+                                       eval { $IkiWiki::hooks{disable}{$plugin}{call}->() };
+                               }
+                       }
+                       exit(0);
+               }
+               else {
+                       waitpid $pid, 0;
+               }
+       }
+}
+
 sub getsetup () {
        # Gets all available setup data from all plugins. Returns an
        # ordered list of [plugin, setup] pairs.
@@ -134,6 +156,7 @@ sub getsetup () {
         $config{syslog}=undef;
 
        # Load all plugins, so that all setup options are available.
+       my %original_loaded_plugins=%IkiWiki::loaded_plugins;
        my @plugins=IkiWiki::listplugins();
        foreach my $plugin (@plugins) {
                eval { IkiWiki::loadplugin($plugin, 1) };
@@ -141,6 +164,7 @@ sub getsetup () {
                        my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
                }
        }
+       %IkiWiki::loaded_plugins=%original_loaded_plugins;
        
        my %sections;
        foreach my $plugin (@plugins) {
index 167d02c6f99344cbee1203eb750790afebab9617..5645b34d462e907dc2d13fdc2bf710495d8d1dd5 100644 (file)
@@ -6,6 +6,8 @@ ikiwiki (3.20100723) UNRELEASED; urgency=low
   * git: Fix gitweb historyurl examples so "diff to current" links work.
     (Thanks jrayhawk)
   * meta: Allow syntax closer to html meta to be used.
+  * Add new disable hook, allowing plugins to perform cleanup after they
+    have been disabled.
 
  -- Joey Hess <joeyh@debian.org>  Fri, 23 Jul 2010 14:00:32 -0400
 
index 2e3902388f5c7bba8cc7d57fc39d288d8b2e4faf..10b4df835f9fde4eac70f6980727fbad8a7aa562 100644 (file)
@@ -600,6 +600,13 @@ function of the ikiwiki wrapper when it is being generated.
 The code runs before anything else -- in particular it runs before
 the suid wrapper has sanitized its environment.
 
+### disable
+
+       hook(type => "disable", id => "foo", call => \&disable);
+
+This hook is only run when a previously enabled plugin gets disabled
+during ikiwiki setup. Plugins can use this to perform cleanups.
+
 ## Exported variables
 
 Several variables are exported to your plugin when you `use IkiWiki;`