#!/usr/bin/perl # TmNetWiki version 0.4 (September 11, 2002) # Copyright (C) 2002 Adam Brate # # Based on the GPLed UseModWiki 0.92 # Copyright (C) 2000-2001 Clifford A. Adams # or # Based on the GPLed AtisWiki 0.3 (C) 1998 Markus Denker # # ...which was based on # the LGPLed CVWiki CVS-patches (C) 1997 Peter Merel # and The Original WikiWikiWeb (C) Ward Cunningham # (code reused with permission) # Email and ThinLine options by Jim Mahoney # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc. # 59 Temple Place, Suite 330 # Boston, MA 02111-1307 USA package Wiki::keep; use strict; # Keep files # needs subs Wiki::page: GetPageDirectory, CreatePageDir, GetPageCache # Wiki::file: AppendStringToFile, # Wiki::error: ReadFileOrDie # needs consts KeepDir, FS1, $Consts->{FS2}, $KeepDays, $KeepMajor, $KeepAuthor # needs vars $OpenPageName, %Section, %Text, %KeptRevisions # Called after OpenKeptRevisions use base 'Exporter'; use Wiki::consts; use Wiki::page; use Wiki::file; use Wiki::error; #if ($^V and $^V gt v5.6.0) { # our @EXPORT; #else { use vars qw(@EXPORT); #} @EXPORT = qw( OpenKeptRevisions OpenKeptRevision KeepFileName SaveKeepSection ExpireKeepFile ); sub OpenKeptList; sub OpenKeptRevision { my ($revision) = @_; my ($section,$text); %{$section} = split(/$$Consts->{FS2}/, $KeptRevisions{$revision}, -1); %{$text} = split(/$Consts->{FS3}/, $section->{'data'}, -1); %Section = %{$section}; %Text = %{$text}; my $textpage = {section=>$section,text=>$text}; return $textpage; } sub KeepFileName { return "$KeepDir/" . &GetPageDirectory($OpenPageName) . "/$OpenPageName.kp"; } sub SaveKeepSection { my $section = shift; my $file = &KeepFileName(); my $data; return if ($section->{revision} < 1); # Don't keep "empty" revision $section->{keepts} = $Now; $data = $Consts->{FS1} . join($$Consts->{FS2}, %{$section}); &CreatePageDir($KeepDir, $OpenPageName); &AppendStringToFile($file, $data); } sub ExpireKeepFile { my ($fname, $data, @kplist, %tempSection, $expirets); my ($anyExpire, $anyKeep, $expire, %keepFlag, $sectName, $sectRev); my ($oldMajor, $oldAuthor); $fname = &KeepFileName(); return if (!(-f $fname)); $data = &ReadFileOrDie($fname); @kplist = split(/$Consts->{FS1}/, $data, -1); # -1 keeps trailing null fields return if (length(@kplist) < 1); # Also empty shift(@kplist) if ($kplist[0] eq ""); # First can be empty return if (length(@kplist) < 1); # Also empty %tempSection = split(/$$Consts->{FS2}/, $kplist[0], -1); if (!defined($tempSection{'keepts'})) { # die("Bad keep file." . join("|", %tempSection)); return; } $expirets = $Now - ($KeepDays * 24 * 60 * 60); return if ($tempSection{'keepts'} >= $expirets); # Nothing old enough $anyExpire = 0; $anyKeep = 0; %keepFlag = (); $oldMajor = &GetPageCache('oldmajor'); $oldAuthor = &GetPageCache('oldauthor'); foreach (reverse @kplist) { %tempSection = split(/$$Consts->{FS2}/, $_, -1); $sectName = $tempSection{'name'}; $sectRev = $tempSection{'revision'}; $expire = 0; if ($sectName eq "text_default") { if (($KeepMajor && ($sectRev == $oldMajor)) || ($KeepAuthor && ($sectRev == $oldAuthor))) { $expire = 0; } elsif ($tempSection{'keepts'} < $expirets) { $expire = 1; } } else { if ($tempSection{'keepts'} < $expirets) { $expire = 1; } } if (!$expire) { $keepFlag{$sectRev . "," . $sectName} = 1; $anyKeep = 1; } else { $anyExpire = 1; } } if (!$anyKeep) { # Empty, so remove file unlink($fname); return; } return if (!$anyExpire); # No sections expired open (OUT, ">$fname") or die (Ts('cant write %s', $fname) . ": $!"); foreach (@kplist) { %tempSection = split(/$$Consts->{FS2}/, $_, -1); $sectName = $tempSection{'name'}; $sectRev = $tempSection{'revision'}; if ($keepFlag{$sectRev . "," . $sectName}) { print OUT $Consts->{FS1}, $_; } } close(OUT); } sub OpenKeptList { my ($fname, $data); @KeptList = (); $fname = &KeepFileName(); return if (!(-f $fname)); $data = &ReadFileOrDie($fname); @KeptList = split(/$Consts->{FS1}/, $data, -1); # -1 keeps trailing null fields } sub OpenKeptRevisions { my ($name) = @_; # Name of section my ($fname, $data, %tempSection,$keptRevisions); $keptRevisions = {}; %KeptRevisions = (); &OpenKeptList(); foreach (@KeptList) { %tempSection = split(/$$Consts->{FS2}/, $_, -1); next if ($tempSection{'name'} ne $name); $keptRevisions->{$tempSection{'revision'}} = $_; } %KeptRevisions = %{$keptRevisions}; return $keptRevisions; } 1;