<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::lockedit;

use warnings;
use strict;
use IkiWiki 2.00;

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

sub getsetup () { #{{{
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		locked_pages =&gt; {
			type =&gt; "pagespec",
			example =&gt; "!*/Discussion",
			description =&gt; "PageSpec controlling which pages are locked",
			link =&gt; "ikiwiki/PageSpec",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
} #}}}

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

	my $user=$session-&gt;param("name");
	return undef if defined $user &amp;&amp; IkiWiki::is_admin($user);

	if (defined $config{locked_pages} &amp;&amp; length $config{locked_pages} &amp;&amp;
	    pagespec_match($page, $config{locked_pages},
		    user =&gt; $session-&gt;param("name"),
		    ip =&gt; $ENV{REMOTE_ADDR},
	    )) {
		if (! defined $user ||
		    ! IkiWiki::userinfo_get($session-&gt;param("name"), "regdate")) {
			return sub { IkiWiki::needsignin($cgi, $session) };
		}
		else {
			return sprintf(gettext("%s is locked and cannot be edited"),
				htmllink("", "", $page, noimageinline =&gt; 1));
			
		}
	}

	# XXX deprecated, should be removed eventually
	foreach my $admin (@{$config{adminuser}}) {
		if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"),
		    user =&gt; $session-&gt;param("name"),
		    ip =&gt; $ENV{REMOTE_ADDR},
		)) {
			if (! defined $user ||
			    ! IkiWiki::userinfo_get($session-&gt;param("name"), "regdate")) {
				return sub { IkiWiki::needsignin($cgi, $session) };
			}
			else {
				return sprintf(gettext("%s is locked and cannot be edited"),
					htmllink("", "", $page, noimageinline =&gt; 1));
			}
		}
	}

	return undef;
} #}}}

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

	# XXX deprecated, should be removed eventually	
	my $form=$params{form};
	if ($form-&gt;title eq "preferences") {
		my $session=$params{session};
		my $cgi=$params{cgi};
		my $user_name=$session-&gt;param("name");

		$form-&gt;field(name =&gt; "locked_pages", size =&gt; 50,
			fieldset =&gt; "admin",
			comment =&gt; "deprecated; please move to locked_pages in setup file"
		);
		if (! IkiWiki::is_admin($user_name)) {
			$form-&gt;field(name =&gt; "locked_pages", type =&gt; "hidden");
		}
		if (! $form-&gt;submitted) {
			my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
			if (length $value) {
				$form-&gt;field(name =&gt; "locked_pages", force =&gt; 1, value =&gt; $value);
			}
			else {
				$form-&gt;field(name =&gt; "locked_pages", type =&gt; "hidden");
			}
		}
		if ($form-&gt;submitted &amp;&amp; $form-&gt;submitted eq 'Save Preferences') {
			if (defined $form-&gt;field("locked_pages")) {
				IkiWiki::userinfo_set($user_name, "locked_pages",
					$form-&gt;field("locked_pages")) ||
						error("failed to set locked_pages");
				if (! length $form-&gt;field("locked_pages")) {
					$form-&gt;field(name =&gt; "locked_pages", type =&gt; "hidden");
				}
			}
		}
	}
} #}}}

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