+}
+
+sub build_affected {
+ my %affected;
+ my ($ayear, $amonth, $valid);
+
+ foreach my $year (keys %changed) {
+ ($ayear, $valid) = nextyear($year, $config{archivebase});
+ $affected{calendarlink($ayear)} = sprintf(gettext("building calendar for %s, its previous or next year has changed"), $ayear) if ($valid);
+ ($ayear, $valid) = previousyear($year, $config{archivebase});
+ $affected{calendarlink($ayear)} = sprintf(gettext("building calendar for %s, its previous or next year has changed"), $ayear) if ($valid);
+ foreach my $month (keys %{$changed{$year}}) {
+ ($ayear, $amonth, $valid) = nextmonth($year, $month, $config{archivebase});
+ $affected{calendarlink($ayear, sprintf("%02d", $amonth))} = sprintf(gettext("building calendar for %s/%s, its previous or next month has changed"), $amonth, $ayear) if ($valid);
+ ($ayear, $amonth, $valid) = previousmonth($year, $month, $config{archivebase});
+ $affected{calendarlink($ayear, sprintf("%02d", $amonth))} = sprintf(gettext("building calendar for %s/%s, its previous or next month has changed"), $amonth, $ayear) if ($valid);
+ }
+ }
+
+ return %affected;
+}
+
+sub autocreate {
+ my ($page, $pagefile, $year, $month) = @_;
+ my $message=sprintf(gettext("creating calendar page %s"), $page);
+ debug($message);
+
+ my $template;
+ if (defined $month) {
+ $template=template("calendarmonth.tmpl");
+ } else {
+ $template=template("calendaryear.tmpl");
+ }
+ $template->param(year => $year);
+ $template->param(month => $month) if defined $month;
+ $template->param(pagespec => $config{archive_pagespec});
+
+ my $dir = $IkiWiki::Plugin::transient::transientdir;
+
+ writefile($pagefile, $dir, $template->output);
+}
+
+sub calendarlink($;$) {
+ my ($year, $month) = @_;
+ if (defined $month) {
+ return $config{archivebase} . "/" . $year . "/" . $month;
+ } else {
+ return $config{archivebase} . "/" . $year;
+ }
+}
+
+sub gencalendarmonth{
+ my $year = shift;
+ my $month = sprintf("%02d", shift);
+
+ my $page = calendarlink($year, $month);
+ my $pagefile = newpagefile($page, $config{default_pageext});
+ add_autofile(
+ $pagefile, "calendar",
+ sub {return autocreate($page, $pagefile, $year, $month);}
+ );
+}
+
+sub gencalendaryear {
+ my $year = shift;
+ my %params = @_;
+
+ # Building year page
+ my $page = calendarlink($year);
+ my $pagefile = newpagefile($page, $config{default_pageext});
+ add_autofile(
+ $pagefile, "calendar",
+ sub {return autocreate($page, $pagefile, $year);}
+ );
+
+ if (not exists $wikistate{calendar}{minyear}) {
+ $wikistate{calendar}{minyear} = $year;
+ }
+ if (not exists $wikistate{calendar}{maxyear}) {
+ $wikistate{calendar}{maxyear} = $year;
+ }
+
+ if ($config{calendar_fill_gaps}) {
+ # Building month pages
+ foreach my $month (1 .. 12) {
+ gencalendarmonth($year, $month);
+ }