/**
  * Create deposit package of article metadata.
  * @param $article Article
  * @return DataversePackager
  */
 function createMetadataPackage($article)
 {
     $journalDao =& DAORegistry::getDAO('JournalDAO');
     $journal =& $journalDao->getById($article->getJournalId());
     $package = new DataversePackager();
     // Add article metadata, in language of article locale
     $package->addMetadata('title', $article->getTitle($article->getLocale()));
     // If study description not provided, use article abstract
     $package->addMetadata('description', $article->getData('studyDescription', $article->getLocale()) ? $article->getData('studyDescription', $article->getLocale()) : String::html2text($article->getAbstract($article->getLocale())));
     foreach ($article->getAuthors() as $author) {
         $package->addMetadata('creator', $author->getFullName(true), array('affiliation' => $this->_formatAffiliation($author, $article->getLocale())));
     }
     // Article metadata: fields with multiple values
     $pattern = '/\\s*' . DATAVERSE_PLUGIN_SUBJECT_SEPARATOR . '\\s*/';
     foreach (String::regexp_split($pattern, $article->getCoverageGeo($article->getLocale())) as $coverage) {
         if ($coverage) {
             $package->addMetadata('coverage', $coverage);
         }
     }
     // Article metadata: filter subject(s) to prevent repeated values in dataset subject field
     $subjects = array();
     foreach (String::regexp_split($pattern, $article->getDiscipline($article->getLocale())) as $subject) {
         if ($subject) {
             $subjects[String::strtolower($subject)] = $subject;
         }
     }
     foreach (String::regexp_split($pattern, $article->getSubjectClass($article->getLocale())) as $subject) {
         if ($subject) {
             $subjects[String::strtolower($subject)] = $subject;
         }
     }
     foreach (String::regexp_split($pattern, $article->getSubject($article->getLocale())) as $subject) {
         if ($subject) {
             $subjects[String::strtolower($subject)] = $subject;
         }
     }
     // Article metadata: filter contributors(s) to prevent repeated values in dataset contributor field
     $contributors = array();
     foreach (String::regexp_split($pattern, $article->getSponsor($article->getLocale())) as $contributor) {
         if ($contributor) {
             $contributors[String::strtolower($contributor)] = $contributor;
         }
     }
     // Published article metadata
     $pubIdAttributes = array();
     if ($article->getStatus() == STATUS_PUBLISHED) {
         // publication date
         $publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
         $publishedArticle =& $publishedArticleDao->getPublishedArticleByArticleId($article->getId(), $article->getJournalId());
         $datePublished = $publishedArticle->getDatePublished();
         if (!$datePublished) {
             // If article has no pub date, use issue pub date
             $issueDao =& DAORegistry::getDAO('IssueDAO');
             $issue =& $issueDao->getIssueByArticleId($article->getId(), $article->getJournalId());
             $datePublished = $issue->getDatePublished();
         }
         $package->addMetadata('date', strftime('%Y-%m-%d', strtotime($datePublished)));
         // isReferencedBy: add persistent URL to citation using specified pubid plugin
         $pubIdPlugin =& PluginRegistry::getPlugin('pubIds', $this->getSetting($article->getJournalId(), 'pubIdPlugin'));
         if ($pubIdPlugin && $pubIdPlugin->getEnabled()) {
             $pubIdAttributes['agency'] = $pubIdPlugin->getDisplayName();
             $pubIdAttributes['IDNo'] = $article->getPubId($pubIdPlugin->getPubIdType());
             $pubIdAttributes['holdingsURI'] = $pubIdPlugin->getResolvingUrl($article->getJournalId(), $pubIdAttributes['IDNo']);
         }
         // If no pubIdP plugin selected or enabled, provide OJS URL
         if (!array_key_exists('holdingsURI', $pubIdAttributes)) {
             $pubIdAttributes['holdingsURI'] = Request::url($journal->getPath(), 'article', 'view', array($article->getId()));
         }
         // Add copyright notice.
         if ($article->getCopyrightYear() && $article->getCopyrightHolder($article->getLocale())) {
             AppLocale::requireComponents(LOCALE_COMPONENT_APPLICATION_COMMON);
             $package->addMetadata('rights', __('submission.copyrightStatement', array('copyrightYear' => $article->getCopyrightYear(), 'copyrightHolder' => $article->getCopyrightHolder($article->getLocale()))));
         }
     }
     // Journal metadata
     $package->addMetadata('publisher', $journal->getSetting('publisherInstitution'));
     $package->addMetadata('isReferencedBy', String::html2text($this->getCitation($article)), $pubIdAttributes);
     // Suppfile metadata
     $suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
     $dvFileDao =& DAORegistry::getDAO('DataverseFileDAO');
     $dvFiles =& $dvFileDao->getDataverseFilesBySubmissionId($article->getId());
     // Filter type field to prevent repeated values in dataset 'Kind of data' field
     $suppFileTypes = array();
     foreach ($dvFiles as $dvFile) {
         $suppFile =& $suppFileDao->getSuppFile($dvFile->getSuppFileId(), $article->getId());
         if ($suppFile) {
             // Split & filter subjects and/or contributors that may be repeated in article metadata
             foreach (String::regexp_split($pattern, $suppFile->getSubject($article->getLocale())) as $subject) {
                 $subjects[String::strtolower($subject)] = $subject;
             }
             foreach (String::regexp_split($pattern, $suppFile->getSponsor($article->getLocale())) as $contributor) {
                 if ($contributor) {
                     $contributors[String::strtolower($contributor)] = $contributor;
                 }
             }
             // File type has single value but possibly repeated across suppfiles
             if ($suppFile->getType()) {
                 $suppFileTypes[String::strtolower($suppFile->getType())] = $suppFile->getType();
             }
             if ($suppFile->getTypeOther($article->getLocale())) {
                 $suppFileTypes[String::strtolower($suppFile->getTypeOther($article->getLocale()))] = $suppFile->getTypeOther($article->getLocale());
             }
         }
     }
     // Add subjects, contributors & types to entry
     foreach (array_values($subjects) as $subject) {
         $package->addMetadata('subject', $subject);
     }
     foreach (array_values($contributors) as $contributor) {
         $package->addMetadata('contributor', $contributor, array('type' => 'funder'));
     }
     foreach (array_values($suppFileTypes) as $type) {
         $package->addMetadata('type', $type);
     }
     // Write metadata as Atom entry
     $package->createAtomEntry();
     // Return package for deposit
     return $package;
 }
 /**
  * Update cataloguing information for an existing study.
  * @param Article $article
  * @param DataverseStudy $study
  * @return DataverseStudy
  */
 function &updateStudy(&$article, &$study)
 {
     $journal =& Request::getJournal();
     $packager = new DataversePackager();
     // Add article metadata
     $packager->addMetadata('title', $article->getLocalizedTitle());
     $packager->addMetadata('description', $article->getLocalizedAbstract());
     foreach ($article->getAuthors() as $author) {
         $packager->addMetadata('creator', $author->getFullName(true));
     }
     // subject: academic disciplines
     $split = '/\\s*' . DATAVERSE_PLUGIN_SUBJECT_SEPARATOR . '\\s*/';
     foreach (preg_split($split, $article->getLocalizedDiscipline(), NULL, PREG_SPLIT_NO_EMPTY) as $subject) {
         $packager->addMetadata('subject', $subject);
     }
     // subject: subject classifications
     foreach (preg_split($split, $article->getLocalizedSubjectClass(), NULL, PREG_SPLIT_NO_EMPTY) as $subject) {
         $packager->addMetadata('subject', $subject);
     }
     // subject:	 keywords
     foreach (preg_split($split, $article->getLocalizedSubject(), NULL, PREG_SPLIT_NO_EMPTY) as $subject) {
         $packager->addMetadata('subject', $subject);
     }
     // geographic coverage
     foreach (preg_split($split, $article->getLocalizedCoverageGeo(), NULL, PREG_SPLIT_NO_EMPTY) as $coverage) {
         $packager->addMetadata('coverage', $coverage);
     }
     // rights
     $packager->addMetadata('rights', $journal->getLocalizedSetting('copyrightNotice'));
     // publisher
     $packager->addMetadata('publisher', $journal->getSetting('publisherInstitution'));
     // metadata for published articles: public IDs, publication dates
     $pubIdAttributes = array();
     if ($article->getStatus() == STATUS_PUBLISHED) {
         // publication date
         $publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
         $publishedArticle =& $publishedArticleDao->getPublishedArticleByArticleId($article->getId(), $article->getJournalId());
         $datePublished = $publishedArticle->getDatePublished();
         if (!$datePublished) {
             // If article has no pub date, use issue pub date
             $issueDao =& DAORegistry::getDAO('IssueDAO');
             $issue =& $issueDao->getIssueByArticleId($article->getId(), $article->getJournalId());
             $datePublished = $issue->getDatePublished();
         }
         $packager->addMetadata('date', strftime('%Y-%m-%d', strtotime($datePublished)));
         // isReferencedBy: If article is published, add a persistent URL to citation using specified pubid plugin
         $pubIdPlugin =& PluginRegistry::getPlugin('pubIds', $this->getSetting($article->getJournalId(), 'pubIdPlugin'));
         if ($pubIdPlugin && $pubIdPlugin->getEnabled()) {
             $pubIdAttributes['agency'] = $pubIdPlugin->getDisplayName();
             $pubIdAttributes['IDNo'] = $article->getPubId($pubIdPlugin->getPubIdType());
             $pubIdAttributes['holdingsURI'] = $pubIdPlugin->getResolvingUrl($article->getJournalId(), $pubIdAttributes['IDNo']);
         } else {
             // If no pub id plugin selected, use OJS URL
             $pubIdAttributes['holdingsURI'] = Request::url($journal->getPath(), 'article', 'view', array($article->getId()));
         }
     }
     // isReferencedBy
     $packager->addMetadata('isReferencedBy', $this->getCitation($article), $pubIdAttributes);
     // Include (some) suppfile metadata in study
     $suppFileDao =& DAORegistry::getDAO('SuppFileDAO');
     $dataverseFileDao =& DAORegistry::getDAO('DataverseFileDAO');
     $dvFiles =& $dataverseFileDao->getDataverseFilesByStudyId($study->getId());
     foreach ($dvFiles as $dvFile) {
         $suppFile =& $suppFileDao->getSuppFile($dvFile->getSuppFileId(), $article->getId());
         if (isset($suppFile)) {
             // subject
             foreach (preg_split($split, $suppFile->getSuppFileSubject(), NULL, PREG_SPLIT_NO_EMPTY) as $subject) {
                 $packager->addMetadata('subject', $subject);
             }
             // Type of file
             if ($suppFile->getType()) {
                 $packager->addMetadata('type', $suppFile->getType());
             }
             // Type of file, user-defined:
             if ($suppFile->getSuppFileTypeOther()) {
                 $packager->addMetadata('type', $suppFile->getSuppFileTypeOther());
             }
         }
     }
     // Write atom entry to file
     $packager->createAtomEntry();
     // Update the study in Dataverse
     $client = $this->_initSwordClient();
     $depositReceipt = $client->replaceMetadata($study->getEditUri(), $this->getSetting($article->getJournalId(), 'username'), $this->getSetting($article->getJournalId(), 'password'), '', $packager->getAtomEntryFilePath());
     if ($depositReceipt->sac_status != DATAVERSE_PLUGIN_HTTP_STATUS_OK) {
         return false;
     }
     // Updating the metadata may have updated the data citation
     $study->setDataCitation($depositReceipt->sac_dcterms['bibliographicCitation'][0]);
     $dataverseStudyDao =& DAORegistry::getDAO('DataverseStudyDAO');
     $dataverseStudyDao->updateStudy($study);
     return $study;
 }