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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: Comment.pm 2642 2008-06-26 01:43:15Z auno $
package MT::Comment;
use strict;
use base qw( MT::Object MT::Scorable );
use MT::Util qw( weaken );
sub JUNK () { -1 }
sub NOT_JUNK () { 1 }
__PACKAGE__->install_properties({
column_defs => {
'id' => 'integer not null auto_increment',
'blog_id' => 'integer not null',
'entry_id' => 'integer not null',
'author' => 'string(100)',
'commenter_id' => 'integer',
'visible' => 'boolean',
'junk_status' => 'smallint',
'email' => 'string(75)',
'url' => 'string(255)',
'text' => 'text',
'ip' => 'string(16)',
'last_moved_on' => 'datetime not null',
'junk_score' => 'float',
'junk_log' => 'text',
'parent_id' => 'integer',
},
indexes => {
created_on => 1,
entry_visible => {
columns => [ 'entry_id', 'visible', 'created_on' ],
},
email => 1,
commenter_id => 1,
parent_id => 1,
last_moved_on => 1, # used for junk expiration
# For comment throttle check
blog_ip_date => {
columns => [ 'blog_id', 'ip', 'created_on' ],
},
# 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' ],
},
visible_date => {
columns => [ 'visible', 'created_on' ],
},
junk_date => {
columns => [ 'junk_status', 'created_on' ],
},
},
meta => 1,
defaults => {
junk_status => NOT_JUNK,
last_moved_on => '20000101000000',
},
audit => 1,
datasource => 'comment',
primary_key => 'id',
});
my %blocklists = ();
sub class_label {
return MT->translate("Comment");
}
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) = @_;
my($host, @hosts);
# other URI schemes?
require MT::Util;
@hosts = MT::Util::extract_urls($cmt->text);
my $not_blocked = 1;
my $blog_id = $cmt->blog_id;
if (!$blocklists{$blog_id}) {
require MT::Blocklist;
my @blocks = MT::Blocklist->load( { blog_id => $blog_id } );
$blocklists{$blog_id} = [ @blocks ];
}
if (@{$blocklists{$blog_id}}) {
for my $h (@hosts) {
for my $b (@{$blocklists{$blog_id}}) {
$not_blocked = 0 if ($h eq $b->text);
}
}
}
$not_blocked;
}
sub next {
my $comment = shift;
my($publish_only) = @_;
$publish_only = $publish_only ? {'visible' => 1} : {};
$comment->_nextprev('next', $publish_only);
}
sub previous {
my $comment = shift;
my($publish_only) = @_;
$publish_only = $publish_only ? {'visible' => 1} : {};
$comment->_nextprev('previous', $publish_only);
}
sub _nextprev {
my $obj = shift;
my $class = ref($obj);
my ($direction, $terms) = @_;
return undef unless ($direction eq 'next' || $direction eq 'previous');
my $next = $direction eq 'next';
my $label = '__' . $direction;
return $obj->{$label} if $obj->{$label};
my $o = $obj->nextprev(
direction => $direction,
terms => { blog_id => $obj->blog_id, %$terms },
by => 'created_on',
);
weaken($o->{$label} = $o) if $o;
return $o;
}
sub entry {
my ($comment) = @_;
my $entry = $comment->{__entry};
unless ($entry) {
my $entry_id = $comment->entry_id;
return undef unless $entry_id;
require MT::Entry;
$entry = MT::Entry->load($entry_id) or
return $comment->error(MT->translate(
"Load of entry '[_1]' failed: [_2]", $entry_id, MT::Entry->errstr));
$comment->{__entry} = $entry;
}
return $entry;
}
sub blog {
my ($comment) = @_;
my $blog = $comment->{__blog};
unless ($blog) {
my $blog_id = $comment->blog_id;
require MT::Blog;
$blog = MT::Blog->load($blog_id) or
return $comment->error(MT->translate(
"Load of blog '[_1]' failed: [_2]", $blog_id, MT::Blog->errstr));
$comment->{__blog} = $blog;
}
return $blog;
}
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);
}
sub approve {
my ($comment) = @_;
$comment->visible(1);
$comment->junk_status(NOT_JUNK);
}
*publish = \&approve;
sub author {
my $comment = shift;
if (!@_ && $comment->commenter_id) {
require MT::Author;
if (my $auth = MT::Author->load($comment->commenter_id)) {
return $auth->nickname;
}
}
return $comment->column('author', @_);
}
sub all_text {
my $this = shift;
my $text = $this->column('author') || '';
$text .= "\n" . ($this->column('email') || '');
$text .= "\n" . ($this->column('url') || '');
$text .= "\n" . ($this->column('text') || '');
$text;
}
sub is_published {
return $_[0]->visible && !$_[0]->is_junk;
}
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;
}
sub to_hash {
my $cmt = shift;
my $hash = $cmt->SUPER::to_hash();
$hash->{'comment.created_on_iso'} = sub { MT::Util::ts2iso($cmt->blog, $cmt->created_on) };
$hash->{'comment.modified_on_iso'} = sub { MT::Util::ts2iso($cmt->blog, $cmt->modified_on) };
if (my $blog = $cmt->blog) {
$hash->{'comment.text_html'} = sub {
my $txt = defined $cmt->text ? $cmt->text : '';
require MT::Util;
$txt = MT::Util::munge_comment($txt, $blog);
my $convert_breaks = $blog->convert_paras_comments;
$txt = $convert_breaks ?
MT->apply_text_filters($txt, $blog->comment_text_filters) :
$txt;
my $sanitize_spec = $blog->sanitize_spec ||
MT->config->GlobalSanitizeSpec;
require MT::Sanitize;
MT::Sanitize->sanitize($txt, $sanitize_spec);
}
}
if (my $entry = $cmt->entry) {
my $entry_hash = $entry->to_hash;
$hash->{"comment.$_"} = $entry_hash->{$_} foreach keys %$entry_hash;
}
if ($cmt->commenter_id) {
# commenter record exists... populate it
require MT::Author;
if (my $auth = MT::Author->load($cmt->commenter_id)) {
my $auth_hash = $auth->to_hash;
$hash->{"comment.$_"} = $auth_hash->{$_} foreach keys %$auth_hash;
}
}
$hash;
}
sub visible {
my $comment = shift;
return $comment->SUPER::visible unless @_;
## Note transitions in visibility in the object, so that
## other methods can act appropriately.
my $was_visible = $comment->SUPER::visible || 0;
my $is_visible = shift || 0;
my $vis_delta = 0;
if (!$was_visible && $is_visible) {
$vis_delta = 1;
} elsif ($was_visible && !$is_visible) {
$vis_delta = -1;
}
$comment->{__changed}{visibility} ||= 0;
$comment->{__changed}{visibility} += $vis_delta;
return $comment->SUPER::visible($is_visible);
}
sub parent {
my $comment = shift;
$comment->cache_property('parent', sub {
if ($comment->parent_id) {
return MT::Comment->load($comment->parent_id);
}
});
}
1;
__END__
=head1 NAME
MT::Comment - Movable Type comment record
=head1 SYNOPSIS
use MT::Comment;
my $comment = MT::Comment->new;
$comment->blog_id($entry->blog_id);
$comment->entry_id($entry->id);
$comment->author('Foo');
$comment->text('This is a comment.');
$comment->save
or die $comment->errstr;
=head1 DESCRIPTION
An I<MT::Comment> object represents a comment in the Movable Type system. It
contains all of the metadata about the comment (author name, email address,
homepage URL, IP address, etc.), as well as the actual body of the comment.
=head1 USAGE
As a subclass of I<MT::Object>, I<MT::Comment> inherits all of the
data-management and -storage methods from that class; thus you should look
at the I<MT::Object> documentation for details about creating a new object,
loading an existing object, saving an object, etc.
=head1 DATA ACCESS METHODS
The I<MT::Comment> object holds the following pieces of data. These fields can
be accessed and set using the standard data access methods described in the
I<MT::Object> documentation.
=over 4
=item * id
The numeric ID of the comment.
=item * blog_id
The numeric ID of the blog in which the comment is found.
=item * entry_id
The numeric ID of the entry on which the comment has been made.
=item * author
The name of the author of the comment.
=item * commenter_id
The author_id for the commenter; this will only be defined if the
commenter is registered, which is only required if the blog config
option allow_unreg_comments is false.
=item * ip
The IP address of the author of the comment.
=item * email
The email address of the author of the comment.
=item * url
The URL of the author of the comment.
=item * text
The body of the comment.
=item * visible
Returns a true value if the comment should be displayed. Comments can
be hidden if comment registration is required and the commenter is
pending approval.
=item * created_on
The timestamp denoting when the comment record was created, in the format
C<YYYYMMDDHHMMSS>. Note that the timestamp has already been adjusted for the
selected timezone.
=item * modified_on
The timestamp denoting when the comment record was last modified, in the
format C<YYYYMMDDHHMMSS>. Note that the timestamp has already been adjusted
for the selected timezone.
=back
=head1 DATA LOOKUP
In addition to numeric ID lookup, you can look up or sort records by any
combination of the following fields. See the I<load> documentation in
I<MT::Object> for more information.
=over 4
=item * created_on
=item * entry_id
=item * blog_id
=back
=head1 AUTHOR & COPYRIGHTS
Please see the I<MT> manpage for author, copyright, and license information.
=cut
|