From: joey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>
Date: Wed, 3 May 2006 21:50:39 +0000 (+0000)
Subject: rather a lot of changes to make hyperestraier search be a plugin, allowing
X-Git-Tag: 1.1~64
X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=af9566ff1ae55d273ff96c7ca39e34265623a55d;p=ikiwiki.git

rather a lot of changes to make hyperestraier search be a plugin, allowing
for other types of search engine plugins if wanted, and also opening up a
lot of new possibilities for other kinds of plugins later

some notable changes along the way:

- lots of new hook types: cgi, render, delete
- wrapper files fixed to support config strings with newlines in them
- HEADERCONTENT in page template useful for plugins. Probably needs to be
  expanded to more such for other places plugins might want to add content.
- remove unnecessary wrappers field from config info stored in wrappers
---

diff --git a/IkiWiki.pm b/IkiWiki.pm
index 9a7b4fe91..960d26de9 100644
--- a/IkiWiki.pm
+++ b/IkiWiki.pm
@@ -16,9 +16,6 @@ sub checkconfig () { #{{{
 	if ($config{rss} && ! length $config{url}) {
 		error("Must specify url to wiki with --url when using --rss\n");
 	}
-	if ($config{hyperestraier} && ! length $config{url}) {
-		error("Must specify --url when using --hyperestraier\n");
-	}
 	
 	$config{wikistatedir}="$config{srcdir}/.ikiwiki"
 		unless exists $config{wikistatedir};
@@ -40,6 +37,12 @@ sub checkconfig () { #{{{
 			error("Failed to load plugin $mod: $@");
 		}
 	}
+
+	if (exists $hooks{checkconfig}) {
+                foreach my $id (keys %{$hooks{checkconfig}}) {
+                        $hooks{checkconfig}{$id}{call}->();
+                }
+        }
 } #}}}
 
 sub error ($) { #{{{
diff --git a/IkiWiki/CGI.pm b/IkiWiki/CGI.pm
index e219c8c1c..15e86651d 100644
--- a/IkiWiki/CGI.pm
+++ b/IkiWiki/CGI.pm
@@ -484,26 +484,21 @@ sub cgi_editpage ($$) { #{{{
 	}
 } #}}}
 
-sub cgi_hyperestraier () { #{{{
-	# only works for GET requests
-	chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
-	exec("./".basename($config{cgiurl})) || error("estseek.cgi failed");
-} #}}}
-
 sub cgi () { #{{{
 	eval q{use CGI};
 	eval q{use CGI::Session};
 	
 	my $q=CGI->new;
 	
+	if (exists $hooks{cgi}) {
+		foreach my $id (keys %{$hooks{cgi}}) {
+			$hooks{cgi}{$id}{call}->($q);
+		}
+	}
+	
 	my $do=$q->param('do');
 	if (! defined $do || ! length $do) {
-		if (defined $q->param('phrase')) {
-			cgi_hyperestraier();
-		}
-		else {
-			error("\"do\" parameter missing");
-		}
+		error("\"do\" parameter missing");
 	}
 	
 	# Things that do not need a session.
diff --git a/IkiWiki/Plugin/search.pm b/IkiWiki/Plugin/search.pm
new file mode 100644
index 000000000..c71fef76e
--- /dev/null
+++ b/IkiWiki/Plugin/search.pm
@@ -0,0 +1,122 @@
+#!/usr/bin/perl
+# hyperestraier search engine plugin
+package IkiWiki::Plugin::search;
+
+use warnings;
+use strict;
+use IkiWiki;
+
+sub import { #{{{
+	IkiWiki::hook(type => "checkconfig", id => "hyperestraier",
+		call => \&checkconfig);
+	IkiWiki::hook(type => "delete", id => "hyperestraier",
+		call => \&delete);
+	IkiWiki::hook(type => "render", id => "hyperestraier",
+		call => \&render);
+	IkiWiki::hook(type => "cgi", id => "hyperestraier",
+		call => \&cgi);
+} # }}}
+
+sub checkconfig () { #{{{
+	foreach my $required (qw(url cgiurl)) {
+		if (! length $IkiWiki::config{$required}) {
+			IkiWiki::error("Must specify $required when using the search plugin\n");
+		}
+	}
+
+	$IkiWiki::config{headercontent}.=qq{
+<form method="get" action="$IkiWiki::config{cgiurl}" id="searchform">
+<div>
+<input type="text" name="phrase" value="" size="16" />
+<input type="hidden" name="enc" value="UTF-8" />
+<input type="hidden" name="do" value="hyperestraier" />
+</div>
+</form>
+};
+} #}}}
+
+sub delete (@) { #{{{
+	IkiWiki::debug("cleaning hyperestraier search index");
+	IkiWiki::estcmd("purge -cl");
+	IkiWiki::estcfg();
+} #}}}
+
+sub render (@) { #{{{
+	IkiWiki::debug("updating hyperestraier search index");
+	IkiWiki::estcmd("gather -cm -bc -cl -sd",
+		map {
+			$IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)}
+		} @_
+	);
+	IkiWiki::estcfg();
+} #}}}
+
+sub cgi ($) { #{{{
+	my $cgi=shift;
+
+	if (defined $cgi->param('phrase')) {
+		# only works for GET requests
+		chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
+		exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
+	}
+} #}}}
+
+# Easier to keep these in the IkiWiki namespace.
+package IkiWiki;
+
+my $configured=0;
+sub estcfg () { #{{{
+	return if $configured;
+	$configured=1;
+	
+	my $estdir="$config{wikistatedir}/hyperestraier";
+	my $cgi=basename($config{cgiurl});
+	$cgi=~s/\..*$//;
+	open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
+		error("write $estdir/$cgi.tmpl: $!");
+	print TEMPLATE misctemplate("search", 
+		"<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
+	close TEMPLATE;
+	open(TEMPLATE, ">$estdir/$cgi.conf") ||
+		error("write $estdir/$cgi.conf: $!");
+	my $template=HTML::Template->new(
+		filename => "$config{templatedir}/estseek.conf"
+	);
+	eval q{use Cwd 'abs_path'};
+	$template->param(
+		index => $estdir,
+		tmplfile => "$estdir/$cgi.tmpl",
+		destdir => abs_path($config{destdir}),
+		url => $config{url},
+	);
+	print TEMPLATE $template->output;
+	close TEMPLATE;
+	$cgi="$estdir/".basename($config{cgiurl});
+	unlink($cgi);
+	symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
+		error("symlink $cgi: $!");
+} # }}}
+
+sub estcmd ($;@) { #{{{
+	my @params=split(' ', shift);
+	push @params, "-cl", "$config{wikistatedir}/hyperestraier";
+	if (@_) {
+		push @params, "-";
+	}
+	
+	my $pid=open(CHILD, "|-");
+	if ($pid) {
+		# parent
+		foreach (@_) {
+			print CHILD "$_\n";
+		}
+		close(CHILD) || error("estcmd @params exited nonzero: $?");
+	}
+	else {
+		# child
+		open(STDOUT, "/dev/null"); # shut it up (closing won't work)
+		exec("estcmd", @params) || error("can't run estcmd");
+	}
+} #}}}
+
+1
diff --git a/IkiWiki/Plugin/skeleton.pm b/IkiWiki/Plugin/skeleton.pm
index 89308c45f..d6d8cc0ed 100644
--- a/IkiWiki/Plugin/skeleton.pm
+++ b/IkiWiki/Plugin/skeleton.pm
@@ -1,6 +1,7 @@
 #!/usr/bin/perl
 # Ikiwiki skeleton plugin. Replace "skeleton" with the name of your plugin
-# in the lines below, and flesh out the code to make it do something.
+# in the lines below, remove hooks you don't use, and flesh out the code to
+# make it do something.
 package IkiWiki::Plugin::skeleton;
 
 use warnings;
@@ -8,14 +9,44 @@ use strict;
 use IkiWiki;
 
 sub import { #{{{
+	IkiWiki::hook(type => "checkconfig", id => "skeleton", 
+		call => \&checkconfig);
 	IkiWiki::hook(type => "preprocess", id => "skeleton", 
 		call => \&preprocess);
+	IkiWiki::hook(type => "delete", id => "skeleton", 
+		call => \&delete);
+	IkiWiki::hook(type => "render", id => "skeleton", 
+		call => \&render);
+	IkiWiki::hook(type => "cgi", id => "skeleton", 
+		call => \&cgi);
 } # }}}
 
+sub checkconfig () { #{{{
+	IkiWiki::debug("skeleton plugin checkconfig");
+} #}}}
+
 sub preprocess (@) { #{{{
 	my %params=@_;
 
 	return "skeleton plugin result";
 } # }}}
 
+sub delete (@) { #{{{
+	my @files=@_;
+
+	IkiWiki::debug("skeleton plugin told that files were deleted: @files");
+} #}}}
+
+sub render (@) { #{{{
+	my @files=@_;
+
+	IkiWiki::debug("skeleton plugin told that files were rendered: @files");
+} #}}}
+
+sub cgi ($) { #{{{
+	my $cgi=shift;
+
+	IkiWiki::debug("skeleton plugin running in cgi");
+} #}}}
+
 1
diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm
index f90f16335..a4efa0b74 100644
--- a/IkiWiki/Render.pm
+++ b/IkiWiki/Render.pm
@@ -206,9 +206,7 @@ sub genpage ($$$) { #{{{
 		$u=~s/\[\[file\]\]/$pagesources{$page}/g;
 		$template->param(historyurl => $u);
 	}
-	if ($config{hyperestraier}) {
-		$template->param(hyperestraierurl => cgiurl());
-	}
+	$template->param(headercontent => $config{headercontent});
 
 	$template->param(
 		title => $title,
@@ -300,57 +298,6 @@ sub prune ($) { #{{{
 	}
 } #}}}
 
-sub estcfg () { #{{{
-	my $estdir="$config{wikistatedir}/hyperestraier";
-	my $cgi=basename($config{cgiurl});
-	$cgi=~s/\..*$//;
-	open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
-		error("write $estdir/$cgi.tmpl: $!");
-	print TEMPLATE misctemplate("search", 
-		"<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
-	close TEMPLATE;
-	open(TEMPLATE, ">$estdir/$cgi.conf") ||
-		error("write $estdir/$cgi.conf: $!");
-	my $template=HTML::Template->new(
-		filename => "$config{templatedir}/estseek.conf"
-	);
-	eval q{use Cwd 'abs_path'};
-	$template->param(
-		index => $estdir,
-		tmplfile => "$estdir/$cgi.tmpl",
-		destdir => abs_path($config{destdir}),
-		url => $config{url},
-	);
-	print TEMPLATE $template->output;
-	close TEMPLATE;
-	$cgi="$estdir/".basename($config{cgiurl});
-	unlink($cgi);
-	symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
-		error("symlink $cgi: $!");
-} # }}}
-
-sub estcmd ($;@) { #{{{
-	my @params=split(' ', shift);
-	push @params, "-cl", "$config{wikistatedir}/hyperestraier";
-	if (@_) {
-		push @params, "-";
-	}
-	
-	my $pid=open(CHILD, "|-");
-	if ($pid) {
-		# parent
-		foreach (@_) {
-			print CHILD "$_\n";
-		}
-		close(CHILD) || error("estcmd @params exited nonzero: $?");
-	}
-	else {
-		# child
-		open(STDOUT, "/dev/null"); # shut it up (closing won't work)
-		exec("estcmd", @params) || error("can't run estcmd");
-	}
-} #}}}
-
 sub refresh () { #{{{
 	# find existing pages
 	my %exists;
@@ -516,19 +463,15 @@ FILE:		foreach my $file (@files) {
 		}
 	}
 
-	if ($config{hyperestraier} && (%rendered || @del)) {
-		debug("updating hyperestraier search index");
-		if (%rendered) {
-			estcmd("gather -cm -bc -cl -sd", 
-				map { $config{destdir}."/".$renderedfiles{pagename($_)} }
-				keys %rendered);
+	if (@del && exists $hooks{delete}) {
+		foreach my $id (keys %{$hooks{delete}}) {
+			$hooks{delete}{$id}{call}->(@del);
 		}
-		if (@del) {
-			estcmd("purge -cl");
+	}
+	if (%rendered && exists $hooks{render}) {
+		foreach my $id (keys %{$hooks{render}}) {
+			$hooks{render}{$id}{call}->(keys %rendered);
 		}
-		
-		debug("generating hyperestraier cgi config");
-		estcfg();
 	}
 } #}}}
 
diff --git a/IkiWiki/Setup/Standard.pm b/IkiWiki/Setup/Standard.pm
index 9883b922a..786ba70ac 100644
--- a/IkiWiki/Setup/Standard.pm
+++ b/IkiWiki/Setup/Standard.pm
@@ -21,15 +21,20 @@ sub setup_standard {
 
 	if (! $config{refresh}) {
 		debug("generating wrappers..");
+		my @wrappers=@{$setup{wrappers}};
+		delete $setup{wrappers};
 		my %startconfig=(%config);
-		foreach my $wrapper (@{$setup{wrappers}}) {
+		foreach my $wrapper (@wrappers) {
 			%config=(%startconfig, verbose => 0, %setup, %{$wrapper});
 			checkconfig();
 			gen_wrapper();
 		}
 		%config=(%startconfig);
-		delete $config{wrappers};
 	}
+	else {
+		delete $setup{wrappers};
+	}
+	
 	foreach my $c (keys %setup) {
 		if (defined $setup{$c}) {
 			if (! ref $setup{$c}) {
diff --git a/IkiWiki/Wrapper.pm b/IkiWiki/Wrapper.pm
index ee547cefb..52a7ca2c7 100644
--- a/IkiWiki/Wrapper.pm
+++ b/IkiWiki/Wrapper.pm
@@ -51,6 +51,7 @@ EOF
 	my $configstring=Data::Dumper->Dump([\%config], ['*config']);
 	$configstring=~s/\\/\\\\/g;
 	$configstring=~s/"/\\"/g;
+	$configstring=~s/\n/\\\n/g;
 	
 	open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
 	print OUT <<"EOF";
diff --git a/debian/NEWS b/debian/NEWS
index 1b05f36e8..130d1bd57 100644
--- a/debian/NEWS
+++ b/debian/NEWS
@@ -5,6 +5,10 @@ ikiwiki (1.1) unstable; urgency=low
   be changed to set rcs => "svn"; if your setup file sets svn => 0
   then change it to use rcs => "" to disable svn.
 
+  The --hyperestraier switch is gone too. To enable searching, turn on the
+  search plugin, by passing --plugin=search or through the plugin setting in
+  the config file.
+
   You will need to rebuild your wiki when upgrading to this version.
   If you listed your wiki in /etc/ikiwiki/wikilist this will be done
   automatically.
diff --git a/debian/changelog b/debian/changelog
index c072c237b..02e78816d 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -11,7 +11,7 @@ ikiwiki (1.1) UNRELEASED; urgency=low
   * Which means that more than one blog is now supported to appear on a
     single page. (With some limitations, like only the last one getting an
     rss file.)
-  * Added plugin system, currently only supporting for PreProcessorDirectives.
+  * Added a plugin system.
   * Added a pagecount plugin, enabled by default.
   * Support PreProcessorDirectives with no parameters, ie "[[pagecount ]]".
   * Fixed/optimised backlinks code, to avoid rebuilding pages to update
@@ -37,6 +37,8 @@ ikiwiki (1.1) UNRELEASED; urgency=low
   * Load whatever rcs module is specified, so new ones can be just dropped in
     as plugins and work. (Well, in theory.)
   * Add some basic docs about writing RCS modules.
+  * Broke search capability out into a plugin, removed the --hyperestraier
+    switch.
 
  -- Joey Hess <joeyh@debian.org>  Tue,  2 May 2006 14:13:59 -0400
 
diff --git a/doc/hyperestraier.mdwn b/doc/hyperestraier.mdwn
index e208ee631..e1246436a 100644
--- a/doc/hyperestraier.mdwn
+++ b/doc/hyperestraier.mdwn
@@ -1,3 +1,3 @@
-[Hyper Estraier](http://hyperestraier.sourceforge.net/) is a full text search engine. Ikiwiki can be configured to use it as the search engine for the wiki (see [[usage]]). This makes ikiwiki call Hyper Ertaier to update its index of pages each time a change is made to the wiki.
-
-It's possible to configure Hyper Estraier via one of ikiwiki's [[templates]].
\ No newline at end of file
+[Hyper Estraier](http://hyperestraier.sourceforge.net/) is a full text
+search engine. Ikiwiki can be configured to use it as the search engine for
+the wiki, using the [[search_plugin|plugins/search]].
diff --git a/doc/ikiwiki.setup b/doc/ikiwiki.setup
index d6ca7bb0b..3bc15891e 100644
--- a/doc/ikiwiki.setup
+++ b/doc/ikiwiki.setup
@@ -48,10 +48,8 @@ use IkiWiki::Setup::Standard {
 	#anonok => 1,
 	# Generate rss feeds for pages?
 	rss => 1,
-	# Use the Hyper Estraier search engine?
-	#hyperestraier => 1,
 	# Sanitize html?
 	sanitize => 1,
 	# To change the enabled plugins, edit this list
-	#plugin => [qw{pagecount inline brokenlinks}],
+	#plugin => [qw{pagecount inline brokenlinks hyperestraier}],
 }
diff --git a/doc/plugins/search.mdwn b/doc/plugins/search.mdwn
new file mode 100644
index 000000000..afeb0d4aa
--- /dev/null
+++ b/doc/plugins/search.mdwn
@@ -0,0 +1,5 @@
+This plugin is included in ikiwiki, but is not enabled by default. It adds
+full text search to ikiwiki, using the [[HyperEstraier]] engine.
+
+It's possible to configure Hyper Estraier via one of ikiwiki's
+[[templates]].
diff --git a/doc/usage.mdwn b/doc/usage.mdwn
index c30d799fd..eac72cdc7 100644
--- a/doc/usage.mdwn
+++ b/doc/usage.mdwn
@@ -172,11 +172,6 @@ These options configure the wiki.
   Enables the use of the specified plugin in the wiki. See [[plugins]] for
   details. Note that plugin names are case sensative.
 
-* --hyperestraier
-
-  Enables use of the [[HyperEstraier]] search engine for full test page
-  searches.
-
 * --verbose
 
   Be vebose about what is being done.
diff --git a/ikiwiki b/ikiwiki
index 5b4972ab7..745dfddd4 100755
--- a/ikiwiki
+++ b/ikiwiki
@@ -34,7 +34,6 @@ sub getconfig () { #{{{
 			rebuild => 0,
 			refresh => 0,
 			getctime => 0,
-			hyperestraier => 0,
 			wrapper => undef,
 			wrappermode => undef,
 			svnrepo => undef,
@@ -47,6 +46,7 @@ sub getconfig () { #{{{
 			adminuser => undef,
 			adminemail => undef,
 			plugin => [qw{inline}],
+			headercontent => '',
 		);
 
 		eval q{use Getopt::Long};
@@ -61,7 +61,6 @@ sub getconfig () { #{{{
 			"rcs=s" => \$config{rcs},
 			"no-rcs" => sub { $config{rcs}="" },
 			"anonok!" => \$config{anonok},
-			"hyperestraier" => \$config{hyperestraier},
 			"rss!" => \$config{rss},
 			"cgi!" => \$config{cgi},
 			"notify!" => \$config{notify},
@@ -104,6 +103,9 @@ sub getconfig () { #{{{
 		# wrapper passes a full config structure in the environment
 		# variable
 		eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
+		if ($@) {
+			error("WRAPPED_OPTIONS: $@");
+		}
 		checkconfig();
 	}
 } #}}}
diff --git a/templates/page.tmpl b/templates/page.tmpl
index 4dc8e1139..5ac162b2d 100644
--- a/templates/page.tmpl
+++ b/templates/page.tmpl
@@ -15,15 +15,7 @@
 </TMPL_LOOP>
 <TMPL_VAR TITLE>
 </h1>
-<TMPL_IF NAME="HYPERESTRAIERURL">
-<form method="get" action="<TMPL_VAR HYPERESTRAIERURL>" id="searchform">
-<div>
-<input type="text" name="phrase" value="" size="16" />
-<input type="hidden" name="enc" value="UTF-8" />
-<input type="hidden" name="do" value="hyperestraier" />
-</div>
-</form>
-</TMPL_IF>
+<TMPL_VAR HEADERCONTENT>
 </div>
 
 <div id="actions">