<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\models\datasources;
use seekquarry\yioop\configs as C;
/**
* Sqlite3Manager is the DatasourceManager Yioop uses when its databases are
* SQLite files, the default for a new install. It opens a database file
* through PHP's PDO (PHP Data Objects) layer and leaves the reading and
* writing to PdoManager, which it extends. SQLite version 3 files are
* read; older SQLite files are not.
*
* @author Chris Pollett
*/
class Sqlite3Manager extends PdoManager
{
/**
* $pdo_flag is true when PHP has the PDO driver for SQLite, which this
* manager needs to open a database, and false when it does not
* @var bool
*/
public $pdo_flag;
/**
* __construct makes the data folder DATA_DIR where it does not exist
* yet, since SQLite files live there, and notes whether the PDO driver
* for SQLite is present, printing a message when it is not.
*/
public function __construct()
{
parent::__construct();
if (!file_exists(C\DATA_DIR)) {
mkdir(C\DATA_DIR);
chmod(C\DATA_DIR, 0777);
}
if (class_exists("\PDO") &&
in_array("sqlite", \PDO::getAvailableDrivers())) {
$this->pdo_flag = true;
} else {
echo "PDO sqlite needs to be installed!";
$this->pdo_flag = false;
}
$this->db_name = null;
}
/**
* connect opens the SQLite database a model reads and writes. A name
* without a "/" is a file in DATA_DIR given without its ending .db; a
* name with one is a whole path. An empty name is refused with an
* exception: SQLite makes a new empty file for any path it is given,
* so an empty name would quietly open a fresh database called .db in
* place of the site's own.
* @param string $db_host not used but in base constructor
* @param string $db_user not used but in base constructor
* @param string $db_password not used but in base constructor
* @param string $db_name file name of the sqlite database, as above
* @return \PDO the PDO handle for the open SQLite connection
* (reusing an existing connection in active_connections when
* present)
*/
public function connect($db_host = C\DB_HOST,
$db_user = C\DB_USER,
$db_password = C\DB_PASSWORD, $db_name = C\DB_NAME)
{
if (trim((string)$db_name) === "") {
throw new \Exception("Sqlite3Manager: the database name is " .
"empty; DB_NAME or PRIVATE_DB_NAME must name a database");
}
if (!stristr($db_name, "/")) {
$connect = "sqlite:" . C\DATA_DIR . "/$db_name.db";
} else {
$connect = "sqlite:$db_name";
}
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_password = $db_password;
$this->db_name = $db_name;
$this->connect_time = time();
$this->connect_string = "$db_host:$db_user:$db_password:$db_name";
if (empty(self::$active_connections[$this->connect_string])) {
$this->pdo = new \PDO($connect);
/* On macOS a plain fsync leaves writes in the drive's own
cache, where a power cut loses them; these ask SQLite to
use F_FULLFSYNC instead. Other systems ignore them. They
hold for one connection only, so they are set on each one
rather than once when createdb makes the database. */
$this->pdo->exec("PRAGMA fullfsync = ON");
$this->pdo->exec("PRAGMA checkpoint_fullfsync = ON");
} else {
$this->pdo = self::$active_connections[$this->connect_string];
}
$this->updateConnectionCache();
$this->to_upper_dbms = "SQLITE3";
return $this->pdo;
}
}