Skip to content

Commit

Permalink
[#396 state:resolved] Initial commit of the refactoring leading to th…
Browse files Browse the repository at this point in the history
…e creation of MT::Junkable. Includes a schema version bump (i.e. Time to Upgrade!). See full commit for details:

* MT::Junkable is an abstract class for any MT::Object that wishes to be made "junkable".  This was initially done to lay the groundwork for more refactoring necessary to extract TrackBacks out of the Melody core.
* MT::Junkable has POD documentation!
* Implemented currently junkable objects as inheriting from MT::Junkable
* Modified the junk filter to look for MT::Junkable objects for which to
base its periodic task of flushing old junked objects.
* Added an error condition when an object that is not junkable is passed
through the junk filter.
  • Loading branch information
byrnereese authored and jayallen committed Oct 24, 2010
1 parent f12ae93 commit 527b87d
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 144 deletions.
2 changes: 1 addition & 1 deletion lib/MT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BEGIN {
use version; our $VERSION = qv('0.9.24'); # MakeMaker stops here
$PRODUCT_VERSION = $VERSION; # The rightful resting place
$VERSION = '4.34'; # The true API version
$SCHEMA_VERSION = '4.0077';
$SCHEMA_VERSION = '4.0078';
$PRODUCT_NAME = 'Melody';
$PRODUCT_CODE = 'OM';
$VERSION_ID = '0.9.24 (MT 4.34+)';
Expand Down
34 changes: 17 additions & 17 deletions lib/MT/App/CMS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ sub core_list_actions {
$q->param('blog_id')
&& ( $app->user->is_superuser()
|| $app->permissions->can_edit_all_posts )
&& !( defined $q->param('filter_val') && $q->param('filter_val') == MT::Entry::JUNK())
&& !( defined $q->param('filter_val') && $q->param('filter_val') == MT::Junkable::JUNK())
&& !(defined $q->param('filter_key') && $q->param('filter_key') eq 'spam_entries')
},
},
Expand Down Expand Up @@ -797,7 +797,7 @@ sub core_list_filters {
$args->{join} = MT::Comment->join_on(
'entry_id',
{ created_on => [ $ts, undef ],
junk_status => MT::Comment::NOT_JUNK(),
junk_status => MT::Junkable::NOT_JUNK(),
},
{
range_incl => { created_on => 1 },
Expand Down Expand Up @@ -861,7 +861,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::TBPing;
$terms->{junk_status} = MT::TBPing::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
},
},
my_posts => {
Expand All @@ -873,7 +873,7 @@ sub core_list_filters {
my $app = MT->instance;
require MT::TBPing;
require MT::Trackback;
$terms->{junk_status} = MT::TBPing::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
$args->{join} = MT::Trackback->join_on(
undef,
{ id => \'= tbping_tb_id', },
Expand Down Expand Up @@ -901,7 +901,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::TBPing;
$terms->{junk_status} = MT::TBPing::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
$terms->{visible} = 0;
},
},
Expand All @@ -911,7 +911,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::TBPing;
$terms->{junk_status} = MT::TBPing::JUNK();
$terms->{junk_status} = MT::Junkable::JUNK();
},
},
last_7_days => {
Expand All @@ -923,7 +923,7 @@ sub core_list_filters {
$ts = epoch2ts( MT->app->blog, $ts );
$terms->{created_on} = [ $ts, undef ];
$args->{range_incl}{created_on} = 1;
$terms->{junk_status} = MT::TBPing::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
},
},
},
Expand All @@ -934,7 +934,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::Comment;
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
},
},
my_posts => {
Expand All @@ -945,7 +945,7 @@ sub core_list_filters {
require MT::Entry;
require MT::Comment;
my $app = MT->instance;
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();

# This join syntax employs a hack that allows us
# to do joins on abitrary columns. Typically,
Expand Down Expand Up @@ -975,7 +975,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::Comment;
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
$terms->{visible} = 0;
},
},
Expand All @@ -985,7 +985,7 @@ sub core_list_filters {
handler => sub {
my ( $terms, $args ) = @_;
require MT::Comment;
$terms->{junk_status} = MT::Comment::JUNK();
$terms->{junk_status} = MT::Junkable::JUNK();
},
},
published => {
Expand All @@ -1006,7 +1006,7 @@ sub core_list_filters {
$ts = epoch2ts( MT->app->blog, $ts );
$terms->{created_on} = [ $ts, undef ];
$args->{range_incl}{created_on} = 1;
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
},
},
_comments_by_user => {
Expand Down Expand Up @@ -1045,7 +1045,7 @@ sub core_list_filters {
require MT::Comment;
my $entry_id = int( MT->app->param('filter_val') );
$terms->{entry_id} = $entry_id;
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
},
},
_by_date => {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ sub core_list_filters {
$from .= '000000';
$to = undef unless $to =~ m/^\d{8}$/;
$to .= '235959';
$terms->{junk_status} = MT::Comment::NOT_JUNK();
$terms->{junk_status} = MT::Junkable::NOT_JUNK();
$terms->{created_on} = [ $from, $to ];
$args->{range_incl}{created_on} = 1;
},
Expand Down Expand Up @@ -2857,7 +2857,7 @@ sub _entry_prefs_from_params {
$prefs .= ':' . $fields{$_} if $fields{$_} > 1;
}
if ( $type && lc $type eq 'custom' ) {
my @fields = split /,/, $q->param('custom_prefs');
my @fields = split(/,/, $q->param('custom_prefs'));
foreach (@fields) {
$prefs .= ',' if $prefs ne '';
$prefs .= $_;
Expand Down Expand Up @@ -3079,7 +3079,7 @@ sub _build_category_list {
$tb_counts = {};
my $tb_count_iter
= MT::TBPing->count_group_by(
{ blog_id => $blog_id, junk_status => MT::TBPing::NOT_JUNK() },
{ blog_id => $blog_id, junk_status => MT::Junkable::NOT_JUNK() },
{ group => ['tb_id'] } );
while ( my ( $count, $tb_id ) = $tb_count_iter->() ) {
$tb_counts->{$tb_id} = $count;
Expand Down Expand Up @@ -3159,7 +3159,7 @@ sub _build_category_list {
? ( $tb_counts->{ $tb->id } || 0 )
: MT::TBPing->count(
{ tb_id => $tb->id,
junk_status => MT::TBPing::NOT_JUNK(),
junk_status => MT::Junkable::NOT_JUNK(),
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/MT/App/Trackback.pm
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ RSS
}
my $iter = MT::TBPing->load_iter(
{ tb_id => $tb->id,
junk_status => MT::TBPing::NOT_JUNK(),
junk_status => MT::Junkable::NOT_JUNK(),
visible => 1
},
\%arg
Expand Down
4 changes: 2 additions & 2 deletions lib/MT/CMS/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1972,14 +1972,14 @@ sub build_blog_table {
$comment_count
? $comment_count->{$blog_id}
: $comment_count->{$blog_id} = MT::Comment->count(
{ blog_id => $blog_id, junk_status => MT::Comment::NOT_JUNK() }
{ blog_id => $blog_id, junk_status => MT::Junkable::NOT_JUNK() }
)
)
|| 0;
$row->{num_pings} = (
$ping_count ? $ping_count->{$blog_id} : $ping_count->{$blog_id} =
MT::TBPing->count(
{ blog_id => $blog_id, junk_status => MT::TBPing::NOT_JUNK() }
{ blog_id => $blog_id, junk_status => MT::Junkable::NOT_JUNK() }
)
) || 0;
if ( $author->is_superuser ) {
Expand Down
13 changes: 4 additions & 9 deletions lib/MT/CMS/Comment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ sub list {
$terms{visible} = 0;
}
elsif ( $val eq 'junk' ) {
$terms{junk_status} = MT::Comment::JUNK();
$terms{junk_status} = MT::Junkable::JUNK();
}
else {
$terms{junk_status} = MT::Comment::NOT_JUNK();
$terms{junk_status} = MT::Junkable::NOT_JUNK();
}
}
}
Expand Down Expand Up @@ -880,7 +880,7 @@ sub empty_junk {
my $class = $app->model($type);
my $arg = {};
require MT::Comment;
$arg->{junk_status} = MT::Comment::JUNK();
$arg->{junk_status} = MT::Junkable::JUNK();
$arg->{blog_id} = $blog_id if $blog_id;
$class->remove($arg);
$app->add_return_arg( 'emptied' => 1 );
Expand Down Expand Up @@ -947,12 +947,7 @@ sub handle_junk {
my $obj = $class->load($id) or die "No $class $id";
my $old_visible = $obj->visible || 0;
unless ($perm_checked) {
if ( $obj->isa('MT::TBPing') && $obj->parent->isa('MT::Entry') ) {
next if $obj->parent->author_id != $app->user->id;
}
elsif ( $obj->isa('MT::Comment') ) {
next if $obj->entry->author_id != $app->user->id;
}
next unless $obj->isa('MT::Junkable') && $obj->can_be_junked_by( $app->user );
next unless $perms->can_publish_post;
}
$obj->junk;
Expand Down
4 changes: 2 additions & 2 deletions lib/MT/CMS/TrackBack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ sub list {
require MT::TBPing;
if ( ( $q->param('tab') || '' ) eq 'junk' ) {
$q->param( 'filter', 'junk_status' );
$q->param( 'filter_val', MT::TBPing::JUNK() );
$q->param( 'filter_val', MT::Junkable::JUNK() );
$param{filter_special} = 1;
$param{filter_phrase} = $app->translate('Junk TrackBacks');
}
else {
$terms{'junk_status'} = MT::TBPing::NOT_JUNK();
$terms{'junk_status'} = MT::Junkable::NOT_JUNK();
}

my $filter_key = $q->param('filter_key');
Expand Down
58 changes: 11 additions & 47 deletions lib/MT/Comment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
package MT::Comment;

use strict;
use base qw( MT::Object MT::Scorable );
use base qw( MT::Object MT::Scorable MT::Junkable );
use MT::Util qw( weaken );

sub JUNK () { -1 }
sub NOT_JUNK () { 1 }

__PACKAGE__->install_properties({
column_defs => {
'id' => 'integer not null auto_increment',
Expand All @@ -21,33 +18,25 @@ __PACKAGE__->install_properties({
'author' => 'string(100)',
'commenter_id' => 'integer',
'visible' => 'boolean',
'junk_status' => 'smallint',
'email' => 'string(127)',
'url' => 'string(255)',
'text' => 'text',
'ip' => 'string(50)',
'last_moved_on' => 'datetime not null',
'junk_score' => 'float',
'junk_log' => 'text',
'parent_id' => 'integer',
},
indexes => {
entry_visible => {
columns => [ 'entry_id', 'visible', 'created_on' ],
},
author => 1,
author => 1,
email => 1,
commenter_id => 1,
parent_id => 1,
last_moved_on => 1, # used for junk expiration
parent_id => 1,

# For URL lookups to aid spam filtering
blog_url => {
columns => [ 'blog_id', 'visible', 'url' ],
},
blog_stat => {
columns => [ 'blog_id', 'junk_status', 'created_on' ],
},
blog_visible => {
columns => [ 'blog_id', 'visible', 'created_on', 'id' ],
},
Expand All @@ -60,8 +49,6 @@ __PACKAGE__->install_properties({
},
meta => 1,
defaults => {
junk_status => NOT_JUNK,
last_moved_on => '20000101000000',
},
audit => 1,
datasource => 'comment',
Expand All @@ -78,14 +65,6 @@ sub class_label_plural {
return MT->translate("Comments");
}

sub is_junk {
$_[0]->junk_status == JUNK;
}

sub is_not_junk {
$_[0]->junk_status != JUNK;
}

sub is_not_blocked {
my ($eh, $cmt) = @_;

Expand Down Expand Up @@ -159,29 +138,16 @@ sub entry {
return $entry;
}

sub junk {
my ($comment) = @_;
if (($comment->junk_status || 0) != JUNK) {
require MT::Util;
my @ts = MT::Util::offset_time_list(time, $comment->blog_id);
my $ts = sprintf("%04d%02d%02d%02d%02d%02d",
$ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]);
$comment->last_moved_on($ts);
}
$comment->junk_status(JUNK);
$comment->visible(0);
}

sub moderate {
my ($comment) = @_;
$comment->visible(0);
$comment->junk_status(NOT_JUNK);
$comment->junk_status(MT::Junkable::NOT_JUNK);
}

sub approve {
my ($comment) = @_;
$comment->visible(1);
$comment->junk_status(NOT_JUNK);
$comment->junk_status(MT::Junkable::NOT_JUNK);
}

*publish = \&approve;
Expand Down Expand Up @@ -214,16 +180,8 @@ sub is_moderated {
return !$_[0]->visible() && !$_[0]->is_junk();
}

sub log {
# TBD: pre-load __junk_log when loading the comment
my $comment = shift;
push @{$comment->{__junk_log}}, @_;
}

sub save {
my $comment = shift;
$comment->junk_log(join "\n", @{$comment->{__junk_log}})
if ref $comment->{__junk_log} eq 'ARRAY';
my $ret = $comment->SUPER::save();
delete $comment->{__changed}{visibility} if $ret;
return $ret;
Expand Down Expand Up @@ -266,6 +224,12 @@ sub to_hash {
$hash;
}

sub can_be_junked_by {
my $obj = shift;
my ($user) = @_;
return $obj->entry->author_id == $user->id;
}

sub visible {
my $comment = shift;
return $comment->SUPER::visible unless @_;
Expand Down
Loading

0 comments on commit 527b87d

Please sign in to comment.