add migrating textures from v2 to v3

This commit is contained in:
printempw 2016-08-10 18:03:06 +08:00
parent ddfdbb4ab0
commit 8241e98121
6 changed files with 314 additions and 0 deletions

View File

@ -14,4 +14,9 @@ class Migration
require BASE_DIR."/setup/tables.php";
}
public static function importV2Textures()
{
return require BASE_DIR."/setup/migrations/import_v2_textures.php";
}
}

View File

@ -0,0 +1,93 @@
@extends('setup.migrations.master')
@section('content')
<?php $step = isset($_GET['step']) ? $_GET['step'] : '1'; ?>
{{-- Step 1: --}}
@if ($step == '1')
<h1>导入皮肤库</h1>
<p>本功能用于导入 v2 用户皮肤至 v3 的皮肤库</p>
<p>请先将 v2 的 users 表改名导入到当前 v3 的同一数据库中</p>
<form id="setup" method="post" action="index.php?action=import-v2-textures&step=2" novalidate="novalidate">
<table class="form-table">
<tr>
<th scope="row"><label for="v2_table_name">v2 的用户表名</label></th>
<td>
<input name="v2_table_name" type="v2_table_name" id="v2_table_name" size="25" value="" />
<p>就是你改名过的 v2 的 users 表现在的名字</p>
</td>
</tr>
<tr>
<th scope="row"><label for="uploader_uid">材质上传者 uid</label></th>
<td>
<input name="uploader_uid" type="text" id="uploader_uid" size="25" value="0" />
<p>
<span class="description important">
导入后的材质在皮肤库中显示的上传者,填写 0 会显示为「不存在的用户」
</span>
</p>
</td>
</tr>
<tr>
<th scope="row"><label for="texture_name_pattern">导入后的材质名称</label></th>
<td>
<input name="texture_name_pattern" type="text" id="texture_name_pattern" size="25" value="{username} - {model}" />
<p>
<span class="description important">
{username} 表示材质原本的上传者用户名,{model} 表示原来材质的模型
</span>
</p>
</td>
</tr>
</table>
@if (isset($_SESSION['msg']))
<div class="alert alert-warning" role="alert">{{ htmlspecialchars($_SESSION['msg']) }}</div>
<?php unset($_SESSION['msg']); ?>
@endif
<p class="step">
<input type="submit" name="submit" id="submit" class="button button-large" value="开始迁移" />
</p>
</form>
@endif
{{-- Step 2: --}}
@if ($step == '2')
<?php
if (Validate::checkPost(['v2_table_name', 'uploader_uid', 'texture_name_pattern'])) {
if ($_POST['v2_table_name'] == "") {
Http::redirect('index.php?action=import-v2-textures&step=1', 'v2 users 表名不能为空');
} else {
$_POST['uploader_uid'] = ($_POST['uploader_uid'] == "") ? 0 : (int)$_POST['uploader_uid'];
if (!Schema::hasTable($_POST['v2_table_name'])) {
Http::redirect('index.php?action=import-v2-textures&step=1', '不存在的数据表');
}
}
} else {
Http::redirect('index.php?action=import-v2-textures&step=1', '表单信息不完整');
}
?>
<h1>导入成功</h1>
<?php $result = Migration::importV2Textures(); ?>
<p>已导入 {{ $result['imported'] }} 个材质到皮肤库,{{ $result['duplicated'] }} 个材质因重复而未导入。</p>
<p class="step">
<a href="../../" class="button button-large">导入完成</a>
</p>
@endif
@endsection

View File

@ -0,0 +1,18 @@
@extends('setup.migrations.master')
@section('content')
<h1>欢迎</h1>
<p>欢迎使用 Blessing Skin Server 数据迁移工具,此工具用于迁移 v2 的数据至 v3。</p>
<p>目前仅支持从 v2 导入用户皮肤至 v3 的皮肤库中。</p>
<p>更多功能等我有时间再些吧(学业为重</p>
<hr />
<p>选择一个操作以继续:</p>
<p class="step">
<a href="index.php?action=import-v2-textures" class="button button-large">导入 v2 皮肤库</a>
<a class="button button-large" disabled="disabled">导入 v2 用户数据</a>
</p>
@endsection

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title>Blessing Skin Server 数据迁移</title>
<link rel="shortcut icon" href="../../assets/images/favicon.ico">
<link rel="stylesheet" type="text/css" href="../../assets/dist/css/install.css">
<style>
.container a.button {
margin-right: 15px;
}
hr {
border-bottom: 1px solid #bbbbbb;
border-top: 0;
}
</style>
</head>
<body class="container">
<p id="logo"><a href="https://github.com/printempw/blessing-skin-server" tabindex="-1">Blessing Skin Server</a></p>
@yield('content')
</body>
</html>

View File

@ -0,0 +1,108 @@
<?php
/**
* @Author: printempw
* @Date: 2016-08-09 21:44:13
* @Last Modified by: printempw
* @Last Modified time: 2016-08-10 18:00:38
*
* There are still some coupling relationships here but,
* Just let it go :)
*/
$v2_table_name = $_POST['v2_table_name'];
$v3_table_name = Config::getDbConfig()['prefix']."textures";
$imported = 0;
$duplicated = 0;
// use db helper instead of fat ORM
$db = new Database($v2_table_name);
$steps = ceil($db->getRecordNum() / 250);
// chunked
for ($i = 0; $i <= $steps; $i++) {
$start = $i * 250;
$sql = "SELECT * FROM `$v2_table_name` ORDER BY `uid` LIMIT $start, 250";
$result = $db->query($sql);
while ($row = $result->fetch_array()) {
// compile patterns
$name = str_replace('{username}', $row['username'], $_POST['texture_name_pattern']);
if ($row['hash_steve'] != "") {
$name = str_replace('{model}', 'steve', $name);
if (!$db->has('hash', $row['hash_steve'], $v3_table_name)) {
$db->insert([
'name' => $name,
'type' => 'steve',
'likes' => 0,
'hash' => $row['hash_steve'],
'size' => 0,
'uploader' => $_POST['uploader_uid'],
'public' => '1',
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
$imported += 1;
// echo $row['hash_steve']." saved. <br />";
} else {
$duplicated += 1;
// echo $row['hash_steve']." duplicated. <br />";
}
}
if ($row['hash_alex'] != "") {
$name = str_replace('{model}', 'alex', $name);
if (!$db->has('hash', $row['hash_alex'], $v3_table_name)) {
$db->insert([
'name' => $name,
'type' => 'alex',
'likes' => 0,
'hash' => $row['hash_steve'],
'size' => 0,
'uploader' => $_POST['uploader_uid'],
'public' => '1',
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
$imported += 1;
// echo $row['hash_alex']." saved. <br />";
} else {
$duplicated += 1;
// echo $row['hash_alex']." duplicated. <br />";
}
}
if ($row['hash_cape'] != "") {
$name = str_replace('{model}', 'cape', $name);
if (!$db->has('hash', $row['hash_cape'], $v3_table_name)) {
$db->insert([
'name' => $name,
'type' => 'cape',
'likes' => 0,
'hash' => $row['hash_steve'],
'size' => 0,
'uploader' => $_POST['uploader_uid'],
'public' => '1',
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
$imported += 1;
// echo $row['hash_cape']." saved. <br />";
} else {
$duplicated += 1;
// echo $row['hash_cape']." duplicated. <br />";
}
}
}
}
return [
'imported' => $imported,
'duplicated' => $duplicated
];

View File

@ -0,0 +1,61 @@
<?php
/**
* Migrations Bootstrap of Blessing Skin Server
*/
// Define Base Directory
define('BASE_DIR', dirname(dirname(dirname(__FILE__))));
// Register Composer Auto Loader
require BASE_DIR.'/vendor/autoload.php';
// Boot Services
App\Services\Boot::loadServices();
Config::checkPHPVersion();
Boot::loadDotEnv(BASE_DIR);
Boot::registerErrorHandler();
Boot::startSession();
$db_config = Config::getDbConfig();
// Boot Eloquent to make Schema available
if (Config::checkDbConfig($db_config)) {
Boot::bootEloquent($db_config);
}
if (isset($_COOKIE['email']) && isset($_COOKIE['token'])) {
$_SESSION['email'] = $_COOKIE['email'];
$_SESSION['token'] = $_COOKIE['token'];
}
// check permission
if (isset($_SESSION['email'])) {
$user = new App\Models\User($_SESSION['email']);
if ($_SESSION['token'] != $user->getToken())
Http::redirect('../../auth/login', '无效的 token请重新登录~');
if ($user->getPermission() != "2")
Http::abort(403, '此页面仅超级管理员可访问');
} else {
Http::redirect('../../auth/login', '非法访问,请先登录');
}
$action = isset($_GET['action']) ? $_GET['action'] : 'index';
switch ($action) {
case 'index':
View::show('setup.migrations.index');
break;
case 'import-v2-textures':
View::show('setup.migrations.import-v2-textures');
break;
default:
throw new App\Exceptions\E('非法参数', 1, true);
break;
}