diff options
| author | moderndevslulw <moderndevslulw@alright.party> | 2025-07-08 14:05:24 +0500 |
|---|---|---|
| committer | moderndevslulw <moderndevslulw@alright.party> | 2025-07-08 14:05:24 +0500 |
| commit | 23383a8a635e6a71fe0c972d942f40ad1af7f776 (patch) | |
| tree | bd0e4c085f5530aa2d105ea29a0ebd2c8a2891a3 | |
| parent | 202d3e2936644143faa37adf56f98ea71c275d0b (diff) | |
fix: build metadata for scripts only when there are changes
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | lib/utils.php | 69 | ||||
| -rw-r--r-- | public/scripts.php | 84 |
3 files changed, 93 insertions, 62 deletions
@@ -1,2 +1,4 @@ config.php /vendor/ +scriptmetadata.json +*.ini
\ No newline at end of file diff --git a/lib/utils.php b/lib/utils.php index 6de3dfb..639eed4 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -1,4 +1,7 @@ <?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php'; +use Raudius\Luar\Luar; + function format_timestamp($timestamp) { $days = (int) floor($timestamp / (60.0 * 60.0 * 24.0)); @@ -18,4 +21,70 @@ function format_timestamp($timestamp) } else { return "$years year" . ($years > 1 ? "s" : ""); } +} + +function parse_lua_script($value) +{ + if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) { + return $value->getValue(); + } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) { + return parse_lua_script($value->getValue()); + } + + if (is_array($value)) { + $out = []; + foreach ($value as $k => $v) { + if ($k == 'handle') { + continue; + } + $out[$k] = parse_lua_script($v); + } + return $out; + } + + return $value; +} + +function build_script_metadata(string $folder_path) +{ + $file_path = $_SERVER["DOCUMENT_ROOT"] . "/../scriptmetadata.json"; + $should_create = is_dir($folder_path); + $files = glob("$folder_path/*.lua"); + + if (file_exists($file_path)) { + $file_last_change = filemtime($file_path); + $script_last_change = 0; + foreach ($files as $file) { + $l = filemtime($file); + if ($l > $script_last_change) { + $l = $script_last_change; + } + } + $should_create = $script_last_change > $file_last_change; + } + + if ($should_create) { + $scripts = []; + + $luar = new Luar(); + $parsedown = new Parsedown(); + + foreach ($files as $file) { + $contents = file_get_contents($file); + + $result = $luar->eval($contents); + $script = parse_lua_script($result); + if (!isset($script["description"])) { + $script["description"] = "_No description provided._"; + } + $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']); + $script['description_formatted'] = $parsedown->text($script['description']); + + array_push($scripts, $script); + } + + file_put_contents($file_path, json_encode($scripts, JSON_UNESCAPED_SLASHES)); + } + + return true; }
\ No newline at end of file diff --git a/public/scripts.php b/public/scripts.php index 0f80be4..988b4bc 100644 --- a/public/scripts.php +++ b/public/scripts.php @@ -1,79 +1,39 @@ <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php'; -use Raudius\Luar\Luar; - -function parse_lua_script($value) -{ - if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) { - return $value->getValue(); - } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) { - return parse_lua_script($value->getValue()); - } - - if (is_array($value)) { - $out = []; - foreach ($value as $k => $v) { - if ($k == 'handle') { - continue; - } - $out[$k] = parse_lua_script($v); - } - return $out; - } - - return $value; -} - if (empty(LUA_SCRIPT_DIRECTORY)) { http_response_code(403); exit; } -$command = null; -$scripts = []; - -// loading lua scripts -$luar = new Luar(); -$parsedown = new Parsedown(); -$file_paths = glob(LUA_SCRIPT_DIRECTORY . '/*.lua'); -foreach ($file_paths as $file_path) { - $script = file_get_contents($file_path); - - $result = $luar->eval($script); - - $script = parse_lua_script($result); - $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']); - - array_push($scripts, $script); +if (!build_script_metadata(LUA_SCRIPT_DIRECTORY)) { + http_response_code(500); + exit("Failed to build a metadata for scripts."); } +$script = null; +$scripts = json_decode(file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/../scriptmetadata.json'), true); + if ($cmd_id = $_GET['id'] ?? false) { - $command = array_find( + $script = array_find( $scripts, fn($x) => $x['name'] == $cmd_id || (isset($x['aliases']) ? in_array($cmd_id, $x['aliases']) : false) ); - if (!$command) { + if (!$script) { http_response_code(404); exit; } } -if ($command) { - if (isset($command['description'])) { - $command['description_formatted'] = $parsedown->text($command['description']); - } -} ?> <html> <head> - <title><?= isset($command) ? "!{$command['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?></title> + <title><?= isset($script) ? "!{$script['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?></title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <link rel="stylesheet" href="/static/style.css"> <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon"> @@ -91,9 +51,9 @@ if ($command) { <?php if (!empty($scripts)): ?> <!-- SIDE BAR --> <div class="wiki-sidebar"> - <?php foreach ($scripts as $script): ?> - <p><a href="/!<?= $script['name'] ?>" - title="<?= substr($script['description_text'] ?? '', 0, 64) . (strlen($script['description_text']) > 64 ? '...' : '') ?>"><?= $script['name'] ?></a> + <?php foreach ($scripts as $s): ?> + <p><a href="/!<?= $s['name'] ?>" + title="<?= substr($s['description_text'] ?? '', 0, 64) . (strlen($s['description_text']) > 64 ? '...' : '') ?>"><?= $s['name'] ?></a> </p> <?php endforeach; ?> </div> @@ -101,38 +61,38 @@ if ($command) { <!-- WIKI CONTENT --> <div class="wiki-content"> - <?php if (isset($command)): ?> - <h1>!<?= $command['name'] ?><a href="/!/<?= $command['name'] ?>.lua"><img + <?php if (isset($script)): ?> + <h1>!<?= $script['name'] ?><a href="/!/<?= $script['name'] ?>.lua"><img src="/static/img/icons/code.png" alt="[code]" title="Check the source code"></a></h1> <table class="vertical"> <tr> <th>Delay</th> - <td><?= format_timestamp($command['delay_sec']) ?></td> + <td><?= format_timestamp($script['delay_sec']) ?></td> </tr> - <?php if (!empty($command['subcommands'])): ?> + <?php if (!empty($script['subcommands'])): ?> <tr> <th>Subcommands</th> <td> - <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['subcommand'])) ?> + <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $script['subcommands'])) ?> </td> </tr> <?php endif; ?> - <?php if (!empty($command['aliases'])): ?> + <?php if (!empty($script['aliases'])): ?> <tr> <th>Aliases</th> <td> - <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['aliases'])) ?> + <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $script['aliases'])) ?> </td> </tr> <?php endif; ?> <tr> <th>Minimal rights</th> - <td><?= $command['minimal_rights'] ?></td> + <td><?= $script['minimal_rights'] ?></td> </tr> </table> <div> - <?php if (isset($command['description_formatted'])): ?> - <?= $command['description_formatted'] ?> + <?php if (isset($script['description_formatted'])): ?> + <?= $script['description_formatted'] ?> <?php else: ?> <p><i>No description.</i></p> <?php endif; ?> |
