mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 23:24:36 +00:00
Compare commits
22 Commits
temp-noop
...
fix/09630-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa8beb3794 | ||
|
|
3ad6e9eb8e | ||
|
|
21118f6fb3 | ||
|
|
c23302197a | ||
|
|
d54aa482f7 | ||
|
|
25133bc895 | ||
|
|
62cc1af8bf | ||
|
|
cf300bc5c3 | ||
|
|
0dde87cda0 | ||
|
|
17a76c4de2 | ||
|
|
2366f6366c | ||
|
|
43780cb9a3 | ||
|
|
7b915c310a | ||
|
|
6df469ab7a | ||
|
|
55294643b4 | ||
|
|
0f3c78f02c | ||
|
|
d80163a704 | ||
|
|
9e57c4ea3d | ||
|
|
30f1c0af18 | ||
|
|
00d14db57d | ||
|
|
6e52ba824c | ||
|
|
a1bac43150 |
165
include/nc_connector_scheduler.inc.php
Normal file
165
include/nc_connector_scheduler.inc.php
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
<?php
|
||||||
|
if (!defined('PHPWG_ROOT_PATH'))
|
||||||
|
{
|
||||||
|
die('Hacking attempt!');
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_paths()
|
||||||
|
{
|
||||||
|
$base = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools';
|
||||||
|
return array(
|
||||||
|
'base'=>$base,
|
||||||
|
'runtime'=>$base.'/nc-connector-runtime',
|
||||||
|
'state_root'=>$base.'/nc-connector-state',
|
||||||
|
'scheduler'=>$base.'/nc-connector-scheduler',
|
||||||
|
'state'=>$base.'/nc-connector-scheduler/state.json',
|
||||||
|
'trigger_lock'=>$base.'/nc-connector-scheduler/trigger.lock',
|
||||||
|
'worker_lock'=>$base.'/nc-connector-scheduler/worker.lock',
|
||||||
|
'log'=>$base.'/nc-connector-scheduler/last-worker.log',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_interval()
|
||||||
|
{
|
||||||
|
global $conf;
|
||||||
|
$interval = isset($conf['bratonien_nc_scheduler_interval']) ? (int)$conf['bratonien_nc_scheduler_interval'] : 60;
|
||||||
|
return max(60, $interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_ensure_dirs()
|
||||||
|
{
|
||||||
|
foreach (bratonien_tools_nc_scheduler_paths() as $key=>$path)
|
||||||
|
{
|
||||||
|
if (in_array($key, array('state','trigger_lock','worker_lock','log'), true)) continue;
|
||||||
|
if (!is_dir($path) && !@mkdir($path, 0750, true) && !is_dir($path))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_read_state()
|
||||||
|
{
|
||||||
|
$paths = bratonien_tools_nc_scheduler_paths();
|
||||||
|
if (!is_readable($paths['state'])) return array();
|
||||||
|
$decoded = json_decode((string)@file_get_contents($paths['state']), true);
|
||||||
|
return is_array($decoded) ? $decoded : array();
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_write_state(array $state)
|
||||||
|
{
|
||||||
|
$paths = bratonien_tools_nc_scheduler_paths();
|
||||||
|
if (!bratonien_tools_nc_scheduler_ensure_dirs()) return false;
|
||||||
|
$json = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
|
||||||
|
if (!is_string($json)) return false;
|
||||||
|
$tmp = $paths['state'].'.tmp';
|
||||||
|
if (@file_put_contents($tmp, $json."\n", LOCK_EX) === false) return false;
|
||||||
|
@chmod($tmp, 0640);
|
||||||
|
return @rename($tmp, $paths['state']);
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_install()
|
||||||
|
{
|
||||||
|
if (!bratonien_tools_nc_scheduler_ensure_dirs()) return false;
|
||||||
|
$state = bratonien_tools_nc_scheduler_read_state();
|
||||||
|
if (empty($state['next_due']))
|
||||||
|
{
|
||||||
|
$state['next_due'] = time();
|
||||||
|
$state['enabled'] = true;
|
||||||
|
$state['mode'] = 'piwigo-native';
|
||||||
|
bratonien_tools_nc_scheduler_write_state($state);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_php_binary()
|
||||||
|
{
|
||||||
|
foreach (array('/usr/bin/php', '/usr/local/bin/php', PHP_BINARY) as $candidate)
|
||||||
|
{
|
||||||
|
if (is_string($candidate) && $candidate !== '' && is_executable($candidate)) return $candidate;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_spawn($force = false)
|
||||||
|
{
|
||||||
|
$paths = bratonien_tools_nc_scheduler_paths();
|
||||||
|
if (!bratonien_tools_nc_scheduler_ensure_dirs())
|
||||||
|
{
|
||||||
|
throw new RuntimeException('Der native NC-Scheduler kann sein Laufzeitverzeichnis nicht anlegen.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$lock = @fopen($paths['trigger_lock'], 'c+');
|
||||||
|
if (!is_resource($lock) || !@flock($lock, LOCK_EX | LOCK_NB))
|
||||||
|
{
|
||||||
|
if (is_resource($lock)) fclose($lock);
|
||||||
|
return array('started'=>false, 'message'=>'Ein NC-Abgleich wird bereits vorbereitet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$state = bratonien_tools_nc_scheduler_read_state();
|
||||||
|
$now = time();
|
||||||
|
$next_due = (int)($state['next_due'] ?? 0);
|
||||||
|
if (!$force && $next_due > $now)
|
||||||
|
{
|
||||||
|
@flock($lock, LOCK_UN);
|
||||||
|
fclose($lock);
|
||||||
|
return array('started'=>false, 'message'=>'Der nächste NC-Abgleich ist noch nicht fällig.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$php = bratonien_tools_nc_scheduler_php_binary();
|
||||||
|
if ($php === '')
|
||||||
|
{
|
||||||
|
@flock($lock, LOCK_UN);
|
||||||
|
fclose($lock);
|
||||||
|
throw new RuntimeException('Kein ausführbares PHP-CLI für den nativen NC-Scheduler gefunden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$state['enabled'] = true;
|
||||||
|
$state['mode'] = 'piwigo-native';
|
||||||
|
$state['queued_at'] = $now;
|
||||||
|
$state['next_due'] = $now + bratonien_tools_nc_scheduler_interval();
|
||||||
|
bratonien_tools_nc_scheduler_write_state($state);
|
||||||
|
|
||||||
|
$runner = BRATONIEN_TOOLS_PATH.'runtime/native-runner.php';
|
||||||
|
$command = escapeshellarg($php).' '.escapeshellarg($runner).' >> '.escapeshellarg($paths['log']).' 2>&1 &';
|
||||||
|
$spec = array(
|
||||||
|
0=>array('file','/dev/null','r'),
|
||||||
|
1=>array('file','/dev/null','a'),
|
||||||
|
2=>array('file','/dev/null','a'),
|
||||||
|
);
|
||||||
|
$process = function_exists('proc_open') ? @proc_open(array('/bin/sh','-c',$command), $spec, $pipes) : false;
|
||||||
|
$exit = is_resource($process) ? proc_close($process) : 1;
|
||||||
|
|
||||||
|
@flock($lock, LOCK_UN);
|
||||||
|
fclose($lock);
|
||||||
|
|
||||||
|
if ($exit !== 0)
|
||||||
|
{
|
||||||
|
throw new RuntimeException('Der native NC-Abgleich konnte nicht gestartet werden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return array('started'=>true, 'message'=>'NC-Abgleich wurde gestartet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_scheduler_tick()
|
||||||
|
{
|
||||||
|
$state = bratonien_tools_nc_scheduler_read_state();
|
||||||
|
if (isset($state['enabled']) && !$state['enabled']) return;
|
||||||
|
$next_due = (int)($state['next_due'] ?? 0);
|
||||||
|
if ($next_due > time()) return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
bratonien_tools_nc_scheduler_spawn(false);
|
||||||
|
}
|
||||||
|
catch (Throwable $e)
|
||||||
|
{
|
||||||
|
$state = bratonien_tools_nc_scheduler_read_state();
|
||||||
|
$state['state'] = 'error';
|
||||||
|
$state['message'] = $e->getMessage();
|
||||||
|
$state['timestamp'] = time();
|
||||||
|
$state['next_due'] = time() + bratonien_tools_nc_scheduler_interval();
|
||||||
|
bratonien_tools_nc_scheduler_write_state($state);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,109 +4,13 @@ if (!defined('PHPWG_ROOT_PATH'))
|
|||||||
die('Hacking attempt!');
|
die('Hacking attempt!');
|
||||||
}
|
}
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_systemctl_value(array $args)
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_scheduler.inc.php');
|
||||||
{
|
|
||||||
if (!function_exists('proc_open'))
|
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$command = array_merge(array('/usr/bin/systemctl'), $args);
|
|
||||||
$spec = array(
|
|
||||||
0 => array('file', '/dev/null', 'r'),
|
|
||||||
1 => array('pipe', 'w'),
|
|
||||||
2 => array('pipe', 'w'),
|
|
||||||
);
|
|
||||||
$environment = array_merge($_ENV, array('LC_ALL'=>'C', 'LANG'=>'C'));
|
|
||||||
$process = @proc_open($command, $spec, $pipes, null, $environment);
|
|
||||||
if (!is_resource($process))
|
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
$stdout = stream_get_contents($pipes[1]);
|
|
||||||
stream_get_contents($pipes[2]);
|
|
||||||
fclose($pipes[1]);
|
|
||||||
fclose($pipes[2]);
|
|
||||||
$exit = proc_close($process);
|
|
||||||
return $exit === 0 ? trim((string)$stdout) : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_parse_systemd_time($value)
|
|
||||||
{
|
|
||||||
$value = trim((string)$value);
|
|
||||||
if ($value === '' || strtolower($value) === 'n/a')
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
$parsed = strtotime($value);
|
|
||||||
return $parsed === false ? 0 : (int)$parsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_monotonic_to_timestamp($value)
|
|
||||||
{
|
|
||||||
$value = trim((string)$value);
|
|
||||||
if ($value === '' || $value === '0' || strtolower($value) === 'n/a')
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preg_match('/^([0-9]+)(?:us)?$/', $value, $matches))
|
|
||||||
{
|
|
||||||
$next_boot_seconds = ((float)$matches[1]) / 1000000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$uptime_raw = @file_get_contents('/proc/uptime');
|
|
||||||
if (!is_string($uptime_raw) || !preg_match('/^([0-9]+(?:\.[0-9]+)?)/', trim($uptime_raw), $uptime_match))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$remaining = $next_boot_seconds - (float)$uptime_match[1];
|
|
||||||
if ($remaining < -1)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)round(time() + max(0, $remaining));
|
|
||||||
}
|
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_next_from_timer_list($timer)
|
|
||||||
{
|
|
||||||
$line = bratonien_tools_nc_connector_systemctl_value(array(
|
|
||||||
'list-timers', '--all', '--no-pager', '--no-legend', $timer,
|
|
||||||
));
|
|
||||||
if ($line === '')
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$first_line = trim((string)strtok($line, "\n"));
|
|
||||||
if (preg_match('/^(\S+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\s+\S+)/', $first_line, $matches))
|
|
||||||
{
|
|
||||||
$parsed = strtotime($matches[1]);
|
|
||||||
return $parsed === false ? 0 : (int)$parsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
||||||
{
|
{
|
||||||
$empty = array(
|
$empty = array(
|
||||||
'timestamp'=>0,
|
'timestamp'=>0,'label'=>'Nicht verfügbar','state'=>'','message'=>'','auth_mode'=>'',
|
||||||
'label'=>'Nicht verfügbar',
|
'api_state'=>'','api_message'=>'','fallback_state'=>'','fallback_message'=>'','error_detail'=>'',
|
||||||
'state'=>'',
|
|
||||||
'message'=>'',
|
|
||||||
'auth_mode'=>'',
|
|
||||||
'api_state'=>'',
|
|
||||||
'api_message'=>'',
|
|
||||||
'fallback_state'=>'',
|
|
||||||
'fallback_message'=>'',
|
|
||||||
'error_detail'=>'',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$connection_id = (int)($connection['id'] ?? 0);
|
$connection_id = (int)($connection['id'] ?? 0);
|
||||||
@@ -114,42 +18,25 @@ function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
|||||||
if ($connection_id > 0)
|
if ($connection_id > 0)
|
||||||
{
|
{
|
||||||
$candidates[] = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-connector-status/connection-'.$connection_id.'.json';
|
$candidates[] = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-connector-status/connection-'.$connection_id.'.json';
|
||||||
|
$candidates[] = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-connector-state/connection-'.$connection_id.'/connector-status.json';
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array();
|
$config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array();
|
||||||
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||||
if ($state_dir === '' && $connection_id > 0)
|
if ($state_dir !== '') $candidates[] = $state_dir.'/connector-status.json';
|
||||||
{
|
|
||||||
$state_dir = '/var/lib/bratonien-tools/nc-connector/connection-'.$connection_id;
|
|
||||||
}
|
|
||||||
if ($state_dir !== '')
|
|
||||||
{
|
|
||||||
$candidates[] = $state_dir.'/connector-status.json';
|
|
||||||
}
|
|
||||||
|
|
||||||
$decoded = null;
|
$decoded = null;
|
||||||
foreach ($candidates as $candidate)
|
foreach (array_unique($candidates) as $candidate)
|
||||||
{
|
{
|
||||||
if (!is_readable($candidate))
|
if (!is_readable($candidate)) continue;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$value = json_decode((string)@file_get_contents($candidate), true);
|
$value = json_decode((string)@file_get_contents($candidate), true);
|
||||||
if (is_array($value))
|
if (is_array($value)) { $decoded = $value; break; }
|
||||||
{
|
|
||||||
$decoded = $value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!is_array($decoded))
|
|
||||||
{
|
|
||||||
return $empty;
|
|
||||||
}
|
}
|
||||||
|
if (!is_array($decoded)) return $empty;
|
||||||
|
|
||||||
$timestamp = (int)($decoded['timestamp'] ?? 0);
|
$timestamp = (int)($decoded['timestamp'] ?? 0);
|
||||||
$api = isset($decoded['api']) && is_array($decoded['api']) ? $decoded['api'] : array();
|
$api = isset($decoded['api']) && is_array($decoded['api']) ? $decoded['api'] : array();
|
||||||
$fallback = isset($decoded['fallback']) && is_array($decoded['fallback']) ? $decoded['fallback'] : array();
|
$fallback = isset($decoded['fallback']) && is_array($decoded['fallback']) ? $decoded['fallback'] : array();
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'timestamp'=>$timestamp,
|
'timestamp'=>$timestamp,
|
||||||
'label'=>$timestamp > 0 ? date('d.m.Y H:i:s', $timestamp) : 'Nicht verfügbar',
|
'label'=>$timestamp > 0 ? date('d.m.Y H:i:s', $timestamp) : 'Nicht verfügbar',
|
||||||
@@ -166,99 +53,69 @@ function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
|||||||
|
|
||||||
function bratonien_tools_nc_connector_last_status(array $connections)
|
function bratonien_tools_nc_connector_last_status(array $connections)
|
||||||
{
|
{
|
||||||
$latest = array('timestamp'=>0, 'state'=>'', 'message'=>'', 'auth_mode'=>'', 'api_state'=>'', 'api_message'=>'', 'fallback_state'=>'', 'fallback_message'=>'', 'error_detail'=>'');
|
$latest = array('timestamp'=>0,'state'=>'','message'=>'','auth_mode'=>'','api_state'=>'','api_message'=>'','fallback_state'=>'','fallback_message'=>'','error_detail'=>'');
|
||||||
|
|
||||||
foreach ($connections as $connection)
|
foreach ($connections as $connection)
|
||||||
{
|
{
|
||||||
if (empty($connection['enabled']))
|
if (empty($connection['enabled'])) continue;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$status = bratonien_tools_nc_connector_connection_last_status($connection);
|
$status = bratonien_tools_nc_connector_connection_last_status($connection);
|
||||||
if ((int)$status['timestamp'] >= (int)$latest['timestamp'])
|
if ((int)$status['timestamp'] >= (int)$latest['timestamp']) $latest = $status;
|
||||||
{
|
|
||||||
$latest = $status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $latest;
|
return $latest;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_system_status(array $connections = array())
|
function bratonien_tools_nc_connector_system_status(array $connections = array())
|
||||||
{
|
{
|
||||||
$timer = 'bratonien-nc-connector.timer';
|
$scheduler = bratonien_tools_nc_scheduler_read_state();
|
||||||
$service = 'bratonien-nc-connector.service';
|
$enabled = !isset($scheduler['enabled']) || !empty($scheduler['enabled']);
|
||||||
|
$running = (string)($scheduler['state'] ?? '') === 'running';
|
||||||
$active = bratonien_tools_nc_connector_systemctl_value(array('is-active', $timer));
|
$started = (int)($scheduler['started_at'] ?? 0);
|
||||||
$enabled = bratonien_tools_nc_connector_systemctl_value(array('is-enabled', $timer));
|
$next = (int)($scheduler['next_due'] ?? 0);
|
||||||
$service_active_raw = bratonien_tools_nc_connector_systemctl_value(array('is-active', $service));
|
|
||||||
$service_active = in_array($service_active_raw, array('active', 'activating'), true);
|
|
||||||
$service_started_raw = bratonien_tools_nc_connector_systemctl_value(array('show', $service, '--property=ActiveEnterTimestamp', '--value'));
|
|
||||||
$service_started_timestamp = bratonien_tools_nc_connector_parse_systemd_time($service_started_raw);
|
|
||||||
|
|
||||||
$next_realtime_raw = bratonien_tools_nc_connector_systemctl_value(array('show', $timer, '--property=NextElapseUSecRealtime', '--value'));
|
|
||||||
$next_timestamp = bratonien_tools_nc_connector_parse_systemd_time($next_realtime_raw);
|
|
||||||
|
|
||||||
if ($next_timestamp <= 0)
|
|
||||||
{
|
|
||||||
$next_monotonic_raw = bratonien_tools_nc_connector_systemctl_value(array('show', $timer, '--property=NextElapseUSecMonotonic', '--value'));
|
|
||||||
$next_timestamp = bratonien_tools_nc_connector_monotonic_to_timestamp($next_monotonic_raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($next_timestamp <= 0)
|
|
||||||
{
|
|
||||||
$next_timestamp = bratonien_tools_nc_connector_next_from_timer_list($timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
$last = bratonien_tools_nc_connector_last_status($connections);
|
$last = bratonien_tools_nc_connector_last_status($connections);
|
||||||
if ($last['timestamp'] <= 0)
|
|
||||||
{
|
|
||||||
$last_raw = bratonien_tools_nc_connector_systemctl_value(array('show', $service, '--property=ExecMainExitTimestamp', '--value'));
|
|
||||||
$last['timestamp'] = bratonien_tools_nc_connector_parse_systemd_time($last_raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($next_timestamp > 0)
|
if ($next > 0)
|
||||||
{
|
{
|
||||||
$next_label = date('d.m.Y H:i:s', $next_timestamp);
|
$next_label = date('d.m.Y H:i:s', $next).' (beim nächsten Piwigo-Aufruf)';
|
||||||
}
|
|
||||||
elseif ($service_active && $active === 'active' && $enabled === 'enabled')
|
|
||||||
{
|
|
||||||
$next_label = 'Nach Abschluss des aktuellen Laufs';
|
|
||||||
}
|
|
||||||
elseif ($active === 'active' && $enabled === 'enabled')
|
|
||||||
{
|
|
||||||
$next_label = 'Wird von systemd neu geplant';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$next_label = 'Nicht verfügbar';
|
$next_label = 'Beim nächsten Piwigo-Aufruf';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int)$last['timestamp'] <= 0 && !empty($scheduler['finished_at']))
|
||||||
|
{
|
||||||
|
$last['timestamp'] = (int)$scheduler['finished_at'];
|
||||||
|
$last['state'] = (string)($scheduler['state'] ?? '');
|
||||||
|
$last['message'] = (string)($scheduler['message'] ?? '');
|
||||||
|
if ((string)$last['state'] === 'error')
|
||||||
|
{
|
||||||
|
$detail = trim((string)($scheduler['stderr'] ?? ''));
|
||||||
|
if ($detail === '') $detail = trim((string)($scheduler['stdout'] ?? ''));
|
||||||
|
$last['error_detail'] = $detail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'timer_name' => $timer,
|
'timer_name'=>'Piwigo nativer NC-Scheduler',
|
||||||
'timer_active' => $active === 'active',
|
'timer_active'=>$enabled,
|
||||||
'timer_enabled' => $enabled === 'enabled',
|
'timer_enabled'=>$enabled,
|
||||||
'service_active' => $service_active,
|
'service_active'=>$running,
|
||||||
'current_run_timestamp' => $service_started_timestamp,
|
'current_run_timestamp'=>$started,
|
||||||
'current_run_label' => $service_active
|
'current_run_label'=>$running ? ($started > 0 ? 'Läuft seit '.date('d.m.Y H:i:s', $started) : 'Läuft gerade') : 'Kein Lauf aktiv',
|
||||||
? ($service_started_timestamp > 0 ? 'Läuft seit '.date('d.m.Y H:i:s', $service_started_timestamp) : 'Läuft gerade')
|
'last_run_timestamp'=>(int)$last['timestamp'],
|
||||||
: 'Kein Lauf aktiv',
|
'last_run_label'=>(int)$last['timestamp'] > 0 ? date('d.m.Y H:i:s', (int)$last['timestamp']) : 'Nicht verfügbar',
|
||||||
'last_run_timestamp' => (int)$last['timestamp'],
|
'last_run_state'=>(string)$last['state'],
|
||||||
'last_run_label' => $last['timestamp'] > 0 ? date('d.m.Y H:i:s', (int)$last['timestamp']) : 'Nicht verfügbar',
|
'last_run_message'=>(string)$last['message'],
|
||||||
'last_run_state' => (string)$last['state'],
|
'last_run_auth_mode'=>(string)$last['auth_mode'],
|
||||||
'last_run_message' => (string)$last['message'],
|
'last_run_api_state'=>(string)$last['api_state'],
|
||||||
'last_run_auth_mode' => (string)$last['auth_mode'],
|
'last_run_api_message'=>(string)$last['api_message'],
|
||||||
'last_run_api_state' => (string)$last['api_state'],
|
'last_run_fallback_state'=>(string)$last['fallback_state'],
|
||||||
'last_run_api_message' => (string)$last['api_message'],
|
'last_run_fallback_message'=>(string)$last['fallback_message'],
|
||||||
'last_run_fallback_state' => (string)$last['fallback_state'],
|
'last_run_error_detail'=>(string)$last['error_detail'],
|
||||||
'last_run_fallback_message' => (string)$last['fallback_message'],
|
'next_run_timestamp'=>$next,
|
||||||
'last_run_error_detail' => (string)$last['error_detail'],
|
'next_run_label'=>$next_label,
|
||||||
'next_run_timestamp' => $next_timestamp,
|
'legacy_runtime_exists'=>is_dir('/opt/piwigo-sync'),
|
||||||
'next_run_label' => $next_label,
|
'legacy_config_exists'=>is_dir('/etc/piwigo-sync'),
|
||||||
'legacy_runtime_exists' => is_dir('/opt/piwigo-sync'),
|
'legacy_service_exists'=>is_file('/etc/systemd/system/piwigo-sync.service'),
|
||||||
'legacy_config_exists' => is_dir('/etc/piwigo-sync'),
|
'legacy_timer_exists'=>is_file('/etc/systemd/system/piwigo-sync.timer'),
|
||||||
'legacy_service_exists' => is_file('/etc/systemd/system/piwigo-sync.service'),
|
|
||||||
'legacy_timer_exists' => is_file('/etc/systemd/system/piwigo-sync.timer'),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function bratonien_tools_register_nc_productive_ws_methods($arr)
|
|||||||
'info' => 'Piwigo storage site to synchronize. Default: 1.',
|
'info' => 'Piwigo storage site to synchronize. Default: 1.',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
'Runs the approved direct Bratonien filesystem synchronization for the NC Connector.',
|
'Synchronizes the NC Connector into the existing Piwigo album hierarchy.',
|
||||||
null,
|
null,
|
||||||
array(
|
array(
|
||||||
'admin_only' => true,
|
'admin_only' => true,
|
||||||
@@ -35,6 +35,99 @@ function bratonien_tools_nc_productive_error(&$errors, $path, $type)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_relative_path($basedir, $path)
|
||||||
|
{
|
||||||
|
$basedir = rtrim(str_replace('\\', '/', (string)$basedir), '/');
|
||||||
|
$path = str_replace('\\', '/', (string)$path);
|
||||||
|
if ($path === $basedir) return '';
|
||||||
|
if (strpos($path, $basedir.'/') !== 0)
|
||||||
|
{
|
||||||
|
throw new RuntimeException('WebDAV-Pfad liegt ausserhalb der Connector-Wurzel: '.$path);
|
||||||
|
}
|
||||||
|
return trim(substr($path, strlen($basedir)), '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_find_album($parent_id, $dir, $name, $excluded_site_id)
|
||||||
|
{
|
||||||
|
$where_parent = $parent_id === null ? 'id_uppercat IS NULL' : 'id_uppercat='.(int)$parent_id;
|
||||||
|
$dir_sql = pwg_db_real_escape_string((string)$dir);
|
||||||
|
$name_sql = pwg_db_real_escape_string((string)$name);
|
||||||
|
$query = '
|
||||||
|
SELECT id, dir, name
|
||||||
|
FROM '.CATEGORIES_TABLE.'
|
||||||
|
WHERE '.$where_parent.'
|
||||||
|
AND (site_id IS NULL OR site_id <> '.(int)$excluded_site_id.')
|
||||||
|
AND (
|
||||||
|
dir = \''.$dir_sql.'\'
|
||||||
|
OR LOWER(name) = LOWER(\''.$name_sql.'\')
|
||||||
|
)
|
||||||
|
ORDER BY CASE WHEN dir = \''.$dir_sql.'\' THEN 0 ELSE 1 END, id
|
||||||
|
LIMIT 1
|
||||||
|
;';
|
||||||
|
$result = pwg_query($query);
|
||||||
|
if (!pwg_db_num_rows($result)) return null;
|
||||||
|
$row = pwg_db_fetch_assoc($result);
|
||||||
|
return (int)$row['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_ensure_album_path($relative_dir, $excluded_site_id, array &$cache, array &$created_ids)
|
||||||
|
{
|
||||||
|
$relative_dir = trim((string)$relative_dir, '/');
|
||||||
|
if ($relative_dir === '') return null;
|
||||||
|
if (isset($cache[$relative_dir])) return $cache[$relative_dir];
|
||||||
|
|
||||||
|
$parts = explode('/', $relative_dir);
|
||||||
|
$parent_id = null;
|
||||||
|
$path = '';
|
||||||
|
foreach ($parts as $part)
|
||||||
|
{
|
||||||
|
if ($part === '') continue;
|
||||||
|
$path = $path === '' ? $part : $path.'/'.$part;
|
||||||
|
if (isset($cache[$path]))
|
||||||
|
{
|
||||||
|
$parent_id = $cache[$path];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$display_name = str_replace('_', ' ', $part);
|
||||||
|
$album_id = bratonien_tools_nc_find_album($parent_id, $part, $display_name, $excluded_site_id);
|
||||||
|
if ($album_id === null)
|
||||||
|
{
|
||||||
|
$created = create_virtual_category($display_name, $parent_id);
|
||||||
|
if (!is_array($created) || empty($created['id']))
|
||||||
|
{
|
||||||
|
$detail = is_array($created) && !empty($created['error']) ? (string)$created['error'] : 'unbekannter Fehler';
|
||||||
|
throw new RuntimeException('Album "'.$display_name.'" konnte nicht angelegt werden: '.$detail);
|
||||||
|
}
|
||||||
|
$album_id = (int)$created['id'];
|
||||||
|
pwg_query('UPDATE '.CATEGORIES_TABLE." SET status='private' WHERE id=".$album_id.' LIMIT 1');
|
||||||
|
add_permission_on_category(array($album_id), get_admins());
|
||||||
|
$created_ids[] = $album_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache[$path] = $album_id;
|
||||||
|
$parent_id = $album_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parent_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_managed_images($basedir)
|
||||||
|
{
|
||||||
|
$prefix = rtrim((string)$basedir, '/').'/';
|
||||||
|
$escaped = pwg_db_real_escape_string(addcslashes($prefix, '_%\\'));
|
||||||
|
$query = "SELECT id, path FROM ".IMAGES_TABLE." WHERE path LIKE '".$escaped."%' ESCAPE '\\\\'";
|
||||||
|
return simple_hash_from_query($query, 'id', 'path');
|
||||||
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_remove_storage_categories($site_id)
|
||||||
|
{
|
||||||
|
$ids = query2array('SELECT id FROM '.CATEGORIES_TABLE.' WHERE site_id='.(int)$site_id.' AND dir IS NOT NULL', null, 'id');
|
||||||
|
if (!$ids) return 0;
|
||||||
|
delete_categories(array_map('intval', $ids));
|
||||||
|
return count($ids);
|
||||||
|
}
|
||||||
|
|
||||||
function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
||||||
{
|
{
|
||||||
global $conf, $user;
|
global $conf, $user;
|
||||||
@@ -42,181 +135,110 @@ function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
|||||||
$piwigo_version = defined('PHPWG_VERSION') ? (string)PHPWG_VERSION : '';
|
$piwigo_version = defined('PHPWG_VERSION') ? (string)PHPWG_VERSION : '';
|
||||||
if ($piwigo_version !== '16.4.0')
|
if ($piwigo_version !== '16.4.0')
|
||||||
{
|
{
|
||||||
return new PwgError(
|
return new PwgError(409, 'Bratonien API synchronization is not approved for Piwigo '.$piwigo_version.'.');
|
||||||
409,
|
|
||||||
'Bratonien API synchronization is not approved for Piwigo '.$piwigo_version.'. Use the administrator fallback until this Piwigo version has been verified.'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($conf['enable_synchronization']))
|
if (empty($conf['enable_synchronization']))
|
||||||
{
|
{
|
||||||
return new PwgError(403, 'Piwigo filesystem synchronization is disabled.');
|
return new PwgError(403, 'Piwigo filesystem synchronization is disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$site_id = isset($params['site_id']) ? (int)$params['site_id'] : 1;
|
$site_id = isset($params['site_id']) ? (int)$params['site_id'] : 1;
|
||||||
if ($site_id < 1)
|
if ($site_id < 1) return new PwgError(400, 'Invalid site_id.');
|
||||||
{
|
|
||||||
return new PwgError(400, 'Invalid site_id.');
|
|
||||||
}
|
|
||||||
|
|
||||||
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
||||||
include_once(PHPWG_ROOT_PATH.'admin/site_reader_local.php');
|
include_once(PHPWG_ROOT_PATH.'admin/site_reader_local.php');
|
||||||
|
|
||||||
$query = 'SELECT galleries_url FROM '.SITES_TABLE.' WHERE id = '.$site_id.' LIMIT 1';
|
$result = pwg_query('SELECT galleries_url FROM '.SITES_TABLE.' WHERE id='.$site_id.' LIMIT 1');
|
||||||
$result = pwg_query($query);
|
if (!pwg_db_num_rows($result)) return new PwgError(404, 'Piwigo site does not exist.');
|
||||||
if (!pwg_db_num_rows($result))
|
|
||||||
{
|
|
||||||
return new PwgError(404, 'Piwigo site does not exist.');
|
|
||||||
}
|
|
||||||
|
|
||||||
list($site_url) = pwg_db_fetch_row($result);
|
list($site_url) = pwg_db_fetch_row($result);
|
||||||
if (url_is_remote($site_url))
|
if (url_is_remote($site_url)) return new PwgError(400, 'Remote Piwigo sites are not supported.');
|
||||||
{
|
|
||||||
return new PwgError(400, 'Remote Piwigo sites are not supported by this synchronization method.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$site_reader = new LocalSiteReader($site_url);
|
$site_reader = new LocalSiteReader($site_url);
|
||||||
if (!$site_reader->open())
|
if (!$site_reader->open()) return new PwgError(500, 'Piwigo could not open the configured local site.');
|
||||||
{
|
|
||||||
return new PwgError(500, 'Piwigo could not open the configured local site.');
|
|
||||||
}
|
|
||||||
|
|
||||||
list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW()'));
|
$basedir = preg_replace('#/*$#', '', (string)$site_url);
|
||||||
$errors = array();
|
$errors = array();
|
||||||
$counts = array(
|
$counts = array(
|
||||||
'new_categories' => 0,
|
'reused_categories'=>0,
|
||||||
'del_categories' => 0,
|
'new_categories'=>0,
|
||||||
'new_elements' => 0,
|
'removed_duplicate_categories'=>0,
|
||||||
'del_elements' => 0,
|
'new_elements'=>0,
|
||||||
'upd_elements' => 0,
|
'del_elements'=>0,
|
||||||
'new_formats' => 0,
|
'upd_elements'=>0,
|
||||||
'del_formats' => 0,
|
|
||||||
'metadata_candidates' => 0,
|
|
||||||
'metadata_updated' => 0,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$query = 'SELECT id, id_uppercat, uppercats, global_rank, status, visible FROM '.CATEGORIES_TABLE.' WHERE dir IS NOT NULL AND site_id = '.$site_id;
|
$counts['removed_duplicate_categories'] = bratonien_tools_nc_remove_storage_categories($site_id);
|
||||||
$db_categories = hash_from_query($query, 'id');
|
|
||||||
$db_fulldirs = get_fulldirs(array_keys($db_categories));
|
|
||||||
$basedir = preg_replace('#/*$#', '', $site_url);
|
|
||||||
$db_fulldirs = array_flip($db_fulldirs);
|
|
||||||
$fs_fulldirs = $site_reader->get_full_directories($basedir);
|
|
||||||
|
|
||||||
$next_rank = array('NULL'=>1);
|
list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW()'));
|
||||||
$result = pwg_query('SELECT id FROM '.CATEGORIES_TABLE);
|
$fs_dirs = $site_reader->get_full_directories($basedir);
|
||||||
while ($row = pwg_db_fetch_assoc($result))
|
usort($fs_dirs, function($a, $b)
|
||||||
{
|
{
|
||||||
$next_rank[$row['id']] = 1;
|
return substr_count((string)$a, '/') <=> substr_count((string)$b, '/');
|
||||||
}
|
});
|
||||||
$result = pwg_query('SELECT id_uppercat, MAX(`rank`)+1 AS next_rank FROM '.CATEGORIES_TABLE.' GROUP BY id_uppercat');
|
|
||||||
while ($row = pwg_db_fetch_assoc($result))
|
|
||||||
{
|
|
||||||
$key = empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'];
|
|
||||||
$next_rank[$key] = (int)$row['next_rank'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$next_id = pwg_db_nextval('id', CATEGORIES_TABLE);
|
$album_cache = array();
|
||||||
$category_inserts = array();
|
$created_ids = array();
|
||||||
|
$dir_to_album = array();
|
||||||
foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
|
foreach ($fs_dirs as $full_dir)
|
||||||
{
|
{
|
||||||
$dir = basename($fulldir);
|
$relative = bratonien_tools_nc_relative_path($basedir, $full_dir);
|
||||||
if (!preg_match($conf['sync_chars_regex'], $dir))
|
if ($relative === '') continue;
|
||||||
|
$before = count($created_ids);
|
||||||
|
$album_id = bratonien_tools_nc_ensure_album_path($relative, $site_id, $album_cache, $created_ids);
|
||||||
|
if ($album_id !== null)
|
||||||
{
|
{
|
||||||
bratonien_tools_nc_productive_error($errors, $fulldir, 'PWG-UPDATE-1');
|
$dir_to_album[$full_dir] = $album_id;
|
||||||
continue;
|
if (count($created_ids) === $before) $counts['reused_categories']++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$insert = array(
|
|
||||||
'id' => $next_id++,
|
|
||||||
'dir' => $dir,
|
|
||||||
'name' => str_replace('_', ' ', $dir),
|
|
||||||
'site_id' => $site_id,
|
|
||||||
'commentable' => boolean_to_string($conf['newcat_default_commentable']),
|
|
||||||
'status' => 'private',
|
|
||||||
'visible' => boolean_to_string($conf['newcat_default_visible']),
|
|
||||||
);
|
|
||||||
|
|
||||||
$parent_path = dirname($fulldir);
|
|
||||||
if (isset($db_fulldirs[$parent_path]))
|
|
||||||
{
|
|
||||||
$parent = $db_fulldirs[$parent_path];
|
|
||||||
$insert['id_uppercat'] = $parent;
|
|
||||||
$insert['uppercats'] = $db_categories[$parent]['uppercats'].','.$insert['id'];
|
|
||||||
$insert['rank'] = $next_rank[$parent]++;
|
|
||||||
$insert['global_rank'] = $db_categories[$parent]['global_rank'].'.'.$insert['rank'];
|
|
||||||
if ((string)$db_categories[$parent]['visible'] === 'false')
|
|
||||||
{
|
|
||||||
$insert['visible'] = 'false';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$insert['uppercats'] = (string)$insert['id'];
|
|
||||||
$insert['rank'] = $next_rank['NULL']++;
|
|
||||||
$insert['global_rank'] = (string)$insert['rank'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$category_inserts[] = $insert;
|
|
||||||
$db_categories[$insert['id']] = array(
|
|
||||||
'id' => $insert['id'],
|
|
||||||
'id_uppercat' => $insert['id_uppercat'] ?? null,
|
|
||||||
'uppercats' => $insert['uppercats'],
|
|
||||||
'global_rank' => $insert['global_rank'],
|
|
||||||
'status' => 'private',
|
|
||||||
'visible' => $insert['visible'],
|
|
||||||
);
|
|
||||||
$db_fulldirs[$fulldir] = $insert['id'];
|
|
||||||
$next_rank[$insert['id']] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($category_inserts)
|
|
||||||
{
|
|
||||||
mass_inserts(
|
|
||||||
CATEGORIES_TABLE,
|
|
||||||
array('id','dir','name','site_id','id_uppercat','uppercats','commentable','visible','status','rank','global_rank'),
|
|
||||||
$category_inserts
|
|
||||||
);
|
|
||||||
$category_ids = array_map(function ($row) { return (int)$row['id']; }, $category_inserts);
|
|
||||||
pwg_activity('album', $category_ids, 'add', array('sync'=>true));
|
|
||||||
add_permission_on_category($category_ids, get_admins());
|
|
||||||
$counts['new_categories'] = count($category_ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
$to_delete_categories = array();
|
|
||||||
foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
|
|
||||||
{
|
|
||||||
$to_delete_categories[] = (int)$db_fulldirs[$fulldir];
|
|
||||||
unset($db_fulldirs[$fulldir]);
|
|
||||||
}
|
|
||||||
if ($to_delete_categories)
|
|
||||||
{
|
|
||||||
delete_categories($to_delete_categories);
|
|
||||||
$counts['del_categories'] = count($to_delete_categories);
|
|
||||||
}
|
}
|
||||||
|
$counts['new_categories'] = count($created_ids);
|
||||||
|
|
||||||
$fs = $site_reader->get_elements($basedir);
|
$fs = $site_reader->get_elements($basedir);
|
||||||
$cat_ids = array_diff(array_keys($db_categories), $to_delete_categories);
|
$db_elements = bratonien_tools_nc_managed_images($basedir);
|
||||||
$db_elements = array();
|
$db_by_path = array_flip($db_elements);
|
||||||
if ($cat_ids)
|
|
||||||
|
$to_delete = array();
|
||||||
|
foreach ($db_elements as $id=>$path)
|
||||||
{
|
{
|
||||||
$query = 'SELECT id, path FROM '.IMAGES_TABLE.' WHERE storage_category_id IN ('.implode(',', array_map('intval', $cat_ids)).')';
|
if (!array_key_exists($path, $fs)) $to_delete[] = (int)$id;
|
||||||
$db_elements = simple_hash_from_query($query, 'id', 'path');
|
}
|
||||||
|
if ($to_delete)
|
||||||
|
{
|
||||||
|
delete_elements($to_delete, false);
|
||||||
|
$counts['del_elements'] = count($to_delete);
|
||||||
|
foreach ($to_delete as $id)
|
||||||
|
{
|
||||||
|
if (isset($db_elements[$id])) unset($db_by_path[$db_elements[$id]], $db_elements[$id]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$next_element_id = pwg_db_nextval('id', IMAGES_TABLE);
|
$next_element_id = pwg_db_nextval('id', IMAGES_TABLE);
|
||||||
$image_inserts = array();
|
$image_inserts = array();
|
||||||
$image_links = array();
|
$image_links = array();
|
||||||
$format_inserts = array();
|
$new_ids = array();
|
||||||
$new_image_ids = array();
|
$all_ids = array();
|
||||||
|
|
||||||
foreach (array_diff(array_keys($fs), $db_elements) as $path)
|
foreach ($fs as $path=>$file_info)
|
||||||
{
|
{
|
||||||
$dirname = dirname($path);
|
$dirname = dirname($path);
|
||||||
if (!isset($db_fulldirs[$dirname]))
|
$relative_dir = bratonien_tools_nc_relative_path($basedir, $dirname);
|
||||||
|
$category_id = null;
|
||||||
|
if ($relative_dir !== '')
|
||||||
{
|
{
|
||||||
|
$category_id = $dir_to_album[$dirname] ?? bratonien_tools_nc_ensure_album_path($relative_dir, $site_id, $album_cache, $created_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($db_by_path[$path]))
|
||||||
|
{
|
||||||
|
$id = (int)$db_by_path[$path];
|
||||||
|
$all_ids[] = $id;
|
||||||
|
pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id='.$id);
|
||||||
|
if ($category_id !== null)
|
||||||
|
{
|
||||||
|
single_insert(IMAGE_CATEGORY_TABLE, array('image_id'=>$id, 'category_id'=>$category_id));
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,116 +251,40 @@ function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
|||||||
|
|
||||||
$id = $next_element_id++;
|
$id = $next_element_id++;
|
||||||
$image_inserts[] = array(
|
$image_inserts[] = array(
|
||||||
'id' => $id,
|
'id'=>$id,
|
||||||
'file' => $filename,
|
'file'=>$filename,
|
||||||
'name' => get_name_from_file($filename),
|
'name'=>get_name_from_file($filename),
|
||||||
'date_available' => $dbnow,
|
'date_available'=>$dbnow,
|
||||||
'path' => $path,
|
'path'=>$path,
|
||||||
'representative_ext' => $fs[$path]['representative_ext'],
|
'representative_ext'=>$file_info['representative_ext'],
|
||||||
'storage_category_id' => $db_fulldirs[$dirname],
|
'storage_category_id'=>null,
|
||||||
'added_by' => (int)$user['id'],
|
'added_by'=>(int)$user['id'],
|
||||||
);
|
);
|
||||||
$image_links[] = array('image_id'=>$id, 'category_id'=>$db_fulldirs[$dirname]);
|
if ($category_id !== null)
|
||||||
$new_image_ids[] = $id;
|
|
||||||
|
|
||||||
if (!empty($conf['enable_formats']) && !empty($fs[$path]['formats']))
|
|
||||||
{
|
{
|
||||||
foreach ($fs[$path]['formats'] as $ext => $filesize)
|
$image_links[] = array('image_id'=>$id, 'category_id'=>$category_id);
|
||||||
{
|
|
||||||
$format_inserts[] = array('image_id'=>$id, 'ext'=>$ext, 'filesize'=>$filesize);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$new_ids[] = $id;
|
||||||
|
$all_ids[] = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($image_inserts)
|
if ($image_inserts)
|
||||||
{
|
{
|
||||||
mass_inserts(IMAGES_TABLE, array_keys($image_inserts[0]), $image_inserts);
|
mass_inserts(IMAGES_TABLE, array_keys($image_inserts[0]), $image_inserts);
|
||||||
mass_inserts(IMAGE_CATEGORY_TABLE, array_keys($image_links[0]), $image_links);
|
if ($image_links) mass_inserts(IMAGE_CATEGORY_TABLE, array_keys($image_links[0]), $image_links);
|
||||||
pwg_activity('photo', $new_image_ids, 'add', array('sync'=>true));
|
pwg_activity('photo', $new_ids, 'add', array('sync'=>true));
|
||||||
$counts['new_elements'] = count($image_inserts);
|
$counts['new_elements'] = count($new_ids);
|
||||||
}
|
|
||||||
if ($format_inserts)
|
|
||||||
{
|
|
||||||
mass_inserts(IMAGE_FORMAT_TABLE, array_keys($format_inserts[0]), $format_inserts);
|
|
||||||
$counts['new_formats'] += count($format_inserts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($conf['enable_formats']) && $db_elements)
|
|
||||||
{
|
|
||||||
$db_elements_flip = array_flip($db_elements);
|
|
||||||
$existing_ids = array();
|
|
||||||
foreach (array_intersect_key($fs, $db_elements_flip) as $path => $unused)
|
|
||||||
{
|
|
||||||
$existing_ids[] = (int)$db_elements_flip[$path];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($existing_ids)
|
|
||||||
{
|
|
||||||
$db_formats = array();
|
|
||||||
$result = pwg_query('SELECT * FROM '.IMAGE_FORMAT_TABLE.' WHERE image_id IN ('.implode(',', $existing_ids).')');
|
|
||||||
while ($row = pwg_db_fetch_assoc($result))
|
|
||||||
{
|
|
||||||
$db_formats[$row['image_id']][$row['ext']] = $row['format_id'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$formats_to_delete = array();
|
|
||||||
$formats_to_insert = array();
|
|
||||||
foreach ($existing_ids as $image_id)
|
|
||||||
{
|
|
||||||
$path = $db_elements[$image_id];
|
|
||||||
$known = $db_formats[$image_id] ?? array();
|
|
||||||
$present = $fs[$path]['formats'] ?? array();
|
|
||||||
foreach (array_diff_key($known, $present) as $format_id)
|
|
||||||
{
|
|
||||||
$formats_to_delete[] = (int)$format_id;
|
|
||||||
}
|
|
||||||
foreach (array_diff_key($present, $known) as $ext => $filesize)
|
|
||||||
{
|
|
||||||
$formats_to_insert[] = array('image_id'=>$image_id, 'ext'=>$ext, 'filesize'=>$filesize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($formats_to_delete)
|
|
||||||
{
|
|
||||||
pwg_query('DELETE FROM '.IMAGE_FORMAT_TABLE.' WHERE format_id IN ('.implode(',', $formats_to_delete).')');
|
|
||||||
$counts['del_formats'] = count($formats_to_delete);
|
|
||||||
}
|
|
||||||
if ($formats_to_insert)
|
|
||||||
{
|
|
||||||
mass_inserts(IMAGE_FORMAT_TABLE, array_keys($formats_to_insert[0]), $formats_to_insert);
|
|
||||||
$counts['new_formats'] += count($formats_to_insert);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$to_delete_elements = array();
|
|
||||||
foreach (array_diff($db_elements, array_keys($fs)) as $path)
|
|
||||||
{
|
|
||||||
$id = array_search($path, $db_elements, true);
|
|
||||||
if ($id !== false)
|
|
||||||
{
|
|
||||||
$to_delete_elements[] = (int)$id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($to_delete_elements)
|
|
||||||
{
|
|
||||||
delete_elements($to_delete_elements);
|
|
||||||
$counts['del_elements'] = count($to_delete_elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
update_category('all');
|
|
||||||
update_global_rank();
|
|
||||||
|
|
||||||
$files = get_filelist('', $site_id, true, false);
|
|
||||||
$updates = array();
|
$updates = array();
|
||||||
foreach ($files as $id => $file)
|
foreach ($all_ids as $id)
|
||||||
{
|
{
|
||||||
$data = $site_reader->get_element_update_attributes($file['path']);
|
$path_result = pwg_query('SELECT path FROM '.IMAGES_TABLE.' WHERE id='.(int)$id.' LIMIT 1');
|
||||||
if (!is_array($data))
|
if (!pwg_db_num_rows($path_result)) continue;
|
||||||
{
|
list($path) = pwg_db_fetch_row($path_result);
|
||||||
continue;
|
$data = $site_reader->get_element_update_attributes($path);
|
||||||
}
|
if (!is_array($data)) continue;
|
||||||
$data['id'] = $id;
|
$data['id'] = (int)$id;
|
||||||
$updates[] = $data;
|
$updates[] = $data;
|
||||||
}
|
}
|
||||||
if ($updates)
|
if ($updates)
|
||||||
@@ -351,94 +297,25 @@ function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
|||||||
}
|
}
|
||||||
$counts['upd_elements'] = count($updates);
|
$counts['upd_elements'] = count($updates);
|
||||||
|
|
||||||
$metadata_files = get_filelist('', $site_id, true, true);
|
|
||||||
$counts['metadata_candidates'] = count($metadata_files);
|
|
||||||
$metadata_updates = array();
|
|
||||||
$tags_of = array();
|
|
||||||
|
|
||||||
foreach ($metadata_files as $id => $element_infos)
|
|
||||||
{
|
|
||||||
$data = $site_reader->get_element_metadata($element_infos);
|
|
||||||
if (!is_array($data))
|
|
||||||
{
|
|
||||||
bratonien_tools_nc_productive_error($errors, $element_infos['path'], 'PWG-ERROR-NO-FS');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$data['date_metadata_update'] = $dbnow;
|
|
||||||
$data['id'] = $id;
|
|
||||||
$metadata_updates[] = $data;
|
|
||||||
|
|
||||||
foreach (array('keywords','tags') as $key)
|
|
||||||
{
|
|
||||||
if (!isset($data[$key]))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$tags_of[$id] = $tags_of[$id] ?? array();
|
|
||||||
foreach (explode(',', $data[$key]) as $tag_name)
|
|
||||||
{
|
|
||||||
$tags_of[$id][] = tag_id_from_tag_name($tag_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($metadata_updates)
|
|
||||||
{
|
|
||||||
mass_updates(
|
|
||||||
IMAGES_TABLE,
|
|
||||||
array(
|
|
||||||
'primary'=>array('id'),
|
|
||||||
'update'=>array_unique(array_merge(
|
|
||||||
array_diff($site_reader->get_metadata_attributes(), array('keywords','tags')),
|
|
||||||
array('date_metadata_update')
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
$metadata_updates,
|
|
||||||
MASS_UPDATES_SKIP_EMPTY
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if ($tags_of)
|
|
||||||
{
|
|
||||||
set_tags_of($tags_of);
|
|
||||||
}
|
|
||||||
$counts['metadata_updated'] = count($metadata_updates);
|
|
||||||
|
|
||||||
// Mirror Piwigo 16.4.0 Maintenance -> "Update albums informations".
|
|
||||||
// This repairs the derived album hierarchy and counters that the direct
|
|
||||||
// API sync otherwise bypasses when no admin maintenance page is invoked.
|
|
||||||
images_integrity();
|
images_integrity();
|
||||||
categories_integrity();
|
categories_integrity();
|
||||||
update_uppercats();
|
update_uppercats();
|
||||||
update_category('all');
|
update_category('all');
|
||||||
update_global_rank();
|
update_global_rank();
|
||||||
invalidate_user_cache(true);
|
|
||||||
|
|
||||||
// Mirror Piwigo 16.4.0 Maintenance -> "Update photos information".
|
|
||||||
// This finalizes physical paths, ratings and derived photo information.
|
|
||||||
images_integrity();
|
|
||||||
update_path();
|
|
||||||
include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
|
|
||||||
update_rating_score();
|
|
||||||
invalidate_user_cache();
|
invalidate_user_cache();
|
||||||
}
|
|
||||||
catch (Throwable $e)
|
|
||||||
{
|
|
||||||
return new PwgError(500, 'Bratonien direct synchronization failed: '.$e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'mode' => 'productive',
|
'mode'=>'productive',
|
||||||
'engine' => 'bratonien-direct',
|
'piwigo_version'=>$piwigo_version,
|
||||||
'approved_piwigo_version' => '16.4.0',
|
'site_id'=>$site_id,
|
||||||
'piwigo_version' => $piwigo_version,
|
'site_url'=>$site_url,
|
||||||
'site_id' => $site_id,
|
'counts'=>$counts,
|
||||||
'site_url' => $site_url,
|
'errors'=>$errors,
|
||||||
'counts' => $counts,
|
'database_writes'=>array_sum($counts) > 0,
|
||||||
'errors' => $errors,
|
);
|
||||||
'error_count' => count($errors),
|
}
|
||||||
'database_writes' => true,
|
catch (Throwable $error)
|
||||||
'username' => isset($user['username']) ? (string)$user['username'] : '',
|
{
|
||||||
'status' => isset($user['status']) ? (string)$user['status'] : '',
|
return new PwgError(500, 'Bratonien NC synchronization failed: '.$error->getMessage());
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ if (!defined('PHPWG_ROOT_PATH'))
|
|||||||
|
|
||||||
if (isset($GLOBALS['template']) && is_object($GLOBALS['template']) && method_exists($GLOBALS['template'], 'func_combine_script'))
|
if (isset($GLOBALS['template']) && is_object($GLOBALS['template']) && method_exists($GLOBALS['template'], 'func_combine_script'))
|
||||||
{
|
{
|
||||||
$script_version = function_exists('bratonien_tools_current_version') ? bratonien_tools_current_version() : '0.9.6.27';
|
$script_version = function_exists('bratonien_tools_current_version') ? bratonien_tools_current_version() : '0.9.6.30';
|
||||||
$GLOBALS['template']->func_combine_script(array(
|
$GLOBALS['template']->func_combine_script(array(
|
||||||
'id'=>'bratonien_nc_connector_edit_v2',
|
'id'=>'bratonien_nc_connector_edit_v2',
|
||||||
'path'=>BRATONIEN_TOOLS_PATH.'js/nc_connector_edit_v2.js',
|
'path'=>BRATONIEN_TOOLS_PATH.'js/nc_connector_edit_v2.js',
|
||||||
@@ -41,57 +41,13 @@ require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_delete_safe.inc.php');
|
|||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_webdav.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_webdav.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_wizard_webdav_flow.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_wizard_webdav_flow.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_edit.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_edit.inc.php');
|
||||||
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_scheduler.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_system.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_system.inc.php');
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_run_now()
|
function bratonien_tools_nc_connector_run_now()
|
||||||
{
|
{
|
||||||
$service = 'bratonien-nc-connector.service';
|
$result = bratonien_tools_nc_scheduler_spawn(true);
|
||||||
$active = bratonien_tools_nc_connector_systemctl_value(array('is-active', $service));
|
return array('message'=>(string)($result['message'] ?? 'NC-Abgleich wurde gestartet.'));
|
||||||
if ($active === 'active' || $active === 'activating')
|
|
||||||
{
|
|
||||||
return array('message'=>'Der NC-Abgleich läuft bereits.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('proc_open'))
|
|
||||||
{
|
|
||||||
throw new RuntimeException('Der NC-Abgleich konnte nicht gestartet werden: proc_open ist nicht verfügbar.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$commands = array(
|
|
||||||
array('/usr/bin/sudo', '-n', '/usr/bin/systemctl', 'start', '--no-block', $service),
|
|
||||||
array('/usr/bin/systemctl', 'start', '--no-block', $service),
|
|
||||||
);
|
|
||||||
$last_error = '';
|
|
||||||
|
|
||||||
foreach ($commands as $command)
|
|
||||||
{
|
|
||||||
if (!is_executable($command[0]))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$spec = array(
|
|
||||||
0=>array('file','/dev/null','r'),
|
|
||||||
1=>array('pipe','w'),
|
|
||||||
2=>array('pipe','w'),
|
|
||||||
);
|
|
||||||
$process = @proc_open($command, $spec, $pipes, null, array_merge($_ENV, array('LC_ALL'=>'C','LANG'=>'C')));
|
|
||||||
if (!is_resource($process))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$stdout = trim((string)stream_get_contents($pipes[1]));
|
|
||||||
$stderr = trim((string)stream_get_contents($pipes[2]));
|
|
||||||
fclose($pipes[1]);
|
|
||||||
fclose($pipes[2]);
|
|
||||||
$exit = proc_close($process);
|
|
||||||
if ($exit === 0)
|
|
||||||
{
|
|
||||||
return array('message'=>'NC-Abgleich wurde gestartet.');
|
|
||||||
}
|
|
||||||
$last_error = $stderr !== '' ? $stderr : $stdout;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new RuntimeException('Der NC-Abgleich konnte nicht gestartet werden'.($last_error !== '' ? ': '.$last_error : '.'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function bratonien_tools_get_tools()
|
function bratonien_tools_get_tools()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
Plugin Name: Bratonien Tools
|
Plugin Name: Bratonien Tools
|
||||||
Version: 0.9.6.27
|
Version: 0.9.6.30
|
||||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||||
Author: Bratonien
|
Author: Bratonien
|
||||||
@@ -24,6 +24,7 @@ require_once(BRATONIEN_TOOLS_PATH . 'include/album_shares.inc.php');
|
|||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_ws.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_ws.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_orphan_ws.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_orphan_ws.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_productive_ws.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_productive_ws.inc.php');
|
||||||
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_scheduler.inc.php');
|
||||||
|
|
||||||
add_event_handler('get_admin_plugin_menu_links', 'bratonien_tools_admin_menu');
|
add_event_handler('get_admin_plugin_menu_links', 'bratonien_tools_admin_menu');
|
||||||
add_event_handler('get_derivative_url', 'bratonien_tools_filter_derivative_url', EVENT_HANDLER_PRIORITY_NEUTRAL, 4);
|
add_event_handler('get_derivative_url', 'bratonien_tools_filter_derivative_url', EVENT_HANDLER_PRIORITY_NEUTRAL, 4);
|
||||||
@@ -35,6 +36,7 @@ add_event_handler('init', 'bratonien_tools_prepare_connector_private_import', EV
|
|||||||
add_event_handler('init', 'bratonien_tools_prepare_private_album_permissions', EVENT_HANDLER_PRIORITY_NEUTRAL - 20);
|
add_event_handler('init', 'bratonien_tools_prepare_private_album_permissions', EVENT_HANDLER_PRIORITY_NEUTRAL - 20);
|
||||||
add_event_handler('init', 'bratonien_tools_preserve_private_album_access', EVENT_HANDLER_PRIORITY_NEUTRAL - 10);
|
add_event_handler('init', 'bratonien_tools_preserve_private_album_access', EVENT_HANDLER_PRIORITY_NEUTRAL - 10);
|
||||||
add_event_handler('init', 'bratonien_tools_album_shares_init');
|
add_event_handler('init', 'bratonien_tools_album_shares_init');
|
||||||
|
add_event_handler('init', 'bratonien_tools_nc_scheduler_tick', EVENT_HANDLER_PRIORITY_NEUTRAL + 100);
|
||||||
add_event_handler('delete_categories', 'bratonien_tools_album_shares_on_delete_categories');
|
add_event_handler('delete_categories', 'bratonien_tools_album_shares_on_delete_categories');
|
||||||
add_event_handler('ws_add_methods', 'bratonien_tools_register_ws_methods');
|
add_event_handler('ws_add_methods', 'bratonien_tools_register_ws_methods');
|
||||||
add_event_handler('ws_add_methods', 'bratonien_tools_register_nc_orphan_ws_methods');
|
add_event_handler('ws_add_methods', 'bratonien_tools_register_nc_orphan_ws_methods');
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ require_once(dirname(__FILE__) . '/include/album_shares.inc.php');
|
|||||||
require_once(dirname(__FILE__) . '/include/database.class.php');
|
require_once(dirname(__FILE__) . '/include/database.class.php');
|
||||||
require_once(dirname(__FILE__) . '/tools/watermark_profiles.inc.php');
|
require_once(dirname(__FILE__) . '/tools/watermark_profiles.inc.php');
|
||||||
require_once(dirname(__FILE__) . '/include/dependencies.inc.php');
|
require_once(dirname(__FILE__) . '/include/dependencies.inc.php');
|
||||||
|
require_once(dirname(__FILE__) . '/include/nc_connector_scheduler.inc.php');
|
||||||
|
|
||||||
class bratonien_tools_maintain extends PluginMaintain
|
class bratonien_tools_maintain extends PluginMaintain
|
||||||
{
|
{
|
||||||
@@ -18,9 +19,14 @@ class bratonien_tools_maintain extends PluginMaintain
|
|||||||
|
|
||||||
$dependency_messages = array();
|
$dependency_messages = array();
|
||||||
bratonien_tools_ensure_dependencies($dependency_messages);
|
bratonien_tools_ensure_dependencies($dependency_messages);
|
||||||
|
if (!bratonien_tools_nc_scheduler_install())
|
||||||
|
{
|
||||||
|
$dependency_messages[] = 'Der native NC-Scheduler konnte sein Piwigo-Laufzeitverzeichnis nicht anlegen.';
|
||||||
|
}
|
||||||
|
|
||||||
if (function_exists('conf_update_param'))
|
if (function_exists('conf_update_param'))
|
||||||
{
|
{
|
||||||
|
conf_update_param('bratonien_nc_scheduler_interval', 60);
|
||||||
conf_update_param('bratonien_dependency_status', json_encode(array(
|
conf_update_param('bratonien_dependency_status', json_encode(array(
|
||||||
'checked_at' => time(),
|
'checked_at' => time(),
|
||||||
'messages' => $dependency_messages,
|
'messages' => $dependency_messages,
|
||||||
|
|||||||
42
runtime/lib/build_webdav_placeholder_source.py
Normal file → Executable file
42
runtime/lib/build_webdav_placeholder_source.py
Normal file → Executable file
@@ -1,9 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Build a placeholder-backed local source tree from Nextcloud WebDAV.
|
"""Build a placeholder-backed local source tree from Nextcloud WebDAV.
|
||||||
|
|
||||||
This is intentionally additive: it does not replace the existing local-storage
|
This creates only tiny placeholder files plus a metadata mapping; no Nextcloud
|
||||||
connector path. It creates only tiny placeholder files plus a metadata mapping;
|
original media is downloaded. The authenticated Nextcloud user is never used as
|
||||||
no Nextcloud original media is downloaded.
|
an album name.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -26,8 +26,6 @@ from pathlib import Path, PurePosixPath
|
|||||||
DAV = "DAV:"
|
DAV = "DAV:"
|
||||||
OC = "http://owncloud.org/ns"
|
OC = "http://owncloud.org/ns"
|
||||||
SUPPORTED_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
SUPPORTED_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
||||||
# 1x1 transparent GIF, 34 bytes. The filename keeps the remote extension;
|
|
||||||
# the placeholder exists only so Piwigo can discover the logical image entry.
|
|
||||||
PLACEHOLDER = base64.b64decode("R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==")
|
PLACEHOLDER = base64.b64decode("R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==")
|
||||||
|
|
||||||
|
|
||||||
@@ -148,13 +146,7 @@ def link_placeholder(seed: Path, target: Path) -> None:
|
|||||||
target.write_bytes(PLACEHOLDER)
|
target.write_bytes(PLACEHOLDER)
|
||||||
|
|
||||||
|
|
||||||
def build_root(
|
def build_root(client: WebDavClient, remote_root: str, local_root: Path, seed: Path, mapping: dict[str, dict[str, object]]) -> tuple[int, int, int]:
|
||||||
client: WebDavClient,
|
|
||||||
remote_root: str,
|
|
||||||
local_root: Path,
|
|
||||||
seed: Path,
|
|
||||||
mapping: dict[str, dict[str, object]],
|
|
||||||
) -> tuple[int, int, int]:
|
|
||||||
files = 0
|
files = 0
|
||||||
folders = 0
|
folders = 0
|
||||||
skipped = 0
|
skipped = 0
|
||||||
@@ -253,22 +245,36 @@ def main() -> int:
|
|||||||
mapping: dict[str, dict[str, object]] = {}
|
mapping: dict[str, dict[str, object]] = {}
|
||||||
manifest: list[str] = []
|
manifest: list[str] = []
|
||||||
total_files = total_folders = total_skipped = 0
|
total_files = total_folders = total_skipped = 0
|
||||||
used_names: set[str] = set()
|
used_fileids: set[int] = set()
|
||||||
|
|
||||||
for remote_root_raw in args.root:
|
for remote_root_raw in args.root:
|
||||||
remote_root = validate_relative(remote_root_raw)
|
remote_root = validate_relative(remote_root_raw)
|
||||||
current, _ = client.list_collection(remote_root)
|
current, root_children = client.list_collection(remote_root)
|
||||||
fileid = int(current["fileid"])
|
fileid = int(current["fileid"])
|
||||||
display = str(current.get("display_name", "")).strip() or (PurePosixPath(remote_root).name if remote_root else args.user)
|
if fileid in used_fileids:
|
||||||
local_name = f"root-{fileid}"
|
|
||||||
if local_name in used_names:
|
|
||||||
fail(f"duplicate selected Nextcloud root fileid: {fileid}")
|
fail(f"duplicate selected Nextcloud root fileid: {fileid}")
|
||||||
used_names.add(local_name)
|
used_fileids.add(fileid)
|
||||||
|
local_name = f"root-{fileid}"
|
||||||
local_root = staging / local_name
|
local_root = staging / local_name
|
||||||
files, folders, skipped = build_root(client, remote_root, local_root, seed, mapping)
|
files, folders, skipped = build_root(client, remote_root, local_root, seed, mapping)
|
||||||
total_files += files
|
total_files += files
|
||||||
total_folders += folders
|
total_folders += folders
|
||||||
total_skipped += skipped
|
total_skipped += skipped
|
||||||
|
|
||||||
|
if remote_root == "":
|
||||||
|
for child in sorted(root_children, key=lambda item: str(item.get("display_name", "")).casefold()):
|
||||||
|
name = safe_local_name(str(child.get("display_name", "")))
|
||||||
|
child_fileid = int(child.get("fileid", 0))
|
||||||
|
if child_fileid < 1:
|
||||||
|
fail(f"Nextcloud returned no stable fileid for root child {name!r}")
|
||||||
|
child_path = source_dir / local_name / name
|
||||||
|
if bool(child.get("is_dir")):
|
||||||
|
manifest.append(f"webdav:{child_fileid}\tfolder\t{name}\t{child_path}")
|
||||||
|
elif Path(name).suffix.lower() in SUPPORTED_IMAGE_EXTENSIONS:
|
||||||
|
manifest.append(f"webdav:{child_fileid}\tfile\t{name}\t{child_path}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
display = str(current.get("display_name", "")).strip() or PurePosixPath(remote_root).name
|
||||||
manifest.append(f"webdav:{fileid}\tfolder\t{display}\t{source_dir / local_name}")
|
manifest.append(f"webdav:{fileid}\tfolder\t{display}\t{source_dir / local_name}")
|
||||||
|
|
||||||
if previous.exists():
|
if previous.exists():
|
||||||
|
|||||||
29
runtime/lib/piwigo-sync.php
Normal file → Executable file
29
runtime/lib/piwigo-sync.php
Normal file → Executable file
@@ -252,8 +252,6 @@ try
|
|||||||
);
|
);
|
||||||
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncProductive', 'site_id'=>$site_id), $headers));
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncProductive', 'site_id'=>$site_id), $headers));
|
||||||
$orphan = decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0), $headers));
|
$orphan = decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0), $headers));
|
||||||
// Entfernt alte technische bratonien-webdav-N Wrapper aus Site 1,
|
|
||||||
// nachdem deren generierte Verzeichnisse beim Reconcile entfernt wurden.
|
|
||||||
if ($site_id !== 1)
|
if ($site_id !== 1)
|
||||||
{
|
{
|
||||||
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0), $headers));
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0), $headers));
|
||||||
@@ -284,16 +282,31 @@ try
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'pwg.session.login', 'username'=>$fallback_user, 'password'=>$fallback_password), array(), $cookie_file));
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'pwg.session.login', 'username'=>$fallback_user, 'password'=>$fallback_password), array(), $cookie_file));
|
||||||
http_request(
|
|
||||||
$base_url.'/admin.php?page=site_update&site='.$site_id,
|
// Der Fallback darf keinen zweiten Strukturpfad benutzen. Auch mit
|
||||||
array('sync'=>'files','display_info'=>1,'privacy_level'=>0,'sync_meta'=>1,'simulate'=>0,'subcats-included'=>1,'bratonien_connector'=>1,'submit'=>1),
|
// Benutzer/Passwort wird exakt derselbe Bratonien-Sync wie mit API-Key
|
||||||
|
// ausgefuehrt. Dadurch werden alte technische WebDAV-Kategorien entfernt
|
||||||
|
// und vorhandene Piwigo-Alben wiederverwendet.
|
||||||
|
decode_ws(http_request(
|
||||||
|
$base_url.'/ws.php?format=json',
|
||||||
|
array('method'=>'bratonien.nc.syncProductive', 'site_id'=>$site_id),
|
||||||
array(),
|
array(),
|
||||||
$cookie_file
|
$cookie_file
|
||||||
);
|
));
|
||||||
$orphan = decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0), array(), $cookie_file));
|
$orphan = decode_ws(http_request(
|
||||||
|
$base_url.'/ws.php?format=json',
|
||||||
|
array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0),
|
||||||
|
array(),
|
||||||
|
$cookie_file
|
||||||
|
));
|
||||||
if ($site_id !== 1)
|
if ($site_id !== 1)
|
||||||
{
|
{
|
||||||
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0), array(), $cookie_file));
|
decode_ws(http_request(
|
||||||
|
$base_url.'/ws.php?format=json',
|
||||||
|
array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0),
|
||||||
|
array(),
|
||||||
|
$cookie_file
|
||||||
|
));
|
||||||
}
|
}
|
||||||
$added = (int)($orphan['added_orphans'] ?? 0);
|
$added = (int)($orphan['added_orphans'] ?? 0);
|
||||||
$deleted = (int)($orphan['deleted_orphans'] ?? 0);
|
$deleted = (int)($orphan['deleted_orphans'] ?? 0);
|
||||||
|
|||||||
0
runtime/lib/shadow_tree.py
Normal file → Executable file
0
runtime/lib/shadow_tree.py
Normal file → Executable file
100
runtime/native-runner.php
Normal file
100
runtime/native-runner.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
if (PHP_SAPI !== 'cli')
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "CLI only\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pluginRoot = dirname(__DIR__);
|
||||||
|
$piwigoRoot = dirname($pluginRoot, 2);
|
||||||
|
$base = rtrim($piwigoRoot, '/').'/_data/bratonien-tools';
|
||||||
|
$schedulerDir = $base.'/nc-connector-scheduler';
|
||||||
|
$stateFile = $schedulerDir.'/state.json';
|
||||||
|
$runtimeDir = $base.'/nc-connector-runtime';
|
||||||
|
$stateRoot = $base.'/nc-connector-state';
|
||||||
|
|
||||||
|
function native_scheduler_state($path)
|
||||||
|
{
|
||||||
|
if (!is_readable($path)) return array();
|
||||||
|
$decoded = json_decode((string)@file_get_contents($path), true);
|
||||||
|
return is_array($decoded) ? $decoded : array();
|
||||||
|
}
|
||||||
|
|
||||||
|
function native_scheduler_write($path, array $state)
|
||||||
|
{
|
||||||
|
$json = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
|
||||||
|
if (!is_string($json)) return false;
|
||||||
|
$tmp = $path.'.tmp';
|
||||||
|
if (@file_put_contents($tmp, $json."\n", LOCK_EX) === false) return false;
|
||||||
|
@chmod($tmp, 0640);
|
||||||
|
return @rename($tmp, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array($schedulerDir, $runtimeDir, $stateRoot) as $dir)
|
||||||
|
{
|
||||||
|
if (!is_dir($dir) && !@mkdir($dir, 0750, true) && !is_dir($dir))
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "Runtime-Verzeichnis konnte nicht angelegt werden: {$dir}\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$state = native_scheduler_state($stateFile);
|
||||||
|
$state['enabled'] = true;
|
||||||
|
$state['mode'] = 'piwigo-native';
|
||||||
|
$state['state'] = 'running';
|
||||||
|
$state['message'] = 'NC-Abgleich läuft.';
|
||||||
|
$state['started_at'] = time();
|
||||||
|
$state['timestamp'] = time();
|
||||||
|
native_scheduler_write($stateFile, $state);
|
||||||
|
|
||||||
|
$env = $_ENV;
|
||||||
|
$env['PATH'] = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
|
||||||
|
$env['BRATONIEN_NC_NATIVE'] = '1';
|
||||||
|
$env['BRATONIEN_NC_PIWIGO_ROOT'] = $piwigoRoot;
|
||||||
|
$env['BRATONIEN_NC_CONFIG_DIR'] = $runtimeDir;
|
||||||
|
$env['BRATONIEN_NC_STATE_ROOT'] = $stateRoot;
|
||||||
|
$env['LC_ALL'] = 'C';
|
||||||
|
$env['LANG'] = 'C';
|
||||||
|
|
||||||
|
$bash = is_executable('/usr/bin/bash') ? '/usr/bin/bash' : '/bin/bash';
|
||||||
|
$command = array($bash, $pluginRoot.'/runtime/run-all.sh');
|
||||||
|
$spec = array(
|
||||||
|
0=>array('file','/dev/null','r'),
|
||||||
|
1=>array('pipe','w'),
|
||||||
|
2=>array('pipe','w'),
|
||||||
|
);
|
||||||
|
$process = @proc_open($command, $spec, $pipes, null, $env);
|
||||||
|
$stdout = '';
|
||||||
|
$stderr = '';
|
||||||
|
$exit = 1;
|
||||||
|
if (is_resource($process))
|
||||||
|
{
|
||||||
|
$stdout = (string)stream_get_contents($pipes[1]);
|
||||||
|
$stderr = (string)stream_get_contents($pipes[2]);
|
||||||
|
fclose($pipes[1]);
|
||||||
|
fclose($pipes[2]);
|
||||||
|
$exit = proc_close($process);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$stderr = 'run-all.sh konnte nicht gestartet werden.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$state = native_scheduler_state($stateFile);
|
||||||
|
$state['enabled'] = true;
|
||||||
|
$state['mode'] = 'piwigo-native';
|
||||||
|
$state['state'] = $exit === 0 ? 'success' : 'error';
|
||||||
|
$state['message'] = $exit === 0 ? 'NC-Abgleich erfolgreich abgeschlossen.' : 'NC-Abgleich fehlgeschlagen.';
|
||||||
|
$state['timestamp'] = time();
|
||||||
|
$state['finished_at'] = time();
|
||||||
|
$state['exit_code'] = $exit;
|
||||||
|
$state['stdout'] = trim($stdout);
|
||||||
|
$state['stderr'] = trim($stderr);
|
||||||
|
if (empty($state['next_due']) || (int)$state['next_due'] < time())
|
||||||
|
{
|
||||||
|
$state['next_due'] = time() + 60;
|
||||||
|
}
|
||||||
|
native_scheduler_write($stateFile, $state);
|
||||||
|
exit($exit === 0 ? 0 : 1);
|
||||||
@@ -78,8 +78,11 @@ function webdav_source_fingerprint($baseUrl, $user, array $roots)
|
|||||||
$pluginRoot = dirname(__DIR__);
|
$pluginRoot = dirname(__DIR__);
|
||||||
$piwigoRoot = dirname($pluginRoot, 2);
|
$piwigoRoot = dirname($pluginRoot, 2);
|
||||||
$dbConfig = $piwigoRoot.'/local/config/database.inc.php';
|
$dbConfig = $piwigoRoot.'/local/config/database.inc.php';
|
||||||
$configDir = '/etc/bratonien-tools/nc-connector';
|
$nativeMode = getenv('BRATONIEN_NC_NATIVE') === '1';
|
||||||
$stateRoot = '/var/lib/bratonien-tools/nc-connector';
|
$configDir = trim((string)getenv('BRATONIEN_NC_CONFIG_DIR'));
|
||||||
|
if ($configDir === '') $configDir = $nativeMode ? rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-connector-runtime' : '/etc/bratonien-tools/nc-connector';
|
||||||
|
$stateRoot = trim((string)getenv('BRATONIEN_NC_STATE_ROOT'));
|
||||||
|
if ($stateRoot === '') $stateRoot = $nativeMode ? rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-connector-state' : '/var/lib/bratonien-tools/nc-connector';
|
||||||
$publicSourceRoot = rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-webdav-source';
|
$publicSourceRoot = rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-webdav-source';
|
||||||
$publicGalleryRoot = rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-webdav-gallery';
|
$publicGalleryRoot = rtrim($piwigoRoot, '/').'/_data/bratonien-tools/nc-webdav-gallery';
|
||||||
$legacyGalleryRoot = rtrim($piwigoRoot, '/').'/galleries';
|
$legacyGalleryRoot = rtrim($piwigoRoot, '/').'/galleries';
|
||||||
@@ -107,11 +110,12 @@ try
|
|||||||
$rows = $db->query("SELECT id,name,adapter,config_json,secret_blob FROM `{$table}` ORDER BY id DESC");
|
$rows = $db->query("SELECT id,name,adapter,config_json,secret_blob FROM `{$table}` ORDER BY id DESC");
|
||||||
if (!$rows) fail_webdav_reconcile('Connector-Verbindungen konnten nicht gelesen werden: '.$db->error);
|
if (!$rows) fail_webdav_reconcile('Connector-Verbindungen konnten nicht gelesen werden: '.$db->error);
|
||||||
|
|
||||||
foreach (array($configDir, $publicSourceRoot, $publicGalleryRoot) as $dir)
|
foreach (array($configDir, $stateRoot, $publicSourceRoot, $publicGalleryRoot) as $dir)
|
||||||
{
|
{
|
||||||
if (!is_dir($dir) && !mkdir($dir, $dir === $configDir ? 0700 : 0755, true)) fail_webdav_reconcile('Runtime-Verzeichnis konnte nicht angelegt werden: '.$dir);
|
if (!is_dir($dir) && !mkdir($dir, $dir === $configDir ? 0700 : 0750, true)) fail_webdav_reconcile('Runtime-Verzeichnis konnte nicht angelegt werden: '.$dir);
|
||||||
}
|
}
|
||||||
@chmod($configDir, 0700);
|
@chmod($configDir, 0700);
|
||||||
|
@chmod($stateRoot, 0750);
|
||||||
@chmod($publicSourceRoot, 0755);
|
@chmod($publicSourceRoot, 0755);
|
||||||
@chmod($publicGalleryRoot, 0755);
|
@chmod($publicGalleryRoot, 0755);
|
||||||
|
|
||||||
@@ -150,7 +154,7 @@ try
|
|||||||
$seenFingerprints[$fingerprint] = $id;
|
$seenFingerprints[$fingerprint] = $id;
|
||||||
$known[$id] = true;
|
$known[$id] = true;
|
||||||
|
|
||||||
$stateDir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
$stateDir = $nativeMode ? $stateRoot.'/connection-'.$id : rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||||
if ($stateDir === '') $stateDir = $stateRoot.'/connection-'.$id;
|
if ($stateDir === '') $stateDir = $stateRoot.'/connection-'.$id;
|
||||||
if (!is_dir($stateDir) && !mkdir($stateDir, 0750, true)) fail_webdav_reconcile('State-Verzeichnis konnte nicht angelegt werden.');
|
if (!is_dir($stateDir) && !mkdir($stateDir, 0750, true)) fail_webdav_reconcile('State-Verzeichnis konnte nicht angelegt werden.');
|
||||||
@chmod($stateDir, 0750);
|
@chmod($stateDir, 0750);
|
||||||
@@ -219,7 +223,7 @@ try
|
|||||||
$config['parallel_gallery_root'] = $galleryRoot;
|
$config['parallel_gallery_root'] = $galleryRoot;
|
||||||
$config['source_fingerprint'] = $fingerprint;
|
$config['source_fingerprint'] = $fingerprint;
|
||||||
$config['runtime'] = array(
|
$config['runtime'] = array(
|
||||||
'mode'=>'webdav',
|
'mode'=>$nativeMode ? 'piwigo-native-webdav' : 'webdav',
|
||||||
'config'=>$configPath,
|
'config'=>$configPath,
|
||||||
'piwigo_sync_enabled'=>true,
|
'piwigo_sync_enabled'=>true,
|
||||||
'reconciled_at'=>date('Y-m-d H:i:s'),
|
'reconciled_at'=>date('Y-m-d H:i:s'),
|
||||||
|
|||||||
@@ -1,8 +1,18 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
|
|
||||||
CONFIG_DIR="/etc/bratonien-tools/nc-connector"
|
|
||||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
PIWIGO_ROOT_DEFAULT="${BRATONIEN_NC_PIWIGO_ROOT:-$(cd -- "$SCRIPT_DIR/../../.." && pwd)}"
|
||||||
|
CONFIG_DIR="${BRATONIEN_NC_CONFIG_DIR:-/etc/bratonien-tools/nc-connector}"
|
||||||
|
NATIVE_MODE="${BRATONIEN_NC_NATIVE:-0}"
|
||||||
|
GLOBAL_LOCK_DIR="${PIWIGO_ROOT_DEFAULT%/}/_data/bratonien-tools/nc-connector-scheduler"
|
||||||
|
GLOBAL_LOCK_FILE="$GLOBAL_LOCK_DIR/worker.lock"
|
||||||
|
mkdir -p -- "$GLOBAL_LOCK_DIR"
|
||||||
|
exec 8>"$GLOBAL_LOCK_FILE"
|
||||||
|
if ! flock -n 8; then
|
||||||
|
echo "NC Connector: ein Lauf ist bereits aktiv."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
shopt -s nullglob
|
shopt -s nullglob
|
||||||
|
|
||||||
read_config_value() {
|
read_config_value() {
|
||||||
@@ -39,10 +49,14 @@ write_route_status() {
|
|||||||
' "$ROUTE_STATUS_FILE" "$route" "$label" "$detail" "$success"
|
' "$ROUTE_STATUS_FILE" "$route" "$label" "$detail" "$success"
|
||||||
}
|
}
|
||||||
|
|
||||||
php "$SCRIPT_DIR/reconcile.php"
|
if [[ "$NATIVE_MODE" != "1" ]]; then
|
||||||
|
php "$SCRIPT_DIR/reconcile.php"
|
||||||
|
fi
|
||||||
php "$SCRIPT_DIR/reconcile-webdav.php"
|
php "$SCRIPT_DIR/reconcile-webdav.php"
|
||||||
php "$SCRIPT_DIR/cleanup-webdav-piwigo.php"
|
php "$SCRIPT_DIR/cleanup-webdav-piwigo.php"
|
||||||
php "$SCRIPT_DIR/cleanup-stale.php"
|
if [[ "$NATIVE_MODE" != "1" ]]; then
|
||||||
|
php "$SCRIPT_DIR/cleanup-stale.php"
|
||||||
|
fi
|
||||||
|
|
||||||
configs=("$CONFIG_DIR"/connection-*.conf)
|
configs=("$CONFIG_DIR"/connection-*.conf)
|
||||||
webdav_configs=("$CONFIG_DIR"/webdav-connection-*.conf)
|
webdav_configs=("$CONFIG_DIR"/webdav-connection-*.conf)
|
||||||
@@ -58,7 +72,7 @@ for candidate in "${webdav_configs[@]}" "${configs[@]}"; do
|
|||||||
route_piwigo_root="$(read_config_value PIWIGO_ROOT "$candidate")"
|
route_piwigo_root="$(read_config_value PIWIGO_ROOT "$candidate")"
|
||||||
[[ -n "$route_piwigo_root" ]] && break
|
[[ -n "$route_piwigo_root" ]] && break
|
||||||
done
|
done
|
||||||
[[ -n "$route_piwigo_root" ]] || route_piwigo_root="/var/www/piwigo"
|
[[ -n "$route_piwigo_root" ]] || route_piwigo_root="$PIWIGO_ROOT_DEFAULT"
|
||||||
ROUTE_STATUS_FILE="${route_piwigo_root%/}/_data/bratonien-tools/nc-connector-status/route-status.json"
|
ROUTE_STATUS_FILE="${route_piwigo_root%/}/_data/bratonien-tools/nc-connector-status/route-status.json"
|
||||||
|
|
||||||
failure_count=0
|
failure_count=0
|
||||||
@@ -91,43 +105,45 @@ for config in "${webdav_configs[@]}"; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
for config in "${configs[@]}"; do
|
if [[ "$NATIVE_MODE" != "1" ]]; then
|
||||||
[[ -f "$config" ]] || continue
|
for config in "${configs[@]}"; do
|
||||||
name="$(basename "$config")"
|
[[ -f "$config" ]] || continue
|
||||||
connection_id="0"
|
name="$(basename "$config")"
|
||||||
if [[ "$name" =~ ^connection-([0-9]+)\.conf$ ]]; then
|
connection_id="0"
|
||||||
connection_id="${BASH_REMATCH[1]}"
|
if [[ "$name" =~ ^connection-([0-9]+)\.conf$ ]]; then
|
||||||
fi
|
connection_id="${BASH_REMATCH[1]}"
|
||||||
if [[ "$connection_id" -lt 1 ]]; then
|
fi
|
||||||
echo "NC Connector: $name besitzt keine gueltige Verbindungs-ID." >&2
|
if [[ "$connection_id" -lt 1 ]]; then
|
||||||
failure_count=$((failure_count + 1))
|
echo "NC Connector: $name besitzt keine gueltige Verbindungs-ID." >&2
|
||||||
summary_parts+=("$name: ungueltige Verbindungs-ID")
|
failure_count=$((failure_count + 1))
|
||||||
continue
|
summary_parts+=("$name: ungueltige Verbindungs-ID")
|
||||||
fi
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
piwigo_root="$(read_config_value PIWIGO_ROOT "$config")"
|
piwigo_root="$(read_config_value PIWIGO_ROOT "$config")"
|
||||||
[[ -n "$piwigo_root" ]] || piwigo_root="/var/www/piwigo"
|
[[ -n "$piwigo_root" ]] || piwigo_root="$PIWIGO_ROOT_DEFAULT"
|
||||||
tombstone_dir="${piwigo_root%/}/_data/bratonien-tools/nc-connector-status"
|
tombstone_dir="${piwigo_root%/}/_data/bratonien-tools/nc-connector-status"
|
||||||
if [[ -f "$tombstone_dir/deleted-$connection_id" ]]; then
|
if [[ -f "$tombstone_dir/deleted-$connection_id" ]]; then
|
||||||
echo "NC Connector: Verbindung $connection_id wurde geloescht; Laufzeitdateien werden entfernt."
|
echo "NC Connector: Verbindung $connection_id wurde geloescht; Laufzeitdateien werden entfernt."
|
||||||
rm -f -- "$CONFIG_DIR/connection-$connection_id.conf" \
|
rm -f -- "$CONFIG_DIR/connection-$connection_id.conf" \
|
||||||
"$CONFIG_DIR/connection-$connection_id.db-password" \
|
"$CONFIG_DIR/connection-$connection_id.db-password" \
|
||||||
"$CONFIG_DIR/connection-$connection_id.piwigo-password" \
|
"$CONFIG_DIR/connection-$connection_id.piwigo-password" \
|
||||||
"$CONFIG_DIR/connection-$connection_id.storages.tsv" \
|
"$CONFIG_DIR/connection-$connection_id.storages.tsv" \
|
||||||
"$CONFIG_DIR/connection-$connection_id.roots.tsv"
|
"$CONFIG_DIR/connection-$connection_id.roots.tsv"
|
||||||
rm -f -- "$tombstone_dir/deleted-$connection_id"
|
rm -f -- "$tombstone_dir/deleted-$connection_id"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local_count=$((local_count + 1))
|
local_count=$((local_count + 1))
|
||||||
echo "NC Connector Local #$connection_id: $name"
|
echo "NC Connector Local #$connection_id: $name"
|
||||||
if env PIWIGO_CONFIG="$config" bash "$SCRIPT_DIR/sync.sh"; then
|
if env PIWIGO_CONFIG="$config" bash "$SCRIPT_DIR/sync.sh"; then
|
||||||
summary_parts+=("Local #$connection_id erfolgreich")
|
summary_parts+=("Local #$connection_id erfolgreich")
|
||||||
else
|
else
|
||||||
failure_count=$((failure_count + 1))
|
failure_count=$((failure_count + 1))
|
||||||
summary_parts+=("Local #$connection_id fehlgeschlagen")
|
summary_parts+=("Local #$connection_id fehlgeschlagen")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
summary_detail="$(IFS='; '; printf '%s' "${summary_parts[*]}")"
|
summary_detail="$(IFS='; '; printf '%s' "${summary_parts[*]}")"
|
||||||
[[ -n "$summary_detail" ]] || summary_detail="Keine Verbindung wurde ausgefuehrt."
|
[[ -n "$summary_detail" ]] || summary_detail="Keine Verbindung wurde ausgefuehrt."
|
||||||
|
|||||||
@@ -134,21 +134,33 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
exit "$PIWIGO_EXIT"
|
exit "$PIWIGO_EXIT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
MEDIA_UNIT="bratonien-nc-media-${CONNECTION_ID}-$(date +%s)"
|
if [[ "${BRATONIEN_NC_NATIVE:-0}" == "1" ]]; then
|
||||||
if ! systemd-run \
|
if command -v timeout >/dev/null 2>&1; then
|
||||||
--quiet \
|
if ! timeout 30m env PIWIGO_CONFIG="$CONFIG_FILE" bash "$SCRIPT_DIR/build-webdav-media.sh"; then
|
||||||
--collect \
|
write_status error "Bildaufbereitung ist fehlgeschlagen oder hat das 30-Minuten-Limit erreicht"
|
||||||
--unit="$MEDIA_UNIT" \
|
exit 1
|
||||||
--property=RuntimeMaxSec=30min \
|
fi
|
||||||
--setenv="PIWIGO_CONFIG=$CONFIG_FILE" \
|
elif ! env PIWIGO_CONFIG="$CONFIG_FILE" bash "$SCRIPT_DIR/build-webdav-media.sh"; then
|
||||||
/usr/bin/env bash "$SCRIPT_DIR/build-webdav-media.sh"; then
|
write_status error "Bildaufbereitung ist fehlgeschlagen"
|
||||||
write_status error "Bildaufbereitung konnte nicht im Hintergrund gestartet werden"
|
exit 1
|
||||||
exit 1
|
fi
|
||||||
|
else
|
||||||
|
MEDIA_UNIT="bratonien-nc-media-${CONNECTION_ID}-$(date +%s)"
|
||||||
|
if ! systemd-run \
|
||||||
|
--quiet \
|
||||||
|
--collect \
|
||||||
|
--unit="$MEDIA_UNIT" \
|
||||||
|
--property=RuntimeMaxSec=30min \
|
||||||
|
--setenv="PIWIGO_CONFIG=$CONFIG_FILE" \
|
||||||
|
/usr/bin/env bash "$SCRIPT_DIR/build-webdav-media.sh"; then
|
||||||
|
write_status error "Bildaufbereitung konnte nicht im Hintergrund gestartet werden"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if grep -q 'Piwigo-Synchronisierung per API erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
if grep -q 'Piwigo-Synchronisierung per API erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
||||||
write_status ok \
|
write_status ok \
|
||||||
"WebDAV eingelesen und Piwigo synchronisiert; Bildaufbereitung läuft im Hintergrund" \
|
"WebDAV eingelesen und Piwigo synchronisiert; Bildaufbereitung abgeschlossen" \
|
||||||
"" \
|
"" \
|
||||||
"api" \
|
"api" \
|
||||||
"ok" \
|
"ok" \
|
||||||
@@ -157,7 +169,7 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
"Fallback wurde nicht benötigt"
|
"Fallback wurde nicht benötigt"
|
||||||
elif grep -q 'Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
elif grep -q 'Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
||||||
write_status ok \
|
write_status ok \
|
||||||
"WebDAV eingelesen und Piwigo über Fallback synchronisiert; Bildaufbereitung läuft im Hintergrund" \
|
"WebDAV eingelesen und Piwigo über Fallback synchronisiert; Bildaufbereitung abgeschlossen" \
|
||||||
"" \
|
"" \
|
||||||
"fallback" \
|
"fallback" \
|
||||||
"not_used" \
|
"not_used" \
|
||||||
@@ -165,7 +177,7 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
"ok" \
|
"ok" \
|
||||||
"Benutzername/Passwort-Fallback erfolgreich"
|
"Benutzername/Passwort-Fallback erfolgreich"
|
||||||
else
|
else
|
||||||
write_status ok "WebDAV eingelesen und Piwigo synchronisiert; Bildaufbereitung läuft im Hintergrund"
|
write_status ok "WebDAV eingelesen und Piwigo synchronisiert; Bildaufbereitung abgeschlossen"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
write_status ok "WebDAV eingelesen; Piwigo-Synchronisierung ist für diese Verbindung deaktiviert"
|
write_status ok "WebDAV eingelesen; Piwigo-Synchronisierung ist für diese Verbindung deaktiviert"
|
||||||
|
|||||||
Reference in New Issue
Block a user