Changeset 394

Show
Ignore:
Timestamp:
08/26/07 17:26:26 (1 year ago)
Author:
ogawa
Message:

refine fiscal_start_month caching mechanism.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/fiscal_yearly_archives.pl

    r393 r394  
    2929        ['fiscal_start_month', { Default => 4, Scope => 'system' }], 
    3030    ]), 
    31      
    3231}); 
    3332MT->add_plugin($plugin); 
     
    7776} 
    7877 
    79 # This is a dirty hack to omit multiple get_config_value() calls for 
    80 # optimization.  Basically, I don't like the way which a plugin loads 
    81 # and stores its configuration by using MT::PluginSettings.  Because I 
    82 # believe it should be able to get_and_set to the 'registry' in a more 
    83 # flexible and transparent manner. 
    84 our $START_MONTH; 
    85 sub init_request { 
    86     my $plugin = shift; 
    87     $plugin->SUPER::init_request(@_); 
    88     $START_MONTH = $plugin->get_config_value('fiscal_start_month') || 4; 
     78# a dirty hack to avoid multiple get_config_value() calls for 
     79# optimization 
     80
     81    my $start_month; 
     82 
     83    # fetch 'fiscal_start_month' at the first time the method is 
     84    # called, and cache it to $start_month 
     85    sub fiscal_start_month { 
     86        return $start_month if $start_month; 
     87        $start_month = $plugin->get_config_value('fiscal_start_month') || 4; 
     88    } 
     89 
     90    # invalidate $start_month cache everytime init_request() called 
     91    sub init_request { 
     92        my $plugin = shift; 
     93        $plugin->SUPER::init_request(@_); 
     94        $start_month = undef; 
     95    } 
    8996} 
    9097 
     
    9299    my ($ts) = @_; 
    93100    my ($y, $m) = unpack('A4A2', $ts); 
    94     $y-- if $m < $START_MONTH; 
     101    my $start_month = fiscal_start_month(); 
     102    $y-- if $m < $start_month; 
    95103    $y; 
    96104} 
     
    108116sub start_end_fiscal_year { 
    109117    my ($ts) = @_; 
    110     my $start_year = ts2fiscal($ts); 
    111     my $start = sprintf("%04d%02d%02d000000", $start_year, $START_MONTH, 1); 
     118    my ($start_year, $start_month) = (ts2fiscal($ts), fiscal_start_month()); 
     119    my $start = sprintf("%04d%02d%02d000000", $start_year, $start_month, 1); 
    112120    return $start unless wantarray; 
    113121 
    114122    my ($end_year, $end_month, $end_day); 
    115     if ($START_MONTH == 1) { 
     123    if ($start_month == 1) { 
    116124        ($end_year, $end_month, $end_day) = ($start_year, 12, 31); 
    117125    } else { 
    118         ($end_year, $end_month) = ($start_year + 1, $START_MONTH - 1); 
     126        ($end_year, $end_month) = ($start_year + 1, $start_month - 1); 
    119127        $end_day = MT::Util::days_in($end_month, $end_year); 
    120128    } 
     
    203211sub archive_group_entries { 
    204212    my ($ctx, %param) = @_; 
    205     my $ts = sprintf("%04d%02d%02d000000", $param{fiscal_year}, $START_MONTH, 1) 
     213    my $ts = sprintf("%04d%02d%02d000000", $param{fiscal_year}, fiscal_start_month(), 1) 
    206214        if %param; 
    207215    my ($start, $end); 
     
    236244    my $auth = $params->{Author}; 
    237245 
    238     my ($start, $end); 
    239     if ($ts) { 
    240         ($start, $end) = start_end_fiscal_year($ts); 
    241     } 
     246    my ($start, $end) = start_end_fiscal_year($ts) 
     247        if $ts; 
    242248    my $count = MT->model('entry')->count({ 
    243249        blog_id => $blog->id,