<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::websetup;

use warnings;
use strict;
use IkiWiki 2.00;

sub import { #{{{
	hook(type =&gt; "getsetup", id =&gt; "websetup", call =&gt; \&amp;getsetup);
	hook(type =&gt; "checkconfig", id =&gt; "websetup", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "sessioncgi", id =&gt; "websetup", call =&gt; \&amp;sessioncgi);
	hook(type =&gt; "formbuilder_setup", id =&gt; "websetup", 
	     call =&gt; \&amp;formbuilder_setup);
} # }}}

sub getsetup () { #{{{
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		websetup_force_plugins =&gt; {
			type =&gt; "string",
			example =&gt; [],
			description =&gt; "list of plugins that cannot be enabled/disabled via the web interface",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
		websetup_show_unsafe =&gt; {
			type =&gt; "boolean",
			example =&gt; 1,
			description =&gt; "show unsafe settings, read-only, in web interface?",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
} #}}}

sub checkconfig () { #{{{
	if (! exists $config{websetup_show_unsafe}) {
		$config{websetup_show_unsafe}=1;
	}
} #}}}

sub formatexample ($$) { #{{{
	my $example=shift;
	my $value=shift;

	if (defined $value &amp;&amp; length $value) {
		return "";
	}
	elsif (defined $example &amp;&amp; ! ref $example &amp;&amp; length $example) {
		return "&lt;br/ &gt;&lt;small&gt;Example: &lt;tt&gt;$example&lt;/tt&gt;&lt;/small&gt;";
	}
	else {
		return "";
	}
} #}}}

sub showfields ($$$@) { #{{{
	my $form=shift;
	my $plugin=shift;
	my $enabled=shift;

	my @show;
	my %plugininfo;
	while (@_) {
		my $key=shift;
		my %info=%{shift()};

		# skip internal settings
		next if defined $info{type} &amp;&amp; $info{type} eq "internal";
		# XXX hashes not handled yet
		next if ref $config{$key} &amp;&amp; ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
		# maybe skip unsafe settings
		next if ! $info{safe} &amp;&amp; ! ($config{websetup_show_unsafe} &amp;&amp; $config{websetup_advanced});
		# maybe skip advanced settings
		next if $info{advanced} &amp;&amp; ! $config{websetup_advanced};
		# these are handled specially, so don't show
		next if $key eq 'add_plugins' || $key eq 'disable_plugins';

		if ($key eq 'plugin') {
			%plugininfo=%info;
			next;
		}
		
		push @show, $key, \%info;
	}

	my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
	my %enabledfields;
	my $shownfields=0;
	
	my $plugin_forced=defined $plugin &amp;&amp; (! $plugininfo{safe} ||
		(exists $config{websetup_force_plugins} &amp;&amp; grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
	if ($plugin_forced &amp;&amp; ! $enabled) {
		# plugin is forced disabled, so skip its settings
		@show=();
	}

	# show plugin toggle
	if (defined $plugin &amp;&amp; (! $plugin_forced || $config{websetup_advanced})) {
		my $name="enable.$plugin";
		$form-&gt;field(
			name =&gt; $name,
			label =&gt; "",
			type =&gt; "checkbox",
			fieldset =&gt; $section,
			options =&gt; [ [ 1 =&gt; sprintf(gettext("enable %s?"), $plugin) ]]
		);
		if (! $form-&gt;submitted) {
			$form-&gt;field(name =&gt; $name, value =&gt; $enabled);
		}
		if ($plugin_forced) {
			$form-&gt;field(name =&gt; $name, disabled =&gt; 1);
		}
		else {
			$enabledfields{$name}=[$name, \%plugininfo];
		}
	}

	# show plugin settings
	while (@show) {
		my $key=shift @show;
		my %info=%{shift @show};

		my $description=$info{description};
		if (exists $info{link} &amp;&amp; length $info{link}) {
			if ($info{link} =~ /^\w+:\/\//) {
				$description="&lt;a href=\"$info{link}\"&gt;$description&lt;/a&gt;";
			}
			else {
				$description=htmllink("", "", $info{link}, noimageinline =&gt; 1, linktext =&gt; $description);
			}
		}

		# multiple plugins can have the same field
		my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;

		my $value=$config{$key};

		if ($info{safe} &amp;&amp; (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
			push @{$value}, "", ""; # blank items for expansion
		}

		if ($info{type} eq "string") {
			$form-&gt;field(
				name =&gt; $name,
				label =&gt; $description,
				comment =&gt; formatexample($info{example}, $value),
				type =&gt; "text",
				value =&gt; $value,
				size =&gt; 60,
				fieldset =&gt; $section,
			);
		}
		elsif ($info{type} eq "pagespec") {
			$form-&gt;field(
				name =&gt; $name,
				label =&gt; $description,
				comment =&gt; formatexample($info{example}, $value),
				type =&gt; "text",
				value =&gt; $value,
				size =&gt; 60,
				validate =&gt; \&amp;IkiWiki::pagespec_valid,
				fieldset =&gt; $section,
			);
		}
		elsif ($info{type} eq "integer") {
			$form-&gt;field(
				name =&gt; $name,
				label =&gt; $description,
				comment =&gt; formatexample($info{example}, $value),
				type =&gt; "text",
				value =&gt; $value,
				size =&gt; 5,
				validate =&gt; '/^[0-9]+$/',
				fieldset =&gt; $section,
			);
		}
		elsif ($info{type} eq "boolean") {
			$form-&gt;field(
				name =&gt; $name,
				label =&gt; "",
				type =&gt; "checkbox",
				options =&gt; [ [ 1 =&gt; $description ] ],
				fieldset =&gt; $section,
			);
			if (! $form-&gt;submitted) {
				$form-&gt;field(name =&gt; $name, value =&gt; $value);
			}
		}
		
		if (! $info{safe}) {
			$form-&gt;field(name =&gt; $name, disabled =&gt; 1);
		}
		else {
			$enabledfields{$name}=[$key, \%info];
		}
		$shownfields++;
	}
	
	# if no fields were shown for the plugin, drop it into the
	# plugins fieldset
	if (defined $plugin &amp;&amp; (! $plugin_forced || $config{websetup_advanced}) &amp;&amp;
	    ! $shownfields) {
		$form-&gt;field(name =&gt; "enable.$plugin", fieldset =&gt; "plugins");
	}

	return %enabledfields;
} #}}}

sub showform ($$) { #{{{
	my $cgi=shift;
	my $session=shift;

	if (! defined $session-&gt;param("name") || 
	    ! IkiWiki::is_admin($session-&gt;param("name"))) {
		error(gettext("you are not logged in as an admin"));
	}

	eval q{use CGI::FormBuilder};
	error($@) if $@;

	my $form = CGI::FormBuilder-&gt;new(
		title =&gt; "setup",
		name =&gt; "setup",
		header =&gt; 0,
		charset =&gt; "utf-8",
		method =&gt; 'POST',
		javascript =&gt; 0,
		reset =&gt; 1,
		params =&gt; $cgi,
		fieldsets =&gt; [
			[main =&gt; gettext("main")], 
			[plugins =&gt; gettext("plugins")]
		],
		action =&gt; $config{cgiurl},
		template =&gt; {type =&gt; 'div'},
		stylesheet =&gt; IkiWiki::baseurl()."style.css",
	);
	
	$form-&gt;field(name =&gt; "do", type =&gt; "hidden", value =&gt; "setup",
		force =&gt; 1);

	if ($form-&gt;submitted eq 'Basic Mode') {
		$form-&gt;field(name =&gt; "showadvanced", type =&gt; "hidden", 
			value =&gt; 0, force =&gt; 1);
	}
	elsif ($form-&gt;submitted eq 'Advanced Mode') {
		$form-&gt;field(name =&gt; "showadvanced", type =&gt; "hidden", 
			value =&gt; 1, force =&gt; 1);
	}
	my $advancedtoggle;
	if ($form-&gt;field("showadvanced")) {
		$config{websetup_advanced}=1;
		$advancedtoggle="Basic Mode";
	}
	else {
		$config{websetup_advanced}=0;
		$advancedtoggle="Advanced Mode";
	}

	my $buttons=["Save Setup", $advancedtoggle, "Cancel"];

	IkiWiki::decode_form_utf8($form);
	IkiWiki::run_hooks(formbuilder_setup =&gt; sub {
		shift-&gt;(form =&gt; $form, cgi =&gt; $cgi, session =&gt; $session,
			buttons =&gt; $buttons);
	});
	IkiWiki::decode_form_utf8($form);

	my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
	
	# record all currently enabled plugins before all are loaded
	my %enabled_plugins=%IkiWiki::loaded_plugins;

	# per-plugin setup
	require IkiWiki::Setup;
	foreach my $pair (IkiWiki::Setup::getsetup()) {
		my $plugin=$pair-&gt;[0];
		my $setup=$pair-&gt;[1];

		my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
		if (%shown) {
			$fields{$_}=$shown{$_} foreach keys %shown;
		}
	}
	
	if ($form-&gt;submitted eq "Cancel") {
		IkiWiki::redirect($cgi, $config{url});
		return;
	}
	elsif (($form-&gt;submitted eq 'Save Setup' || $form-&gt;submitted eq 'Rebuild Wiki') &amp;&amp; $form-&gt;validate) {
		# Push values from form into %config, avoiding unnecessary
		# changes, and keeping track of which changes need a
		# rebuild.
		my %rebuild;
		foreach my $field (keys %fields) {
			my %info=%{$fields{$field}-&gt;[1]};
			my $key=$fields{$field}-&gt;[0];
			my @value=$form-&gt;field($field);
			if (! @value) {
				@value=0;
			}
		
			if (! $info{safe}) {
	 			error("unsafe field $key"); # should never happen
			}
		
			if (exists $info{rebuild} &amp;&amp;
			    ($info{rebuild} || ! defined $info{rebuild})) {
				$rebuild{$field}=$info{rebuild};
			}
					
			if ($field=~/^enable\.(.*)/) {
				my $plugin=$1;
				if ($value[0] != exists $enabled_plugins{$plugin}) {
					# TODO plugin enable/disable
				}
				else {
					delete $rebuild{$field};
				}
				next;
			}

			if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
				@value=sort grep { length $_ } @value;
				my @oldvalue=sort grep { length $_ }
					(defined $config{$key} ? @{$config{$key}} : ());
				if ((@oldvalue) == (@value)) {
					delete $rebuild{$field};
				}
				else {
					$config{$key}=\@value;
				}
			}
			elsif (ref $config{$key} || ref $info{example}) {
				error("complex field $key"); # should never happen
			}
			else {
				if (defined $config{$key} &amp;&amp; $config{$key} eq $value[0]) {
					delete $rebuild{$field};
				}
				elsif (! defined $config{$key} &amp;&amp; ! length $value[0]) {
					delete $rebuild{$field};
				}
				elsif (! defined $config{$key} &amp;&amp; ! $value[0] &amp;&amp;
				       $info{type} eq "boolean") {
					delete $rebuild{$field};
				}
				else {
					$config{$key}=$value[0];
				}
			}
		}
		
		if (%rebuild &amp;&amp; ! $form-&gt;field("rebuild_asked")) {
			my $required=0;
			foreach my $field ($form-&gt;field) {
				$required=1 if $rebuild{$field};
				next if exists $rebuild{$field};
				$form-&gt;field(name =&gt; $field, type =&gt; "hidden",
					force =&gt; 1);
			}
			if ($required) {
				$form-&gt;text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
				$buttons=["Rebuild Wiki", "Cancel"];
			}
			else {
				$form-&gt;text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
				$buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
			}
			$form-&gt;field(name =&gt; "rebuild_asked", type =&gt; "hidden",
				value =&gt; 1, force =&gt; 1);
			$form-&gt;reset(0); # doesn't really make sense here
		}
		else {
			$form-&gt;field(name =&gt; "rebuild_asked", type =&gt; "hidden",
				value =&gt; 0, force =&gt; 1);
			# TODO save to real path
			IkiWiki::Setup::dump("/tmp/s");
			$form-&gt;text(gettext("Setup saved."));
	
			if ($form-&gt;submitted eq 'Rebuild Wiki') {
				# TODO rebuild
			}
		}
	}

	IkiWiki::showform($form, $buttons, $session, $cgi);
} #}}}

sub sessioncgi ($$) { #{{{
	my $cgi=shift;
	my $session=shift;

	if ($cgi-&gt;param("do") eq "setup") {
		showform($cgi, $session);
		exit;
	}
} #}}}

sub formbuilder_setup (@) { #{{{
	my %params=@_;

	my $form=$params{form};
	if ($form-&gt;title eq "preferences") {
		push @{$params{buttons}}, "Wiki Setup";
		if ($form-&gt;submitted &amp;&amp; $form-&gt;submitted eq "Wiki Setup") {
			showform($params{cgi}, $params{session});
			exit;
		}
	}
} #}}}

1
</pre></body></html>