1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#!/usr/bin/perl -w
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::Tool::Upgrade;
use strict;
use lib qw( extlib lib );
use base qw( MT::Tool );
use Carp qw(confess);
use MT::Upgrade;
sub usage { '[--ttl <days>] [--kind <comma,separated,list,of,kinds>]' }
sub help {
return q{
Removes old and stale records from mt_session table.
--ttl <days> Required: the script uses the value to
determine if the records are old enough
to be removed.
--kind <comma,separated,list,of,kinds> Optional:
if specified, only the matching kind of
session records are removed.
};
}
my ($ttl, $kind);
sub options {
return (
'ttl=s' => \$ttl,
'kind=s' => \$kind,
);
}
sub main {
my $class = shift;
my ($verbose) = $class->SUPER::main(@_);
unless ( $ttl ) {
print "Please specify the duration (in days) of session records to be removed. cf: remove_old_sessions --ttl 30";
exit;
}
my $days = $ttl * 60 * 60 * 24; # ttl comes in days
my @kinds;
if ( $kind ) {
@kinds = split ',', $kind;
}
my $terms = {
@kinds ? ( kind => \@kinds ) : (),
start => [ undef, time - $days ],
};
my $args = {
range => { start => 1 }
};
require MT::Session;
my %kinds;
my $group_iter = MT::Session->count_group_by(
$terms,
{ %$args, group => [ 'kind' ] }
);
while ( my ($count, $kind ) = $group_iter->() ) {
# Don't remove user session records in this script
# unless explicitly specified
next if !@kinds &&
( ( $kind eq 'US' ) || ( $kind eq 'UA' ) || ( $kind eq 'SI' ) );
$kinds{$kind} = $count;
}
unless ( %kinds ) {
print "No records that are older than $ttl days found. Quitting...\n";
exit;
}
print "We are going to remove the following records:\n";
while ( my ( $key, $val ) = each %kinds ) {
print "\t$key: $val\n";
}
print "Proceed? [n]: ";
my $proceed = <STDIN>;
chomp($proceed);
exit unless $proceed =~ /^[Yy][Ee]?[Ss]?$/;
$terms->{kind} = [ keys %kinds ] unless @kinds;
unless ( MT::Session->remove( $terms, $args ) ) {
print "Error: " . MT::Session->errstr . "\n";
exit;
}
print "Success!\n";
1;
}
__PACKAGE__->main() unless caller;
1;
|