package MT::Plugin::TimeCount; use strict; use MT; use MT::Template::Context; use MT::Entry; use MT::Comment; use MT::TBPing; use MT::Util qw(format_ts offset_time_list); # show plugin information to main menu my $plugin = MT::Plugin->new; $plugin->name('Time Count 0.01'); $plugin->description('Count entries/comments/trackbacks by time.'); MT->add_plugin($plugin); # add tag MT::Template::Context->add_container_tag(TimeCountContainer => \&time_count_container); MT::Template::Context->add_tag(TimeCount => \&time_count); MT::Template::Context->add_tag(TimeCountPercent => \&time_count_percent); MT::Template::Context->add_tag(TimeCountPerMax => \&time_count_per_max); MT::Template::Context->add_tag(TimeCountStartTime => \&time_start); MT::Template::Context->add_tag(TimeCountEndTime => \&time_end); # time_count_container main sub time_count_container { my ($ctx, $args) = @_; my $tokens = $ctx->stash('tokens'); my $builder = $ctx->stash('builder'); my ($lastn, %load_args, %load_terms, $split_time, $mode); my (@counts, $iter, $res); my ($maxcount, $totalcount); # backup stash('entry') my $entrybackup = $ctx->stash('entry'); # get attribute $split_time = $args->{'split'} or $split_time = 60; $mode = $args->{'mode'} or $mode = 'entry'; # load entries $load_terms{blog_id} = $ctx->stash('blog_id'); if ($mode eq 'entry') { $load_terms{status} = MT::Entry::RELEASE(); } elsif ($mode eq 'comment') { $load_terms{visible} = 1; } if ($lastn = $args->{'lastn'}) { $load_args{limit} = $lastn } elsif (my $days = $args->{'days'}) { my @ago = offset_time_list(time - 3600 * 24 * $days, $ctx->stash('blog_id')); my $ago = sprintf "%04d%02d%02d%02d%02d%02d", $ago[5] + 1900, $ago[4] + 1, @ago[3,2,1,0]; $load_terms{created_on} = [ $ago ]; $load_args{range} = { created_on => 1 }; } if ($mode eq 'entry') { $iter = MT::Entry->load_iter(\%load_terms, \%load_args); } elsif ($mode eq 'comment') { $iter = MT::Comment->load_iter(\%load_terms, \%load_args); } elsif ($mode eq 'trackback') { $iter = MT::TBPing->load_iter(\%load_terms, \%load_args); } # loop each entry $maxcount = 1; while (my $obj = $iter->()) { my $obj_hour = substr $obj->created_on, 8, 2; my $obj_minute = substr $obj->created_on, 10, 2; my $obj_time = int($obj_hour) * 60 + int($obj_minute); my $array_index = ($obj_time - $obj_time % $split_time) / $split_time; $counts[$array_index]++; if ($maxcount < $counts[$array_index]) { $maxcount = $counts[$array_index]; } $totalcount++; } # out counts my ($start_time, $end_time, $ctr_end); $ctr_end = 1440 / $split_time; for (my $ctr = 0; $ctr < $ctr_end; $ctr++) { my $tmp_time; if ($counts[$ctr]) { $ctx->stash('count', $counts[$ctr]); } else { $ctx->stash('count', 0); } $tmp_time = $ctr * $split_time; $start_time = '20000101' . sprintf("%02d", ($tmp_time - $tmp_time % 60) / 60); $start_time .= sprintf("%02d", $tmp_time % 60) . '00'; $ctx->stash('start_time', $start_time); $tmp_time = ($ctr + 1) * $split_time; $end_time = '20000101' . sprintf("%02d", ($tmp_time - $tmp_time % 60) / 60); $end_time .= sprintf("%02d", $tmp_time % 60) . '00'; $ctx->stash('end_time', $end_time); $ctx->stash('totalcount', $totalcount); $ctx->stash('maxcount', $maxcount); defined(my $out = $builder->build($ctx, $tokens)) or return $ctx->error($ctx->errstr); $res .= $out; } # restore stash('entry') $ctx->stash('entry', $entrybackup); $res; } # time_coount main sub time_count { my ($ctx, $args) = @_; $ctx->stash('count'); } # time_coount_percent main sub time_count_percent { my ($ctx, $args) = @_; my ($mult, $isint); $mult = $args->{'mult'} or $mult = 1; $isint = $args->{'int'} or $isint = 0; my $percent = $ctx->stash('count') / $ctx->stash('totalcount'); $percent *= $mult; if ($isint) { $percent = int($percent); } $percent; } # time_coount_per_max main sub time_count_per_max { my ($ctx, $args) = @_; my ($mult, $isint); $mult = $args->{'mult'} or $mult = 1; $isint = $args->{'int'} or $isint = 0; my $per_max = $ctx->stash('count') / $ctx->stash('maxcount'); $per_max *= $mult; if ($isint) { $per_max = int($per_max); } $per_max; } # time_start main sub time_start { my ($ctx, $args) = @_; format_ts($args->{'format'}, $ctx->stash('start_time'), $ctx->stash('blog'), $args->{language}); } # time_end main sub time_end { my ($ctx, $args) = @_; format_ts($args->{'format'}, $ctx->stash('end_time'), $ctx->stash('blog'), $args->{language}); } 1;