protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $um = $this->getContainer()->get('fos_user.user_manager');
     $locale = $this->getContainer()->getParameter('locale');
     $tokenGenrator = $this->getContainer()->get('fos_user.util.token_generator');
     $ui = new UserImporter($this->connection, $this->em, $this->logger, $output, $um, $tokenGenrator, $locale);
     $ii = new IssueImporter($this->connection, $this->em, $this->logger, $output);
     $ai = new ArticleImporter($this->connection, $this->em, $this->logger, $output, $ui);
     $oldIssueId = $input->getArgument('id');
     $journalId = $input->getArgument('journal');
     if (is_numeric($journalId)) {
         $journal = $this->em->find('OjsJournalBundle:Journal', $journalId);
     } else {
         $journal = $this->em->getRepository(Journal::class)->findOneBy(['slug' => $journalId]);
     }
     if (!$journal) {
         $output->writeln('<error>Journal does not exist.</error>');
         return;
     }
     $sectionRepo = $this->em->getRepository(Section::class);
     $currentSectionIds = array_column($sectionRepo->getIdsByJournal($journal), 'id');
     $ii->importIssues([['issue_id' => $oldIssueId]], $journal->getId(), $currentSectionIds);
     $mapRepo = $this->em->getRepository(ImportMap::class);
     if (!$input->getOption('without-articles')) {
         $sql = "SELECT article_id FROM published_articles WHERE issue_id = ?";
         $articleIds = $this->connection->executeQuery($sql, [$oldIssueId])->fetchAll();
         $oldSectionIds = $mapRepo->getSectionIds($journal);
         if (empty($oldSectionIds) && !empty($currentSectionIds)) {
             $oldSectionIds = $currentSectionIds[0];
         }
         $ai->importArticles($articleIds, $journal->getId(), $oldSectionIds, $mapRepo->getIssueIds($journal));
     }
 }
 /**
  * @param OutputInterface $output
  * @return array
  * @throws \Doctrine\DBAL\DBALException
  * @throws \Doctrine\ORM\ORMException
  */
 protected function syncIssues(OutputInterface $output)
 {
     $ii = new IssueImporter($this->connection, $this->em, $this->logger, $output, $this->ui);
     $importRepo = $this->em->getRepository(ImportMap::class);
     $journalMaps = $importRepo->findBy(['type' => Journal::class]);
     $createdIssueIds = [];
     /** @var ImportMap $journalMap */
     foreach ($journalMaps as $journalMap) {
         $output->writeln('Synchronizing #' . $journalMap->getNewId());
         $journalRef = $this->em->getReference(Journal::class, $journalMap->getNewId());
         $importedIssueIds = null;
         $importedIssues = array_values($this->em->createQueryBuilder()->select('map.oldId')->from(ImportMap::class, 'map')->join(Issue::class, 'issue', 'WITH', 'map.newId = issue.id')->where('map.type = :type')->andWhere('issue.journal = :journal')->setParameter('type', Issue::class)->setParameter('journal', $journalRef)->getQuery()->getScalarResult());
         foreach ($importedIssues as $key => $importedIssue) {
             $importedIssueIds[] = $importedIssue['oldId'];
         }
         $issuesSql = 'SELECT issue_id FROM issues WHERE issue_id NOT IN (?) AND journal_id = ?';
         $issuesStatement = $this->connection->executeQuery($issuesSql, [$importedIssueIds, [$journalMap->getOldId()]], [Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY]);
         $nonImportedIssues = $issuesStatement->fetchAll();
         $importedSectionIds = $this->getSectionIds($journalRef);
         $createdIssueIds = $ii->importIssues($nonImportedIssues, $journalMap->getNewId(), $importedSectionIds);
     }
     return $createdIssueIds;
 }
 /**
  * Imports the journal with given ID
  * @param  int $id Journal's ID
  * @return array New IDs as keys, old IDs as values
  * @throws Exception
  * @throws \Doctrine\DBAL\DBALException
  */
 public function importJournal($id)
 {
     $this->consoleOutput->writeln("Importing the journal...");
     $journalSql = "SELECT path, primary_locale FROM journals WHERE journal_id = :id LIMIT 1";
     $journalStatement = $this->dbalConnection->prepare($journalSql);
     $journalStatement->bindValue('id', $id);
     $journalStatement->execute();
     $settingsSql = "SELECT locale, setting_name, setting_value FROM journal_settings WHERE journal_id = :id";
     $settingsStatement = $this->dbalConnection->prepare($settingsSql);
     $settingsStatement->bindValue('id', $id);
     $settingsStatement->execute();
     $pkpJournal = $journalStatement->fetch();
     $pkpSettings = $settingsStatement->fetchAll();
     $primaryLocale = $pkpJournal['primary_locale'];
     $languageCode = mb_substr($primaryLocale, 0, 2, 'UTF-8');
     !$pkpJournal && die('Journal not found.' . PHP_EOL);
     $this->consoleOutput->writeln("Reading journal settings...");
     foreach ($pkpSettings as $setting) {
         $locale = !empty($setting['locale']) ? mb_substr($setting['locale'], 0, 2, 'UTF-8') : $languageCode;
         $name = $setting['setting_name'];
         $value = $setting['setting_value'];
         $this->settings[$locale][$name] = $value;
     }
     $this->journal = new Journal();
     $this->journal->setStatus(1);
     $this->journal->setSlug($pkpJournal['path']);
     // Fill translatable fields in all available languages except the primary one
     foreach ($this->settings as $fieldLocale => $fields) {
         if ($fieldLocale === $languageCode) {
             // We will fill fields of the primary language later.
             continue;
         }
         $this->journal->setCurrentLocale(mb_substr($fieldLocale, 0, 2, 'UTF-8'));
         !empty($fields['title']) ? $this->journal->setTitle($fields['title']) : $this->journal->setTitle('Unknown Journal');
         !empty($fields['description']) ? $this->journal->setDescription($fields['description']) : $this->journal->setDescription('-');
     }
     $this->journal->setCurrentLocale($languageCode);
     // Fill fields for the primary language
     !empty($this->settings[$languageCode]['title']) ? $this->journal->setTitle($this->settings[$languageCode]['title']) : $this->journal->setTitle('Unknown Journal');
     !empty($this->settings[$languageCode]['description']) ? $this->journal->setDescription($this->settings[$languageCode]['description']) : $this->journal->setDescription('-');
     !empty($this->settings[$languageCode]['journalPageFooter']) ? $this->journal->setFooterText($this->settings[$languageCode]['journalPageFooter']) : $this->journal->setFooterText(null);
     !empty($this->settings[$languageCode]['printIssn']) && count($this->settings[$languageCode]['printIssn']) == 9 ? $this->journal->setIssn($this->settings[$languageCode]['printIssn']) : $this->journal->setIssn('');
     !empty($this->settings[$languageCode]['onlineIssn']) && count($this->settings[$languageCode]['onlineIssn']) == 9 ? $this->journal->setEissn($this->settings[$languageCode]['onlineIssn']) : $this->journal->setEissn('');
     $date = sprintf('%d-01-01 00:00:00', !empty($this->settings[$languageCode]['initialYear']) ? $this->settings[$languageCode]['initialYear'] : '2015');
     $this->journal->setFounded(DateTime::createFromFormat('Y-m-d H:i:s', $date));
     // Set view and download counts
     !empty($this->settings[$languageCode]['total_views']) ? $this->journal->setViewCount($this->settings[$languageCode]['total_views']) : $this->journal->setViewCount(0);
     !empty($this->settings[$languageCode]['total_downloads']) ? $this->journal->setDownloadCount($this->settings[$languageCode]['total_downloads']) : $this->journal->setDownloadCount(0);
     !empty($this->settings[$languageCode]['homeHeaderTitleImage']) ? $header = unserialize($this->settings[$languageCode]['homeHeaderTitleImage']) : ($header = null);
     if ($header) {
         $baseDir = '/../web/uploads/journal/imported/';
         $croppedBaseDir = '/../web/uploads/journal/croped/imported/';
         $headerPath = $id . '/' . $header['uploadName'];
         $pendingDownload = new PendingDownload();
         $pendingDownload->setSource('public/journals/' . $headerPath)->setTarget($baseDir . $headerPath)->setTag('journal-header');
         $croppedPendingDownload = new PendingDownload();
         $croppedPendingDownload->setSource('public/journals/' . $headerPath)->setTarget($croppedBaseDir . $headerPath)->setTag('journal-header');
         $history = $this->em->getRepository(FileHistory::class)->findOneBy(['fileName' => 'imported/' . $headerPath]);
         if (!$history) {
             $history = new FileHistory();
             $history->setFileName('imported/' . $headerPath);
             $history->setOriginalName('imported/' . $headerPath);
             $history->setType('journal');
         }
         $this->em->persist($croppedPendingDownload);
         $this->em->persist($pendingDownload);
         $this->em->persist($history);
         $this->journal->setHeader('imported/' . $headerPath);
     }
     $subjects = $this->importSubjects($languageCode);
     foreach ($subjects as $subject) {
         $this->journal->addSubject($subject);
     }
     // Set publisher
     !empty($this->settings[$languageCode]['publisherInstitution']) ? $this->importAndSetPublisher($this->settings[$languageCode]['publisherInstitution'], $languageCode) : $this->journal->setPublisher($this->getUnknownPublisher($languageCode));
     // Use existing languages or create if needed
     $language = $this->em->getRepository('OjsJournalBundle:Lang')->findOneBy(['code' => $languageCode]);
     $this->journal->setMandatoryLang($language ? $language : $this->createLanguage($languageCode));
     $this->journal->addLanguage($language ? $language : $this->createLanguage($languageCode));
     $this->importContacts($languageCode);
     $this->importSubmissionChecklist($languageCode);
     $this->consoleOutput->writeln("Read journal's settings.");
     $this->em->beginTransaction();
     // Outer transaction
     try {
         $this->em->beginTransaction();
         // Inner transaction
         $this->em->persist($this->journal);
         $this->em->flush();
         $this->em->commit();
     } catch (Exception $exception) {
         $this->em->rollback();
         throw $exception;
     }
     $this->consoleOutput->writeln("Imported journal #" . $id);
     // Those below also create their own inner transactions
     $createdSections = $this->sectionImporter->importJournalSections($id, $this->journal->getId());
     $createdIssues = $this->issueImporter->importJournalIssues($id, $this->journal->getId(), $createdSections);
     $this->articleImporter->importJournalArticles($id, $this->journal->getId(), $createdIssues, $createdSections);
     $this->journalPageImporter->importPages($id, $this->journal->getId());
     $createdBoards = $this->boardImporter->importBoards($id, $this->journal->getId());
     foreach ($createdBoards as $oldBoardId => $newBoardId) {
         $this->boardMemberImporter->importBoardMembers($oldBoardId, $newBoardId);
     }
     $this->importAboutPage();
     $map = new ImportMap($id, $this->journal->getId(), Journal::class);
     $this->em->persist($map);
     $this->em->flush();
     $this->em->commit();
     return ['new' => $this->journal->getId(), 'old' => $id];
 }