Skip to content

Commit 3bd9fdc

Browse files
committed
Add a database browser to the settings (thanks Dockwatch)
1 parent ae5c2f0 commit 3bd9fdc

10 files changed

Lines changed: 820 additions & 13 deletions

File tree

‎root/app/www/public/ajax/settings.php‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,84 @@
307307
'html' => ob_get_clean(),
308308
];
309309
break;
310+
case 'browseDatabase':
311+
$browseView = strval($_POST['view'] ?? 'list');
312+
$result = [
313+
'error' => true,
314+
'message' => translate('browseDatabaseFailed'),
315+
];
316+
317+
switch ($browseView) {
318+
case 'list':
319+
$browseSummary = $database->browseTablesSummary();
320+
ob_start();
321+
require RELATIVE_PATH . 'pages/settings/browseDatabase.php';
322+
$result = [
323+
'error' => false,
324+
'html' => ob_get_clean(),
325+
];
326+
break;
327+
case 'table':
328+
$browse = $database->browseTablePage($_POST['table'] ?? '', $_POST['page'] ?? 1, 25);
329+
if (!empty($browse['error'])) {
330+
$result = [
331+
'error' => true,
332+
'message' => $browse['message'] ?? translate('browseDatabaseFailed'),
333+
];
334+
break;
335+
}
336+
ob_start();
337+
require RELATIVE_PATH . 'pages/settings/browseDatabase.php';
338+
$result = [
339+
'error' => false,
340+
'html' => ob_get_clean(),
341+
'page' => intval($browse['page'] ?? 1),
342+
'pages' => intval($browse['pages'] ?? 1),
343+
'total' => intval($browse['total'] ?? 0),
344+
'perPage' => intval($browse['perPage'] ?? 25),
345+
];
346+
break;
347+
case 'schema':
348+
$browse = $database->browseTableSchema($_POST['table'] ?? '');
349+
if (!empty($browse['error'])) {
350+
$result = [
351+
'error' => true,
352+
'message' => $browse['message'] ?? translate('browseDatabaseSchemaFailed'),
353+
];
354+
break;
355+
}
356+
ob_start();
357+
require RELATIVE_PATH . 'pages/settings/browseDatabase.php';
358+
$result = [
359+
'error' => false,
360+
'html' => ob_get_clean(),
361+
'table' => strval($browse['table'] ?? ''),
362+
];
363+
break;
364+
case 'query':
365+
$browse = $database->browseRunQuery($_POST['sql'] ?? '', 500);
366+
if (!empty($browse['error'])) {
367+
$result = [
368+
'error' => true,
369+
'message' => $browse['message'] ?? translate('browseDatabaseQueryFailed'),
370+
];
371+
break;
372+
}
373+
ob_start();
374+
require RELATIVE_PATH . 'pages/settings/browseDatabase.php';
375+
$result = [
376+
'error' => false,
377+
'html' => ob_get_clean(),
378+
];
379+
break;
380+
default:
381+
$result = [
382+
'error' => true,
383+
'message' => translate('browseDatabaseFailed'),
384+
];
385+
break;
386+
}
387+
break;
310388
case 'downloadBackup':
311389
$folder = basename($_REQUEST['folder'] ?? '');
312390
$run = basename($_REQUEST['run'] ?? '');

‎root/app/www/public/classes/Database.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Database
2929
use UserEpisodeLink;
3030
use SyncHistory;
3131
use Settings;
32+
use DatabaseBrowse;
3233

3334
public $db;
3435
public $notificationPlatformTable;

‎root/app/www/public/classes/traits/Cron/History.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,25 @@ public function historyRepairUnlinkedItem($type, $item, $listenerApp, &$index)
428428
$beforeFlag = !empty($item['flag'][$listenerPlatform]);
429429
$beforeRemote = trim(strval($item['remote'][$listenerPlatform] ?? ''));
430430
$this->database->setMediaLibraryPlatformById($table, $listenerPlatform, $liveRemote, $fixedPath, $itemId);
431-
$saved = $type == 'movie' ? $this->database->getMovie($itemId) : $this->database->getEpisode($itemId);
431+
$dbError = trim(strval($this->database->getLastError()));
432+
$saved = $type == 'movie' ? $this->database->getMovie($itemId) : $this->database->getEpisode($itemId);
432433
if (!$saved) {
433434
return [];
434435
}
435436
$savedFlag = intval($saved[$flagName] ?? 0);
436437
$savedRemote = trim(strval($saved[$remoteName] ?? ''));
437438
if (!$savedFlag || $savedRemote != $liveRemote) {
439+
$owner = $type == 'movie'
440+
? $this->database->getMovieByRemoteId($listenerPlatform, $liveRemote)
441+
: $this->database->getEpisodeByRemoteId($listenerPlatform, $liveRemote);
442+
$ownerId = intval($owner['id'] ?? 0);
443+
$reason = $dbError != '' ? $dbError : 'remote not saved';
444+
if ($ownerId && $ownerId != $itemId) {
445+
$reason = 'unique remote already on id=' . $ownerId;
446+
}
438447
logger($this->logfile, 'history link repair failed id=' . $itemId . ' type=' . $type
439-
. ' app=' . strval($listenerApp['name'] ?? '') . ' remote=' . $liveRemote);
448+
. ' app=' . strval($listenerApp['name'] ?? '') . ' remote=' . $liveRemote
449+
. ' reason=' . $reason);
440450
loggerFlush($this->logfile);
441451
return [];
442452
}
@@ -813,7 +823,7 @@ public function syncWatchUser($members, $index, $both, $dry, $debug, &$dbRows, &
813823
public function clearAbsentLocalWatchLinks($members, $index, &$existing, $incoming, $dry, $debug, &$dbRows, &$pushRows, &$dryPlans, &$counts)
814824
{
815825
if (!empty($this->currentJob['history_libraries'])) {
816-
logger($this->logfile, 'skip local clear: history library scope is not authoritative for absences');
826+
logger($this->logfile, 'skip local clear: history sync wont mess with media library data');
817827
loggerFlush($this->logfile);
818828

819829
return;

‎root/app/www/public/classes/traits/Cron/Jobs.php‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,18 +1328,19 @@ public function queueHistoryAfterLibraryFinish()
13281328
return false;
13291329
}
13301330
$libraries = array_values($this->currentJob['history_libraries'] ?? []);
1331-
if (!$libraries) {
1332-
logger($log, 'no new libraries found, skipping auto history sync');
1331+
$movies = 0;
1332+
$series = 0;
1333+
$episodes = 0;
1334+
foreach ($this->currentJob['stats']['apps'] ?? [] as $appStats) {
1335+
$movies += intval($appStats['media']['movies']['added'] ?? 0);
1336+
$series += intval($appStats['media']['series']['added'] ?? 0);
1337+
$episodes += intval($appStats['media']['episodes']['added'] ?? 0);
1338+
}
1339+
if (!$libraries || ($movies + $series + $episodes) <= 0) {
1340+
logger($log, 'nothing new found, skipping auto history sync');
13331341
return false;
13341342
}
13351343

1336-
$ids = [];
1337-
foreach ($libraries as $library) {
1338-
$ids[] = strval($library['key'] ?? '');
1339-
}
1340-
$ids = array_values(array_filter($ids, fn($id) => $id != ''));
1341-
logger($log, count($libraries) . ' new libraries found, queuing history sync for libraries: ' . implode(', ', $ids));
1342-
13431344
$master = $mediaApps->masterMediaApp();
13441345
if (!$master) {
13451346
return false;
@@ -1365,6 +1366,8 @@ public function queueHistoryAfterLibraryFinish()
13651366
return false;
13661367
}
13671368

1369+
logger($log, 'new movies: ' . $movies . ', new series: ' . $series . ', new episodes: ' . $episodes . ' -> history sync queued');
1370+
13681371
return true;
13691372
}
13701373

0 commit comments

Comments
 (0)