DBASE LAST MODIFIED AND SIZE HACK v1.1
--------------------------------------
What it does:
When updating your database, isn't it a pain to have to manually edit HTML so users can see when the last update was, or how many items are in your database?
This hack prints these values into your HTML using the same technique that inserts the correct cart id and page file name into your HTML, for the obligatory hidden variables on every page.
What's in this update:
v1.1 13/06/00 16:20:55
- The previous version read in *the entire datafile every time* stats were required, even if the datafile hadn't been updated since the last time the script served the stats. This version works more efficiently by saving the "last modified" and "size" data in a small text file. When stats are required it reads this stored date and compares it to the last modified date of the live datafile - if it is the same it uses the corresponding "size" figure from the text file, rather than reading in the entire database to re-count every time. This speeds up page serving considerably, the bigger the datafile the more time is saved but it amounted to seconds (on my server) with a file 2000+ lines. And as these stats are likely to go on the most requested page on your site this is important.
THINGS YOU HAVE TO CHANGE:
*1) in setup_file add two new variables:
@statsonpages - This new array contains the paths + filenames of the HTML pages you want to display the stats on (case-sensitive, naturally!), e.g.
@statsonpages=("../splash.html","../search.html");
$sc_db_stats_path - path'n'file for the small text file used to record current database stats. This could be as simple as:
$sc_db_stats_path= "/path/to/user/carts/dbm.sts";
I use the User Carts folder because I know we aren't going to have any problems reading/writing files due the the permissive permissions required on that folder. If you are sharing multiple cart folders, admin files etc. across various scripts, for example 3 web stores, then you might prefer to use the following:
$sc_db_stats_path= $sc_user_carts_directory_path . "/" . $sc_main_script_url . "dbm.sts";
... where $sc_main_script_url = "web_store.cgi"; or something similar that is not going to cause file naming problems.
*2) in all of the HTML files listed in @statsonpages:
Insert something like the following HTML where you want the stats to appear:
%%howbigsdb%% Items - Database updated %%data_lastmod%%.
This introduces two new special tag sequences;
%%howbigsdb%% -- how big is the database, i.e. how many records are in it.
%%data_lastmod%% -- when the database was last modified.
*3) in webstore.cgi, it'll probably be easiest replace the entire display_page Subroutine with the following, if you haven't made any other modifications from the original code.
#######################################################################
# display_page Subroutine #
#######################################################################
# starts off with original code:
sub display_page {
local ($page, $routine, $file, $line) = @_;
local ($filter_stats,$datalmod,$howbigsdb); # JPW newline for DBLMod hack
open (PAGE, "<$page") ||
&file_open_error("$page", "$routine", $file, $line);
# -----------Database LastMod hack----------------START JPW: 14.06.00
foreach $pgs (@statsonpages){
if ($page =~ /$pgs/) {
# The above if check is v important cos we dont want to waste time
# gathering the stats every time any page is served. Hence our new
# array of pages we want stats on, @statsonpages, defined in the
# setup file, which is compared with the incoming page name.
# If we find a match then we get $mtime, the last modified time:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($sc_data_file_path);
# if a 'stats file' does not already exist write one...
if (!(-e $sc_db_stats_path)) {
($datalmod,$howbigsdb)=&write_dbstats_file($mtime);
} else { # ...open existing stats file:
open(DBMODSTATS, "$sc_db_stats_path") ||
&file_open_error("$sc_db_stats_path",
"Read DBStats file",__FILE__,__LINE__);
# Read in the saved values for $datalmod and $howbigsdb
# from the stats file.
while () {
($datalmod,$howbigsdb)=split(/\|/, $_)
}
close(DBMODSTATS);
# Compare the live database's last modified time ($mtime, which we
# gathered with the PERL stat() command earlier), with the recorded
# last modified time - if they are not equal, the database has been
# modified and we need to write a new stats file.
if ($datalmod != $mtime){
($datalmod,$howbigsdb)=
&write_dbstats_file($mtime);
}
} # end else
# Set a flag $filter_stats we'll use later, and format $mtime into
# a nicer form to read.
$filter_stats=1;
$datalmod = &timetostr($datalmod);
} # end if (($form_data{'page'}) =~ /^$pgs/)
} # end foreach @statsonpages
# ------------------------------------------------------------END JPW
# original code continues ...
while ()
{
s/cart_id=/cart_id=$cart_id/g;
s/%%cart_id%%/$cart_id/g;
s/%%page%%/$form_data{'page'}/g;
# ------------------------------------------------START JPW: 14.06.00
if ($filter_stats){
# we dont want to search through the PAGE line and do regular
# expressions if we are not filtering in stats on this page.
s/%%data_lastmod%%/$datalmod/g; # filter in date
s/%%howbigsdb%%/$howbigsdb/g; # filter no. of records
}
# ------------------------------------------------------------END JPW
# Original code now continues to end of routine....
if ($form_data{'add_to_cart_button'} ne "" &&
$sc_shall_i_let_client_know_item_added eq "yes")
{
if ($_ =~ /