<?php require_once __DIR__ . '/../src/core.php'; $limit = 500; $userProcessor = new UserProcessor(); $oldNames = []; $newNames = []; $query = 'SELECT name FROM userfriend' . ' GROUP BY name' . ' ORDER BY RANDOM()' . ' LIMIT ?'; $newNames = array_map(function ($x) { return $x['name']; }, R::getAll($query, [$limit])); $query = 'SELECT name FROM user ORDER BY name LIMIT ?'; $oldNames = array_map(function ($x) { return $x['name']; }, R::getAll($query, [$limit])); $newNames = array_diff($newNames, $oldNames); $newNames = array_slice($newNames, 0, $limit); $pad = strlen(count($newNames)); $done = 0; $exitCode = 0; foreach ($newNames as $name) { try { ++$done; printf("(%0{$pad}d/%d) Processing user %s" . PHP_EOL, $done, count($newNames), $name); $userProcessor->process($name); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; $exitCode = 1; } } exit($exitCode);
public static function work(&$controllerContext, &$viewContext) { try { if (empty($_POST['sender'])) { throw new Exception('No sender specified'); } $sender = $_POST['sender']; if (empty($_POST['action'])) { throw new Exception('No action specified'); } $action = $_POST['action']; if (empty($_POST['model-ids'])) { throw new Exception('No model ids specified'); } $modelIds = array_map('trim', preg_split('/[,;]/', $_POST['model-ids'])); $chosenMedia = []; $chosenUsers = []; switch ($sender) { case 'media': $chosenMedia = self::getChosenMedia($modelIds); break; case 'user': $chosenUsers = self::getChosenUsers($modelIds); break; default: throw new Exception('Unknown sender: ' . $sender); } if ($action == 'refresh') { $num = 0; $startTime = microtime(true); $mediaProcessors = [Media::Anime => new AnimeProcessor(), Media::Manga => new MangaProcessor()]; $userProcessor = new UserProcessor(); foreach ($chosenMedia as $media => $ids) { foreach ($ids as $id) { $mediaProcessors[$media]->process($id); ++$num; } } foreach ($chosenUsers as $user) { $userProcessor->process($user); ++$num; } $viewContext->messageType = 'info'; $viewContext->message = sprintf('Successfully processed %d entities in %.02fs', $num, microtime(true) - $startTime); } elseif ($action == 'wipe-cache') { $deleted = 0; foreach ($chosenUsers as $userName) { $cache = new Cache(); $cache->setPrefix($userName); foreach ($cache->getAllFiles() as $path) { $deleted++; unlink($path); } } $viewContext->messageType = 'info'; $viewContext->message = 'Deleted ' . $deleted . ' files'; } elseif ($action == 'unban' or $action == 'soft-ban' or $action == 'hard-ban') { switch ($action) { case 'unban': $banState = BanHelper::USER_BAN_NONE; break; case 'soft-ban': $banState = BanHelper::USER_BAN_QUEUE_ONLY; break; case 'hard-ban': $banState = BanHelper::USER_BAN_TOTAL; break; default: throw new Exception('Wrong ban state'); } $changed = 0; foreach ($chosenUsers as $userName) { BanHelper::setUserBanState($userName, $banState); ++$changed; } $viewContext->messageType = 'info'; $viewContext->message = sprintf('Successfully updated %d users', $changed); } elseif ($action == 'reset-franchise') { $num = 0; foreach ($chosenMedia as $media => $ids) { $query = 'UPDATE media SET franchise = NULL WHERE media = ? AND mal_id IN (' . R::genSlots($ids) . ')'; R::exec($query, array_merge([$media], $ids)); $num += count($ids); } $viewContext->messageType = 'info'; $viewContext->message = sprintf('Successfully reset franchise for %d entities. Don\'t forget to refresh them now!', $num); } elseif ($action == 'remove') { $num = 0; foreach ($chosenMedia as $media => $ids) { $query = 'DELETE FROM media WHERE media = ? AND mal_id IN (' . R::genSlots($ids) . ')'; R::exec($query, array_merge([$media], $ids)); $num += count($ids); } $viewContext->messageType = 'info'; $viewContext->message = sprintf('Successfully removed %d entities.', $num); } else { throw new Exception('Unknown action: ' . $action); } } catch (Exception $e) { $viewContext->messageType = 'error'; $viewContext->message = $e->getMessage(); } $viewContext->viewName = 'admin-index'; $viewContext->meta->title = 'Admin — ' . Config::$title; WebMediaHelper::addCustom($viewContext); }
$userProcessor = new UserProcessor(); $mediaProcessors = [Media::Anime => new AnimeProcessor(), Media::Manga => new MangaProcessor()]; $userQueue = new Queue(Config::$userQueuePath); $userQueueSize = (new Queue(Config::$userQueuePath))->size(); $mediaQueue = new Queue(Config::$mediaQueuePath); if ($userQueueSize > 30) { Config::$usersPerCronRun = Config::$usersPerCronRunMore; Config::$mediaPerCronRun = Config::$mediaPerCronRunMore; } Downloader::setLogger($logger); #process users processQueue($userQueue, Config::$usersPerCronRun, Config::$userQueueMaxAttempts, $logger, function ($userName) use($userProcessor, $mediaQueue, $logger) { Database::selectUser($userName); $logger->log('Processing user %s... ', $userName); #process the user $userContext = $userProcessor->process($userName); #remove associated cache $cache = new Cache(); $cache->setPrefix($userName); foreach ($cache->getAllFiles() as $path) { unlink($path); } #append media to queue $mediaIds = []; foreach (Media::getConstList() as $media) { foreach ($userContext->user->getMixedUserMedia($media) as $entry) { $mediaAge = time() - strtotime($entry->processed); if ($mediaAge > Config::$mediaQueueMinWait) { $mediaIds[] = TextHelper::serializeMediaId($entry); } }