Renamed function so it might be more understandable
[mailer.git] / inc / stats-functions.php
index da5815c1828dd854afd4978e41f72165b3fa11a3..d863ccd068978f9052d150ad38dd368fa458bc36 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 10/25/2009 *
- * ===============                              Last change: 10/25/2009 *
+ * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
+ * ===================                          Last change: 10/25/2009 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : stats-functions.php                              *
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -44,18 +43,12 @@ if (!defined('__SECURITY')) {
 // Init stats system
 function initStatsSystem () {
        // Is stats cache loaded?
-       if ((!isset($GLOBALS['stats_loaded'])) && (getConfig('STATS_ENABLED') == 'Y')) {
-               // Load stats cache
-               $GLOBALS['stats_fqfn'] = sprintf("%sinc/cache/stats.cache", getConfig('PATH'));
-
-               // Is the file readable?
-               if (isFileReadable($GLOBALS['stats_fqfn'])) {
-                       // Yes, so load it and eval
-                       eval(readFromFile($GLOBALS['stats_fqfn']));
-               } else {
-                       // Generate it only
-                       touch($GLOBALS['stats_fqfn']);
-               }
+       if ((!isset($GLOBALS['stats_loaded'])) && (ifStatsAreEnabled())) {
+               // Init statistics array
+               $GLOBALS['stats'] = array();
+
+               // Load statistics entry from temporary table
+               loadStatsTable();
 
                // Stats are loaded!
                $GLOBALS['stats_loaded'] = true;
@@ -70,6 +63,9 @@ function isStatsEntrySet ($entry) {
 
 // Increments a statistics entry
 function incrementStatsEntry ($entry, $amount=1) {
+       // Do we have stats enabled?
+       if (!ifStatsAreEnabled()) return;
+
        // Is it there?
        if (isStatsEntrySet($entry)) {
                // Then increment it
@@ -83,7 +79,7 @@ function incrementStatsEntry ($entry, $amount=1) {
 // Getter for stats
 function getStatsEntry ($entry) {
        // Default is zero
-       $stats = 0;
+       $stats = '0';
 
        // Is it there?
        if (isStatsEntrySet($entry)) {
@@ -100,20 +96,87 @@ function setStatsEntry ($entry, $value) {
        $GLOBALS['stats'][$entry] = $value;
 }
 
+// Loads stats table
+function loadStatsTable () {
+       // Check if the link is up
+       if (!SQL_IS_LINK_UP()) return false;
+
+       // Do we have it there for today?
+       if (!isStatsTableCreated()) {
+               // Then create a default one
+               createStatsTable();
+       } // END - if
+
+       // Load it from database
+       $result = SQL_QUERY_ESC("SELECT `stats_entry`, `stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
+               array(
+                       generateDateTime(time(), '6')
+               ), __FUNCTION__, __LINE__);
+
+       // Read all rows
+       while ($row = SQL_FETCHARRAY($result)) {
+               $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
+       } // END - while
+
+       // Free the result
+       SQL_FREERESULT($result);
+}
+
+// Checks if the the statistics table is created
+function isStatsTableCreated () {
+       // Check if the link is up
+       if (!SQL_IS_LINK_UP()) return false;
+
+       // Ask for it
+       $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
+               array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
+
+       // Do we have a row?
+       return SQL_NUMROWS($result);
+}
+
+// Create the dummy table
+function createStatsTable () {
+       // Check if the link is up
+       if (!SQL_IS_LINK_UP()) return false;
+
+       // Create it here
+       $result = SQL_QUERY_ESC("CREATE TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
+`stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
+`stats_value` BIGINT(20) NOT NULL DEFAULT 0,
+PRIMARY KEY (`stats_entry`)
+) TYPE=HEAP COMMENT='Temporary statistics table'",
+               array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
+}
+
+// Write all entries to the table
+function writeStatsTable () {
+       // Check if the link is up
+       if (!SQL_IS_LINK_UP()) return false;
+
+       // Empty the table first
+       SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
+               array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
+
+       // Begin the SQL command
+       $sql = sprintf("REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`,`stats_value`) VALUES ",
+               generateDateTime(time(), '6'));
+
+       // Add all entries to the final query
+       foreach ($GLOBALS['stats'] as $entry => $value) {
+               $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
+       } // END - foreach
+
+       // Finalize it and run it
+       SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
+}
+
 // Filter for flushing statistics
 function FILTER_FLUSH_STATS () {
        // Now do we have stats?
-       if ((isset($GLOBALS['stats'])) && (getConfig('STATS_ENABLED') == 'Y')) {
-               // Then prepare it for writing
-               foreach ($GLOBALS['stats'] as $key=>$value) {
-                       $stats[] = '$GLOBALS[\'stats\'][\'' . $key . '\'] = ' . $value . ';';
-               } // END - foreach
-
-               // Add empty line
-               $stats[] = '';
-
-               // And flush all out
-               writeToFile($GLOBALS['stats_fqfn'], implode("\n", $stats), true);
+       if ((isset($GLOBALS['stats'])) && (!isInstallationPhase()) && (ifStatsAreEnabled())) {
+               // Write statistics to temporary table
+               writeStatsTable();
        } // END - if
 }