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

use warnings;
use strict;
use IkiWiki 2.00;

sub import { #{{{
	hook(type =&gt; "checkconfig", id =&gt; "recentchanges", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "refresh", id =&gt; "recentchanges", call =&gt; \&amp;refresh);
	hook(type =&gt; "pagetemplate", id =&gt; "recentchanges", call =&gt; \&amp;pagetemplate);
	hook(type =&gt; "htmlize", id =&gt; "_change", call =&gt; \&amp;htmlize);
	hook(type =&gt; "cgi", id =&gt; "recentchanges", call =&gt; \&amp;cgi);
} #}}}

sub checkconfig () { #{{{
	$config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
	$config{recentchangesnum}=100 unless defined $config{recentchangesnum};
} #}}}

sub refresh ($) { #{{{
	my %seen;

	# add new changes
	foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
		$seen{store($change, $config{recentchangespage})}=1;
	}
	
	# delete old and excess changes
	foreach my $page (keys %pagesources) {
		if ($pagesources{$page} =~ /\._change$/ &amp;&amp; ! $seen{$page}) {
			unlink($config{srcdir}.'/'.$pagesources{$page});
		}
	}
} #}}}

# Enable the recentchanges link on wiki pages.
sub pagetemplate (@) { #{{{
	my %params=@_;
	my $template=$params{template};
	my $page=$params{page};

	if (defined $config{recentchangespage} &amp;&amp; $config{rcs} &amp;&amp;
	    $page ne $config{recentchangespage} &amp;&amp;
	    $template-&gt;query(name =&gt; "recentchangesurl")) {
		$template-&gt;param(recentchangesurl =&gt; urlto($config{recentchangespage}, $page));
		$template-&gt;param(have_actions =&gt; 1);
	}
} #}}}

# Pages with extension _change have plain html markup, pass through.
sub htmlize (@) { #{{{
	my %params=@_;
	return $params{content};
} #}}}

sub cgi ($) { #{{{
	my $cgi=shift;
	if (defined $cgi-&gt;param('do') &amp;&amp; $cgi-&gt;param('do') eq "recentchanges_link") {
		# This is a link from a change page to some
		# other page. Since the change pages are only generated
		# once, statically, links on them won't be updated if the
		# page they link to is deleted, or newly created, or
		# changes for whatever reason. So this CGI handles that
		# dynamic linking stuff.
		my $page=$cgi-&gt;param("page");
		if (!defined $page) {
			error("missing page parameter");
		}

		IkiWiki::loadindex();

		my $link=bestlink("", $page);
		if (! length $link) {
			print "Content-type: text/html\n\n";
			print IkiWiki::misctemplate(gettext(gettext("missing page")),
				"&lt;p&gt;".
				sprintf(gettext("The page %s does not exist."),
					htmllink("", "", $page)).
				"&lt;/p&gt;");
		}
		else {
			IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
		}

		exit;
	}
}

sub store ($$$) { #{{{
	my $change=shift;

	my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change-&gt;{rev});

	# Optimisation to avoid re-writing pages. Assumes commits never
	# change (or that any changes are not important).
	return $page if exists $pagesources{$page} &amp;&amp; ! $config{rebuild};

	# Limit pages to first 10, and add links to the changed pages.
	my $is_excess = exists $change-&gt;{pages}[10];
	delete @{$change-&gt;{pages}}[10 .. @{$change-&gt;{pages}}] if $is_excess;
	$change-&gt;{pages} = [
		map {
			if (length $config{cgiurl}) {
				$_-&gt;{link} = "&lt;a href=\"".
					IkiWiki::cgiurl(
						do =&gt; "recentchanges_link",
						page =&gt; $_-&gt;{page}
					).
					"\"&gt;".
					IkiWiki::pagetitle($_-&gt;{page}).
					"&lt;/a&gt;"
			}
			else {
				$_-&gt;{link} = IkiWiki::pagetitle($_-&gt;{page});
			}
			$_-&gt;{baseurl}="$config{url}/" if length $config{url};

			$_;
		} @{$change-&gt;{pages}}
	];
	push @{$change-&gt;{pages}}, { link =&gt; '...' } if $is_excess;

	# See if the committer is an openid.
	$change-&gt;{author}=$change-&gt;{user};
	my $oiduser=eval { IkiWiki::openiduser($change-&gt;{user}) };
	if (defined $oiduser) {
		$change-&gt;{authorurl}=$change-&gt;{user};
		$change-&gt;{user}=$oiduser;
	}
	elsif (length $config{cgiurl}) {
		$change-&gt;{authorurl} = IkiWiki::cgiurl(
			do =&gt; "recentchanges_link",
			page =&gt; (length $config{userdir} ? "$config{userdir}/" : "").$change-&gt;{author},
		);
	}

	# escape wikilinks and preprocessor stuff in commit messages
	if (ref $change-&gt;{message}) {
		foreach my $field (@{$change-&gt;{message}}) {
			if (exists $field-&gt;{line}) {
				$field-&gt;{line} =~ s/(?&lt;!\\)\[\[/\\\[\[/g;
			}
		}
	}

	# Fill out a template with the change info.
	my $template=template("change.tmpl", blind_cache =&gt; 1);
	$template-&gt;param(
		%$change,
		commitdate =&gt; displaytime($change-&gt;{when}, "%X %x"),
		wikiname =&gt; $config{wikiname},
	);
	IkiWiki::run_hooks(pagetemplate =&gt; sub {
		shift-&gt;(page =&gt; $page, destpage =&gt; $page,
			template =&gt; $template, rev =&gt; $change-&gt;{rev});
	});

	my $file=$page."._change";
	writefile($file, $config{srcdir}, $template-&gt;output);
	utime $change-&gt;{when}, $change-&gt;{when}, "$config{srcdir}/$file";

	return $page;
} #}}}

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