-
Patrick Barroca authored8c029c06
user_data_migration.php 5.84 KiB
<?php
require './console.php';
echo 'User Data Migration by Stl And Pat since 1854
\ . ./
\ .:";\'.:.." /
(M^^.^~~:.\'").
- (/ . . . \ \) -
O ((| :. ~ ^ :. .|))
|\\ - (\- | \ / | /) -
| T -\ \ / /-
/ \[_]..........................\ \ / /
This will detect user with login starting with CHAM
try to rename them without CHAM or give their datas
to another user if a double exists.
';
readline('press enter to continue...');
$valid_count = Class_Users::countBy(['statut' => 0, 'role_level' => 2]);
$invalid_count = Class_Users::countBy(['statut' => 1, 'role_level' => 2]);
$to_migrate = Class_Users::countBy(['where' => 'login like \'CHAM%\' AND SUBSTR(login,5) < 51001']);
echo $valid_count . ' valid users in database
' . $invalid_count . ' users marked for deletion
' . $to_migrate . ' users starting with CHAM[id < 51001]
';
if (0 == $valid_count) {
echo 'Oups, not enough valid users, aborting...
..-^~~~^-..
.~ ~.
(;: :;)
(: :)
\':._ _.:\'
| |
(=====)
| |
\O/ | |
\ | |
/\ ((/ \))
';
exit(255);
}
class UserDataMigrationLog extends Class_Entity {
public function __construct($path) {
$this->setPath($path)
->reset();
}
public function write($content) {
file_put_contents($this->getPath(), implode('|', $content) . "\n", FILE_APPEND);
return $this;
}
public function reset() {
if (file_exists($this->getPath()))
unlink($this->getPath());
return $this;
}
}
$for_real = ('--force' == $argv[1]);
if ($for_real)
readline('CAUTION : you asked to run for real, users may be deleted !');
echo $for_real
? 'OH MY, RUNNING FOR REAL, GOOD LUCK'
: 'pro tip: use --force option to run for real, with great powers comes great responsibility
Simulation mode engaged...';
echo '
';
$rename = 0;
$without_datas = 0;
$without_target = 0;
$success = 0;
$rename_log = (new UserDataMigrationLog('renamed.txt'))
->write(['id_bokeh', 'old_carte', 'new_carte', 'nom', 'prenom', 'naissance']);
$no_datas_log = (new UserDataMigrationLog('deleted_without_datas.txt'))
->write(['id_bokeh', 'carte', 'nom', 'prenom', 'naissance']);
$no_target_log = (new UserDataMigrationLog('not_deleted_target_problem.txt'))
->write(['id_bokeh', 'carte', 'nom', 'prenom', 'naissance', 'cause']);
$success_log = (new UserDataMigrationLog('deleted_with_datas.txt'))
->write(['id_bokeh_source', 'carte_source', 'nom_source', 'prenom_source', 'naissance_source',
'id_bokeh_dest', 'carte_dest', 'nom_dest', 'prenom_dest', 'naissance_dest',]);
echo 'Started at ' . date('c') . "\n";
/**
* migrate users from CHAMXXXXX login to XXXXX without CHAM, dedup if needed
*/
foreach(Class_Users::findAllBy(['where' => 'login like "CHAM%" and role_level=2']) as $user) {
// My login starts with cham (lowercase), I should not migrate
if ('CHAM' !== substr($user->getLogin(), 0, 4))
continue;
// My login is not strictly CHAM[0-9]+, I should not migrate
$id = substr($user->getLogin(), 4);
if (!is_numeric($id)) {
echo 'S';
continue;
}
// My login contains id above 51000, I should not migrate
if (51000 <= (int)$id) {
echo 'S';
continue;
}
// no user with my future id, simply rename me
if (!$double = Class_Users::findFirstBy(['login' => $id])) {
$rename_log->write([$user->getId(),
$user->getLogin(),
$id,
$user->getNom(),
$user->getPrenom(),
$user->getNaissance()]);
$user
->setLogin($id)
->setIdabon($id);
if ($for_real)
$user->save();
echo '.';
$rename++;
continue;
}
// another user has my target but I have no datas, simply delete me
$datas = new Class_User_Datas($user);
if (!$datas->hasDatas()) {
$no_datas_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance()]);
if ($for_real)
$user->delete();
echo '.';
$without_datas++;
continue;
}
if (!$target = $datas->giveTo(['login' => $id, 'role_level' => 2],
$for_real)) {
$no_target_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance(),
$datas->getError()]);
$without_target++;
echo 'F';
continue;
}
$success_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance(),
$target->getId(),
$target->getLogin(),
$target->getNom(),
$target->getPrenom(),
$target->getNaissance()]);
if ($for_real)
$user->delete();
$success++;
echo '.';
}
echo "\nEnded at " . date('c') . "\n";
printf('
%s renamed, log in renamed.txt
%s without datas, log in deleted_without_datas.txt
%s without target, log in not_deleted_target_problem.txt
%s given to another user, log in deleted_with_datas.txt
', $rename, $without_datas, $without_target, $success);