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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
|
# 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::Log;
use strict;
use base qw( MT::Object );
use MT::Util qw( ts2epoch epoch2ts offset_time );
# use constant is slow
sub INFO () {1}
sub WARNING () {2}
sub ERROR () {4}
sub SECURITY () {8}
sub DEBUG () {16}
use Exporter;
*import = \&Exporter::import;
our @EXPORT_OK = qw( INFO WARNING ERROR SECURITY DEBUG );
our %EXPORT_TAGS = ( constants => [@EXPORT_OK] );
use MT::Blog;
__PACKAGE__->install_properties(
{ column_defs => {
'id' => 'integer not null auto_increment',
'message' => 'text',
'ip' => 'string(50)',
'blog_id' => 'integer',
'author_id' => 'integer',
'level' => 'integer',
'category' => 'string(255)',
'metadata' => 'text',
},
indexes => {
created_on => 1,
blog_id => 1,
level => 1,
},
defaults => {
blog_id => 0,
author_id => 0,
level => 1,
},
child_of => 'MT::Blog',
datasource => 'log',
audit => 1,
primary_key => 'id',
class_column => 'class',
class_type => 'system',
}
);
sub class_label {
return MT->translate('Log message');
}
sub class_label_plural {
return MT->translate('Log messages');
}
sub list_props {
return {
created_on => {
auto => 1,
label => 'Date Created',
order => 100,
display => 'force',
raw => sub {
my $prop = shift;
my ( $obj, $app, $opts ) = @_;
my $ts = $obj->created_on;
my $blog = $opts->{blog};
## All Log records are saved with GMT, so do trick here.
my $epoch = ts2epoch( undef, $ts, 1 )
; # just get epoch with "no_offset" option
$epoch = offset_time( $epoch, $blog )
; # from GMT to Blog( or system ) Timezone
return epoch2ts( $blog, $epoch, 1 ); # back to timestamp
},
},
message => {
auto => 1,
label => 'Message',
order => 200,
display => 'force',
html => sub {
my $prop = shift;
my ( $obj, $app ) = @_;
my $msg = $obj->message;
my $desc;
if ( 'MT::Log' eq ref $obj ) {
$desc = MT::Util::encode_html( $obj->metadata ) || '';
}
else {
$desc = $obj->description;
}
$desc = $desc->() if ref $desc eq 'CODE';
$desc = '' if $msg eq $desc;
$desc = MT::Util::encode_html($desc);
$msg = MT::Util::encode_html($msg);
return $desc
? qq{
<div class="log-message can-select">
<a href="#" class="toggle-link icon-left icon-spinner detail-link">$msg</a>
<div class="log-metadata detail">
<pre>$desc</pre>
</div>
</div>
}
: qq{
<div class="log-message can-select">$msg</div>
};
},
},
blog_name => {
base => '__common.blog_name',
order => 300,
display => 'force',
},
by => {
base => '__virtual.string',
label => 'By',
view_filter => [],
order => 400,
display => 'force',
raw => sub {
my $prop = shift;
my ( $obj, $app ) = shift;
if ( $obj->author_id ) {
if ( my $user
= MT->model('author')->load( $obj->author_id ) )
{
return $user->name;
}
else {
return MT->translate('*User deleted*');
}
}
return '';
},
sort => sub {
my $prop = shift;
my ( $terms, $args ) = @_;
$args->{joins} ||= [];
push @{ $args->{joins} }, MT->model('author')->join_on(
undef, undef,
{ sort => 'nickname',
condition => { id => \'= log_author_id', },
direction => ( $args->{direction} || 'ascend' ),
type => 'left',
},
);
$args->{sort} = [];
return;
},
},
class => {
label => 'Class',
col => 'class',
display => 'none',
base => '__virtual.single_select',
single_select_options => sub {
my $prop = shift;
my $app = shift;
my $terms = {};
if ( my $blog_id = $app->param('blog_id') ) {
my $blog = MT->model('blog')->load($blog_id);
if ( $blog->is_blog ) {
$terms->{blog_id} = $blog->id;
}
else {
my $blogs = $blog->blogs;
$terms->{blog_id}
= [ map { $_->id } ( $blog, @$blogs ) ];
}
}
my $iter = MT->model('log')->count_group_by(
$terms,
{ group => ['class'],
no_class => 1,
},
);
my @options;
while ( my ( $count, $class ) = $iter->() ) {
push @options,
{
label => $class
? MT->translate($class)
: MT->translate('none'),
value => $class ? $class : '',
};
}
return \@options;
},
terms => sub {
my $prop = shift;
my ( $args, $db_terms, $db_args ) = @_;
delete $db_args->{no_class};
if ( $args->{value} ) {
$prop->super(@_);
}
else {
$db_terms->{class} = \' is null';
}
},
},
#category => {
# label => 'Category',
# col => 'category',
# base => '__virtual.single_select',
# single_select_options => sub {
# my $iter = MT->model('log')->count_group_by(
# undef,
# { group => ['category'],
# no_class => 1,
# },
# );
# my @options;
# while ( my ( $count, $cat ) = $iter->() ) {
# next unless $cat;
# my $label = $cat;
# $label =~ s/_/ /g;
# push @options,
# {
# label => MT->translate($cat),
# value => $cat
# };
# }
# return \@options;
# },
#},
level => {
label => 'Level',
base => '__virtual.single_select',
display => 'none',
col => 'level',
terms => sub {
my $prop = shift;
my ($args) = @_;
my @types;
my $val = $args->{value};
for ( 1, 2, 4, 8, 16 ) {
push @types, $_ if $val & $_;
}
return { level => \@types };
},
single_select_options => [
{ label => MT->translate('Security'), value => SECURITY() },
{ label => MT->translate('Error'), value => ERROR() },
{ label => MT->translate('Warning'), value => WARNING() },
{ label => MT->translate('Information'), value => INFO() },
{ label => MT->translate('Debug'), value => DEBUG() },
{ label => MT->translate('Security or error'),
value => SECURITY() | ERROR()
},
{ label => MT->translate('Security/error/warning'),
value => SECURITY() | ERROR() | WARNING()
},
{ label => MT->translate('Not debug'),
value => SECURITY() | ERROR() | WARNING() | INFO()
},
{ label => MT->translate('Debug/error'),
value => DEBUG() | ERROR()
},
],
},
metadata => {
auto => 1,
label => 'Metadata',
display => 'none',
},
modified_on => {
base => '__virtual.modified_on',
display => 'none',
},
author_name => {
base => '__virtual.author_name',
display => 'none',
},
ip => {
auto => 1,
label => 'IP Address',
display => 'default',
},
id => {
base => '__virtual.id',
label_via_param => sub {
my $prop = shift;
my ($app) = @_;
my $id = $app->param('filter_val');
return MT->translate( 'Showing only ID: [_1]', $id );
},
display => 'none',
filter_editable => 0,
},
};
}
sub system_filters {
return {
current_website => {
label => 'Logs on This Website',
items => [ { type => 'current_context' } ],
order => 100,
view => 'website',
},
show_only_errors => {
label => 'Show only errors',
items => [ { type => 'level', args => { value => ERROR() } }, ],
order => 200,
},
};
}
sub init {
my $log = shift;
$log->SUPER::init(@_);
my @ts = gmtime(time);
my $ts = sprintf '%04d%02d%02d%02d%02d%02d',
$ts[5] + 1900, $ts[4] + 1, @ts[ 3, 2, 1, 0 ];
$log->created_on($ts);
$log->modified_on($ts);
$log;
}
sub description {
my $log = shift;
my $msg = '';
if ( $log->message =~ m/\n/ ) {
$msg = $log->message;
}
if ( defined $log->metadata && ( $log->metadata ne '' ) ) {
$msg .= "\n\n" if $msg ne '';
$msg .= $log->metadata;
}
if ( $msg ne '' ) {
require MT::Util;
$msg = MT::Util::encode_html($msg);
}
$msg;
}
sub metadata_object {
my $log = shift;
my $class = $log->metadata_class;
return undef unless $class;
my $id = int( $log->metadata );
return undef unless $id;
eval "require $class;" or return undef;
$class->load($id);
}
sub metadata_class {
my $log = shift;
my $pkg = ref $log || $log;
if ( $pkg =~ m/::Log::/ ) {
$pkg =~ s/::Log::/::/;
$pkg;
}
else {
undef;
}
}
sub to_hash {
my $log = shift;
my $hash = $log->SUPER::to_hash(@_);
$hash->{ "log.level_" . $log->level } = 1 if $log->level;
$hash->{ "log.class_" . $log->class } = 1 if $log->class;
$hash->{ "log.category_" . $log->category } = 1 if $log->category;
$hash->{'log.description'} = $log->description;
if ( my $obj = $log->metadata_object ) {
my $obj_hash = $obj->to_hash;
$hash->{"log.$_"} = $obj_hash->{$_} foreach keys %$obj_hash;
}
if ( $log->author_id ) {
require MT::Author;
if ( my $auth = MT::Author->load( $log->author_id ) ) {
# prefix these hash keys with "log" since this
# log record may also refer to an entry/comment that
# has an associated author/commenter record and that
# would potentially clash with the log author record.
# ie, author 1 deletes entry written by author 2
# or author 1 deletes comment posted by commenter author 3
my $auth_hash = $auth->to_hash;
$hash->{"log.$_"} = $auth_hash->{$_} foreach keys %$auth_hash;
}
}
return $hash;
}
package MT::Log::Page;
our @ISA = qw( MT::Log );
__PACKAGE__->install_properties( { class_type => 'page', } );
sub class_label { MT->translate("Pages") }
sub description {
my $log = shift;
my $msg;
if ( my $entry = $log->metadata_object ) {
$msg = $entry->to_hash->{'entry.text_html'};
}
else {
$msg = MT->translate( 'Page # [_1] not found.', $log->metadata );
}
$msg;
}
package MT::Log::Entry;
our @ISA = qw( MT::Log );
__PACKAGE__->install_properties( { class_type => 'entry', } );
sub class_label { MT->translate("Entries") }
sub description {
my $log = shift;
my $msg;
if ( my $entry = $log->metadata_object ) {
$msg = $entry->to_hash->{'entry.text_html'};
}
else {
$msg = MT->translate( 'Entry # [_1] not found.', $log->metadata );
}
$msg;
}
package MT::Log::Comment;
our @ISA = qw( MT::Log );
__PACKAGE__->install_properties( { class_type => 'comment', } );
sub class_label { MT->translate("Comments") }
sub description {
my $log = shift;
my $cmt = $log->metadata_object;
my $msg;
if ($cmt) {
$msg = $cmt->to_hash->{'comment.text_html'};
}
else {
$msg = MT->translate( "Comment # [_1] not found.", $log->metadata );
}
$msg;
}
package MT::Log::TBPing;
our @ISA = qw( MT::Log );
__PACKAGE__->install_properties( { class_type => 'ping', } );
sub class_label { MT->translate("TrackBacks") }
sub description {
my $log = shift;
my $id = int( $log->metadata );
my $ping = $log->metadata_object;
my $msg;
if ($ping) {
$msg = $ping->to_hash->{'tbping.excerpt_html'};
}
else {
$msg = MT->translate( "TrackBack # [_1] not found.", $log->metadata );
}
$msg;
}
# Hint for tranlsate
# trans('blog');
# trans('website');
# trans('search');
# trans('author');
# trans('ping');
# trans('theme');
# trans('folder');
# trans('plugin');
1;
__END__
=head1 NAME
MT::Log - Movable Type activity log record
=head1 SYNOPSIS
use MT::Log;
my $log = MT::Log->new;
$log->message('This is a message in the activity log.');
$log->save
or die $log->errstr;
Extended log example:
use MT::Log;
my $log = MT::Log->new;
$log->message("This is a debug message");
$log->level(MT::Log::DEBUG());
$log->save or die $log->errstr;
You can also log directly with the MT package:
MT->log({
message => "A new entry has been posted.",
metadata => $entry->id,
class => 'entry'
});
=head1 DESCRIPTION
An I<MT::Log> object represents a record in the Movable Type activity log.
=head1 USAGE
As a subclass of I<MT::Object>, I<MT::Log> 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 SUBCLASSING
I<MT::Log> may be subclassed for different types of log records. The MT
Activity Feeds make use of this feature.
# MyCustomLog.pm
package MyCustomLog;
use base 'MT::Log';
sub description {
# returns html presentation of log message; used in activity
# log view and activity feeds...
}
1;
If you do define your own subclass, you will want to register it when
your plugin loads. The key/value pairs assigned to the plugin's "log_classes"
element should be unique enough to not conflict with other plugins.
# Plugin file
MT->add_plugin({
name => "My custom plugin",
log_classes => { customlog => "MyCustomLog" }
});
1;
=head1 METHODS
=head2 CLASS->new()
Creates a new log object.
=head2 $log->init()
Initializes the created_on and modified_on properties of the object
to the current time (these are overwritten if an object is being loaded
from the database).
=head2 CLASS->class_label
Returns a localized string identifying the kind of log record the class is
for.
=head2 $log->description
Provides an extended view of the log data; this may contain HTML.
=head2 $log->metadata_class()
Returns a Perl package name for the metadata object related to the
log record.
=head2 $log->metadata_object
The base I<MT::Log> metadata_object method will return a MT data object that
is related to this log object in the event that:
=over 4
=item 1. The metadata column is populated with a number.
=item 2. The class column has an identifier that maps to a registered
I<MT::Log> subclass.
=item 3. The class identified for the log record is provided by the C<metadata_class> method.
=back
If these conditions are met, this method will attempt to load a record using
the package name provided by the C<metadata_class> method with the id given in
the metadata column.
=head2 MT::Log->add_class($identifier => $package_name)
Registers a I<MT::Log> subclass with an identifier that is stored in the
'class' column of log records.
=head2 $log->set_values(\%values)
Overrides the I<MT::Object> set_values method and reblesses the object
with the appropriate I<MT::Log> subclass based on the identifier in
the 'class' column.
=head2 $log->to_hash()
Returns a hashref of data that represents the data held by the log
object and any associated metadata_object that it points to.
=head1 DATA ACCESS METHODS
The I<MT::Log> 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 log record.
=item * message
The log entry.
=item * blog_id
For log messages that occurred within the context of a blog, the
blog's id is stored in this column.
=item * author_id
For log messages that were created from the action of a user, their
author id is recorded in this column.
=item * class
Optional. An identifier that relates to a Perl package name that is a
valid I<MT::Log> descendant class (or relates to I<MT::Log> itself).
=item * category
Optional. A field to further categorize the message being logged. Being
an indexed column, it is possible to filter on this column when selecting
log records.
=item * level
Optional. A number that defines the level or priority of the log message. This
column may be one of the following values (shown below are the package
constants for each level followed by their numeric equivalent).
=over 4
=item * INFO / 1
=item * WARNING / 2
=item * ERROR / 4
=item * SECURITY / 8
=item * DEBUG / 16
=back
The default value for level is 1 (INFO).
=item * metadata
A storage field for additional information about this particular log
message. Different log classes utilize this field in different ways.
=item * ip
The IP address related with the message; this is useful, for example, when
the message pertains to a failed login, to determine the IP address of the
user who attempted to log in.
=item * created_on
The timestamp denoting when the log record was created, in the format
C<YYYYMMDDHHMMSS>. This timestamp is always in GMT.
=item * modified_on
The timestamp denoting when the log 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 * blog_id
=item * level
=item * class
=back
=head1 AUTHOR & COPYRIGHTS
Please see the I<MT> manpage for author, copyright, and license information.
=cut
|