/** * Imports users and creates roles based on addition data in that row. * * @return void */ public function importInfinityRolesAndBoards() { # BORROW SEEDERS require base_path() . "/database/seeds/OptionSeeder.php"; require base_path() . "/database/seeds/PermissionSeeder.php"; require base_path() . "/database/seeds/RoleSeeder.php"; # DESTROY SEQUENCE if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) { $this->comment("\tDropping role sequence."); DB::statement("DROP SEQUENCE IF EXISTS roles_role_id_seq CASCADE;"); } $PermissionSeeder = new \PermissionSeeder(); $PermissionSeeder->setCommand($this); $PermissionSeeder->run(); $PermissionGroupSeeder = new \PermissionGroupSeeder(); $PermissionGroupSeeder->setCommand($this); $PermissionGroupSeeder->run(); $OptionSeeder = new \OptionSeeder(); $OptionSeeder->setCommand($this); $OptionSeeder->run(); $OptionGroupSeeder = new \OptionGroupSeeder(); $OptionGroupSeeder->setCommand($this); $OptionGroupSeeder->run(); $RoleSeeder = new \RoleSeeder(); $RoleSeeder->setCommand($this); $RoleSeeder->runMaster(); $RolePermissionSeeder = new \RolePermissionSeeder(); $RolePermissionSeeder->setCommand($this); $RolePermissionSeeder->run(); \Artisan::call('cache:clear'); # REPAIR SEQUENCE if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) { $this->comment("\tCreating role_id sequence again."); DB::statement("CREATE SEQUENCE roles_role_id_seq;"); $pgSeqNext = DB::table('roles')->select(DB::raw("(MAX(\"role_id\") + 1) AS next"))->pluck("next"); DB::statement("ALTER SEQUENCE roles_role_id_seq OWNED BY \"roles\".\"role_id\" RESTART WITH {$pgSeqNext};"); DB::statement("ALTER TABLE roles ALTER COLUMN role_id SET DEFAULT nextval('roles_role_id_seq');"); } # THEIR TABLES $tBoardsTable = $this->tcon->table("boards")->join('board_create', 'boards.uri', '=', 'board_create.uri')->select('boards.*', 'board_create.time'); $tModsTable = $this->tcon->table("mods"); # BEGIN USER IMPORT $this->info("\tImporting Users ..."); $userAdmin = null; $userBoardRelationships = []; $usersImported = 0; $tModsTable->chunk(100, function ($mods) use(&$userAdmin, &$userBoardRelationships, &$usersImported) { $this->line("\t\tHandling 100 users ..."); foreach ($mods as $mod) { # CREATE USER $user = new User(['username' => $mod->username, 'email' => property_exists($mod, "email") ? $mod->email ?: null : null, 'password' => null, 'password_legacy' => json_encode(['hasher' => "Vichan", 'hash' => $mod->password, 'salt' => $mod->salt])]); // 8chan has an issue with duplicates. try { $saved = $user->save(); } catch (\Exception $e) { $saved = false; } if ($saved) { ++$usersImported; # REMEMBER ROLES if ($mod->boards) { // TODO // Pull these values from config when formal importer created. switch ($mod->type) { // Janitor (Disabled) case 10: // Disabled // Disabled case 99: $this->comment("\t\tMod {$user->username} is disabled."); break; // Board volunteer // Board volunteer case 19: // Board owner // Board owner case 20: if (!isset($userBoardRelationships[$mod->boards])) { $userBoardRelationships[$mod->boards] = []; } $userBoardRelationships[$mod->boards][$user->user_id] = $mod->type; break; // Global volunteer // Global volunteer case 25: $this->comment("\t\tSetting {$user->username} to Global Mod."); $user->roles()->attach(Role::ID_MODERATOR); break; // Admin // Admin case 30: $this->comment("\t\tSetting {$user->username} to Global Admin."); $user->roles()->attach(Role::ID_ADMIN); $userAdmin = $userAdmin ?: $user; break; // Not identified! // Not identified! default: $this->error("\t\tMod {$user->username} has invalid mod type {$mod->type}."); break; } } } } }); $this->info("\tImported {$usersImported} users(s)."); if (!$userAdmin) { $this->comment("\tFailed to import an admin. This may cause problems."); } unset($mods, $mod, $user); # BEGIN BOARD IMPORT $this->info("\tImporting Boards ..."); $boardsImported = 0; $tBoardsTable->chunk(100, function ($boards) use(&$boardsImported, $userAdmin, $userBoardRelationships) { $this->line("\t\tHandling 100 boards ..."); foreach ($boards as $tBoard) { // We have an array like [board_uri => [ user_id => user_role ]] in the user import. // This fetches the first key of the biggest item. $boardOwner = []; if (isset($userBoardRelationships[$tBoard->uri])) { $boardOwner = array_keys($userBoardRelationships[$tBoard->uri], max($userBoardRelationships[$tBoard->uri])); } // Or it defaults to our admin. if (!isset($boardOwner[0])) { $this->comment("\t\t/{$tBoard->uri}/ has no known owner, assuming it is {$userAdmin->username}."); $boardOwner = $userAdmin->user_id; } else { $boardOwner = isset($boardOwner) ? $boardOwner[0] : $userAdmin->user_id; } $hBoard = new Board(['board_uri' => $tBoard->uri, 'title' => $tBoard->title, 'description' => $tBoard->subtitle, 'created_at' => new Carbon($tBoard->time), 'created_by' => $boardOwner, 'operated_by' => $boardOwner, 'posts_total' => $tBoard->posts_total, 'is_indexed' => !!$tBoard->indexed, 'is_overboard' => !!$tBoard->indexed, 'is_worksafe' => !!$tBoard->sfw]); if ($hBoard->save()) { ++$boardsImported; if (!$tBoard->public_bans || !$tBoard->public_logs || $tBoard->public_logs == 2) { $role = Role::getAnonymousRoleForBoard($hBoard); $perms = []; if (!$tBoard->public_bans) { $perms[] = ['permission_id' => "board.bans", 'value' => false]; } if (!$tBoard->public_logs || $tBoard->public_logs == 2) { $perms[] = ['permission_id' => "board.logs", 'value' => false]; } $role->permissionAssignments()->createMany($perms); } } else { $this->error("Failed to save /{$hBoard->board_uri}/."); } } }); $this->info("\tImported {$boardsImported} board(s)."); unset($boards, $tBoard, $hBoard); # BEGIN ROLE CONFIGURATION $this->info("\tCreating roles ..."); $roleBoardVols = 0; $roleBoardOwners = 0; $roleBoardSkips = 0; // Okay, so apparently sometimes there are users that own boards that don't exist. // We cannot crate roles for these boards, so lets prune useless data. $boardsWeCareAbout = Board::select('board_uri')->whereIn('board_uri', array_keys($userBoardRelationships))->get()->pluck('board_uri'); if ($boardsWeCareAbout->count() != count($userBoardRelationships)) { $this->comment("\t\tThere are " . (count($userBoardRelationships) - $boardsWeCareAbout->count()) . " board(s) which users own that do not exist!"); } foreach ($boardsWeCareAbout as $board) { $ownerRole = null; $janitorRole = null; $roles = $userBoardRelationships[$board]; foreach ($roles as $mod => $role) { // TODO // Pull these values from config when formal importer created. switch ($role) { // Board volunteer case 19: $janitorRole = $janitorRole ?: Role::getJanitorRoleForBoard($board); $userRole = new UserRole(['user_id' => $mod, 'role_id' => $janitorRole->role_id]); if ($userRole->save()) { ++$roleBoardVols; } break; // Board owner // Board owner case 20: $ownerRole = $ownerRole ?: Role::getOwnerRoleForBoard($board); $userRole = new UserRole(['user_id' => $mod, 'role_id' => $ownerRole->role_id]); if ($userRole->save()) { ++$roleBoardOwners; } break; default: $this->line("\t\tI don't know what to do with role {$role} for user id {$mod} in {$board}."); break; } } } $this->info("\t\tCreated {$roleBoardOwners} owner(s) and {$roleBoardVols} janitor(s) "); $this->info("\t\tPulled roles from " . count($userBoardRelationships) . " boards with relationships."); unset($board, $roles, $mod, $role, $userRole, $janitorRole, $ownerRole); }