blessing-skin-server/setup/migrations/import_v2_textures.php

113 lines
3.5 KiB
PHP
Raw Normal View History

2016-08-10 18:03:06 +08:00
<?php
/**
* @Author: printempw
* @Date: 2016-08-09 21:44:13
* @Last Modified by: printempw
2016-08-19 23:09:32 +08:00
* @Last Modified time: 2016-08-19 22:48:54
2016-08-10 18:03:06 +08:00
*
* There are still some coupling relationships here but,
* Just let it go :)
*/
2016-08-19 23:09:32 +08:00
if (!defined('BASE_DIR')) exit('Permission denied.');
2016-08-10 18:03:06 +08:00
$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
2016-08-14 08:31:07 +08:00
$db = Database::table($v2_table_name, true);
2016-08-10 18:03:06 +08:00
$steps = ceil($db->getRecordNum() / 250);
2016-08-19 23:09:32 +08:00
$public = isset($_POST['import_as_private']) ? '1' : '0';
2016-08-10 18:03:06 +08:00
// 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'],
2016-08-19 23:09:32 +08:00
'public' => $public,
2016-08-10 18:03:06 +08:00
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
2016-08-19 23:09:32 +08:00
$imported++;
2016-08-10 18:03:06 +08:00
// echo $row['hash_steve']." saved. <br />";
} else {
2016-08-19 23:09:32 +08:00
$duplicated++;
2016-08-10 18:03:06 +08:00
// 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,
2016-08-10 22:46:17 +08:00
'hash' => $row['hash_alex'],
2016-08-10 18:03:06 +08:00
'size' => 0,
'uploader' => $_POST['uploader_uid'],
2016-08-19 23:09:32 +08:00
'public' => $public,
2016-08-10 18:03:06 +08:00
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
2016-08-19 23:09:32 +08:00
$imported++;
2016-08-10 18:03:06 +08:00
// echo $row['hash_alex']." saved. <br />";
} else {
2016-08-19 23:09:32 +08:00
$duplicated++;
2016-08-10 18:03:06 +08:00
// 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,
2016-08-10 22:46:17 +08:00
'hash' => $row['hash_cape'],
2016-08-10 18:03:06 +08:00
'size' => 0,
'uploader' => $_POST['uploader_uid'],
2016-08-19 23:09:32 +08:00
'public' => $public,
2016-08-10 18:03:06 +08:00
'upload_at' => Utils::getTimeFormatted()
], $v3_table_name);
2016-08-19 23:09:32 +08:00
$imported++;
2016-08-10 18:03:06 +08:00
// echo $row['hash_cape']." saved. <br />";
} else {
2016-08-19 23:09:32 +08:00
$duplicated++;
2016-08-10 18:03:06 +08:00
// echo $row['hash_cape']." duplicated. <br />";
}
}
}
}
return [
'imported' => $imported,
'duplicated' => $duplicated
];