nsec = $bag->get('nsec'); parent::__construct(); } protected function configure(): void { $this ->addArgument('folder', InputArgument::REQUIRED, 'The folder location to start scanning from.'); } protected function execute(InputInterface $input, OutputInterface $output): int { $folder = $input->getArgument('folder'); // Use Symfony Finder to locate YAML files recursively $finder = new Finder(); $finder->files() ->in($folder) ->name('*.yaml') ->name('*.yml'); if (!$finder->hasResults()) { $output->writeln('No YAML files found in the specified directory.'); return Command::SUCCESS; } $articleSlugsList = []; foreach ($finder as $file) { $filePath = $file->getRealPath(); $output->writeln("Processing file: $filePath"); $yamlContent = Yaml::parseFile($filePath); // This parses the YAML file try { // Deserialize YAML content into an Event object $event = new Event(); $event->setKind(30040); $tags = $yamlContent['tags']; $event->setTags($tags); $items = array_filter($tags, function ($tag) { return ($tag[0] === 'a'); }); foreach ($items as $one) { $parts = explode(':', $one[1]); $articleSlugsList[] = end($parts); } $signer = new Sign(); $signer->signEvent($event, $this->nsec); // Save to cache $slug = array_filter($tags, function ($tag) { return ($tag[0] === 'd'); }); // Generate a Redis key $cacheKey = 'magazine-' . $slug[0][1]; $cacheItem = $this->redisCache->getItem($cacheKey); $cacheItem->set($event); $this->redisCache->save($cacheItem); $output->writeln("Saved index."); } catch (\Exception $e) { $output->writeln("Error deserializing YAML in file: $filePath. Message: {$e->getMessage()}"); continue; } } // crawl relays for all the articles and save to db $fresh = $this->client->getArticles($articleSlugsList); foreach ($fresh as $item) { $article = $this->factory->createFromLongFormContentEvent($item); $this->entityManager->persist($article); } $this->entityManager->flush(); // look up all articles in the db and push to index whatever you find $articles = $this->entityManager->getRepository(Article::class)->createQueryBuilder('a') ->where('a.slug IN (:slugs)') ->setParameter('slugs', $articleSlugsList) ->getQuery() ->getResult(); // mark all of those for indexing foreach ($articles as $article) { if ($article->getIndexStatus() === IndexStatusEnum::NOT_INDEXED) { $article->setIndexStatus(IndexStatusEnum::TO_BE_INDEXED); $this->entityManager->persist($article); } } $this->entityManager->flush(); // to elastic if (count($articles) > 0 ) { $this->itemPersister->insertMany($articles); // Insert or skip existing $output->writeln('Added to index.'); } $output->writeln('Conversion complete.'); return Command::SUCCESS; } }