Continued on AJAX installer to start first step (more are easily to add)
[mailer.git] / inc / stats-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
4  * ===================                          Last change: 10/25/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : stats-functions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Automatical purging of outdated mail links       *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auto-Loeschung von veralteten Mail-Links         *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Init stats system
44 function initStatsSystem () {
45         // Is stats cache loaded?
46         if ((!isset($GLOBALS['stats_loaded'])) && (ifInternalStatsEnabled())) {
47                 // Init statistics array
48                 $GLOBALS['stats'] = array();
49
50                 // Load statistics entry from temporary table
51                 loadStatsTable();
52
53                 // Stats are loaded!
54                 $GLOBALS['stats_loaded'] = TRUE;
55         } // END - if
56 }
57
58 // Checks if a statistics entry has been set
59 function isStatsEntrySet ($entry) {
60         // Is there the entry?
61         return (isset($GLOBALS['stats'][$entry]));
62 }
63
64 // Increments a statistics entry
65 function incrementStatsEntry ($entry, $amount=1) {
66         // Is there stats enabled?
67         if ((!ifInternalStatsEnabled()) && ($entry != 'db_hits')) {
68                 // Abort here
69                 return;
70         } // END - if
71
72         // Is it there?
73         if (isStatsEntrySet($entry)) {
74                 // Then increment it
75                 $GLOBALS['stats'][$entry] += $amount;
76         } else {
77                 // Write it
78                 setStatsEntry($entry, $amount);
79         }
80 }
81
82 // Getter for stats
83 function getStatsEntry ($entry) {
84         // Default is zero
85         $stats = '0';
86
87         // Is it there?
88         if (isStatsEntrySet($entry)) {
89                 // Then use it
90                 $stats = $GLOBALS['stats'][$entry];
91         } // END - if
92
93         // Return the value
94         return $stats;
95 }
96
97 // Setter for stats
98 function setStatsEntry ($entry, $value) {
99         $GLOBALS['stats'][$entry] = $value;
100 }
101
102 // Loads stats table
103 function loadStatsTable () {
104         // Check if the link is up
105         if (!SQL_IS_LINK_UP()) return FALSE;
106
107         // Is it there for today?
108         if (!isStatsTableCreated()) {
109                 // Then create a default one
110                 createStatsTable();
111         } // END - if
112
113         // Load it from database
114         $result = SQL_QUERY_ESC("SELECT `stats_entry`, `stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
115                 array(
116                         generateDateTime(time(), '6')
117                 ), __FUNCTION__, __LINE__);
118
119         // Read all rows
120         while ($row = SQL_FETCHARRAY($result)) {
121                 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
122         } // END - while
123
124         // Free the result
125         SQL_FREERESULT($result);
126 }
127
128 // Checks if the the statistics table is created
129 function isStatsTableCreated () {
130         // Check if the link is up
131         if (!SQL_IS_LINK_UP()) return FALSE;
132
133         // Ask for it
134         $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
135                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
136
137         // Is there a row?
138         return SQL_NUMROWS($result);
139 }
140
141 // Create the dummy table
142 function createStatsTable () {
143         // Check if the link is up
144         if (!SQL_IS_LINK_UP()) return FALSE;
145
146         // Create it here
147         $result = SQL_QUERY_ESC("CREATE TEMPORARY TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
148 `stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
149 `stats_value` BIGINT(20) NOT NULL DEFAULT 0,
150 PRIMARY KEY (`stats_entry`)
151 ) ENGINE = HEAP COMMENT = 'Temporary statistics table'",
152                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
153 }
154
155 // Write all entries to the table
156 function writeStatsTable () {
157         // Check if the link is up
158         if (!SQL_IS_LINK_UP()) return FALSE;
159
160         // Empty the table first
161         SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
162                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
163
164         // Begin the SQL command
165         $sql = sprintf("REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`, `stats_value`) VALUES ",
166                 generateDateTime(time(), '6'));
167
168         // Add all entries to the final query
169         foreach ($GLOBALS['stats'] as $entry => $value) {
170                 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
171         } // END - foreach
172
173         // Finalize it and run it
174         SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
175 }
176
177 // Filter for flushing statistics
178 function FILTER_FLUSH_STATS () {
179         // Now do we have stats?
180         if ((isset($GLOBALS['stats'])) && (!isInstallationPhase()) && (ifInternalStatsEnabled())) {
181                 // Write statistics to temporary table
182                 writeStatsTable();
183
184                 // Count db/cache hits in database
185                 updateConfiguration(
186                         array(
187                                 'db_hits',
188                                 'cache_hits'
189                         ), array(
190                                 getStatsEntry('db_hits'),
191                                 getStatsEntry('cache_hits')
192                         ),
193                         '+'
194                 );
195         } // END - if
196 }
197
198 // [EOF]
199 ?>