From 8b7ccda1b1c2f90dc18ff2d482c7e6a284745ffc Mon Sep 17 00:00:00 2001
From: Junior <junior@jaj.com>
Date: Tue, 6 May 2025 12:11:38 -0400
Subject: [PATCH] Add total, new, purged counts to output. Output json if from
 browser.

---
 install/parsebooks.php | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/install/parsebooks.php b/install/parsebooks.php
index db4c986..54be1d4 100644
--- a/install/parsebooks.php
+++ b/install/parsebooks.php
@@ -5,8 +5,14 @@ require dirname(__FILE__) . "/../variables.php";
 $dbbooks = array();
 $parsedbooks = array();
 
+$cli = ( php_sapi_name() != "cli" ) ? false : true;
+
+$count_total = 0;
+$count_new = 0;
+$count_purged = 0;
+
 function walkBooks($path = BOOKDIR) {
-   global $globaldbh, $parsedbooks;
+   global $globaldbh, $parsedbooks, $count_new, $count_total;
    $contents = glob($path . "/*");
    foreach ( $contents as $item ) {
       if ( is_dir($item) ) {
@@ -16,12 +22,17 @@ function walkBooks($path = BOOKDIR) {
          $dbfile = basename($item);
          $dbmtime = date("Y-m-d H:i:s", filemtime($item));
          $parsedbooks[] = $dbpath . (($path == BOOKDIR) ? "" : "/") . $dbfile;
-         $query = "INSERT INTO books (path, filename, mtime) VALUES(:path, :filename, :mtime) ON DUPLICATE KEY UPDATE id=id";
+         $query = "INSERT INTO books (path, filename, mtime) VALUES(:path, :filename, :mtime)";
          $sth = $globaldbh->prepare($query);
          $sth->bindValue(":path", $dbpath, PDO::PARAM_STR);
          $sth->bindValue(":filename", $dbfile, PDO::PARAM_STR);
          $sth->bindValue(":mtime", $dbmtime, PDO::PARAM_STR);
-         $sth->execute();
+         try {
+            $sth->execute();
+            $count_new++;
+         } catch (PDOException $e) {
+         }
+         $count_total++;
       }
    }
 }
@@ -40,7 +51,22 @@ foreach ( $dbbooks as $dbbook ) {
    if ( !is_file(BOOKDIR . $dbbook) ) {
       $sth->bindValue(":target", $dbbook, PDO::PARAM_STR);
       $sth->execute();
+      $count_purged++;
    }
 }
 
+if ( $cli ) {
+   echo "New Books: {$count_new}\n";
+   echo "Purged Books: {$count_purged}\n";
+   echo "Total Books: {$count_total}\n";
+} else {
+   $data = array();
+   $data["new"] = $count_new;
+   $data["purged"] = $count_purged;
+   $data["total"] = $count_total;
+   header('Content-Type: application/json');
+   echo json_encode($data);
+   exit();
+}
+
 // vim: set sw=3 ts=3: