Add new disable hook, allowing plugins to perform cleanup after they have been disabled.

master
Joey Hess 2010-07-26 16:24:17 -04:00
parent b300bc9650
commit c401b6958a
5 changed files with 48 additions and 7 deletions

View 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};
}

View 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

View 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) {

2
debian/changelog vendored
View 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

View 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;`