mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Start on SQLite database driver
This commit is contained in:
parent
2e5e229474
commit
03b86c222f
10 changed files with 100 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@ vendor/simplepie/*
|
||||||
#temp files
|
#temp files
|
||||||
cache/*
|
cache/*
|
||||||
test.php
|
test.php
|
||||||
|
newssync.db*
|
||||||
|
|
||||||
# Windows image file caches
|
# Windows image file caches
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace JKingWeb\NewsSync;
|
||||||
const BASE = __DIR__.DIRECTORY_SEPARATOR;
|
const BASE = __DIR__.DIRECTORY_SEPARATOR;
|
||||||
const NS_BASE = __NAMESPACE__."\\";
|
const NS_BASE = __NAMESPACE__."\\";
|
||||||
|
|
||||||
|
if(!defined(NS_BASE."INSTALL")) define(NS_BASE."INSTALL", false);
|
||||||
|
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
if($class=="SimplePie") return;
|
if($class=="SimplePie") return;
|
||||||
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class);
|
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class);
|
||||||
|
@ -14,4 +16,4 @@ spl_autoload_register(function ($class) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$conf = new Conf();
|
$data = new RuntimeData(new Conf());
|
|
@ -5,4 +5,18 @@ return [
|
||||||
"Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"",
|
"Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"",
|
||||||
"Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format",
|
"Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format",
|
||||||
"Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})",
|
"Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})",
|
||||||
|
|
||||||
|
"Exception.JKingWeb/NewsSync/Conf/Exception.fileMissing" => "Configuration file \"{0}\" does not exist",
|
||||||
|
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUnreadable" => "Insufficient permissions to read configuration file \"{0}\"",
|
||||||
|
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUncreatable" => "Insufficient permissions to write new configuration file \"{0}\"",
|
||||||
|
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUnwritable" => "Insufficient permissions to overwrite configuration file \"{0}\"",
|
||||||
|
"Exception.JKingWeb/NewsSync/Conf/Exception.fileCorrupt" => "Configuration file \"{0}\" is corrupt or does not conform to expected format",
|
||||||
|
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.extMissing" => "Required PHP extension for driver \"{0}\" not installed",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileMissing" => "Database file \"{0}\" does not exist",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnreadable" => "Insufficient permissions to open database file \"{0}\" for reading",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnwritable" => "Insufficient permissions to open database file \"{0}\" for writing",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnusable" => "Insufficient permissions to open database file \"{0}\" for reading or writing",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileUncreatable" => "Insufficient permissions to create new database file \"{0}\"",
|
||||||
|
"Exception.JKingWeb/NewsSync/Db/Exception.fileCorrupt" => "Database file \"{0}\" is corrupt or not a valid database",
|
||||||
];
|
];
|
|
@ -1,7 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace JKingWeb\NewsSync\Auth;
|
namespace JKingWeb\NewsSync\Auth;
|
||||||
use JKingWeb\NewsSync;
|
|
||||||
|
|
||||||
class Internal implements AuthInterface {
|
class Internal implements AuthInterface {
|
||||||
protected $conf;
|
protected $conf;
|
||||||
|
|
22
vendor/JKingWeb/NewsSync/Conf.php
vendored
22
vendor/JKingWeb/NewsSync/Conf.php
vendored
|
@ -6,16 +6,14 @@ class Conf {
|
||||||
public $lang = "en";
|
public $lang = "en";
|
||||||
|
|
||||||
public $dbClass = NS_BASE."Db\\DriverSQLite3";
|
public $dbClass = NS_BASE."Db\\DriverSQLite3";
|
||||||
public $dbSQLite3PDO = false;
|
|
||||||
public $dbSQLite3File = BASE."newssync.db";
|
public $dbSQLite3File = BASE."newssync.db";
|
||||||
public $dbPostgreSQLPDO = false;
|
public $dbSQLite3Key = "";
|
||||||
public $dbPostgreSQLHost = "localhost";
|
public $dbPostgreSQLHost = "localhost";
|
||||||
public $dbPostgreSQLUser = "newssync";
|
public $dbPostgreSQLUser = "newssync";
|
||||||
public $dbPostgreSQLPass = "";
|
public $dbPostgreSQLPass = "";
|
||||||
public $dbPostgreSQLPort = 5432;
|
public $dbPostgreSQLPort = 5432;
|
||||||
public $dbPostgreSQLDb = "newssync";
|
public $dbPostgreSQLDb = "newssync";
|
||||||
public $dbPostgreSQLSchema = "";
|
public $dbPostgreSQLSchema = "";
|
||||||
public $dbMySQLPDO = false;
|
|
||||||
public $dbMySQLHost = "localhost";
|
public $dbMySQLHost = "localhost";
|
||||||
public $dbMySQLUser = "newssync";
|
public $dbMySQLUser = "newssync";
|
||||||
public $dbMySQLPass = "";
|
public $dbMySQLPass = "";
|
||||||
|
@ -30,30 +28,26 @@ class Conf {
|
||||||
|
|
||||||
|
|
||||||
public function __construct(string $import_file = "") {
|
public function __construct(string $import_file = "") {
|
||||||
if($import_file != "") $this->import_file($import_file);
|
if($import_file != "") $this->importFile($import_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function importFile(string $file): self {
|
public function importFile(string $file): self {
|
||||||
if(!file_exists($file)) throw new Conf\Exception("fileMissing");
|
if(!file_exists($file)) throw new Conf\Exception("fileMissing");
|
||||||
if(!is_readable($file)) throw new Conf\Exception("fileUnreadable");
|
if(!is_readable($file)) throw new Conf\Exception("fileUnreadable");
|
||||||
$json = @file_get_contents($file);
|
$arr = (@include $file);
|
||||||
if($json===false) throw new Conf\Exception("fileUnreadable");
|
if(!is_array($arr)) throw new Conf\Exception("fileCorrupt");
|
||||||
return $this->import($json);
|
return $this->import($arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function import(string $json): self {
|
public function import(array $arr): self {
|
||||||
if($json=="") throw new Conf\Exception("blank");
|
foreach($arr as $key => $value) {
|
||||||
$json = json_decode($json, true);
|
|
||||||
if(!is_array($json)) throw new Conf\Exception("corrupt");
|
|
||||||
foreach($json as $key => $value) {
|
|
||||||
$this->$$key = $value;
|
$this->$$key = $value;
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function export(string $file = ""): string {
|
public function export(string $file = ""): string {
|
||||||
return json_encode($this, JSON_PRETTY_PRINT);
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string {
|
public function __toString(): string {
|
||||||
|
|
15
vendor/JKingWeb/NewsSync/Database.php
vendored
Normal file
15
vendor/JKingWeb/NewsSync/Database.php
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
namespace JKingWeb\NewsSync;
|
||||||
|
|
||||||
|
class Database {
|
||||||
|
protected $drv;
|
||||||
|
|
||||||
|
public function __construct(Conf $conf) {
|
||||||
|
$driver = $conf->dbClass;
|
||||||
|
$this->drv = new $driver($conf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function listDrivers() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
7
vendor/JKingWeb/NewsSync/Db/DriverInterface.php
vendored
Normal file
7
vendor/JKingWeb/NewsSync/Db/DriverInterface.php
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
namespace JKingWeb\NewsSync\Db;
|
||||||
|
|
||||||
|
interface DriverInterface {
|
||||||
|
function __construct(\JKingWeb\NewsSync\Conf $conf);
|
||||||
|
static function driverName(): string;
|
||||||
|
}
|
44
vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php
vendored
Normal file
44
vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php
vendored
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
namespace JKingWeb\NewsSync\Db;
|
||||||
|
|
||||||
|
class DriverSQLite3 implements DriverInterface {
|
||||||
|
protected $db;
|
||||||
|
protected $pdo = false;
|
||||||
|
|
||||||
|
public function __construct(\JKingWeb\NewsSync\Conf $conf, bool $install = false) {
|
||||||
|
// check to make sure required extensions are loaded
|
||||||
|
if(class_exists("SQLite3")) {
|
||||||
|
$this->pdo = false;
|
||||||
|
} else if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) {
|
||||||
|
$this->pdo = true;
|
||||||
|
} else {
|
||||||
|
throw new Exception("extMissing", self::driverName());
|
||||||
|
}
|
||||||
|
// if the file exists (or we're initializing the database), try to open it and set initial options
|
||||||
|
if((!$install && file_exists($conf->dbSQLite3File)) || $install) {
|
||||||
|
try {
|
||||||
|
$this->db = ($this->PDO) ? (new \SQLite3($conf->dbSQLite3File, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $conf->dbSQLite3Key)) : (new PDO("sqlite:".$conf->dbSQLite3File));
|
||||||
|
//FIXME: add foreign key enforcement, WAL mode
|
||||||
|
} catch(\Throwable $e) {
|
||||||
|
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
|
||||||
|
foreach([$conf->dbSQLite3File, $conf->dbSQLite3File."-wal", $conf->dbSQLite3File."-shm"] as $file) {
|
||||||
|
if(!file_exists($file)) {
|
||||||
|
if($install && !is_writable(dirname($file))) throw new Exception("fileUncreatable", dirname($file));
|
||||||
|
throw new Exception("fileMissing", $file);
|
||||||
|
}
|
||||||
|
if(!is_readable($file) && !is_writable($file)) throw new Exception("fileUnusable", $file);
|
||||||
|
if(!is_readable($file)) throw new Exception("fileUnreadable", $file);
|
||||||
|
if(!is_writable($file)) throw new Exception("fileUnwritable", $file);
|
||||||
|
}
|
||||||
|
// otherwise the database is probably corrupt
|
||||||
|
throw new Exception("fileCorrupt", $conf->dbSQLite3File);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Exception("fileMissing", $conf->dbSQLite3File);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function driverName(): string {
|
||||||
|
return "SQLite3";
|
||||||
|
}
|
||||||
|
}
|
6
vendor/JKingWeb/NewsSync/Db/Exception.php
vendored
Normal file
6
vendor/JKingWeb/NewsSync/Db/Exception.php
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\NewsSync\Db;
|
||||||
|
|
||||||
|
class Exception extends \JKingWeb\NewsSync\Exception {
|
||||||
|
}
|
4
vendor/JKingWeb/NewsSync/RuntimeData.php
vendored
4
vendor/JKingWeb/NewsSync/RuntimeData.php
vendored
|
@ -8,9 +8,9 @@ class RuntimeData {
|
||||||
protected $auth;
|
protected $auth;
|
||||||
|
|
||||||
public function __construct(Conf $conf) {
|
public function __construct(Conf $conf) {
|
||||||
Lang::set();
|
|
||||||
$this->conf = $conf;
|
$this->conf = $conf;
|
||||||
//$this->db = new Database($this);
|
Lang::set($conf->lang);
|
||||||
|
$this->db = new Database($this->conf);
|
||||||
//$this->auth = new Authenticator($this);
|
//$this->auth = new Authenticator($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue