Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Rework DateTime handling in class.sqlLogDriver.php
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne CHAMPETIER <etienne.champetier@fiducial.net>
  • Loading branch information
Etienne CHAMPETIER committed Oct 7, 2013
1 parent 4a45efe commit e3dc03d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
7 changes: 7 additions & 0 deletions core/src/core/classes/class.AJXP_Utils.php
Expand Up @@ -1553,6 +1553,13 @@ public static function cleanDibiDriverParameters($params)
unset($params[$k]);
}
}
switch ($params["driver"]) {
case "sqlite":
case "sqlite3":
$params["formatDateTime"] = "'Y-m-d H:i:s'";
$params["formatDate"] = "'Y-m-d'";
break;
}
return $params;
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/plugins/core.ajaxplorer/ajxp_mixins.xml
Expand Up @@ -62,6 +62,8 @@
<global_param group_switch_name="dibi_provider" group_switch_label="MySQL" group_switch_value="mysql" name="mysql_username" default="" label="User" description="User name" type="string" mandatory="true"/>
<global_param group_switch_name="dibi_provider" group_switch_label="MySQL" group_switch_value="mysql" name="mysql_password" default="" label="Password" description="User password" type="password" mandatory="true"/>
<global_param group_switch_name="dibi_provider" group_switch_label="Sqlite 3" group_switch_value="sqlite3" name="sqlite3_driver" default="sqlite3" label="Driver" description="Driver type (do not touch)" type="hidden" mandatory="true"/>
<global_param group_switch_name="dibi_provider" group_switch_label="Sqlite 3" group_switch_value="sqlite3" name="sqlite3_formatDate" default="'Y-m-d'" label="formatDate" description="formatDate (do not touch)" type="hidden" mandatory="true"/>
<global_param group_switch_name="dibi_provider" group_switch_label="Sqlite 3" group_switch_value="sqlite3" name="sqlite3_formatDateTime" default="'Y-m-d H:i:s'" label="formatDateTime" description="formatDateTime (do not touch)" type="hidden" mandatory="true"/>
<global_param group_switch_name="dibi_provider" group_switch_label="Sqlite 3" group_switch_value="sqlite3" name="sqlite3_database" default="AJXP_DATA_PATH/plugins/conf.sql/ajaxplorer.db" label="File" description="Database file" type="string" mandatory="true"/>
</server_settings>
</dibidriver_provider>
Expand Down
56 changes: 23 additions & 33 deletions core/src/plugins/log.sql/class.sqlLogDriver.php
Expand Up @@ -64,18 +64,6 @@ public function performChecks()
}
}

/**
* Simple function to format Date objects to fit MySQL expected where condition
*
* @param Integer $unix_time timestamp to convert
* @return String Date string formatted to fit a MySQL where condition.
*/
public function toMysqlDateTime($unix_time)
{
$t = date('Y-m-d G:i:s', $unix_time);
return $t;
}

/**
* formats the error message in representable manner
*
Expand All @@ -102,7 +90,7 @@ public function formatMessage($message, $severity)
$severity = strtoupper((string) $severity);

$log_row = Array(
'logdate' => $this->toMysqlDateTime(strtotime('NOW')), // DateTime Constant introduced in PHP 5.1
'logdate' => new DateTime('NOW'),
'remote_ip' => $this->inet_ptod($_SERVER['REMOTE_ADDR']),
'severity' => $severity,
'user' => $user,
Expand Down Expand Up @@ -193,12 +181,21 @@ public function xmlListLogFiles($nodeName="file", $year=null, $month=null, $root
{
$xml_strings = array();

if ($this->sqlDriver["driver"] == "sqlite3") {
$yFunc = "strftime('%Y', [logdate])";
$mFunc = "strftime('%m', [logdate])";
} else {
$yFunc = "YEAR([logdate])";
$mFunc = "MONTH([logdate])";
switch ($this->sqlDriver["driver"]) {
case "sqlite":
case "sqlite3":
$yFunc = "strftime('%Y', [logdate])";
$mFunc = "strftime('%m', [logdate])";
$dFunc = "date([logdate])";
break;
case "mysql":
$yFunc = "YEAR([logdate])";
$mFunc = "MONTH([logdate])";
$dFunc = "DATE([logdate])";
break;
default:
echo "ERROR!, DB driver "+ $this->sqlDriver["driver"] +" not supported yet in __FUNCTION__";
exit(1);
}

try {
Expand All @@ -209,10 +206,10 @@ public function xmlListLogFiles($nodeName="file", $year=null, $month=null, $root
$end_time = mktime(0,0,0,$month+1,1,$year);

$q = 'SELECT
DISTINCT DATE([logdate]) AS logdate
DISTINCT '.$dFunc.' AS logdate
FROM [ajxp_log]
WHERE [logdate] >= %s AND [logdate] < %s';
$result = dibi::query($q, $this->toMysqlDateTime($start_time), $this->toMysqlDateTime($end_time));
WHERE [logdate] >= %t AND [logdate] < %t';
$result = dibi::query($q, $start_time, $end_time);

foreach ($result as $r) {
$log_time = strtotime($r['logdate']);
Expand All @@ -236,8 +233,8 @@ public function xmlListLogFiles($nodeName="file", $year=null, $month=null, $root
DISTINCT '.$yFunc.' AS year,
'.$mFunc.' AS month
FROM [ajxp_log]
WHERE [logdate] >= %s AND [logdate] < %s';
$result = dibi::query($q, $this->toMysqlDateTime($year_start_time), $this->toMysqlDateTime($year_end_time));
WHERE [logdate] >= %t AND [logdate] < %t';
$result = dibi::query($q, $year_start_time, $year_end_time);

foreach ($result as $r) {
/* We always recreate a unix timestamp while looping because it provides us with a uniform way to format the date.
Expand Down Expand Up @@ -293,15 +290,8 @@ public function xmlLogs($parentDir, $date, $nodeName = "log", $rootPath = "/logs
$end_time = mktime(0,0,0,date('m', $start_time), date('d', $start_time) + 1, date('Y', $start_time));

try {
if ($this->sqlDriver["driver"] == "sqlite3") {
$dateEnd = date('Y-m-d', $end_time);
$q = 'SELECT * FROM [ajxp_log] WHERE strftime("%s", logdate) BETWEEN strftime("%s", "'.$date.'") AND strftime("%s", "'.$dateEnd.'") ORDER BY logdate';
$result = dibi::query($q);
} else {
$q = 'SELECT * FROM [ajxp_log] WHERE [logdate] BETWEEN %d AND %d';
$result = dibi::query($q, $this->toMysqlDateTime($start_time), $this->toMysqlDateTime($end_time));
}
//$all = $result->fetchAll();
$q = 'SELECT * FROM [ajxp_log] WHERE [logdate] BETWEEN %t AND %t';
$result = dibi::query($q, $start_time, $end_time);
$log_items = "";

foreach ($result as $r) {
Expand Down

0 comments on commit e3dc03d

Please sign in to comment.