Skip to content

Commit

Permalink
Merge pull request #10256 from jonasraoni/bugfix-stable-3_3_0-10249-f…
Browse files Browse the repository at this point in the history
…ix-data-loss-on-settings

#10249 Added migration to recover from data loss on the pr…
  • Loading branch information
jonasraoni committed Jul 31, 2024
2 parents 4f2216e + 17b51e3 commit 121d5f9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @file classes/migration/upgrade/I10249_FixProfileImageDataLoss.inc.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
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Support\Collection;

class I10249_FixProfileImageDataLoss extends Migration {
/**
* Run the migrations.
* @return void
*/
public function up() {
$orderByModifiedDate = function (string $a, string $b) {
return filemtime($a) - filemtime($b);
};
$publicFilesPath = Config::getVar('files', 'public_files_dir') . '/site';
Capsule::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)) {
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();
}
}
2 changes: 1 addition & 1 deletion classes/scheduledTask/ScheduledTaskDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getLastRunTime($className) {
[$className]
);
$row = $result->current();
return $row ? strtotime($this->datetimeFromDB($row->last_run)) : null;
return $row && $row->last_run ? strtotime($this->datetimeFromDB($row->last_run)) : 0;
}

/**
Expand Down

0 comments on commit 121d5f9

Please sign in to comment.