File: find-junk

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (189 lines) | stat: -rwxr-xr-x 5,462 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/perl -w

use strict;
use lib 'lib', 'extlib';
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";

use MT;
use MT::Author;
use MT::Entry;
use MT::Comment;
use MT::Trackback;
use MT::TBPing;

package MT;

my %param;
sub param {
    my $app = shift;
    my $p = shift;
    @_ ? $param{$p} = shift : $param{$p};
}

package main;

use Getopt::Long;
use Pod::Usage;
my $comments = 1;
my $trackbacks = 1;
GetOptions("comments!" => \$comments,
           "trackbacks!" => \$trackbacks,
           "help|?" => \my($help),
           "blog_id=i" => \my($blog_id),
           "offset=i" => \my($offset),
           "limit=i" => \my($limit),
           "config=s" => \my($cfg)) or pod2usage(2);
pod2usage(1) if $help;
($comments || $trackbacks) or pod2usage(2);

my $app = new MT(($cfg ? ('Config' => $cfg) : ()));
$app->param('blog_id', $blog_id) if $blog_id;
$app->param('limit', $limit) if $limit;
$app->param('offset', $offset) if $offset;
if ($comments) {
    $app->param('_type', 'comment');
    find_junk($app);
}
if ($trackbacks) {
    $app->param('_type', 'tb');
    find_junk($app);
}

sub _cb_notjunktest_filter {
    my ($eh, $obj) = @_;
    require MT::JunkFilter;
    MT::JunkFilter->filter($obj);
    $obj->is_junk == 1 ? 0 : 1;
}

sub find_junk {
    my $app = MT->instance;
    my $blog_id = $app->param('blog_id');
    my $offset = $app->param('offset') || 0;
    my $limit = $app->param('limit');
    my $iter;
    my $type = $app->param('_type');
    if ($type eq 'comment') {
        $iter = MT::Comment->load_iter({
            ($blog_id ? (blog_id => $blog_id) : ()),
            junk_status => 0 },
            {'sort' => 'created_on',
             'direction' => 'descend',
             offset => $offset });
    } elsif ($type eq 'tb') {
        $iter = MT::TBPing->load_iter({
            ($blog_id ? (blog_id => $blog_id) : ()),
            junk_status => 0 },
            {'sort' => 'created_on',
             'direction' => 'descend',
             offset => $offset });
    } else {
        return;
    }
    my $i;
    MT->_register_core_callbacks({NotJunkTest =>
                                  \&_cb_notjunktest_filter});

    print "Scanning " . ($type eq 'comment' ? "Comments" : "TrackBacks") . "...\n";
    my $count = 0;
    my $junk = 0;
    while (my $obj = $iter->()) {
        last if $limit && $count == $limit;
        $count++;
        my $subject = $obj->clone;
        if (!MT->run_callbacks('NotJunkTest', $subject)) {
            if ((my $err = MT->errstr) =~ m/\w/) {
                print STDERR "** error from callback: " . $err . "\n" if $err;
                MT->error(undef);
            }
            display_junk($subject);
            $junk++;
        }
    }

    print "\nScanned: $count records\n";
    print "  Found: $junk junk items\n\n";
}

sub display_junk {
    my ($obj) = @_;

    my $entry;
    my $cat;
    my $type;
    if (ref $obj eq 'MT::Comment') {
        $entry = MT::Entry->load($obj->entry_id);
        $type = 'comment';
    } else {
        $type = 'trackback';
        my $tb = MT::Trackback->load($obj->tb_id);
        if ($tb->entry_id) {
            $entry = MT::Entry->load($tb->entry_id);
        } elsif ($tb->category_id) {
            $cat = MT::Category->load($tb->category_id);
        }
    }

    print "Junk $type found -- ID " . $obj->id . "\n";
    print "\tTarget: ";
    if ($entry) {
        print "Entry \"". $entry->title . "\" (" . $entry->id . ")\n";
    } elsif ($cat) {
        print "Category \"" . $cat->label . "\" (" . $cat->id . ")\n";
    }
    print "\t   Log:\n";
    my $log = $obj->junk_log;
    $log = "\t\t" . ($log || '');
    $log =~ s/\n/\n\t\t/gs;
    $log =~ s/\n+$//s;
    print $log . "\n";
    print "\t Score: " . $obj->junk_score . "\n";
    print "\tContent:\n";
    if ($type eq 'comment') {
        print "\t\t   Name: " . $obj->author . "\n";
        print "\t\t    URL: " . $obj->url . "\n";
        print "\t\t E-mail: " . $obj->email . "\n";
        print "\t\t     IP: " . $obj->ip . "\n";
        print "\t\t  Authn: " . ($obj->commenter_id ? 'Yes: ' . (MT::Author->load($obj->commenter_id)->name) : 'No') . "\n";
        my $text = $obj->text;
        $text = "\t\t\t" . $text;
        $text =~ s/\n/\n\t\t\t/sg;
        $text =~ s/\n+$//s;
        print "\t\t   Text:\n$text\n";
    } elsif ($type eq 'trackback') {
        print "\t\t   Blog: " . $obj->blog_name . "\n";
        print "\t\t    URL: " . $obj->source_url . "\n";
        print "\t\t E-mail: " . $obj->email . "\n";
        print "\t\t     IP: " . $obj->ip . "\n";
        print "\t\t  Title: " . $obj->title . "\n";
        my $text = $obj->excerpt;
        $text = "\t\t\t" . $text;
        $text =~ s/\n/\n\t\t\t/sg;
        $text =~ s/\n+$//s;
        print "\t\tExcerpt:\n$text\n";
    }
    print "\n";
}

__END__

=head1 NAME

find-junk

=head1 SYNOPSIS

    find-junk
        --nocomments       Prevents filtering comments
        --notrackbacks     Prevents filtering trackback pings
        --blog_id <id>     Limit scan to a particular blog
        --offset <n>       Specify number of records to skip
        --limit <n>        Specify number of records to process
        --config <cfg>     Specify MT configuration path and file
        --help | -?        Usage information

By default, all comments and trackbacks are scanned (unless the comment
or trackback record has been intentionally identified by the user as
"not junk" or "is junk").