Permalink
Browse files

wiki: anonymize rc.log entries even if they are not moved

  • Loading branch information...
kensanata committed May 6, 2018
1 parent ad39c96 commit 70a58f724dd25a40e14e1a90eae31a3267a2a6f6
Showing with 84 additions and 21 deletions.
  1. +61 −12 t/maintain.t
  2. +23 −9 wiki.pl
@@ -1,4 +1,4 @@
# Copyright (C) 2009–2015 Alex Schroeder <alex@gnu.org>
# Copyright (C) 2009–2018 Alex Schroeder <alex@gnu.org>
#
# 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
@@ -15,26 +15,75 @@

require './t/test.pl';
package OddMuse;
use Test::More tests => 10;
use Test::More tests => 20;

my $hundred_days_ago = $Now - 100 * 24 * 60 * 60;
my $ten_days_ago = $Now - 10 * 24 * 60 * 60;
my $two_days_ago = $Now - 2 * 24 * 60 * 60;
my $ip = '127.0.0.1';
# $ts, $id, $minor, $summary, $host, @rest

# First, make sure that moving all the entries from rc.log leaves no newline in
# the file.

my $log = join("\n",
join($FS, $hundred_days_ago, 'Two_Hundred_Days_Ago', '',
'Boring', $ip,
'Alex', '1', '', ''),
'');
WriteStringToFile($RcFile, $log);

test_page(get_page('action=maintain pwd=foo'),
'Moving 1 log entries',
'Removing IP numbers from 0 log entries');
$log = ReadFileOrDie($RcOldFile);
test_page($log, "Hundred_Days_Ago.*Anonymous");
test_page_negative($log, $ip);
$log = ReadFileOrDie($RcFile);
is($log, '', 'rc.log is empty');

# Now let's make sure that an old entry get anonymized and moved (like the
# previous test), and that those that are not moved care anonymized if they are
# old enough.

$log = join("\n",
join($FS, $hundred_days_ago, 'One_Hundred_Days_Ago', '',
'Boring', $ip,
'Alex', '1', '', ''),
join($FS, $ten_days_ago, 'Ten_Days_Ago', '',
'Boring', $ip,
'Alex', '1', '', ''),
join($FS, $two_days_ago, 'Two_Days_Ago', '',
'Boring', $ip,
'Alex', '1', '', ''),
'');
WriteStringToFile($RcFile, $log);

# old log entry to be moved
my $log = join($FS, '1235079422', 'Ganz_und_Gar', '',
'Ladenbeschreibung und Preisliste', '62.12.165.34',
'Alex', '1', '', '');
WriteStringToFile($RcFile, $log . "\n");
test_page(get_page('action=maintain pwd=foo'),
'Moving 1 log entries');
test_page(ReadFileOrDie($RcOldFile),
"^1235079422$FS");
is(ReadFileOrDie($RcFile), '', 'empty rc.log');
'Moving 1 log entries',
'Removing IP numbers from 1 log entries');
$log = ReadFileOrDie($RcOldFile);
test_page($log,
"Two_Hundred_Days_Ago.*Anonymous",
"One_Hundred_Days_Ago.*Anonymous");
test_page_negative($log, $ip);
$log = ReadFileOrDie($RcFile);
test_page($log,
"Ten_Days_Ago.*Anonymous",
"Two_Days_Ago.*$ip");
test_page_negative($log, "Hundred_Days_Ago");

# Let's make sure that updating pages write the right rc lines.

update_page('test', 'this is a test');
my $log = ReadFileOrDie($RcFile);
test_page($log,
"${FS}test${FS}",
"${FS}this is a test${FS}");
test_page_negative($log, "^\n");

# old page to be deleted
# Make sure that pages to be deleted are in fact deleted.

OpenPage('test');
$Page{ts} = 1;
$Page{revision} = 1;
32 wiki.pl
@@ -1,5 +1,5 @@
#! /usr/bin/perl
# Copyright (C) 2001-2015
# Copyright (C) 2001-2018
# Alex Schroeder <alex@gnu.org>
# Copyright (C) 2014-2015
# Alex Jakimenko <alex.jakimenko@gmail.com>
@@ -125,6 +125,7 @@ package OddMuse;
our $RCName = 'RecentChanges'; # Name of changes page
our @RcDays = qw(1 3 7 30 90); # Days for links on RecentChanges
our $RcDefault = 30; # Default number of RecentChanges days
our $KeepHostDays = 4; # Days to keep IP numbers for
our $KeepDays = 0; # Days to keep old revisions (0 means keep forever)
our $KeepMajor = 1; # 1 = keep at least one major rev when expiring pages
our $SummaryHours = 4; # Hours to offer the old subject when editing a page
@@ -3896,7 +3897,7 @@ sub DoMaintain {
RequestLockOrError();
print $q->p(T('Main lock obtained.'));
print $q->p(Ts('Moving part of the %s log file.', $RCName));
# Determine the number of days to go back
# Determine the number of days to go back, default is largest of @RcDays
my $days = 0;
foreach (@RcDays) {
$days = $_ if $_ > $days;
@@ -3907,23 +3908,36 @@ sub DoMaintain {
if (not $status) {
print $q->p($q->strong(Ts('Could not open %s log file', $RCName) . ':') . ' ' . $RcFile),
$q->p(T('Error was') . ':'), $q->pre($!), $q->p(T('Note: This error is normal if no changes have been made.'));
} else {
WriteStringToFile($RcFile . '.old', $data);
}
# Move the old stuff from rc to temp
my @rc = split(/\n/, $data);
my @tmp = ();
for my $line (@rc) {
my $line;
my $changed = 0;
while ($line = shift(@rc)) {
my ($ts, $id, $minor, $summary, $host, @rest) = split(/$FS/, $line);
last if $ts >= $starttime;
push(@tmp, join($FS, $ts, $id, $minor, $summary, T('Anonymous'), @rest));
$changed = 1;
}
unshift(@rc, $line) if $line; # this one ended the loop
print $q->p(Ts('Moving %s log entries.', scalar(@tmp)));
if (@tmp) {
# Write new files, and backups
AppendStringToFile($RcOldFile, join("\n", @tmp) . "\n");
WriteStringToFile($RcFile . '.old', $data);
splice(@rc, 0, scalar(@tmp)); # strip
WriteStringToFile($RcFile, @rc ? join("\n", @rc) . "\n" : '');
AppendStringToFile($RcOldFile, join("\n", @tmp) . "\n") if @tmp;
# remove IP numbers from all but the last few days
$starttime = $Now - $KeepHostDays * 86400; # 24*60*60
@tmp = ();
while ($line = shift(@rc)) {
my ($ts, $id, $minor, $summary, $host, @rest) = split(/$FS/, $line);
last if $ts >= $starttime;
push(@tmp, join($FS, $ts, $id, $minor, $summary, T('Anonymous'), @rest));
$changed = 1;
}
unshift(@rc, $line) if $line; # this one ended the loop
unshift(@rc, @tmp) if @tmp;
print $q->p(Ts('Removing IP numbers from %s log entries.', scalar(@tmp)));
WriteStringToFile($RcFile, @rc ? join("\n", @rc) . "\n" : '') if $changed;
if (opendir(DIR, $RssDir)) { # cleanup if they should expire anyway
foreach (readdir(DIR)) {
Unlink("$RssDir/$_") if $Now - Modified($_) > $RssCacheHours * 3600;

0 comments on commit 70a58f7

Please sign in to comment.