root/trunk/tools/remove_old_sessions

Revision 2969, 2.5 kB (checked in by fumiakiy, 15 months ago)

Script to clean up old session records from the database. BugId:69801

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