( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
@mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "Pw4TheBrand!";
$dbname = "thebrand";
header('Content-Type: application/json');
$logDir = __DIR__ . '/v/uploads/converted';
if (!is_dir($logDir)) { @mkdir($logDir, 0777, true); }
$errorLogPath = $logDir . '/transfer_correction_errors.log';
@ini_set('log_errors','1');
@ini_set('error_log',$errorLogPath);
$phpErrFp = @fopen($errorLogPath, 'a');
function logErr($fp, $type, $data = []) { if ($fp) { $row = array_merge(["ts" => date('c'), "type" => $type], $data); @fwrite($fp, json_encode($row) . PHP_EOL); } }
set_error_handler(function($severity, $message, $file, $line) use ($phpErrFp) { logErr($phpErrFp, 'php_error', ["severity" => $severity, "message" => $message, "file" => $file, "line" => $line]); });
set_exception_handler(function($ex) use ($phpErrFp) { logErr($phpErrFp, 'php_exception', ["message" => $ex->getMessage(), "file" => $ex->getFile(), "line" => $ex->getLine(), "code" => $ex->getCode()]); });
register_shutdown_function(function() use ($phpErrFp) { $e = error_get_last(); if ($e) { logErr($phpErrFp, 'php_fatal', ["type" => $e['type'], "message" => $e['message'], "file" => $e['file'], "line" => $e['line']]); } if ($phpErrFp) { @fclose($phpErrFp); } });
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
http_response_code(500);
echo json_encode(["success" => false, "message" => "db_connect_error"]);
exit;
}
$conn->set_charset("utf8");
$catalogid = 152;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 100;
if ($limit < 1) { $limit = 100; }
$stmt = $conn->prepare("SELECT id, title FROM profilepicture WHERE catalogid = ? AND converted = 1 AND public = 1 AND is_deleted = 0 AND isdisplay = 13 ORDER BY id DESC LIMIT ?");
$stmt->bind_param("ii", $catalogid, $limit);
// continuous pagination until exhausted
$maxBatches = isset($_GET['max_batches']) ? (int)$_GET['max_batches'] : 1000;
if ($maxBatches < 1) { $maxBatches = 1000; }
$stmtUpdate = $conn->prepare("UPDATE profilepicture SET converted = 2 WHERE id = ?");
$dir = __DIR__ . '/v/uploads/converted';
if (!is_dir($dir)) { @mkdir($dir, 0777, true); }
$stream = isset($_GET['stream']) ? (int)$_GET['stream'] : 0;
if ($stream === 1) { header('Content-Type: text/plain'); @ini_set('output_buffering','0'); @ini_set('zlib.output_compression','0'); @ini_set('implicit_flush','1'); ob_implicit_flush(true); }
$progressPath = $dir . '/transfer_correction_progress.log';
$progressFp = @fopen($progressPath, 'a');
function logp($fp, $event, $payload = []) { if ($fp) { $row = array_merge(["ts" => date('c'), "event" => $event], $payload); @fwrite($fp, json_encode($row) . PHP_EOL); } }
function out($stream, $msg) { if ($stream === 1) { echo $msg . "\n"; if (function_exists('flush')) { @flush(); } if (function_exists('ob_flush')) { @ob_flush(); } } }
$processed = [];
$errors = [];
$skipped = [];
$batchesRun = 0;
while (true) {
if ($batchesRun >= $maxBatches) { break; }
logp($progressFp, 'batch_begin', ["batch" => $batchesRun + 1]);
out($stream, 'batch_begin ' . ($batchesRun + 1));
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) { break; }
$batchesRun++;
$updatesInBatch = 0;
while ($row = $result->fetch_assoc()) {
$themeid = (int)$row['id'];
$titleStr = (string)$row['title'];
logp($progressFp, 'row_begin', ["themeid" => $themeid, "title" => $titleStr]);
out($stream, 'row_begin ' . $themeid);
// Old filename logic: based on title
$oldBasename = sha1($themeid . ':' . $titleStr);
$oldPath = $dir . '/' . $oldBasename . '.json';
// New filename logic: based on catalogid
$newBasename = sha1($themeid . ':' . $catalogid);
$newPath = $dir . '/' . $newBasename . '.json';
if (file_exists($oldPath)) {
// Rename file
$renamed = rename($oldPath, $newPath);
if ($renamed) {
// Update DB
$stmtUpdate->bind_param("i", $themeid);
$stmtUpdate->execute();
$updatesInBatch++;
$processed[] = ["themeid" => $themeid, "old_file" => $oldBasename . '.json', "new_file" => $newBasename . '.json'];
logp($progressFp, 'row_converted', ["themeid" => $themeid, "new_file" => $newBasename . '.json']);
out($stream, 'row_converted ' . $themeid);
} else {
$errors[] = ["themeid" => $themeid, "error" => "rename_failed"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "rename_failed"]);
out($stream, 'error rename_failed ' . $themeid);
}
} else {
// File not found
$skipped[] = ["themeid" => $themeid, "reason" => "file_not_found"];
logp($progressFp, 'skipped', ["themeid" => $themeid, "reason" => "file_not_found"]);
out($stream, 'skipped file_not_found ' . $themeid);
}
}
// Break infinite loop if no progress was made on this batch
if ($updatesInBatch === 0) {
logp($progressFp, 'batch_aborted', ["reason" => "no_updates_in_batch"]);
out($stream, 'batch_aborted no_updates_in_batch');
break;
}
}
$stmt->close();
$stmtUpdate->close();
$conn->close();
if ($progressFp) { @fclose($progressFp); }
echo json_encode([
"success" => true,
"count_processed" => count($processed),
"processed" => $processed,
"count_skipped" => count($skipped),
"skipped" => $skipped,
"count_errors" => count($errors),
"errors" => $errors,
"batches_run" => $batchesRun
]);
?>