File Coverage

blib/lib/Labyrinth/Plugin/Hits.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Labyrinth::Plugin::Hits;
2              
3 2     2   4116 use strict;
  2         4  
  2         58  
4 2     2   8 use warnings;
  2         2  
  2         75  
5              
6             our $VERSION = '5.19';
7              
8             =head1 NAME
9              
10             Labyrinth::Plugin::Hits - Handles the hit and update stats for page impressions
11              
12             =head1 DESCRIPTION
13              
14             Contains all the page hit handling functionality for the Labyrinth
15             framework.
16              
17             =cut
18              
19             #----------------------------------------------------------------------------
20             # Libraries
21              
22 2     2   6 use base qw(Labyrinth::Plugin::Base);
  2         2  
  2         592  
23              
24             use Time::Local;
25              
26             use Labyrinth::Audit;
27             use Labyrinth::DBUtils;
28             use Labyrinth::DTUtils;
29             use Labyrinth::MLUtils;
30             use Labyrinth::Support;
31             use Labyrinth::Variables;
32              
33             use Labyrinth::Plugin::Content;
34              
35             #----------------------------------------------------------------------------
36             # Variables
37              
38             # type: 0 = optional, 1 = mandatory
39             # html: 0 = none, 1 = text, 2 = textarea
40              
41             my %fields = (
42             title => { type => 1, html => 1 },
43             tagline => { type => 0, html => 1 },
44             query => { type => 0, html => 2 },
45             );
46              
47             my $WEEKS1 = 60 * 60 * 24 * 7;
48             my $WEEKS4 = $WEEKS1 * 4;
49             my $WEEKS6 = $WEEKS1 * 6;
50              
51             #----------------------------------------------------------------------------
52             # Public Interface Functions
53              
54             =head1 PUBLIC INTERFACE METHODS
55              
56             =over 4
57              
58             =item GetUpdate
59              
60             Provides the dates for the last update to the site and the requested page.
61              
62             =item SetHits
63              
64             Records the hit for the current page and photo (if applicable).
65              
66             =item HitPages
67              
68             List of hit stats for site pages.
69              
70             =item HitAlbums
71              
72             List of hit stats for galley albums.
73              
74             =item HitPhotos
75              
76             List of hit stats for galley photos.
77              
78             =item HitSelects
79              
80             Compile contents of the drop downs on hit pages.
81              
82             =back
83              
84             =cut
85              
86             sub GetUpdate {
87             my $section = $tvars{'section'} || 'unstated';
88             my $pageid = $cgiparams{pid} || $cgiparams{pageid} || 0;
89              
90             my %updates = (
91             siteupdate => ['site',0],
92             pageupdate => [$section,$pageid],
93             );
94              
95             foreach my $update (keys %updates) {
96             my @rs = $dbi->GetQuery('array','PageTimeStamp',@{$updates{$update}});
97             next unless(@rs);
98              
99             # get expanded timestamp [2004-05-07 13:24:56]
100             $rs[0]->[0] =~ /(\d\d\d\d)[^\d]?(\d\d)[^\d]?(\d\d)/;
101             $tvars{$update} = formatDate(5,timelocal(0,0,0,int($3),$2-1,$1));
102             }
103             }
104              
105             sub SetHits {
106             my $counter = 0;
107             my $section = $cgiparams{act} || 'unstated';
108             my $pageid = $cgiparams{pid} || $cgiparams{pageid} || 0;
109             my $photoid = $cgiparams{iid} || $cgiparams{photoid} || 0;
110             my @keys = sort grep {/(name|id|letter|data|volume|month|year)/} keys %cgiparams;
111             my $query = join( '&', map {defined $cgiparams{$_} ? "$_=$cgiparams{$_}" : ''} @keys) if(@keys);
112             my $dt = formatDate(0);
113              
114             $dbi->DoQuery('AddAHit',1,$section,$pageid,$photoid,$query,$dt);
115             my @rs = $dbi->GetQuery('array','GetAHit',$section,$query);
116             $tvars{hits} = @rs ? $rs[0]->[0] : 0;
117             }
118              
119             sub HitPages {
120             my $dt = time() - $WEEKS4;
121              
122             my @pagesall = $dbi->GetQuery('hash','PageHitsAllTime');
123             for my $row (@pagesall) {
124             for(keys %fields) {
125             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
126             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
127             }
128             }
129             $tvars{pagesall} = \@pagesall if(@pagesall);
130              
131             my @pagesmon = $dbi->GetQuery('hash','PageHitsLastMonth',$dt);
132             for my $row (@pagesmon) {
133             for(keys %fields) {
134             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
135             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
136             }
137             }
138              
139             $tvars{ddmonths} = MonthSelect($tvars{data}->{month},1);
140             $tvars{ddyears} = YearSelect($tvars{data}->{year},2,1);
141             $tvars{today} = formatDate(7);
142             $tvars{pagesmon} = \@pagesmon if(@pagesmon);
143             }
144              
145             sub HitAlbums {
146             my $dt = time() - $WEEKS4;
147              
148             my @albumsall = $dbi->GetQuery('hash','AlbumHitsAllTime');
149             for my $row (@albumsall) {
150             $row->{month} = isMonth($_->{month});
151             $row->{title} =~ s/'/\'/g if $row->{title};
152             for(keys %fields) {
153             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
154             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
155             }
156             }
157             $tvars{albumsall} = \@albumsall if(@albumsall);
158              
159             my @albumsmon = $dbi->GetQuery('hash','AlbumHitsLastMonth',$dt);
160             for my $row (@albumsmon) {
161             $row->{month} = isMonth($_->{month});
162             $row->{title} =~ s/'/\'/g if $row->{title};
163             for(keys %fields) {
164             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
165             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
166             }
167             }
168              
169             $tvars{ddmonths} = MonthSelect($tvars{data}->{month},1);
170             $tvars{ddyears} = YearSelect($tvars{data}->{year},2,1);
171             $tvars{today} = formatDate(7);
172             $tvars{albumsmon} = \@albumsmon if(@albumsmon);
173             }
174              
175             sub HitPhotos {
176             my $dt = time() - $WEEKS4;
177              
178             my @photosall = $dbi->GetQuery('hash','PhotoHitsAllTime');
179             for my $row (@photosall) {
180             $row->{month} = isMonth($row->{month});
181             $row->{tagline} =~ s/'/\'/g if $row->{tagline};
182             $row->{title} =~ s/'/\'/g if $row->{title};
183             for(keys %fields) {
184             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
185             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
186             }
187             }
188             $tvars{photosall} = \@photosall if(@photosall);
189              
190             my @photosmon = $dbi->GetQuery('hash','PhotoHitsLastMonth',$dt);
191             for my $row (@photosmon) {
192             $row->{month} = isMonth($row->{month});
193             $row->{tagline} =~ s/'/\'/g if $row->{tagline};
194             $row->{title} =~ s/'/\'/g if $row->{title};
195             for(keys %fields) {
196             if($fields{$_}->{html} == 1) { $row->{$_} = CleanHTML($row->{$_}); }
197             elsif($fields{$_}->{html} == 2) { $row->{$_} = CleanTags($row->{$_}); }
198             }
199             }
200             $tvars{photosmon} = \@photosmon if(@photosmon);
201             }
202              
203             sub HitSelects {
204             $tvars{ddmonths} = MonthSelect($tvars{data}->{month},1);
205             $tvars{ddyears} = YearSelect($tvars{data}->{year},2,1);
206             $tvars{today} = formatDate(7);
207             }
208              
209             #----------------------------------------------------------------------------
210             # Administration Interface Functions
211              
212             =head1 PUBLIC INTERFACE METHODS
213              
214             =over 4
215              
216             =item AdminUpdates
217              
218             List the last site page updates, via the 'AdminUpdates' phrasebook query.
219              
220             =item SetUpdates
221              
222             Store page and site update from current timestamp. Only called internally.
223              
224             =item AdminHits
225              
226             Shorthand method to list all the hit counters for admin use.
227              
228             =item Reset
229              
230             Original method to reset hit counters, such that individual counters older than
231             a given date, are summed and stored in one counter entry.
232              
233             This is being deprecated in favour of a new script, which will be incorporated
234             here in the future.
235              
236             =back
237              
238             =cut
239              
240             sub AdminUpdates {
241             return unless AccessUser(EDITOR);
242             my @rs = $dbi->GetQuery('hash','AdminUpdates');
243              
244             foreach my $rec (@rs) {
245             $rec->{pagets} =~ /^(\d\d\d\d)(\d\d)(\d\d)/;
246             LogDebug("pagets=".$rec->{pagets});
247             $rec->{updated} = formatDate(5,timelocal(0,0,0,int($3),$2-1,$1));
248             }
249              
250             $tvars{records} = \@rs if(@rs);
251             }
252              
253             sub SetUpdates {
254             my $self = shift;
255             my $area = shift;
256             my $now = formatDate(0);
257              
258             while(@_) {
259             my $pageid = shift @_;
260             # LogDebug("SetUpdates: area=$area, pageid=$pageid");
261              
262             # check whether old or new
263             my @rs = $dbi->GetQuery('hash','GetUpdate',$area,$pageid);
264              
265             # store page update
266             my $key = (@rs ? 'SetUpdate' : 'AddUpdate');
267             $dbi->DoQuery($key,$now,$area,$pageid);
268             }
269              
270             # store index updates
271             $dbi->DoQuery('SetUpdate',$now,'site',0) if($area ne 'site');
272             }
273              
274             sub AdminHits {
275             return unless AccessUser(EDITOR);
276             HitPages();
277             HitAlbums();
278             HitPhotos();
279             HitSelects();
280             }
281              
282             sub Reset {
283             return unless AccessUser(ADMIN);
284             my $dt = formatDate(0) - ($WEEKS6);
285             my @rows = $dbi->GetQuery('hash','SumHits',$dt);
286             for my $row (@rows) {
287             $dbi->DoQuery('DelHits',$row->{area},$row->{query},$dt);
288             $dbi->DoQuery('AddAHit',$row->{counter},$row->{area},$row->{pageid},$row->{photoid},$row->{query},0);
289             }
290             }
291              
292             1;
293              
294             __END__