<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 3.00;
use Encode;
use HTML::Entities;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "recentchanges", call =&gt; \&amp;getsetup);
	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);
	# Load goto to fix up links from recentchanges
	IkiWiki::loadplugin("goto");
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		recentchangespage =&gt; {
			type =&gt; "string",
			example =&gt; "recentchanges",
			description =&gt; "name of the recentchanges page",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		recentchangesnum =&gt; {
			type =&gt; "integer",
			example =&gt; 100,
			description =&gt; "number of changes to track",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
}

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 store ($$$) {
	my $change=shift;

	my $page="$config{recentchangespage}/change_".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; "goto",
						page =&gt; $_-&gt;{page}
					).
					"\" rel=\"nofollow\"&gt;".
					pagetitle($_-&gt;{page}).
					"&lt;/a&gt;"
			}
			else {
				$_-&gt;{link} = 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; "goto",
			page =&gt; IkiWiki::userpage($change-&gt;{author}),
		);
	}

	if (ref $change-&gt;{message}) {
		foreach my $field (@{$change-&gt;{message}}) {
			if (exists $field-&gt;{line}) {
				# escape html
				$field-&gt;{line} = encode_entities($field-&gt;{line});
				# escape links and preprocessor stuff
				$field-&gt;{line} = encode_entities($field-&gt;{line}, '\[\]');
			}
		}
	}

	# 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},
	);
	
	$template-&gt;param(permalink =&gt; "$config{url}/$config{recentchangespage}/#change-".titlepage($change-&gt;{rev}))
		if exists $config{url};
	
	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>