forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp#10249 Added migration to recover from data loss on the profile image
(cherry picked from commit 2622ef6)
- Loading branch information
1 parent
c9a2037
commit cfdede5
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
classes/migration/upgrade/v3_4_0/I10249_FixProfileImageDataLoss.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* @file classes/migration/upgrade/v3_4_0/I10249_FixProfileImageDataLoss.php | ||
* | ||
* Copyright (c) 2024 Simon Fraser University | ||
* Copyright (c) 2024 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class I10249_FixProfileImageDataLoss | ||
* | ||
* @brief Fix data loss at the user profile image | ||
* | ||
* @see https://github.com/pkp/pkp-lib/issues/10249 | ||
*/ | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\DB; | ||
use PKP\install\DowngradeNotSupportedException; | ||
|
||
class I10249_FixProfileImageDataLoss extends \PKP\migration\Migration | ||
{ | ||
/** | ||
* Runs the migration | ||
*/ | ||
public function up(): void | ||
{ | ||
$orderByModifiedDate = fn(string $a, string $b) => filemtime($a) - filemtime($b); | ||
$publicFilesPath = Config::getVar('files', 'public_files_dir') . '/site'; | ||
DB::table('user_settings') | ||
->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)) { | ||
$this->_installer->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]; | ||
DB::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'); | ||
} | ||
|
||
/** | ||
* Reverses the migration | ||
*/ | ||
public function down(): void | ||
{ | ||
throw new DowngradeNotSupportedException(); | ||
} | ||
} |