chore: add upgrader for #3
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2022-10-11 13:34:05 +08:00
parent 5e30f342fd
commit bd5babfa29
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
7 changed files with 132 additions and 20 deletions

View File

@ -19,7 +19,7 @@ if [ ! -f \"/var/uoj_data/.UOJSetupDone\" ]; then\n\
fi\n\ fi\n\
service ntp start\n\ service ntp start\n\
service apache2 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 exec bash\n" >/opt/up && chmod +x /opt/up
ENV LANG=C.UTF-8 TZ=Asia/Shanghai ENV LANG=C.UTF-8 TZ=Asia/Shanghai

View File

@ -2,6 +2,7 @@
$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__); $_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__);
require_once $_SERVER['DOCUMENT_ROOT'] . '/app/vendor/autoload.php';
require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php'; require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php';
// TODO: more beautiful argv parser // TODO: more beautiful argv parser
@ -9,7 +10,7 @@ require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php';
$handlers = [ $handlers = [
'upgrade:up' => function ($name) { 'upgrade:up' => function ($name) {
if (func_num_args() != 1) { if (func_num_args() != 1) {
die("php cli.php upgrade:up <name>\n"); die("php7.4 cli.php upgrade:up <name>\n");
} }
Upgrader::transaction(function() use ($name) { Upgrader::transaction(function() use ($name) {
Upgrader::up($name); Upgrader::up($name);
@ -18,7 +19,7 @@ $handlers = [
}, },
'upgrade:down' => function ($name) { 'upgrade:down' => function ($name) {
if (func_num_args() != 1) { if (func_num_args() != 1) {
die("php cli.php upgrade:down <name>\n"); die("php7.4 cli.php upgrade:down <name>\n");
} }
Upgrader::transaction(function() use ($name) { Upgrader::transaction(function() use ($name) {
Upgrader::down($name); Upgrader::down($name);
@ -27,7 +28,7 @@ $handlers = [
}, },
'upgrade:refresh' => function ($name) { 'upgrade:refresh' => function ($name) {
if (func_num_args() != 1) { if (func_num_args() != 1) {
die("php cli.php upgrade:refresh <name>\n"); die("php7.4 cli.php upgrade:refresh <name>\n");
} }
Upgrader::transaction(function() use ($name) { Upgrader::transaction(function() use ($name) {
Upgrader::refresh($name); Upgrader::refresh($name);
@ -36,7 +37,7 @@ $handlers = [
}, },
'upgrade:remove' => function ($name) { 'upgrade:remove' => function ($name) {
if (func_num_args() != 1) { if (func_num_args() != 1) {
die("php cli.php upgrade:remove <name>\n"); die("php7.4 cli.php upgrade:remove <name>\n");
} }
Upgrader::transaction(function() use ($name) { Upgrader::transaction(function() use ($name) {
Upgrader::remove($name); Upgrader::remove($name);
@ -45,7 +46,7 @@ $handlers = [
}, },
'upgrade:latest' => function () { 'upgrade:latest' => function () {
if (func_num_args() != 0) { if (func_num_args() != 0) {
die("php cli.php upgrade:latest\n"); die("php7.4 cli.php upgrade:latest\n");
} }
Upgrader::transaction(function() { Upgrader::transaction(function() {
Upgrader::upgradeToLatest(); Upgrader::upgradeToLatest();
@ -54,7 +55,7 @@ $handlers = [
}, },
'upgrade:remove-all' => function () { 'upgrade:remove-all' => function () {
if (func_num_args() != 0) { 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::transaction(function() {
Upgrader::removeAll(); Upgrader::removeAll();
@ -67,7 +68,7 @@ $handlers = [
function showHelp() { function showHelp() {
global $handlers; global $handlers;
echo "UOJ Command-Line Interface\n"; echo "UOJ Command-Line Interface\n";
echo "php cli.php <task-name> params1 params2 ...\n"; echo "php7.4 cli.php <task-name> params1 params2 ...\n";
echo "\n"; echo "\n";
echo "The following tasks are available:\n"; echo "The following tasks are available:\n";
foreach ($handlers as $cmd => $handler) { foreach ($handlers as $cmd => $handler) {

View File

@ -121,7 +121,7 @@ class UOJBlogEditor {
$element->setAttribute('class', $element->setAttribute('class',
$element->getAttribute('class') . ' table table-bordered'); $element->getAttribute('class') . ' table table-bordered');
} }
$this->post_data['content'] = $purifier->purify($dom->saveHTML()); $this->post_data['content'] = $dom->saveHTML();
if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) { if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) {
$content_less = substr($this->post_data['content'], 0, $matches[0][1]); $content_less = substr($this->post_data['content'], 0, $matches[0][1]);

View File

@ -9,6 +9,8 @@ class Upgrader {
passthru('mysql '.escapeshellarg(UOJConfig::$data['database']['database']) passthru('mysql '.escapeshellarg(UOJConfig::$data['database']['database'])
.' -u '.escapeshellarg(UOJConfig::$data['database']['username']) .' -u '.escapeshellarg(UOJConfig::$data['database']['username'])
.' --password='.escapeshellarg(UOJConfig::$data['database']['password']) .' --password='.escapeshellarg(UOJConfig::$data['database']['password'])
.' -h '.escapeshellarg(UOJConfig::$data['database']['host'])
.' -P '.escapeshellarg(UOJConfig::$data['database']['port'])
.'<'.escapeshellarg($filename), $ret); .'<'.escapeshellarg($filename), $ret);
if ($ret !== 0) { if ($ret !== 0) {
die("run sql failed: ".HTML::escape($filename)."\n"); die("run sql failed: ".HTML::escape($filename)."\n");

View File

@ -0,0 +1 @@
ref: https://github.com/renbaoshuo/S2OJ/pull/3

View File

@ -0,0 +1,116 @@
<?php
return function ($type) {
if ($type == 'up') {
DB::init();
$purifier = HTML::purifier();
$parsedown = new ParsedownMath([
'math' => [
'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('/^.*<!--.*readmore.*-->.*$/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).'<!-- readmore -->'.$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 .= '<section>';
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 .= '<section>';
$content .= $marked($cslide_content);
$content .= '</section>';
}
}
}
$content .= "</section>\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') {
//
}
};

View File

@ -58,7 +58,7 @@ setWebConf(){
ln -sf /opt/uoj/web /var/www/uoj ln -sf /opt/uoj/web /var/www/uoj
chown -R www-data /var/www/uoj/app/storage chown -R www-data /var/www/uoj/app/storage
# Set web config file # Set web config file
php -a <<UOJEOF php7.4 -a <<UOJEOF
\$config = include '/var/www/uoj/app/.default-config.php'; \$config = include '/var/www/uoj/app/.default-config.php';
\$config['database']['host']='$_database_host_'; \$config['database']['host']='$_database_host_';
\$config['database']['password']='$_database_password_'; \$config['database']['password']='$_database_password_';
@ -76,7 +76,7 @@ UOJEOF
make all -j$(($(nproc) + 1)) && cd /opt/uoj/web make all -j$(($(nproc) + 1)) && cd /opt/uoj/web
} }
dockerInitProgress() { initProgress(){
printf "\n\n==> Doing initial config and start service\n" printf "\n\n==> Doing initial config and start service\n"
#Set uoj_data path #Set uoj_data path
mkdir -p /var/uoj_data/upload 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/submission
mkdir -p /opt/uoj/web/app/storage/tmp mkdir -p /opt/uoj/web/app/storage/tmp
chmod -R 777 /opt/uoj/web/app/storage chmod -R 777 /opt/uoj/web/app/storage
}
initProgress(){
dockerInitProgress;
#Using cli upgrade to latest #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 /var/uoj_data/.UOJSetupDone
#Touch SetupDone flag file #Touch SetupDone flag file
printf "\n\n***Installation complete. Enjoy!***\n" printf "\n\n***Installation complete. Enjoy!***\n"
@ -119,10 +115,6 @@ while [ $# -gt 0 ]; do
echo 'Initing UOJ System web...' echo 'Initing UOJ System web...'
initProgress initProgress
;; ;;
-d | --docker-init)
echo 'Initing UOJ System web for docker...'
dockerInitProgress
;;
-? | --*) -? | --*)
echo "Illegal option $1" echo "Illegal option $1"
;; ;;