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

109 lines
3.4 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-10 22:46:17 +08:00
* @Last Modified time: 2016-08-10 22:41:24
2016-08-10 18:03:06 +08:00
*
* 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
2016-08-10 22:46:17 +08:00
$db = new Database($v2_table_name, null, true);
2016-08-10 18:03:06 +08:00
$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,
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'],
'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,
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'],
'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
];