A first run migration tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
firstrunmigrate/lib/Migration/QuotaJob.php

98 lines
3.1 KiB

<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\FirstRunMigrate\Migration;
use OCA\FirstRunMigrate\Migration\MigrationJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCA\FirstRunMigrate\Migration\Utils;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;
class QuotaJob extends MigrationJob {
protected LoggerInterface $logger;
protected IUserManager $userManager;
protected IJobList $jobList;
public static string $type = 'quota';
public static $next = DataJob::class;
/**
* BackgroundJob constructor.
*
* @param INotificationManager $notificationManager
*/
public function __construct(ITimeFactory $timeFactory, IUserManager $userManager, LoggerInterface $logger, IJobList $jobList) {
parent::__construct($timeFactory);
$this->logger = $logger;
$this->userManager = $userManager;
$this->jobList = $jobList;
}
/**
* @param array $argument
*/
protected function run($argument) {
$this->logger->debug("Starting quota migration job {$this->getId()} with args " . json_encode($argument));
$user = $this->userManager->get($argument['uid']);
$this->logger->debug("{$this->getId()}: Migrating quota");
self::setMigrationStatus('started', $user);
if ($quota = $this->getUserMigration($user)) {
$user->setQuota($quota);
} else {
$this->logger->info("{$this->getId()}: No quota to migrate");
}
self::setMigrationStatus('finished', $user);
self::schredule_next($user, $this->logger, $this->jobList);
}
private static function getMigrationFile() : ?string {
/** @var IConfig */
$config = \OC::$server->get(IConfig::class);
return $config->getSystemValue('firstrunmigrate_quotas', null);
}
public static function isMigration() : bool {
return ($file = self::getMigrationFile()) && file_exists($file);
}
private function getUserMigration(IUser $user) : ?string {
$quotas = json_decode(file_get_contents(self::getMigrationFile()), true);
$id = Utils::getUserId($user);
if (array_key_exists($id, $quotas)) {
return $quotas[$id];
} else {
return null;
}
}
}