#! /usr/bin/perl # zeitungen-tidy - Daily Tidy run for www.zeitungsliste.de # Copyright (c) 2008 Joey Schulze # # 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, USA. use strict; use warnings; use DBI; my $dsn = 'dbi:Pg:dbname=zlist'; my $dbh = DBI->connect('dbi:Pg:dbname=zlist') or die "Can't connect to database\n"; sub tidy_online { my $query = q{SELECT uid FROM online WHERE activity < now() - interval'48 hours'}; my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n"; my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n"; my @uids = (); push @uids, $_->{uid} while ($_ = $sth->fetchrow_hashref); if ($#uids > -1) { $query = sprintf('DELETE FROM online WHERE uid in (%s)', join(',', @uids)); $dbh->do($query); } } sub tidy_activation { my $query = q{SELECT uid,id FROM activation WHERE register_date < now() - interval'35 days'}; my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n"; my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n"; my @uids = (); push @uids, $_->{uid} while ($_ = $sth->fetchrow_hashref); if ($#uids > -1) { $query = sprintf('DELETE FROM users WHERE id in (%s)', join(',', @uids)); $dbh->do($query); } } sub archive_topics { my $query = q{SELECT id FROM topics WHERE archived IS false AND modified < now() - interval'2 weeks'}; my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n"; my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n"; my @ids = (); push @ids, $_->{id} while ($_ = $sth->fetchrow_hashref); if ($#ids > -1) { $query = sprintf('UPDATE topics SET archived=true WHERE id in (%s)', join(',', @ids)); $dbh->do($query); } } sub tidy_tags { my $query = q{SELECT id FROM tags WHERE id NOT IN (SELECT DISTINCT tag FROM zeitung_tags)}; my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n"; my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n"; my @ids = (); push @ids, $_->{id} while ($_ = $sth->fetchrow_hashref); if ($#ids > -1) { $query = sprintf('DELETE FROM SET tags WHERE id in (%s)', join(',', @ids)); $dbh->do($query); } } tidy_online; archive_topics; # tidy_tags; tidy_activation;