-
Notifications
You must be signed in to change notification settings - Fork 1
Handle missing mdwiki database configuration #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| //--- | ||
| use PDO; | ||
| use PDOException; | ||
| use RuntimeException; | ||
|
|
||
| class Database | ||
| { | ||
|
|
@@ -45,7 +46,37 @@ private function set_db($server_name) | |
| // $ts_pw = posix_getpwuid(posix_getuid()); | ||
| // $ts_mycnf = parse_ini_file($ts_pw['dir'] . "/confs/db.ini"); | ||
| // --- | ||
| $ts_mycnf = parse_ini_file($this->home_dir . "/confs/db.ini"); | ||
| $configPath = $this->home_dir . "/confs/db.ini"; | ||
| $ts_mycnf = @parse_ini_file($configPath); | ||
|
|
||
| if ($ts_mycnf === false || !is_array($ts_mycnf)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The if ($ts_mycnf === false) { |
||
| error_log(sprintf('Database configuration file "%s" is missing or unreadable.', $configPath)); | ||
| throw new RuntimeException('Database configuration error.'); | ||
| } | ||
|
|
||
| $requiredKeys = ['user']; | ||
| $missingKeys = []; | ||
| foreach ($requiredKeys as $key) { | ||
| if (!isset($ts_mycnf[$key]) || $ts_mycnf[$key] === '') { | ||
| $missingKeys[] = $key; | ||
| } | ||
| } | ||
|
|
||
| if ($server_name !== 'localhost' && (!isset($ts_mycnf['password']) || $ts_mycnf['password'] === '')) { | ||
| $missingKeys[] = 'password'; | ||
| } | ||
|
|
||
| if (!empty($missingKeys)) { | ||
| error_log( | ||
| sprintf( | ||
| 'Database configuration file "%s" is missing required key(s): %s.', | ||
| $configPath, | ||
| implode(', ', array_unique($missingKeys)) | ||
| ) | ||
| ); | ||
| throw new RuntimeException('Database configuration error.'); | ||
| } | ||
|
|
||
| // --- | ||
| if ($server_name === 'localhost') { | ||
| $this->host = 'localhost:3306'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use APICalls\MdwikiSql\Database; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class MdwikiSqlDatabaseTest extends TestCase | ||
| { | ||
| private string $previousHome = ''; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $home = getenv('HOME'); | ||
| if ($home !== false) { | ||
| $this->previousHome = $home; | ||
| } | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| if ($this->previousHome === '') { | ||
| putenv('HOME'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } else { | ||
| putenv('HOME=' . $this->previousHome); | ||
| } | ||
| } | ||
|
|
||
| public function testMissingConfigThrowsRuntimeException(): void | ||
| { | ||
| $tmpDir = sys_get_temp_dir() . '/mdwiki_sql_test_' . uniqid(); | ||
| mkdir($tmpDir, 0777, true); | ||
| putenv('HOME=' . $tmpDir); | ||
|
|
||
| $this->expectException(\RuntimeException::class); | ||
| $this->expectExceptionMessage('Database configuration error'); | ||
|
|
||
| try { | ||
| new Database('localhost'); | ||
| } finally { | ||
| $this->removeDir($tmpDir); | ||
| } | ||
| } | ||
|
|
||
| private function removeDir(string $dir): void | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of the error suppression operator ( |
||
| { | ||
| if (!is_dir($dir)) { | ||
| return; | ||
| } | ||
|
|
||
| $items = scandir($dir); | ||
| if ($items === false) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($items as $item) { | ||
| if ($item === '.' || $item === '..') { | ||
| continue; | ||
| } | ||
|
|
||
| $path = $dir . DIRECTORY_SEPARATOR . $item; | ||
| if (is_dir($path)) { | ||
| $this->removeDir($path); | ||
| } else { | ||
| @unlink($path); | ||
| } | ||
| } | ||
|
|
||
| @rmdir($dir); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the error suppression operator (
@) is generally discouraged as it can hide important warnings (e.g., if the file is unreadable due to permissions) and make debugging more difficult. It's better practice to explicitly check for the file's existence and readability usingis_readable()before attempting to parse it.