feat : depth for search and download source for updates

This commit is contained in:
Florian Schmitt 2024-01-30 21:17:10 +03:00
parent 1e3942f609
commit 5c7858c4e0
2 changed files with 57 additions and 22 deletions

View file

@ -283,6 +283,12 @@ function removePhpFpmConfig($user)
exec('service ' . $_SERVER['phpservice'] . ' reload', $output);
}
function upgradeWiki($srcDir, $destDir)
{
$output = 'Update ' . $destDir;
return $output;
}
function copyYesWikiFiles($domain, $user, $dbUser, $herseUser = null, $hersePass = null, $nossl = null)
{
$tmpFile = '/tmp/yeswiki.zip';
@ -343,13 +349,15 @@ function addStatistics()
}
function searchWikis($path, $pattern)
function searchWikis($path, $pattern, $depth = 1)
{
$it = new RecursiveDirectoryIterator($path);
$list = array();
$httpClient = HttpClientBuilder::buildDefault();
$nb = 0;
foreach (new RecursiveIteratorIterator($it) as $file) {
$files = new RecursiveIteratorIterator($it);
$files->setMaxDepth($depth);
foreach ($files as $file) {
if (preg_match('/' . preg_quote($pattern) . '$/i', $file)) {
$nb++;
$wakkaConfig = [];

View file

@ -7,8 +7,9 @@ use DevCoder\DotEnv;
$climate = new League\CLImate\CLImate();
$climate->description('yeswiki-updater, find and update YesWiki like a professionnal 🌈🦄');
$isRoot = (0 == posix_getuid());
if (0 == posix_getuid()) {
if ($isRoot) {
$absolutePathToEnvFile = __DIR__ . '/.env';
if (file_exists($absolutePathToEnvFile)) {
(new DotEnv($absolutePathToEnvFile))->load();
@ -36,6 +37,12 @@ if (0 == posix_getuid()) {
'description' => 'Update to latest version of stable yeswiki',
'noValue' => true
],
'depth' => [
'prefix' => 'd',
'longPrefix' => 'depth',
'description' => 'Depth to scan folders for wikis',
'defaultValue' => 1
],
]);
try {
$climate->arguments->parse();
@ -51,29 +58,49 @@ if (0 == posix_getuid()) {
try {
$update = $climate->arguments->get('update');
$output = $climate->arguments->get('output');
$matches = searchWikis($path, 'wakka.config.php');
if (count($matches) > 0 && $update) {
$climate->info('update');
$depth = $climate->arguments->get('depth');
$matches = searchWikis($path, 'wakka.config.php', $depth);
if (count($matches) == 0) {
$climate->info('No yeswiki found on path ' . $path . ' with depth ' . $depth);
exit;
}
$climate->info(count($matches) . ' yeswikis found on ' . $path);
switch ($output) {
if (count($matches) > 0 && $update) { // update yeswiki list
$climate->info('Update yeswiki');
$tmpFile = '/tmp/yeswiki.zip';
$destDir = '/tmp/yeswiki_for_update';
exec('rm -rf ' . $destDir . ' && mkdir -p ' . $destDir, $output);
if (file_exists($tmpFile)) {
unlink($tmpFile);
}
exec('curl --insecure -s -o ' . $tmpFile . ' ' . $_SERVER['source_archive_url']);
exec('unzip ' . $tmpFile . ' "doryphore/*" -d ' . $destDir);
exec('mv ' . $destDir . '/doryphore/* ' . $destDir . '/');
exec('rm -rf ' . $destDir . '/doryphore');
unlink($tmpFile);
foreach ($matches as $k => $wiki) {
$climate->info(upgradeWiki($destDir, $wiki['PATH']));
}
} else { // show yeswiki list info
$climate->info(count($matches) . ' yeswikis found on ' . $path);
switch ($output) {
case 'table':
$climate->table($matches);
break;
case 'table':
$climate->table($matches);
break;
case 'json':
$climate->json($matches);
break;
case 'json':
$climate->json($matches);
break;
case 'csv':
$fileName = 'yeswiki-list.csv';
$file = fopen($fileName, "w");
foreach ($matches as $line) {
fputcsv($file, $line);
}
$climate->info($fileName . ' was created.');
break;
case 'csv':
$fileName = 'yeswiki-list.csv';
$file = fopen($fileName, "w");
foreach ($matches as $line) {
fputcsv($file, $line);
}
$climate->info($fileName . ' was created.');
break;
}
}
} catch (Exception $e) {
$climate->error('ERROR : ' . $e->getMessage());