diff --git a/utils.inc.php b/utils.inc.php index e11d416..82bdd9e 100644 --- a/utils.inc.php +++ b/utils.inc.php @@ -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 = []; diff --git a/yeswiki-updater.php b/yeswiki-updater.php index 8a792d8..2f79dbf 100755 --- a/yeswiki-updater.php +++ b/yeswiki-updater.php @@ -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());