/**
  * Execute import/export tasks using the command-line interface.
  * @param $args Parameters to the plugin
  */
 function executeCLI($scriptName, &$args)
 {
     $command = array_shift($args);
     $xmlFile = array_shift($args);
     $journalPath = array_shift($args);
     AppLocale::requireComponents(LOCALE_COMPONENT_APPLICATION_COMMON);
     $journalDao =& DAORegistry::getDAO('JournalDAO');
     $issueDao =& DAORegistry::getDAO('IssueDAO');
     $sectionDao =& DAORegistry::getDAO('SectionDAO');
     $userDao =& DAORegistry::getDAO('UserDAO');
     $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
     $journal =& $journalDao->getJournalByPath($journalPath);
     if (!$journal) {
         if ($journalPath != '') {
             echo __('plugins.importexport.native.cliError') . "\n";
             echo __('plugins.importexport.native.error.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
         }
         $this->usage($scriptName);
         return;
     }
     $this->import('NativeImportDom');
     if ($xmlFile && NativeImportDom::isRelativePath($xmlFile)) {
         $xmlFile = PWD . '/' . $xmlFile;
     }
     switch ($command) {
         case 'import':
             $userName = array_shift($args);
             $user =& $userDao->getByUsername($userName);
             if (!$user) {
                 if ($userName != '') {
                     echo __('plugins.importexport.native.cliError') . "\n";
                     echo __('plugins.importexport.native.error.unknownUser', array('userName' => $userName)) . "\n\n";
                 }
                 $this->usage($scriptName);
                 return;
             }
             $doc =& $this->getDocument($xmlFile);
             $context = array('user' => $user, 'journal' => $journal);
             switch ($this->getRootNodeName($doc)) {
                 case 'article':
                 case 'articles':
                     // Determine the extra context information required
                     // for importing articles.
                     if (array_shift($args) !== 'issue_id') {
                         return $this->usage($scriptName);
                     }
                     $issue =& $issueDao->getIssueByBestIssueId($issueId = array_shift($args), $journal->getId());
                     if (!$issue) {
                         echo __('plugins.importexport.native.cliError') . "\n";
                         echo __('plugins.importexport.native.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
                         return;
                     }
                     $context['issue'] =& $issue;
                     switch (array_shift($args)) {
                         case 'section_id':
                             $section =& $sectionDao->getSection($sectionIdentifier = array_shift($args));
                             break;
                         case 'section_name':
                             $section =& $sectionDao->getSectionByTitle($sectionIdentifier = array_shift($args), $journal->getId());
                             break;
                         case 'section_abbrev':
                             $section =& $sectionDao->getSectionByAbbrev($sectionIdentifier = array_shift($args), $journal->getId());
                             break;
                         default:
                             return $this->usage($scriptName);
                     }
                     if (!$section) {
                         echo __('plugins.importexport.native.cliError') . "\n";
                         echo __('plugins.importexport.native.export.error.sectionNotFound', array('sectionIdentifier' => $sectionIdentifier)) . "\n\n";
                         return;
                     }
                     $context['section'] =& $section;
             }
             $result = $this->handleImport($context, $doc, $errors, $issues, $articles, true);
             if ($result) {
                 echo __('plugins.importexport.native.import.success.description') . "\n\n";
                 if (!empty($issues)) {
                     echo __('issue.issues') . ":\n";
                 }
                 foreach ($issues as $issue) {
                     echo "\t" . $issue->getIssueIdentification() . "\n";
                 }
                 if (!empty($articles)) {
                     echo __('article.articles') . ":\n";
                 }
                 foreach ($articles as $article) {
                     echo "\t" . $article->getLocalizedTitle() . "\n";
                 }
             } else {
                 $errorsTranslated = array();
                 foreach ($errors as $error) {
                     $errorsTranslated[] = __($error[0], $error[1]);
                 }
                 echo __('plugins.importexport.native.cliError') . "\n";
                 foreach ($errorsTranslated as $errorTranslated) {
                     echo "\t" . $errorTranslated . "\n";
                 }
             }
             return;
             break;
         case 'export':
             if ($xmlFile != '') {
                 switch (array_shift($args)) {
                     case 'article':
                         $articleId = array_shift($args);
                         $publishedArticle =& $publishedArticleDao->getPublishedArticleByBestArticleId($journal->getId(), $articleId);
                         if ($publishedArticle == null) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.articleNotFound', array('articleId' => $articleId)) . "\n\n";
                             return;
                         }
                         $issue =& $issueDao->getIssueById($publishedArticle->getIssueId(), $journal->getId());
                         $sectionDao =& DAORegistry::getDAO('SectionDAO');
                         $section =& $sectionDao->getSection($publishedArticle->getSectionId());
                         if (!$this->exportArticle($journal, $issue, $section, $publishedArticle, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                     case 'articles':
                         $results =& ArticleSearch::formatResults($args);
                         if (!$this->exportArticles($results, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                     case 'issue':
                         $issueId = array_shift($args);
                         $issue =& $issueDao->getIssueByBestIssueId($issueId, $journal->getId());
                         if ($issue == null) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
                             return;
                         }
                         if (!$this->exportIssue($journal, $issue, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                     case 'issues':
                         $issues = array();
                         while (($issueId = array_shift($args)) !== null) {
                             $issue =& $issueDao->getIssueByBestIssueId($issueId, $journal->getId());
                             if ($issue == null) {
                                 echo __('plugins.importexport.native.cliError') . "\n";
                                 echo __('plugins.importexport.native.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
                                 return;
                             }
                             $issues[] =& $issue;
                         }
                         if (!$this->exportIssues($journal, $issues, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                 }
             }
             break;
     }
     $this->usage($scriptName);
 }
 /**
  * Execute import/export tasks using the command-line interface.
  * @param $args Parameters to the plugin
  */
 function executeCLI($scriptName, &$args)
 {
     $command = array_shift($args);
     $xmlFile = array_shift($args);
     $conferencePath = array_shift($args);
     $schedConfPath = array_shift($args);
     $conferenceDao = DAORegistry::getDAO('ConferenceDAO');
     $schedConfDao = DAORegistry::getDAO('SchedConfDAO');
     $trackDao = DAORegistry::getDAO('TrackDAO');
     $userDao = DAORegistry::getDAO('UserDAO');
     $publishedPaperDao = DAORegistry::getDAO('PublishedPaperDAO');
     $conference =& $conferenceDao->getByPath($conferencePath);
     if ($conference) {
         $schedConf =& $schedConfDao->getByPath($schedConfPath, $conference->getId());
     }
     if (!$conference || !$schedConfPath) {
         if ($conferencePath != '') {
             echo __('plugins.importexport.native.cliError') . "\n";
             echo __('plugins.importexport.native.error.unknownConference', array('conferencePath' => $conferencePath, 'schedConfPath' => $schedConfPath)) . "\n\n";
         }
         $this->usage($scriptName);
         return;
     }
     $this->import('NativeImportDom');
     if ($xmlFile && NativeImportDom::isRelativePath($xmlFile)) {
         $xmlFile = PWD . '/' . $xmlFile;
     }
     switch ($command) {
         case 'import':
             $userName = array_shift($args);
             $user =& $userDao->getByUsername($userName);
             if (!$user) {
                 if ($userName != '') {
                     echo __('plugins.importexport.native.cliError') . "\n";
                     echo __('plugins.importexport.native.error.unknownUser', array('userName' => $userName)) . "\n\n";
                 }
                 $this->usage($scriptName);
                 return;
             }
             $doc =& $this->getDocument($xmlFile);
             $context = array('user' => $user, 'conference' => $conference, 'schedConf' => $schedConf);
             switch ($this->getRootNodeName($doc)) {
                 case 'paper':
                 case 'papers':
                     // Determine the extra context information required
                     // for importing papers.
                     switch (array_shift($args)) {
                         case 'track_id':
                             $track =& $trackDao->getTrack($trackIdentifier = array_shift($args));
                             break;
                         case 'track_name':
                             $track =& $trackDao->getTrackByTitle($trackIdentifier = array_shift($args), $schedConf->getId());
                             break;
                         case 'track_abbrev':
                             $track =& $trackDao->getTrackByAbbrev($trackIdentifier = array_shift($args), $schedConf->getId());
                             break;
                         default:
                             return $this->usage($scriptName);
                     }
                     if (!$track) {
                         echo __('plugins.importexport.native.cliError') . "\n";
                         echo __('plugins.importexport.native.export.error.trackNotFound', array('trackIdentifier' => $trackIdentifier)) . "\n\n";
                         return;
                     }
                     $context['track'] =& $track;
             }
             $result = $this->handleImport($context, $doc, $errors, $papers, true);
             if ($result) {
                 echo __('plugins.importexport.native.import.success.description') . "\n\n";
                 AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON);
                 if (!empty($papers)) {
                     echo __('paper.papers') . ":\n";
                 }
                 foreach ($papers as $paper) {
                     echo "\t" . $paper->getLocalizedTitle() . "\n";
                 }
             } else {
                 echo __('plugins.importexport.native.cliError') . "\n";
                 foreach ($errors as $error) {
                     echo "\t" . __($error[0], $error[1]) . "\n";
                 }
             }
             return;
             break;
         case 'export':
             if ($xmlFile != '') {
                 switch (array_shift($args)) {
                     case 'paper':
                         $paperId = array_shift($args);
                         $publishedPaper =& $publishedPaperDao->getPublishedPaperByBestPaperId($schedConf->getId(), $paperId);
                         if ($publishedPaper == null) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.paperNotFound', array('paperId' => $paperId)) . "\n\n";
                             return;
                         }
                         $trackDao = DAORegistry::getDAO('TrackDAO');
                         $track =& $trackDao->getTrack($publishedPaper->getTrackId());
                         if (!$this->exportPaper($schedConf, $track, $publishedPaper, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                     case 'papers':
                         $results =& PaperSearch::formatResults($args);
                         if (!$this->exportPapers($results, $xmlFile)) {
                             echo __('plugins.importexport.native.cliError') . "\n";
                             echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
                         }
                         return;
                 }
             }
             break;
     }
     $this->usage($scriptName);
 }
 function handleSuppFileNode(&$journal, &$suppNode, &$issue, &$section, &$article, &$errors, $isCommandLine, &$articleFileManager)
 {
     $errors = array();
     $journalSupportedLocales = array_keys($journal->getSupportedLocaleNames());
     // => journal locales must be set up before
     $journalPrimaryLocale = $journal->getPrimaryLocale();
     $suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
     $suppFile = new SuppFile();
     $suppFile->setArticleId($article->getId());
     for ($index = 0; $node = $suppNode->getChildByName('title', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileTitleLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setTitle($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('creator', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileCreatorLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setCreator($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('subject', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileSubjectLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setSubject($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('type_other', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileTypeOtherLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setTypeOther($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('description', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileDescriptionLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setDescription($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('publisher', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFilePublisherLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setPublisher($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('sponsor', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileSponsorLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setSponsor($node->getValue(), $locale);
     }
     for ($index = 0; $node = $suppNode->getChildByName('source', $index); $index++) {
         $locale = $node->getAttribute('locale');
         if ($locale == '') {
             $locale = $journalPrimaryLocale;
         } elseif (!in_array($locale, $journalSupportedLocales)) {
             $errors[] = array('plugins.importexport.native.import.error.articleSuppFileSourceLocaleUnsupported', array('suppFileTitle' => $node->getValue(), 'articleTitle' => $article->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification(), 'locale' => $locale));
             return false;
         }
         $suppFile->setSource($node->getValue(), $locale);
     }
     if ($node = $suppNode->getChildByName('date_created')) {
         $createdDate = strtotime($node->getValue());
         if ($createdDate !== -1) {
             $suppFile->setDateCreated($createdDate);
         }
     }
     switch ($suppType = $suppNode->getAttribute('type')) {
         case 'research_instrument':
             $suppFile->setType(__('author.submit.suppFile.researchInstrument'));
             break;
         case 'research_materials':
             $suppFile->setType(__('author.submit.suppFile.researchMaterials'));
             break;
         case 'research_results':
             $suppFile->setType(__('author.submit.suppFile.researchResults'));
             break;
         case 'transcripts':
             $suppFile->setType(__('author.submit.suppFile.transcripts'));
             break;
         case 'data_analysis':
             $suppFile->setType(__('author.submit.suppFile.dataAnalysis'));
             break;
         case 'data_set':
             $suppFile->setType(__('author.submit.suppFile.dataSet'));
             break;
         case 'source_text':
             $suppFile->setType(__('author.submit.suppFile.sourceText'));
             break;
         case 'other':
             $suppFile->setType('');
             break;
         default:
             $errors[] = array('plugins.importexport.native.import.error.unknownSuppFileType', array('suppFileType' => $suppType));
             return false;
     }
     $suppFile->setLanguage($suppNode->getAttribute('language'));
     $suppFile->setPublicSuppFileId($suppNode->getAttribute('public_id'));
     if (!($fileNode = $suppNode->getChildByName('file'))) {
         $errors[] = array('plugins.importexport.native.import.error.suppFileMissing', array('articleTitle' => $article->getLocalizedTitle(), 'sectionTitle' => $section->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification()));
         return false;
     }
     if ($href = $fileNode->getChildByName('href')) {
         $url = $href->getAttribute('src');
         if ($isCommandLine || NativeImportDom::isAllowedMethod($url)) {
             if ($isCommandLine && NativeImportDom::isRelativePath($url)) {
                 // The command-line tool does a chdir; we need to prepend the original pathname to relative paths so we're not looking in the wrong place.
                 $url = PWD . '/' . $url;
             }
             if (($fileId = $articleFileManager->copySuppFile($url, $href->getAttribute('mime_type'))) === false) {
                 $errors[] = array('plugins.importexport.native.import.error.couldNotCopy', array('url' => $url));
                 return false;
             }
         }
     }
     if ($embed = $fileNode->getChildByName('embed')) {
         if (($type = $embed->getAttribute('encoding')) !== 'base64') {
             $errors[] = array('plugins.importexport.native.import.error.unknownEncoding', array('type' => $type));
             return false;
         }
         $originalName = $embed->getAttribute('filename');
         if (($fileId = $articleFileManager->writeSuppFile($originalName, base64_decode($embed->getValue()), $embed->getAttribute('mime_type'))) === false) {
             $errors[] = array('plugins.importexport.native.import.error.couldNotWriteFile', array('originalName' => $originalName));
             return false;
         }
     }
     if (!$fileId) {
         $errors[] = array('plugins.importexport.native.import.error.suppFileMissing', array('articleTitle' => $article->getLocalizedTitle(), 'sectionTitle' => $section->getLocalizedTitle(), 'issueTitle' => $issue->getIssueIdentification()));
         return false;
     }
     $suppFile->setFileId($fileId);
     $suppFileDao->insertSuppFile($suppFile);
     return true;
 }
 /**
  * Import issues from DuraCloud.
  * @param $user User
  * @param $journal Journal
  * @param $contentId string
  * @return array with result for each contentId (see importIssue)
  */
 function importIssues(&$user, &$journal, $contentIds)
 {
     // Get the file from DuraCloud.
     $dcc = $this->getDuraCloudConnection();
     $ds = new DuraStore($dcc);
     $result = array();
     $dependentItems = $errors = array();
     $nativeImportExportPlugin =& $this->getNativeImportExportPlugin();
     foreach ($contentIds as $contentId) {
         $content = $ds->getContent($this->getDuraCloudSpace(), $contentId);
         if (!$content) {
             $result[$contentId] = false;
             continue;
         }
         // Get and reset the resource
         $fp =& $content->getResource();
         fseek($fp, 0);
         // Parse the document
         $doc =& $nativeImportExportPlugin->getDocument($fp);
         // Import the issue
         $nativeImportExportPlugin->import('NativeImportDom');
         $issue = null;
         NativeImportDom::importIssue($journal, $doc, $issue, $errors, $user, false, $dependentItems);
         $result[$contentId] =& $issue;
         unset($issue, $fp);
     }
     return $result;
 }