Query Consolidate Code for API Preparations

All SQL Queries were moved into their respective functions and parameterized, there were some slight logic changes in the front end pages to accommodate data being returned differently but existing functionality is maintained

A common function is used to execute the queries and looks after binding any parameterized values that have been supplied.

Other code for deleting / protecting detection's, frequency shift, Flickr API call etc have also been moved into common.php to be used later by the API

Two new functions getDirectory and getFilePath were introduced in aims and make it easier to relocate files. This will be worked into all existing php files to replace hardcoded paths
This commit is contained in:
jaredb7
2023-05-07 11:59:50 +10:00
parent 08c5abd566
commit bf0131cf27
7 changed files with 1479 additions and 701 deletions
+62 -78
View File
@@ -1,7 +1,5 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
<?php
include_once "./scripts/common.php";
$startdate = strtotime('last sunday') - (7*86400);
$enddate = strtotime('last sunday') - (1*86400);
@@ -16,44 +14,38 @@ if(isset($_GET['ascii'])) {
header("refresh: 0;");
}
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) DESC');
if($statement1 == False){
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute();
$weekly_species_counts = getWeeklyReportSpeciesDetectionCounts();
$statement4 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
if($statement4 == False){
echo "Database is busy";
header("refresh: 0;");
if($weekly_species_counts['detections']['success'] == False){
echo $weekly_species_counts['detections']['message'];
header("refresh: 0;");
}
$result4 = $statement4->execute();
$totalcount = $result4->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
$result1 = $weekly_species_counts['detections']['data'];
$statement5 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
if($statement5 == False){
echo "Database is busy";
header("refresh: 0;");
if($weekly_species_counts['totalcount']['success'] == False){
echo $weekly_species_counts['totalcount']['message'];
header("refresh: 0;");
}
$result5 = $statement5->execute();
$priortotalcount = $result5->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
$totalcount = $weekly_species_counts['totalcount']['data']['COUNT(*)'];
$statement6 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
if($statement6 == False){
echo "Database is busy";
header("refresh: 0;");
if($weekly_species_counts['priortotalcount']['success'] == False){
echo $weekly_species_counts['priortotalcount']['message'];
header("refresh: 0;");
}
$result6 = $statement6->execute();
$totalspeciestally = $result6->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
$priortotalcount = $weekly_species_counts['priortotalcount']['data']['COUNT(*)'];
$statement7 = $db->prepare('SELECT COUNT(DISTINCT(Com_Name)) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate- (7*86400)).'" AND "'.date("Y-m-d",$enddate- (7*86400)).'"');
if($statement7 == False){
echo "Database is busy";
header("refresh: 0;");
$weekly_species_talley = getWeeklyReportSpeciesTalley();
if($weekly_species_talley['totalspeciestally']['success'] == False){
echo $weekly_species_talley['totalspeciestally']['message'];
header("refresh: 0;");
}
$result7= $statement7->execute();
$priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
$totalspeciestally = $weekly_species_talley['totalspeciestally']['data']['COUNT(DISTINCT(Com_Name))'];
if($weekly_species_talley['priortotalspeciestally']['success'] == False){
echo $weekly_species_talley['priortotalspeciestally']['message'];
header("refresh: 0;");
}
$priortotalspeciestally = $weekly_species_talley['priortotalspeciestally']['data']['COUNT(DISTINCT(Com_Name))'];
$percentagedifftotal = round( (($totalcount - $priortotalcount) / $priortotalcount) * 100 );
@@ -72,11 +64,10 @@ if(isset($_GET['ascii'])) {
$detections = [];
$i = 0;
while($detection=$result1->fetchArray(SQLITE3_ASSOC))
foreach ($result1 as $detection)
{
$detections[$detection["Com_Name"]] = $detection["COUNT(*)"];
}
echo "# BirdNET-Pi: Week ".date('W', $enddate)." Report\n";
echo "Total Detections: <b>".$totalcount."</b> (".$percentagedifftotal.")<br>";
@@ -90,14 +81,12 @@ if(isset($_GET['ascii'])) {
$i++;
if($i <= 10) {
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date BETWEEN "'.date("Y-m-d",$startdate - (7*86400)).'" AND "'.date("Y-m-d",$enddate - (7*86400)).'"');
if($statement2 == False){
echo "Database is busy";
header("refresh: 0;");
$statement2 = getWeeklyReportSpeciesDetection($com_name);
if($statement2['success'] == False){
echo $statement2['message'];
header("refresh: 0;");
}
$result2 = $statement2->execute();
$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
$priorweekcount = $totalcount['COUNT(*)'];
$priorweekcount = $statement2['data']['COUNT(*)'];
// really percent changed
if($priorweekcount > 0){
@@ -121,14 +110,12 @@ if(isset($_GET['ascii'])) {
$newspeciescount=0;
foreach($detections as $com_name=>$scount)
{
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
if($statement3 == False){
echo "Database is busy";
header("refresh: 0;");
$statement3 = getWeeklyReportSpeciesDetection($com_name,false);
if($statement3['success'] == False){
echo $statement3['message'];
header("refresh: 0;");
}
$result3 = $statement3->execute();
$totalcount = $result3->fetchArray(SQLITE3_ASSOC);
$nonthisweekcount = $totalcount['COUNT(*)'];
$nonthisweekcount = $statement3['data']['COUNT(*)'];
if($nonthisweekcount == 0) {
$newspeciescount++;
@@ -153,26 +140,27 @@ if(isset($_GET['ascii'])) {
echo "<h1>Week ".date('W', $enddate)." Report</h1>".date('F jS, Y',$startdate)." — ".date('F jS, Y',$enddate)."<br>";
?></div><?php
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
if($db == False){
echo "Database is busy";
header("refresh: 0;");
}
if($debug == false){
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) DESC');
$weekly_species_counts = getWeeklyReportSpeciesDetectionCounts();
if($weekly_species_counts['detections']['success'] == False){
echo $weekly_species_counts['detections']['message'];
header("refresh: 0;");
}
$result1 = $weekly_species_counts['detections']['data'];
} else {
$statement1 = $db->prepare('SELECT DISTINCT(Com_Name), COUNT(*) FROM detections WHERE Date BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'" GROUP By Com_Name ORDER BY COUNT(*) ASC');
$weekly_species_counts = getWeeklyReportSpeciesDetectionCounts(false);
if($weekly_species_counts['detections']['success'] == False){
echo $weekly_species_counts['detections']['message'];
header("refresh: 0;");
}
$result1 = $weekly_species_counts['detections']['data'];
}
if($statement1 == False){
echo "Database is busy";
header("refresh: 0;");
}
$result1 = $statement1->execute();
$detections = [];
$i = 0;
while($detection=$result1->fetchArray(SQLITE3_ASSOC))
foreach ($result1 as $detection)
{
if($debug == true){
if($i > 10) {
@@ -201,14 +189,12 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
{
$i++;
if($i <= 10) {
$statement2 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date BETWEEN "'.date("Y-m-d",$startdate - (7*86400)).'" AND "'.date("Y-m-d",$enddate - (7*86400)).'"');
if($statement2 == False){
echo "Database is busy";
header("refresh: 0;");
$statement2 = getWeeklyReportSpeciesDetection($com_name);
if($statement2['success'] == False){
echo $statement2['message'];
header("refresh: 0;");
}
$result2 = $statement2->execute();
$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
$priorweekcount = $totalcount['COUNT(*)'];
$priorweekcount = $statement2['data']['COUNT(*)'];
if ($priorweekcount > 0) {
$percentagediff = round( (($scount - $priorweekcount) / $priorweekcount) * 100 );
@@ -242,14 +228,12 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
$newspeciescount=0;
foreach($detections as $com_name=>$scount)
{
$statement3 = $db->prepare('SELECT COUNT(*) FROM detections WHERE Com_Name == "'.$com_name.'" AND Date NOT BETWEEN "'.date("Y-m-d",$startdate).'" AND "'.date("Y-m-d",$enddate).'"');
if($statement3 == False){
echo "Database is busy";
header("refresh: 0;");
$statement3 = getWeeklyReportSpeciesDetection($com_name,false);
if($statement3['success'] == False){
echo $statement3['message'];
header("refresh: 0;");
}
$result3 = $statement3->execute();
$totalcount = $result3->fetchArray(SQLITE3_ASSOC);
$nonthisweekcount = $totalcount['COUNT(*)'];
$nonthisweekcount = $statement3['data']['COUNT(*)'];
if($nonthisweekcount == 0) {
$newspeciescount++;