Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding task actions to DB specific actions

  • Loading branch information...
commit da45ac59a44aaff82a474c35308036039ab448e5 1 parent d16144c
@evantahler authored
View
178 API/DB/DRIVERS/MYSQL/DirectDBFunctions.php
@@ -6,13 +6,17 @@
There are certain required global functions for DAVE that are very related to the DB type you are using. Those are defined here. If they make no sense for your DB type, then return true. They still need to be defined.
-- GetAPIRequestsCount()
-- LogAPIRequest()
+- _GetAPIRequestsCount()
+- _LogAPIRequest()
- _DBSetCache()
- _DBGetCache()
-- StartTransaction()
-- CreateDBSaveState()
-- RestoreDBSaveState()
+- _StartTransaction()
+- _CreateDBSaveState()
+- _RestoreDBSaveState()
+- _TruncateTable()
+- _CleanSessions()
+- _CleanLog()
+- _CleanCache()
***********************************************/
@@ -21,7 +25,7 @@
$SQL = 'SELECT COUNT(*) as "total" FROM `'.$CONFIG['LOG_DB'].'`.`'.$CONFIG['LogTable'].'` WHERE (`IP` = "'.$IP.'" AND `TimeStamp` > "'.date('Y-m-d H:i:s',time()-(60*60)).'") ;';
*/
-function GetAPIRequestsCount()
+function _GetAPIRequestsCount()
{
global $IP, $CONFIG, $DBOBJ;
@@ -51,7 +55,7 @@ function GetAPIRequestsCount()
$SQL= 'INSERT INTO `'.$CONFIG['LOG_DB'].'`.`'.$CONFIG['LogTable'].'` (`Action`, `APIKey`, `DeveloperID`, `ERROR`, `IP`, `Params`) VALUES ("'.mysql_real_escape_string($PARAMS["Action"],$Connection).'", "'.mysql_real_escape_string($PARAMS["APIKey"],$Connection).'", "'.mysql_real_escape_string($PARAMS["DeveloperID"],$Connection).'", "'.mysql_real_escape_string($ERROR,$Connection).'", "'.mysql_real_escape_string($IP,$Connection).'" , "'.mysql_real_escape_string(json_encode($PARAMS),$Connection).'");';
*/
-function LogAPIRequest()
+function _LogAPIRequest()
{
global $CONFIG, $PARAMS, $DBOBJ, $IP;
@@ -117,11 +121,10 @@ function _DBGetCache($Key)
else { return false; }
}
-
/*
If your database type supports it, start a transaction for this connection
*/
-function StartTransaction()
+function _StartTransaction()
{
global $DBOBJ;
if (($DBOBJ instanceof DBConnection) == true && $DBOBJ->GetStatus() == true)
@@ -138,7 +141,7 @@ function StartTransaction()
/*
I create a restorable copy of the entire working database. This may be the creation of "backup" tables, a file-based dump of the database, etc. This backup will be restored with RestoreDBSveState. This backup should leave the current state of the data and schema (if applicable) available in the "normal" tables, as well as copy it to the backup. For mySQL, we create backup tables denoted with ~~ before the table name.
*/
-function CreateDBSaveState($PARAMS = array())
+function _CreateDBSaveState($PARAMS = array())
{
global $CONFIG, $TABLES, $DBOBJ;
@@ -161,6 +164,9 @@ function CreateDBSaveState($PARAMS = array())
}
}
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){ $DBOBJ->Query("LOCK TABLES;"); }
+
foreach($TablesToSave as $table)
{
$Status = $DBOBJ->GetStatus();
@@ -177,13 +183,17 @@ function CreateDBSaveState($PARAMS = array())
break;
}
}
+
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){ $DBOBJ->Query("UNLOCK TABLES;"); }
+
return $output;
}
/*
I restore a copy of the entire working database (creted with CreateDBSaveState). I will erase any modifications (D's,A's,V's, or E's) since the backup state was created. For mySQL, we look for any tables with the ~~ name indicating that they are a backup. We then drop the existing table, and rename the backup.
*/
-function RestoreDBSveState($PARAMS = array())
+function _RestoreDBSaveState($PARAMS = array())
{
global $CONFIG, $TABLES, $DBOBJ;
@@ -205,6 +215,9 @@ function RestoreDBSveState($PARAMS = array())
}
}
}
+
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){ $DBOBJ->Query("LOCK TABLES;"); }
foreach($TablesToRestore as $table)
{
@@ -221,7 +234,150 @@ function RestoreDBSveState($PARAMS = array())
break;
}
}
+
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){ $DBOBJ->Query("UNLOCK TABLES;"); }
+
return $output;
}
+/*
+I will clear out all rows/objects from a table. I will also reset any auto-incrament counters to 0. In mySQL, this is the truncate command.
+*/
+function _TruncateTable($PARAMS = array())
+{
+ global $CONFIG, $DBOBJ;
+
+ $resp = "";
+ $stop = false;
+ if (strlen($PARAMS['table']) == 0)
+ {
+ $resp = 'Provide a table name with --table';
+ $stop = true;
+ }
+
+ if (strlen($PARAMS['DB']) > 0)
+ {
+ $ThisDB = $PARAMS['DB'];
+ }
+ else
+ {
+ $ThisDB = $CONFIG['DB'];
+ }
+
+ $Status = $DBOBJ->GetStatus();
+ if ($stop == false && !($Status === true))
+ {
+ $resp = "DB Error: ".$Status;
+ $stop = true;
+ }
+
+ if ($stop == false)
+ {
+ $SQL= 'TRUNCATE TABLE `'.$ThisDB.'`.`'.$PARAMS['table'].'`;';
+ $DBOBJ->Query($SQL);
+ if ($DBOBJ->NumRowsEffected() == 0)
+ {
+ $resp = $PARAMS['table']." table truncated from the ".$ThisDB." DB";
+ }
+ else
+ {
+ $resp = "Table ".$PARAMS['table']." cannot be found in ".$ThisDB;
+ }
+ }
+ else
+ {
+ $resp = "cannot connect to ".$ThisDB;
+ }
+ return $resp;
+}
+
+/*
+I will remove old sessions from the sessions table
+*/
+function _CleanSessions($PARAMS = array())
+{
+ global $CONFIG, $DBOBJ;
+ $stop = false;
+ $resp = "";
+
+ $Status = $DBOBJ->GetStatus();
+ if ($stop == false && !($Status === true))
+ {
+ $resp = "DB Error: ".$Status;
+ $stop = true;
+ }
+
+ if ($stop == false)
+ {
+ $SQL= 'DELETE FROM `SESSIONS` WHERE (`created_at` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['SessionAge'])).'") ;';
+ $DBOBJ->Query($SQL);
+ $resp = 'Deleted '.$DBOBJ->NumRowsEffected()." entries from the SESSIONS Table in the DB";
+ }
+ else
+ {
+ $resp = "cannot connect to database";
+ }
+ return $resp;
+}
+
+/*
+I will remove old log entries from the log table/db
+*/
+function _CleanLog()
+{
+ global $CONFIG, $DBOBJ;
+ $stop = false;
+ $resp = "";
+
+ $Status = $DBOBJ->GetStatus();
+ if ($stop == false && !($Status === true))
+ {
+ $resp = "DB Error: ".$Status;
+ $stop = true;
+ }
+
+ if ($stop == false)
+ {
+ $SQL= 'DELETE FROM `'.$CONFIG['LOG_DB'].'`.`'.$CONFIG['LogTable'].'` WHERE (`TimeStamp` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['LogAge'])).'") ;';
+ $DBOBJ->Query($SQL);
+ $resp = 'Deleted '.$DBOBJ->NumRowsEffected()." entries from the LOG Table in the ".$CONFIG['LOG_DB']." DB";
+ }
+ else
+ {
+ $resp = "cannot connect to ".$CONFIG['LOG_DB'];
+ }
+ return $resp;
+}
+
+/*
+I will remove old log entries from the log table/db
+*/
+function _CleanCache()
+{
+ global $CONFIG, $DBOBJ;
+ $stop = false;
+ $resp = "";
+
+ $Status = $DBOBJ->GetStatus();
+ if ($stop == false && !($Status === true))
+ {
+ $resp = "DB Error: ".$Status;
+ $stop = true;
+ }
+
+ if ($stop == false)
+ {
+ $SQL= 'DELETE FROM `'.$CONFIG['DB'].'`.`'.$CONFIG['CacheTable'].'` WHERE (`ExpireTime` < "'.(time() - $CONFIG['CacheTime']).'") ;';
+ $DBOBJ->Query($SQL);
+ $resp = 'Deleted '.$DBOBJ->NumRowsEffected()." entries from the CACHE DB";
+ }
+ else
+ {
+ $resp = "cannot connect to ".$CONFIG['LOG_DB'];
+ }
+ return $resp;
+}
+
+
?>
View
40 API/Tasks/CleanCache.php
@@ -13,35 +13,27 @@ class CleanCache extends task
public function run($PARAMS = array())
{
- global $CONFIG, $DBOBJ;
+ global $CONFIG;
- if (self::check_DBObj())
+ /////////////////////////////////////////////////////////////////////////
+ // Check the CACHE DB table for old entries, and remove them
+ $resp = _CleanCache($PARAMS); // I am defined in DirectDBFunctions in the DB Driver.
+ $this->task_log($resp);
+
+ /////////////////////////////////////////////////////////////////////////
+ // Check the CACHE Folder table for old entries, and remove them
+ $files = scandir($CONFIG['CacheFolder']);
+ $counter = 0;
+ foreach ($files as $num => $fname)
{
- /////////////////////////////////////////////////////////////////////////
- // Check the CACHE DB table for old entries, and remove them
- $SQL= 'DELETE FROM `'.$CONFIG['DB'].'`.`'.$CONFIG['CacheTable'].'` WHERE (`ExpireTime` < "'.(time() - $CONFIG['CacheTime']).'") ;';
- $Status = $DBOBJ->GetStatus();
- if ($Status === true)
- {
- $DBOBJ->Query($SQL);
- $this->task_log('Deleted '.$DBOBJ->NumRowsEffected()." entries from the CACHE DB");
- }
-
- /////////////////////////////////////////////////////////////////////////
- // Check the CACHE Folder table for old entries, and remove them
- $files = scandir($CONFIG['CacheFolder']);
- $counter = 0;
- foreach ($files as $num => $fname)
+ $ThisFile = $CONFIG['CacheFolder'].$fname;
+ if (file_exists($ThisFile) && ((time() - filemtime($ThisFile)) > $CONFIG['CacheTime']) && $fname != "." && $fname != ".." && $fname != ".svn")
{
- $ThisFile = $CONFIG['CacheFolder'].$fname;
- if (file_exists($ThisFile) && ((time() - filemtime($ThisFile)) > $CONFIG['CacheTime']) && $fname != "." && $fname != ".." && $fname != ".svn")
- {
- unlink($ThisFile);
- $counter++;
- }
+ unlink($ThisFile);
+ $counter++;
}
- $this->task_log('Deleted '.$counter." files from the CACHE directory");
}
+ $this->task_log('Deleted '.$counter." files from the CACHE directory");
}
}
View
10 API/Tasks/CleanLog.php
@@ -13,14 +13,8 @@ class CleanLog extends task
public function run($PARAMS = array())
{
- global $CONFIG, $DBOBJ;
-
- if (self::check_DBObj())
- {
- $SQL= 'DELETE FROM `'.$CONFIG['LOG_DB'].'`.`'.$CONFIG['LogTable'].'` WHERE (`TimeStamp` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['LogAge'])).'") ;';
- $DBOBJ->Query($SQL);
- $this->task_log('Deleted '.$DBOBJ->NumRowsEffected()." entries from the LOG Table in the ".$CONFIG['LOG_DB']." DB");
- }
+ $resp = _CleanLog($PARAMS); // I am defined in DirectDBFunctions in the DB Driver.
+ $this->task_log($resp);
}
}
View
10 API/Tasks/CleanSessions.php
@@ -13,14 +13,8 @@ class CleanSessions extends task
public function run($PARAMS = array())
{
- global $CONFIG, $DBOBJ;
-
- if (self::check_DBObj())
- {
- $SQL= 'DELETE FROM `SESSIONS` WHERE (`created_at` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['SessionAge'])).'") ;';
- $DBOBJ->Query($SQL);
- $this->task_log('Deleted '.$DBOBJ->NumRowsEffected()." entries from the SESSIONS Table in the DB");
- }
+ $resp = _CleanSessions($PARAMS); // I am defined in DirectDBFunctions in the DB Driver.
+ $this->task_log($resp);
}
}
View
2  API/Tasks/CreateDBSaveState.php
@@ -13,7 +13,7 @@ class CreateDBSaveState extends task
public function run($PARAMS = array())
{
- $resp = CreateDBSaveState($PARAMS); // Defined by DB Driver
+ $resp = _CreateDBSaveState($PARAMS); // Defined by DB Driver
foreach($resp as $line){ $this->task_log($line); }
}
}
View
2  API/Tasks/RestoreDBSaveState.php
@@ -13,7 +13,7 @@ class RestoreDBSaveState extends task
public function run($PARAMS = array())
{
- $resp = RestoreDBSveState($PARAMS); // Defined by DB Driver
+ $resp = _RestoreDBSaveState($PARAMS); // Defined by DB Driver
foreach($resp as $line){ $this->task_log($line); }
}
}
View
33 API/Tasks/TruncateTable.php
@@ -13,37 +13,8 @@ class TruncateTable extends task
public function run($PARAMS = array())
{
- global $CONFIG, $DBOBJ;
-
- $stop = false;
- if (strlen($PARAMS['table']) == 0)
- {
- $this->task_log('Provide a table name with --table');
- $stop = true;
- }
-
- if (strlen($PARAMS['DB']) > 0)
- {
- $ThisDB = $PARAMS['DB'];
- }
- else
- {
- $ThisDB = $CONFIG['DB'];
- }
-
- if (self::check_DBObj() && $stop == false)
- {
- $SQL= 'TRUNCATE TABLE `'.$ThisDB.'`.`'.$PARAMS['table'].'`;';
- $DBOBJ->Query($SQL);
- if ($DBOBJ->NumRowsEffected() == 0)
- {
- $this->task_log($PARAMS['table']." table truncated from the ".$ThisDB." DB");
- }
- else
- {
- $this->task_log("Table ".$PARAMS['table']." cannot be found in ".$ThisDB);
- }
- }
+ $resp = _TruncateTable($PARAMS); // I am defined in DirectDBFunctions in the DB Driver.
+ $this->task_log($resp);
}
}
View
6 API/index.php
@@ -50,7 +50,7 @@
{
if ($CONFIG['CorrectLimitLockPass'] != $PARAMS["LimitLockPass"])
{
- $_api_requests_so_far = GetAPIRequestsCount();
+ $_api_requests_so_far = _GetAPIRequestsCount();
if (!is_int($_api_requests_so_far)){ $OUTPUT['ERROR'] = $_api_requests_so_far; require('Output.php'); exit;}
$OUTPUT['APIRequestsRemaining'] = $CONFIG['RequestLimitPerHour'] - $_api_requests_so_far;
if ($OUTPUT['APIRequestsRemaining'] <= 0)
@@ -68,7 +68,7 @@
{
if ($PARAMS['Rollback'] == $CONFIG['RollbackPhrase'])
{
- StartTransaction();
+ _StartTransaction();
}
else
{
@@ -147,7 +147,7 @@
// output and cleanup
require('Output.php');
-if ($CONFIG['Logging'] == true){ LogAPIRequest(); }
+if ($CONFIG['Logging'] == true){ _LogAPIRequest(); }
@$DBOBJ->close();
?>
Please sign in to comment.
Something went wrong with that request. Please try again.