Skip to content

Commit

Permalink
feat: handle db migrations if no path is given
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Nov 23, 2023
1 parent 0b18c93 commit 9a5d438
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
9 changes: 9 additions & 0 deletions utils/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ public function migrate()
$pluginPath = str_replace('utils', '', __DIR__);
$migrationPath = $pluginPath . '/migrations/';

if(!file_exists(option('mauricerenck.indieConnector.sqlitePath', '.sqlite/'))) {
mkdir(option('mauricerenck.indieConnector.sqlitePath', '.sqlite/'));
}

$db = $this->connect();

if ($db === null || $db === false) {
return false;
}

$versionResult = $db->query('SELECT version FROM migrations ORDER BY version DESC LIMIT 1');

if (!Dir::exists($migrationPath)) {
Expand Down
22 changes: 10 additions & 12 deletions utils/WebmentionStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function updateOutbox(string $pageUuid, string $target)
public function getSummaryByMonth(int $year, int $month)
{
try {
$month = (integer)$month;
$month = (int)$month;
$month = $month < 10 ? '0' . $month : $month;

$result = $this->db->query('SELECT COUNT(id) as summary, * FROM webmentions WHERE mention_date LIKE "' . $year . '-' . $month . '-%" GROUP BY mention_type;');
Expand Down Expand Up @@ -114,7 +114,7 @@ public function getDetailsByMonth(int $timestamp)
public function getTargets(int $year, int $month)
{
try {
$month = (integer)$month;
$month = (int)$month;
$month = $month < 10 ? '0' . $month : $month;

$result = $this->db->query('SELECT mention_target, mention_type, COUNT(mention_type) as mentions FROM webmentions WHERE mention_date LIKE "' . $year . '-' . $month . '-%" GROUP BY mention_target, mention_type;');
Expand Down Expand Up @@ -159,7 +159,7 @@ public function getTargets(int $year, int $month)
public function getSources(int $year, int $month)
{
try {
$month = (integer)$month;
$month = (int)$month;
$month = $month < 10 ? '0' . $month : $month;

$result = $this->db->query('SELECT mention_source, mention_type, mention_image, COUNT(mention_type) as mentions FROM webmentions WHERE mention_date LIKE "' . $year . '-' . $month . '-%" GROUP BY mention_source, mention_type;');
Expand Down Expand Up @@ -198,7 +198,7 @@ public function getSources(int $year, int $month)
public function getSentMentions(int $year, int $month)
{
try {
$month = (integer)$month;
$month = (int)$month;
$month = $month < 10 ? '0' . $month : $month;

$result = $this->db->query('SELECT page_uuid, target FROM webmention_outbox WHERE sent_date LIKE "' . $year . '-' . $month . '-%";');
Expand All @@ -207,13 +207,13 @@ public function getSentMentions(int $year, int $month)
foreach ($result->data as $webmention) {
$page = page('page://' . $webmention->page_uuid);

if(is_null($page)) {
if (is_null($page)) {
$targets[] = [
'target' => $webmention->target,
'title' => $webmention->page_uuid,
'pageUrl' => '#',
'panelUrl' => '#',
];
'target' => $webmention->target,
'title' => $webmention->page_uuid,
'pageUrl' => '#',
'panelUrl' => '#',
];
continue;
}

Expand All @@ -223,7 +223,6 @@ public function getSentMentions(int $year, int $month)
'pageUrl' => $page->url(),
'panelUrl' => $page->panel()->url(),
];

}

return $targets;
Expand Down Expand Up @@ -291,7 +290,6 @@ private function connect()

return true;
} catch (Exception $e) {
echo 'Could not connect to Database: ', $e->getMessage(), "\n";
return false;
}
}
Expand Down

0 comments on commit 9a5d438

Please sign in to comment.