From 2622ef626de8d4802ffae4228457f2393afedb1d Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Sun, 28 Jul 2024 18:09:59 +0300 Subject: [PATCH] pkp/pkp-lib#10249 Added migration to recover from data loss on the profile image --- .../I10249_FixProfileImageDataLoss.inc.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.php diff --git a/classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.php b/classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.php new file mode 100644 index 00000000000..b062a9281ba --- /dev/null +++ b/classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.php @@ -0,0 +1,70 @@ +where('setting_name', '=', 'profileImage') + ->whereRaw("COALESCE(setting_value, '') <> ''") + ->whereRaw("setting_value NOT LIKE CONCAT('%profileImage-', user_id, '.%')") + ->select('user_id') + ->chunkById(1000, function (Collection $rows) use ($publicFilesPath, $orderByModifiedDate) { + foreach ($rows as $row) { + $globPattern = "{$publicFilesPath}/profileImage-{$row->user_id}.*"; + $candidates = glob($globPattern, GLOB_NOSORT); + if (empty($candidates)) { + error_log("Failed to locate a profile image for the user ID {$row->user_id} at $globPattern"); + continue; + } + + if (count($candidates) > 1) { + usort($candidates, $orderByModifiedDate); + } + + $filePath = array_pop($candidates); + $fileName = basename($filePath); + [$width, $height] = getimagesize($filePath) ?: [0, 0]; + Capsule::table('user_settings') + ->where('user_id', $row->user_id) + ->where('setting_name', 'profileImage') + ->update(['setting_value' => json_encode([ + 'name' => $fileName, + 'uploadName' => $fileName, + 'width' => $width, + 'height' => $height, + 'dateUploaded' => date('Y-m-d H:i:s', filemtime($filePath)) + ])]); + } + }, 'user_id'); + } + + /** + * Reverse the migration + * @return void + */ + public function down() { + throw new PKP\install\DowngradeNotSupportedException(); + } +}