Skip to content
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

Improve error & deprecation reporting #81

Merged
merged 9 commits into from
Dec 31, 2024
7 changes: 7 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
// Require & load config.
\Porter\Config::getInstance()->set(loadConfig());

// See deprecation notices in debug mode only.
if (\Porter\Config::getInstance()->debugEnabled()) {
error_reporting(E_ALL);
} else {
error_reporting(E_ALL & ~E_DEPRECATED);
}

// Load source & target support.
\Porter\Support::getInstance()->setSources(loadSources());
\Porter\Support::getInstance()->setTargets(loadTargets());
23 changes: 15 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Porter;

use http\Exception;

class Config
{
private static $instance = null;

protected array $config = [];
protected array $config = [
'debug' => false,
'test_alias' => 'test',
'connections' => [],
];

/**
* Make it a singleton; there's only 1 config.
Expand Down Expand Up @@ -41,7 +47,7 @@ public function getConnections(): array
*/
public function debugEnabled(): bool
{
return $this->config['debug'];
return $this->config['debug'] ?? false;
}

/**
Expand Down Expand Up @@ -83,25 +89,26 @@ public function getConnectionAlias(string $alias): array
*
* @param string $alias
* @param array $info
* @throws \Exception
*/
protected function validateConnectionInfo(string $alias, array $info): void
{
// Alias not in config.
if (empty($info)) {
trigger_error('Config error: Alias "' . $alias . '" not found', E_USER_ERROR);
//trigger_error('Config error: Alias "' . $alias . '" not found', E_USER_ERROR);
throw new \Exception('Alias "' . $alias . '" not found in config');
}

// Type is required.
if (empty($info['type'])) {
trigger_error('Config error: No connection `type` for alias "' . $alias . '"', E_USER_ERROR);
throw new \Exception('No connection `type` for alias "' . $alias . '" in config');
}

// Database required fields.
if ($info['type'] === 'database') {
foreach (['adapter', 'host','name','user'] as $required_field) {
if (!array_key_exists($required_field, $info)) {
trigger_error('Config error: Database `' . $required_field .
'` missing for alias "' . $alias . '"', E_USER_ERROR);
foreach (['adapter', 'host','name','user'] as $property) {
if (!array_key_exists($property, $info)) {
throw new \Exception('Database `' . $property . '` missing for alias "' . $alias . '" in config');
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function toTextFormatter(?string $format, ?string $text): string
*/
public static function closeTags(?string $text): ?string
{
if (empty($text)) {
return $text;
}

// <br>
$text = str_replace('<br>', '<br/>', $text);
// <img src="">
Expand Down
2 changes: 1 addition & 1 deletion src/Functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function loadConfig(): array
if (file_exists(ROOT_DIR . '/config.php')) {
return require(ROOT_DIR . '/config.php');
} else {
trigger_error('Missing config.php — Make a copy of config-sample.php!');
//trigger_error('Missing config.php — Make a copy of config-sample.php!');
return require(ROOT_DIR . '/config-sample.php');
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Functions/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ function bb_Decodeit($matches)
function vanillaPhoto($path)
{
// Skip processing for blank entries.
if ($path === '') {
return '';
if (empty($path)) {
return $path;
}

// Vanilla can have URLs in the Photo field.
Expand Down
7 changes: 6 additions & 1 deletion src/Storage/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ private function sendBatch(array $batch)
{
$tableName = $this->getBatchTable();
$action = (in_array($tableName, $this->ignoreErrorsTables)) ? 'insertOrIgnore' : 'insert';
$this->connection->connection()->table($tableName)->$action($batch);
try {
$this->connection->connection()->table($tableName)->$action($batch);
} catch (\Illuminate\Database\QueryException $e) {
echo "\n\nBatch insert error: " . substr($e->getMessage(), 0, 500);
echo "\n[...]\n" . substr($e->getMessage(), -300) . "\n";
}
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Target/Flarum.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,12 @@ protected function comments(ExportModel $ex): void
->first();

// Save value for other associations (e.g. attachments).
$this->discussionPostOffset = $result->LastCommentID;
$this->discussionPostOffset = $result->LastCommentID ?? 0;

// Use DiscussionID but fast-forward it past highest CommentID to insure it's unique.
$discussions = $ex->dbImport()->table('PORT_Discussion')
->select(
$ex->dbImport()->raw('(DiscussionID + ' . $result->LastCommentID . ') as CommentID'),
$ex->dbImport()->raw('(DiscussionID + ' . $this->discussionPostOffset . ') as CommentID'),
'DiscussionID',
'InsertUserID',
'DateInserted',
Expand Down Expand Up @@ -772,11 +772,12 @@ public function reactions(ExportModel $ex)
->table('PORT_Comment')
->select($ex->dbImport()->raw('max(CommentID) as LastCommentID'))
->first();
$lastCommentID = $result->LastCommentID ?? 0;

/* @see Target\Flarum::comments() — replicate our math in the post split */
$discussionReactions = $ex->dbImport()->table('PORT_UserTag')
->select(
$ex->dbImport()->raw('(RecordID + ' . $result->LastCommentID . ') as RecordID'),
$ex->dbImport()->raw('(RecordID + ' . $lastCommentID . ') as RecordID'),
'UserID',
'TagID',
$ex->dbImport()->raw('TIMESTAMP(DateInserted) as DateInserted'),
Expand Down
Loading