From c84dfdf09c0754282bdf92c016d7286385aa4c07 Mon Sep 17 00:00:00 2001
From: HugoFara <hugo.farajallah@protonmail.com>
Date: Thu, 28 Dec 2023 21:16:37 +0100
Subject: [PATCH] Fix: the demo database was unreadable since last commit.
 restore_file is much better at parsing SQL.

---
 docs/CHANGELOG.md       |  2 ++
 docs/info.html          |  2 ++
 inc/session_utility.php | 67 ++++++++++++++++++++++++++++-------------
 3 files changed, 50 insertions(+), 21 deletions(-)

diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 86afce7a..0666def7 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -38,6 +38,8 @@ and `click_faster` were declared two times in `src/js/audio_controller.js`.
 * When opening a text, text position was not saved and a `text_id is unknown` error was triggered. 
 * Do not display an error on step 2 of feed wizard at first initialization 
 ([#129](https://github.com/HugoFara/lwt/issues/129)).
+* It was impossible to install the demo database if they was more or less than one instruction a line.
+This is fixed, and the SQL file was made more readable. 
 
 ### Deprecated
 
diff --git a/docs/info.html b/docs/info.html
index 229eb732..1afad388 100644
--- a/docs/info.html
+++ b/docs/info.html
@@ -2217,6 +2217,8 @@ <h4>Fixed</h4>
 <li>When opening a text, text position was not saved and a <code>text_id is unknown</code> error was triggered.</li>
 <li>Do not display an error on step 2 of feed wizard at first initialization
 (<a href="https://github.com/HugoFara/lwt/issues/129">#129</a>).</li>
+<li>It was impossible to install the demo database if they was more or less than one instruction a line.
+This is fixed, and the SQL file was made more readable.</li>
 </ul>
 <h4>Deprecated</h4>
 <ul>
diff --git a/inc/session_utility.php b/inc/session_utility.php
index 70bab7b8..6a86f0d8 100644
--- a/inc/session_utility.php
+++ b/inc/session_utility.php
@@ -4509,6 +4509,7 @@ function insertExpressions($textlc, $lid, $wid, $len, $mode): null|string
  * @since 2.0.3-fork Function was broken
  * @since 2.5.3-fork Function repaired
  * @since 2.7.0-fork $handle should be for an *uncompressed* file.
+ * @since 2.9.1-fork It can read SQL with more or less than one instruction a line
  */
 function restore_file($handle, $title): string 
 {
@@ -4522,45 +4523,69 @@ function restore_file($handle, $title): string
     $drops = 0;
     $inserts = 0;
     $creates = 0;
-    $start = 1;
+    $start = true;
+    $curr_content = '';
+    $queries_list = array();
     while (!feof($handle)) {
+        $stream = fgets($handle);
+        if ($stream === false) {
+            break;
+        }
+        // Check file header
+        if ($start) {
+            if (!str_starts_with($stream, "-- lwt-backup-")  
+                && !str_starts_with($stream, "-- lwt-exp_version-backup-")
+            ) {
+                $message = "Error: Invalid $title Restore file " .
+                "(possibly not created by LWT backup)";
+                $errors = 1;
+                break;
+            }
+            $start = false;
+            continue;
+        }
+        // Skip comments
+        if (str_starts_with($stream, '-- ')) {
+            continue;
+        }
+        // Add stream to accumulator
+        $curr_content .= $stream;
+        // Get queries
+        $queries = explode(';' . PHP_EOL, $curr_content);
+        // Replace line by remainders of the last element (incomplete line)
+        $curr_content = array_pop($queries);
+        //var_dump("queries", $queries);
+        foreach ($queries as $query) {
+            $queries_list[] = trim($query);
+        }
+    } // while (! feof($handle))
+    fclose($handle);
+    // Now run all queries
+    foreach ($queries_list as $query) {
         $sql_line = trim(
-            str_replace("\r", "", str_replace("\n", "", fgets($handle, 99999)))
+            str_replace("\r", "", str_replace("\n", "", $query))
         );
         if ($sql_line != "") {
-            if ($start) {
-                if (strpos($sql_line, "-- lwt-backup-") === false  
-                    && strpos($sql_line, "-- lwt-exp_version-backup-") === false
-                ) {
-                    $message = "Error: Invalid $title Restore file " .
-                    "(possibly not created by LWT backup)";
-                    $errors = 1;
-                    break;
-                }
-                $start = 0;
-                continue;
-            }
-            if (substr($sql_line, 0, 3) !== '-- ') {
+            if (!str_starts_with($query, '-- ')) {
                 $res = mysqli_query(
-                    $GLOBALS['DBCONNECTION'], insert_prefix_in_sql($sql_line)
+                    $GLOBALS['DBCONNECTION'], insert_prefix_in_sql($query)
                 );
                 $lines++;
                 if ($res == false) { 
                     $errors++; 
                 } else {
                     $ok++;
-                    if (substr($sql_line, 0, 11) == "INSERT INTO") { 
+                    if (str_starts_with($query,  "INSERT INTO")) { 
                         $inserts++; 
-                    } else if (substr($sql_line, 0, 10) == "DROP TABLE") { 
+                    } else if (str_starts_with($query, "DROP TABLE")) { 
                         $drops++;
-                    } else if (substr($sql_line, 0, 12) == "CREATE TABLE") { 
+                    } else if (str_starts_with($query, "CREATE TABLE")) { 
                         $creates++;
                     }
                 }
             }
         }
-    } // while (! feof($handle))
-    fclose($handle);
+    }
     if ($errors == 0) {
         runsql("DROP TABLE IF EXISTS {$tbpref}textitems", '');
         check_update_db($debug, $tbpref, $dbname);