scripts-bash/local/share/ytdll/lib/Config.php

92 lines
2.7 KiB
PHP

<?php
// Auteur : Cédrix pour a5l.fr
// Projet : scripts-bash/ytdll
// Version 2024.07.07-14.31
// Version initiale
class Config {
private $pdo;
private $database;
public function __construct(Database $database) {
$this->database = $database;
$this->pdo = $this->database->getPDO();
}
/**
* Set or update the version in the config table.
*
* @param string $version The version to set.
* @return bool True on success, false on failure.
*/
public function setVersionDefinitionsTables($version) {
try {
$stmt = $this->pdo->prepare("
INSERT INTO config (key, value)
VALUES ('versionDefinitionsTables', :version)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
");
$stmt->execute(['version' => $version]);
} catch (PDOException $e) {
// Log the error or handle it appropriately
error_log('Error setting version: ' . $e->getMessage());
return false;
}
}
/**
* Get the current version from the config table.
*
* @return string|null The current version or null if not set.
*/
public function getVersionDefinitionsTables() {
try {
$stmt = $this->pdo->prepare("SELECT value FROM config WHERE key = 'versionDefinitionsTables'");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row ? $row['value'] : null;
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
// Return a default value or null
return null;
}
}
/**
* Get the current path Definitions Tables from the config table.
*
* @return string The current version or default path if not set.
*/
public function getPathDefinitionsTablesFile() {
$defaultPath = getenv('HOME') . "/.local/share/ytdll/definitionsTables.json";
return $defaultPath;
}
/**
* Verify database privileges by attempting to create and drop a test table.
*
* @return bool True if privileges are sufficient, false otherwise.
*/
public function verifyPrivileges() {
try {
$tableManager = new TableManager($this->database);
$tableManager->createTable('test_table', [
['name' => 'id', 'type' => 'SERIAL PRIMARY KEY'],
['name' => 'name', 'type' => 'VARCHAR(255)']
]);
$tableManager->dropTable('test_table');
return true;
} catch (PDOException $e) {
error_log("Erreur sur les privilèges d'accès à la base de données : " . $e->getMessage());
return false;
}
}
}