From bd5babfa293b440af714b1781e9e6425a04b2b90 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 11 Oct 2022 13:34:05 +0800 Subject: [PATCH] chore: add upgrader for #3 --- web/Dockerfile | 2 +- web/app/cli.php | 15 +-- web/app/models/UOJBlogEditor.php | 2 +- web/app/models/Upgrader.php | 2 + web/app/upgrade/3_parsedown/README.md | 1 + web/app/upgrade/3_parsedown/upgrade.php | 116 ++++++++++++++++++++++++ web/install.sh | 14 +-- 7 files changed, 132 insertions(+), 20 deletions(-) create mode 100644 web/app/upgrade/3_parsedown/README.md create mode 100644 web/app/upgrade/3_parsedown/upgrade.php diff --git a/web/Dockerfile b/web/Dockerfile index 3d916a3..9b16e68 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -19,7 +19,7 @@ if [ ! -f \"/var/uoj_data/.UOJSetupDone\" ]; then\n\ fi\n\ service ntp start\n\ service apache2 start\n\ -cd /opt/uoj/web && sh install.sh -d\n\ +cd /opt/uoj/web && sh install.sh -i\n\ exec bash\n" >/opt/up && chmod +x /opt/up ENV LANG=C.UTF-8 TZ=Asia/Shanghai diff --git a/web/app/cli.php b/web/app/cli.php index cd1d9c7..280a7b6 100644 --- a/web/app/cli.php +++ b/web/app/cli.php @@ -2,6 +2,7 @@ $_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__); +require_once $_SERVER['DOCUMENT_ROOT'] . '/app/vendor/autoload.php'; require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php'; // TODO: more beautiful argv parser @@ -9,7 +10,7 @@ require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php'; $handlers = [ 'upgrade:up' => function ($name) { if (func_num_args() != 1) { - die("php cli.php upgrade:up \n"); + die("php7.4 cli.php upgrade:up \n"); } Upgrader::transaction(function() use ($name) { Upgrader::up($name); @@ -18,7 +19,7 @@ $handlers = [ }, 'upgrade:down' => function ($name) { if (func_num_args() != 1) { - die("php cli.php upgrade:down \n"); + die("php7.4 cli.php upgrade:down \n"); } Upgrader::transaction(function() use ($name) { Upgrader::down($name); @@ -27,7 +28,7 @@ $handlers = [ }, 'upgrade:refresh' => function ($name) { if (func_num_args() != 1) { - die("php cli.php upgrade:refresh \n"); + die("php7.4 cli.php upgrade:refresh \n"); } Upgrader::transaction(function() use ($name) { Upgrader::refresh($name); @@ -36,7 +37,7 @@ $handlers = [ }, 'upgrade:remove' => function ($name) { if (func_num_args() != 1) { - die("php cli.php upgrade:remove \n"); + die("php7.4 cli.php upgrade:remove \n"); } Upgrader::transaction(function() use ($name) { Upgrader::remove($name); @@ -45,7 +46,7 @@ $handlers = [ }, 'upgrade:latest' => function () { if (func_num_args() != 0) { - die("php cli.php upgrade:latest\n"); + die("php7.4 cli.php upgrade:latest\n"); } Upgrader::transaction(function() { Upgrader::upgradeToLatest(); @@ -54,7 +55,7 @@ $handlers = [ }, 'upgrade:remove-all' => function () { if (func_num_args() != 0) { - die("php cli.php upgrade:remove-all\n"); + die("php7.4 cli.php upgrade:remove-all\n"); } Upgrader::transaction(function() { Upgrader::removeAll(); @@ -67,7 +68,7 @@ $handlers = [ function showHelp() { global $handlers; echo "UOJ Command-Line Interface\n"; - echo "php cli.php params1 params2 ...\n"; + echo "php7.4 cli.php params1 params2 ...\n"; echo "\n"; echo "The following tasks are available:\n"; foreach ($handlers as $cmd => $handler) { diff --git a/web/app/models/UOJBlogEditor.php b/web/app/models/UOJBlogEditor.php index c807a67..ce83610 100644 --- a/web/app/models/UOJBlogEditor.php +++ b/web/app/models/UOJBlogEditor.php @@ -121,7 +121,7 @@ class UOJBlogEditor { $element->setAttribute('class', $element->getAttribute('class') . ' table table-bordered'); } - $this->post_data['content'] = $purifier->purify($dom->saveHTML()); + $this->post_data['content'] = $dom->saveHTML(); if (preg_match('/^.*.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) { $content_less = substr($this->post_data['content'], 0, $matches[0][1]); diff --git a/web/app/models/Upgrader.php b/web/app/models/Upgrader.php index 42b3ac6..05d83f7 100644 --- a/web/app/models/Upgrader.php +++ b/web/app/models/Upgrader.php @@ -9,6 +9,8 @@ class Upgrader { passthru('mysql '.escapeshellarg(UOJConfig::$data['database']['database']) .' -u '.escapeshellarg(UOJConfig::$data['database']['username']) .' --password='.escapeshellarg(UOJConfig::$data['database']['password']) + .' -h '.escapeshellarg(UOJConfig::$data['database']['host']) + .' -P '.escapeshellarg(UOJConfig::$data['database']['port']) .'<'.escapeshellarg($filename), $ret); if ($ret !== 0) { die("run sql failed: ".HTML::escape($filename)."\n"); diff --git a/web/app/upgrade/3_parsedown/README.md b/web/app/upgrade/3_parsedown/README.md new file mode 100644 index 0000000..9ca43b6 --- /dev/null +++ b/web/app/upgrade/3_parsedown/README.md @@ -0,0 +1 @@ +ref: https://github.com/renbaoshuo/S2OJ/pull/3 diff --git a/web/app/upgrade/3_parsedown/upgrade.php b/web/app/upgrade/3_parsedown/upgrade.php new file mode 100644 index 0000000..74a197e --- /dev/null +++ b/web/app/upgrade/3_parsedown/upgrade.php @@ -0,0 +1,116 @@ + [ + 'enabled' => true, + 'matchSingleDollar' => true + ] + ]); + + $blogs = DB::query("SELECT * from blogs"); + + while ($blog = DB::fetch($blogs)) { + $id = $blog['id']; + $type = $blog['type']; + $content_md = $blog['content_md']; + $content = ''; + + echo "Processing blog $id...\n"; + + if ($type == 'blog') { + $dom = new DOMDocument; + $dom->loadHTML(mb_convert_encoding($parsedown->text($content_md), 'HTML-ENTITIES', 'UTF-8')); + $elements = $dom->getElementsByTagName('table'); + foreach ($elements as $element) { + $element->setAttribute('class', + $element->getAttribute('class') . ' table table-bordered'); + } + $content = $purifier->purify($dom->saveHTML()); + + if (preg_match('/^.*.*$/m', $content, $matches, PREG_OFFSET_CAPTURE)) { + $content_less = substr($content, 0, $matches[0][1]); + $content_more = substr($content, $matches[0][1] + strlen($matches[0][0])); + $content = $purifier->purify($content_less).''.$purifier->purify($content_more); + } else { + $content = $purifier->purify($content); + } + } elseif ($type == 'slide') { + $content_array = yaml_parse($content_md); + if ($content_array === false || !is_array($content_array)) { + continue; + } + + $marked = function($md) use ($parsedown, $purifier) { + $dom = new DOMDocument; + $dom->loadHTML(mb_convert_encoding($parsedown->text($md), 'HTML-ENTITIES', 'UTF-8')); + $elements = $dom->getElementsByTagName('li'); + + foreach ($elements as $element) { + $element->setAttribute('class', + $element->getAttribute('class') . ' fragment'); + } + + return $purifier->purify($dom->saveHTML()); + }; + + $config = array(); + $content = ''; + foreach ($content_array as $slide_name => $slide_content) { + if (is_array($slide_content) && is_array($slide_content['config'])) { + foreach (array('theme') as $config_key) { + if (is_string($slide_content['config'][$config_key]) && strlen($slide_content['config'][$config_key]) <= 30) { + $config[$config_key] = $slide_content['config'][$config_key]; + } + } + continue; + } + + $content .= '
'; + + if (is_string($slide_content)) { + $content .= $marked($slide_content); + } elseif (is_array($slide_content)) { + if (is_array($slide_content['children'])) { + foreach ($slide_content['children'] as $cslide_name => $cslide_content) { + $content .= '
'; + $content .= $marked($cslide_content); + $content .= '
'; + } + } + } + $content .= "
\n"; + } + $content = json_encode($config) . "\n" . $content; + } + + DB::update("UPDATE blogs SET content = '" . DB::escape($content) . "' WHERE id = $id"); + } + + $problems_contents = DB::query("SELECT * from problems_contents"); + + while ($problem = DB::fetch($problems_contents)) { + $content_md = $problem['statement_md']; + $content = ''; + + echo "Processing problem {$problem['id']}...\n"; + + $dom = new DOMDocument; + $dom->loadHTML(mb_convert_encoding($parsedown->text($content_md), 'HTML-ENTITIES', 'UTF-8')); + $elements = $dom->getElementsByTagName('table'); + foreach ($elements as $element) { + $element->setAttribute('class', + $element->getAttribute('class') . ' table table-bordered'); + } + $content = $purifier->purify($dom->saveHTML()); + + DB::update("UPDATE problems_contents set statement = '" . DB::escape($content) . "' where id = {$problem['id']}"); + } + } elseif ($type == 'down') { + // + } +}; diff --git a/web/install.sh b/web/install.sh index 177dee6..e86112f 100644 --- a/web/install.sh +++ b/web/install.sh @@ -58,7 +58,7 @@ setWebConf(){ ln -sf /opt/uoj/web /var/www/uoj chown -R www-data /var/www/uoj/app/storage # Set web config file - php -a < Doing initial config and start service\n" #Set uoj_data path mkdir -p /var/uoj_data/upload @@ -90,12 +90,8 @@ dockerInitProgress() { mkdir -p /opt/uoj/web/app/storage/submission mkdir -p /opt/uoj/web/app/storage/tmp chmod -R 777 /opt/uoj/web/app/storage -} - -initProgress(){ - dockerInitProgress; #Using cli upgrade to latest - php /var/www/uoj/app/cli.php upgrade:latest + php7.4 /var/www/uoj/app/cli.php upgrade:latest touch /var/uoj_data/.UOJSetupDone #Touch SetupDone flag file printf "\n\n***Installation complete. Enjoy!***\n" @@ -119,10 +115,6 @@ while [ $# -gt 0 ]; do echo 'Initing UOJ System web...' initProgress ;; - -d | --docker-init) - echo 'Initing UOJ System web for docker...' - dockerInitProgress - ;; -? | --*) echo "Illegal option $1" ;;