_binary From 3c092896f38b5b43c2c4ff6bc1df0a376caf92f4 Mon Sep 17 00:00:00 2001
From: Pragya Rana <pragya.rana9@gmail.com>
Date: Fri, 10 Mar 2017 13:20:57 -0800
Subject: [PATCH] WIP: Deliverable 2: Patch 2

---
 src/configs/Config.php      |  5 +++++
 src/configs/Createdb.php    | 25 +++++++++++++++++++++++
 src/models/ProfileModel.php | 48 +++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/src/configs/Config.php b/src/configs/Config.php
index efafbb7..dfe9dfc 100755
--- a/src/configs/Config.php
+++ b/src/configs/Config.php
@@ -333,6 +333,7 @@ if (file_exists(WORK_DIRECTORY . PROFILE_FILE_NAME)) {
     /** @ignore */
     nsdefine('PROFILE', false);
     nsdefine('DBMS', 'Sqlite3');
+    nsdefine('PRIVATE_DBMS', 'Sqlite3');
     nsdefine('AUTHENTICATION_MODE', NORMAL_AUTHENTICATION);
     nsdefine('RECOVERY_MODE', EMAIL_RECOVERY);
     nsdefine('DEBUG_LEVEL', NO_DEBUG_INFO);
@@ -350,8 +351,12 @@ if (file_exists(WORK_DIRECTORY . PROFILE_FILE_NAME)) {
     nsdefine('MEDIA_MODE', 'name_server');
     nsdefine('DB_NAME', "default");
     nsdefine('DB_USER', '');
+    nsdefine('PRIVATE_DB_USER', '');
     nsdefine('DB_PASSWORD', '');
+    nsdefine('PRIVATE_DB_PASSWORD', '');
     nsdefine('DB_HOST', '');
+    nsdefine('PRIVATE_DB_HOST', '');
+    nsdefine('PRIVATE_DB_NAME', "default2");
     /** @ignore */
     nsdefine('CRAWL_DIR', BASE_DIR);
     /** @ignore */
diff --git a/src/configs/Createdb.php b/src/configs/Createdb.php
index eb314c9..34e49ad 100755
--- a/src/configs/Createdb.php
+++ b/src/configs/Createdb.php
@@ -50,33 +50,56 @@ require_once __DIR__."/../library/LocaleFunctions.php";
 /** To make it easy to insert translations */
 require_once __DIR__."/../library/UpgradeFunctions.php";
 $profile_model = new ProfileModel(DB_NAME, false);
+$private_profile_model = new ProfileModel(PRIVATE_DB_NAME, false);
 $db_class = NS_DATASOURCES . ucfirst(DBMS)."Manager";
+$private_db_class = NS_DATASOURCES . ucfirst(PRIVATE_DBMS)."Manager";
 $dbinfo = ["DBMS" => DBMS, "DB_HOST" => DB_HOST, "DB_USER" => DB_USER,
     "DB_PASSWORD" => DB_PASSWORD, "DB_NAME" => DB_NAME];
+$private_dbinfo = ["DBMS" => PRIVATE_DBMS, "DB_HOST" => PRIVATE_DB_HOST, "DB_USER" => PRIVATE_DB_USER,
+    "DB_PASSWORD" => PRIVATE_DB_PASSWORD, "DB_NAME" => PRIVATE_DB_NAME];
 if (!in_array(DBMS, ['sqlite', 'sqlite3'])) {
     $db = new $db_class();
+    $private_db = new $private_db_class();
     $db->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
+    $private_db->connect(PRIVATE_DB_HOST, PRIVATE_DB_USER, PRIVATE_DB_PASSWORD, PRIVATE_DB_NAME);
     /*  postgres doesn't let you drop a database while connected to it so drop
         tables instead first
      */
     $profile_model->initializeSql($db, $dbinfo);
+    $private_profile_model->initializeSqlPrivate($private_db, $private_dbinfo);
     $database_tables = array_keys($profile_model->create_statements);
+    $private_database_tables = array_keys($private_profile_model->private_create_statements);
     foreach ($database_tables as $table) {
         $db->execute("DROP TABLE ".$table);
     }
+    foreach ($private_database_tables as $table) {
+        $private_db->execute("DROP TABLE ".$table);
+    }
     $db->execute("DROP DATABASE IF EXISTS " . DB_NAME);
     $db->execute("CREATE DATABASE " . DB_NAME);
     $db->disconnect();
     $db->connect(); // default connection goes to actual DB
+    $private_db->execute("DROP DATABASE IF EXISTS ". PRIVATE_DB_NAME);
+    $private_db->execute("CREATE DATABASE ". PRIVATE_DB_NAME);
+    $private_db->disconnect();
+    $private_db->connect();
 } else {
     @unlink(CRAWL_DIR."/data/" . DB_NAME . ".db");
     $db = new $db_class();
     $db->connect();
+    @unlink(CRAWL_DIR."/data/" . PRIVATE_DB_NAME . ".db");
+    $private_db = new $private_db_class();
+    $private_db->connect();
 }
 if (!$profile_model->createDatabaseTables($db, $dbinfo)) {
     echo "\n\nCouldn't create database tables!!!\n\n";
     exit();
 }
+if (!$private_profile_model->createDatabaseTablesPrivate($private_db, $private_dbinfo)) {
+    echo "\n\nCouldn't create database tables!!!\n\n";
+    exit();
+}
+$private_db->execute("INSERT INTO TYPE_KEYS VALUES (1, 1, 'activity')");
 $db->execute("INSERT INTO VERSION VALUES (" . YIOOP_VERSION . ")");
 $creation_time = L\microTimestamp();
 //numerical value of the blank password
@@ -540,7 +563,9 @@ if (stristr(DB_HOST, "pgsql") !== false) {
 }
 
 $db->disconnect();
+$private_db->disconnect();
 if (in_array(DBMS, ['sqlite','sqlite3'])){
     chmod(CRAWL_DIR."/data/".DB_NAME.".db", 0666);
+    chmod(CRAWL_DIR."/data/".PRIVATE_DB_NAME.".db", 0666);
 }
 echo "Create DB succeeded\n";
\ No newline at end of file
diff --git a/src/models/ProfileModel.php b/src/models/ProfileModel.php
index 6413d39..5d01fad 100755
--- a/src/models/ProfileModel.php
+++ b/src/models/ProfileModel.php
@@ -59,7 +59,7 @@ class ProfileModel extends Model
         'LINK_WEIGHT', 'LOGO', 'M_LOGO', 'MAIL_PASSWORD',  'MAIL_SECURITY',
         'MAIL_SENDER', 'MAIL_SERVER', 'MAIL_SERVERPORT', 'MAIL_USERNAME',
         'MEMCACHE_SERVERS', 'MIN_RESULTS_TO_GROUP', 'NAME_SERVER',
-        'MEDIA_MODE',
+        'MEDIA_MODE', 'PRIVATE_DB_NAME', 'PRIVATE_DB_HOST', 'PRIVATE_DBMS', 'PRIVATE_DB_PASSWORD', 'PRIVATE_DB_USER',
         'PROXY_SERVERS', 'RECOVERY_MODE', 'REGISTRATION_TYPE', 'RESULT_SCORE',
         'ROBOT_INSTANCE','RSS_ACCESS',
         'SEARCH_ANALYTICS_MODE', 'SEARCHBAR_PATH',
@@ -82,6 +82,7 @@ class ProfileModel extends Model
      * @var array
      */
     public $create_statements;
+    public $private_create_statements;
     /**
      * {@inheritDoc}
      *
@@ -91,8 +92,10 @@ class ProfileModel extends Model
      */
     public function __construct($db_name = C\DB_NAME, $connect = true)
     {
+        echo "\n*****\t:". $db_name."\n";
         parent::__construct($db_name, $connect);
         $this->create_statements = [];
+        $this->private_create_statements = [];
     }
     /**
      * Used to construct $this->create_statements, the list of all SQL
@@ -105,6 +108,10 @@ class ProfileModel extends Model
      */
     public function initializeSql($dbm, $dbinfo)
     {
+        echo "\nCalled function: initializeSql \n";
+        print_r($dbm);
+        print_r($dbinfo);
+
         $auto_increment = $dbm->autoIncrement($dbinfo);
         $serial = $dbm->serialType($dbinfo);
         /**
@@ -330,6 +337,23 @@ class ProfileModel extends Model
             "VERSION" => "CREATE TABLE VERSION(ID INTEGER PRIMARY KEY)",
             ];
     }
+    public function initializeSqlPrivate($dbm, $dbinfo)
+    {
+        echo "\nCalled function: initializeSqlPrivate \n";
+        print_r($dbm);
+        print_r($dbinfo);
+        $auto_increment = $dbm->autoIncrement($dbinfo);
+        $serial = $dbm->serialType($dbinfo);
+        /**
+         * SQL statements used to create the Yioop database. Some of the
+         * these statements could use UNIQUE on some the columns that are
+         * later used in CREATE INDEX statements. However, because of
+         * restrictions on the number of bytes (not chars) in MYSQL for keys
+         * this has not been done.
+         */
+        $this->private_create_statements = ["TYPE_KEYS" =>
+            "CREATE TABLE TYPE_KEYS (KEY_ID INTEGER, TYPE_ID INTEGER, KEY_NAME VARCHAR(30))"];
+    }
     /**
      * Creates a folder to be used to maintain local information about this
      * instance of the Yioop/SeekQuarry engine
@@ -614,7 +638,8 @@ EOT;
      */
     public function createDatabaseTables($dbm, $dbinfo, $skip_list = [])
     {
-        $this->initializeSQL($dbm, $dbinfo);
+        echo "\nCalled function: createDatabaseTables\n";
+        $this->initializeSql($dbm, $dbinfo);
         $create_statements = $this->create_statements;
         foreach ($create_statements as $table_or_index => $statement) {
             if (in_array($table_or_index, $skip_list)) {
@@ -627,6 +652,25 @@ EOT;
         }
         return true;
     }
+    public function createDatabaseTablesPrivate($dbm, $dbinfo, $skip_list = [])
+    {
+        echo "\nCalled function: createDatabaseTablesPrivate\n";
+        $this->initializeSqlPrivate($dbm, $dbinfo);
+        $private_create_statements = $this->private_create_statements;
+        foreach ($private_create_statements as $table_or_index => $statement) {
+            if (in_array($table_or_index, $skip_list)) {
+                continue;
+            }
+            if (!$result = $dbm->execute($statement)) {
+                echo $statement." ERROR!";
+                return false;
+            }
+            // prana start
+            else { echo "STATEMENT: \t ".$statement."\n";}
+            // prana end
+        }
+        return true;
+    }
     /**
      * Checks if $dbinfo provides info to connect to an working instance of
      * app db.
-- 
2.8.4 (Apple Git-73)

