From c401b6958af7e12c1c2c46f870691bfb0a998fd3 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 26 Jul 2010 16:24:17 -0400 Subject: [PATCH] Add new disable hook, allowing plugins to perform cleanup after they have been disabled. --- IkiWiki.pm | 17 ++++++++++------- IkiWiki/Plugin/skeleton.pm.example | 5 +++++ IkiWiki/Setup.pm | 24 ++++++++++++++++++++++++ debian/changelog | 2 ++ doc/plugins/write.mdwn | 7 +++++++ 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/IkiWiki.pm b/IkiWiki.pm index cfa4f5f03..e08d02922 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -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}; } diff --git a/IkiWiki/Plugin/skeleton.pm.example b/IkiWiki/Plugin/skeleton.pm.example index c3a3b0c01..a57a2c8fe 100644 --- a/IkiWiki/Plugin/skeleton.pm.example +++ b/IkiWiki/Plugin/skeleton.pm.example @@ -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 diff --git a/IkiWiki/Setup.pm b/IkiWiki/Setup.pm index 7af744f6a..f34571bcf 100644 --- a/IkiWiki/Setup.pm +++ b/IkiWiki/Setup.pm @@ -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) { diff --git a/debian/changelog b/debian/changelog index 167d02c6f..5645b34d4 100644 --- a/debian/changelog +++ b/debian/changelog @@ -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 Fri, 23 Jul 2010 14:00:32 -0400 diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index 2e3902388..10b4df835 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -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;`