PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/ojs/ojs-2.3.1-2/plugins/importexport/crossref/CrossRefExportPlugin.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 302 lines | 203 code | 47 blank | 52 comment | 22 complexity | a7ca34fc5b1e0487f82496adb55423e8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file plugins/importexport/crossref/CrossRefExportPlugin.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class CrossRefExportPlugin
  9. * @ingroup plugins_importexport_crossref
  10. *
  11. * @brief CrossRef/MEDLINE XML metadata export plugin
  12. */
  13. // $Id: CrossRefExportPlugin.inc.php,v 1.23 2009/10/27 22:33:58 asmecher Exp $
  14. import('classes.plugins.ImportExportPlugin');
  15. class CrossRefExportPlugin extends ImportExportPlugin {
  16. /**
  17. * Called as a plugin is registered to the registry
  18. * @param $category String Name of category plugin was registered to
  19. * @return boolean True if plugin initialized successfully; if false,
  20. * the plugin will not be registered.
  21. */
  22. function register($category, $path) {
  23. $success = parent::register($category, $path);
  24. $this->addLocaleData();
  25. return $success;
  26. }
  27. /**
  28. * Get the name of this plugin. The name must be unique within
  29. * its category.
  30. * @return String name of plugin
  31. */
  32. function getName() {
  33. return 'CrossRefExportPlugin';
  34. }
  35. function getDisplayName() {
  36. return Locale::translate('plugins.importexport.crossref.displayName');
  37. }
  38. function getDescription() {
  39. return Locale::translate('plugins.importexport.crossref.description');
  40. }
  41. function display(&$args) {
  42. $templateMgr =& TemplateManager::getManager();
  43. parent::display($args);
  44. $issueDao =& DAORegistry::getDAO('IssueDAO');
  45. $journal =& Request::getJournal();
  46. switch (array_shift($args)) {
  47. case 'exportIssues':
  48. $issueIds = Request::getUserVar('issueId');
  49. if (!isset($issueIds)) $issueIds = array();
  50. $issues = array();
  51. foreach ($issueIds as $issueId) {
  52. $issue =& $issueDao->getIssueById($issueId);
  53. if (!$issue) Request::redirect();
  54. $issues[] =& $issue;
  55. }
  56. $this->exportIssues($journal, $issues);
  57. break;
  58. case 'exportIssue':
  59. $issueId = array_shift($args);
  60. $issue =& $issueDao->getIssueById($issueId);
  61. if (!$issue) Request::redirect();
  62. $issues = array($issue);
  63. $this->exportIssues($journal, $issues);
  64. break;
  65. case 'exportArticle':
  66. $articleIds = array(array_shift($args));
  67. $result = ArticleSearch::formatResults($articleIds);
  68. $this->exportArticles($journal, $result);
  69. break;
  70. case 'exportArticles':
  71. $articleIds = Request::getUserVar('articleId');
  72. if (!isset($articleIds)) $articleIds = array();
  73. $results =& ArticleSearch::formatResults($articleIds);
  74. $this->exportArticles($journal, $results);
  75. break;
  76. case 'issues':
  77. // Display a list of issues for export
  78. $this->setBreadcrumbs(array(), true);
  79. Locale::requireComponents(array(LOCALE_COMPONENT_OJS_EDITOR));
  80. $issueDao =& DAORegistry::getDAO('IssueDAO');
  81. $issues =& $issueDao->getPublishedIssues($journal->getJournalId(), Handler::getRangeInfo('issues'));
  82. $templateMgr->assign_by_ref('issues', $issues);
  83. $templateMgr->display($this->getTemplatePath() . 'issues.tpl');
  84. break;
  85. case 'articles':
  86. // Display a list of articles for export
  87. $this->setBreadcrumbs(array(), true);
  88. $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
  89. $rangeInfo = Handler::getRangeInfo('articles');
  90. $articleIds = $publishedArticleDao->getPublishedArticleIdsByJournal($journal->getJournalId(), false);
  91. $totalArticles = count($articleIds);
  92. if ($rangeInfo->isValid()) $articleIds = array_slice($articleIds, $rangeInfo->getCount() * ($rangeInfo->getPage()-1), $rangeInfo->getCount());
  93. import('core.VirtualArrayIterator');
  94. $iterator = new VirtualArrayIterator(ArticleSearch::formatResults($articleIds), $totalArticles, $rangeInfo->getPage(), $rangeInfo->getCount());
  95. $templateMgr->assign_by_ref('articles', $iterator);
  96. $templateMgr->display($this->getTemplatePath() . 'articles.tpl');
  97. break;
  98. default:
  99. $this->setBreadcrumbs();
  100. $templateMgr->assign_by_ref('journal', $journal);
  101. $templateMgr->display($this->getTemplatePath() . 'index.tpl');
  102. }
  103. }
  104. function exportArticles(&$journal, &$results, $outputFile = null) {
  105. $this->import('CrossRefExportDom');
  106. $doc =& CrossRefExportDom::generateCrossRefDom();
  107. $doiBatchNode =& CrossRefExportDom::generateDoiBatchDom($doc);
  108. // Create Head Node and all parts inside it
  109. $head =& CrossRefExportDom::generateHeadDom($doc, $journal);
  110. // attach it to the root node
  111. XMLCustomWriter::appendChild($doiBatchNode, $head);
  112. // the body node contains everything
  113. $bodyNode =& XMLCustomWriter::createElement($doc, 'body');
  114. XMLCustomWriter::appendChild($doiBatchNode, $bodyNode);
  115. // now cycle through everything we want to submit in this batch
  116. foreach ($results as $result) {
  117. $journal =& $result['journal'];
  118. $issue =& $result['issue'];
  119. $section =& $result['section'];
  120. $article =& $result['publishedArticle'];
  121. // Create the metadata node
  122. // this does not need to be repeated for every article
  123. // but its allowed to be and its simpler to do so
  124. $journalNode =& XMLCustomWriter::createElement($doc, 'journal');
  125. $journalMetadataNode =& CrossRefExportDom::generateJournalMetadataDom($doc, $journal);
  126. XMLCustomWriter::appendChild($journalNode, $journalMetadataNode);
  127. // Create the journal_issue node
  128. $journalIssueNode =& CrossRefExportDom::generateJournalIssueDom($doc, $journal, $issue, $section, $article);
  129. XMLCustomWriter::appendChild($journalNode, $journalIssueNode);
  130. // Create the article
  131. $journalArticleNode =& CrossRefExportDom::generateJournalArticleDom($doc, $journal, $issue, $section, $article);
  132. XMLCustomWriter::appendChild($journalNode, $journalArticleNode);
  133. // Create the DOI data
  134. $DOIdataNode =& CrossRefExportDom::generateDOIdataDom($doc, $article->getDOI(), Request::url(null, 'article', 'view', $article->getArticleId()));
  135. XMLCustomWriter::appendChild($journalArticleNode, $DOIdataNode);
  136. XMLCustomWriter::appendChild($bodyNode, $journalNode);
  137. }
  138. // dump out the results
  139. if (!empty($outputFile)) {
  140. if (($h = fopen($outputFile, 'w'))===false) return false;
  141. fwrite($h, XMLCustomWriter::getXML($doc));
  142. fclose($h);
  143. } else {
  144. header("Content-Type: application/xml");
  145. header("Cache-Control: private");
  146. header("Content-Disposition: attachment; filename=\"crossref.xml\"");
  147. XMLCustomWriter::printXML($doc);
  148. }
  149. return true;
  150. }
  151. function exportIssues(&$journal, &$issues, $outputFile = null) {
  152. $this->import('CrossRefExportDom');
  153. $doc =& CrossRefExportDom::generateCrossRefDom();
  154. $doiBatchNode =& CrossRefExportDom::generateDoiBatchDom($doc);
  155. $journal =& Request::getJournal();
  156. // Create Head Node and all parts inside it
  157. $head =& CrossRefExportDom::generateHeadDom($doc, $journal);
  158. // attach it to the root node
  159. XMLCustomWriter::appendChild($doiBatchNode, $head);
  160. $bodyNode =& XMLCustomWriter::createElement($doc, 'body');
  161. XMLCustomWriter::appendChild($doiBatchNode, $bodyNode);
  162. $sectionDao =& DAORegistry::getDAO('SectionDAO');
  163. $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
  164. foreach ($issues as $issue) {
  165. foreach ($sectionDao->getSectionsForIssue($issue->getIssueId()) as $section) {
  166. foreach ($publishedArticleDao->getPublishedArticlesBySectionId($section->getSectionId(), $issue->getIssueId()) as $article) {
  167. // Create the metadata node
  168. // this does not need to be repeated for every article
  169. // but its allowed to be and its simpler to do so
  170. $journalNode =& XMLCustomWriter::createElement($doc, 'journal');
  171. $journalMetadataNode =& CrossRefExportDom::generateJournalMetadataDom($doc, $journal);
  172. XMLCustomWriter::appendChild($journalNode, $journalMetadataNode);
  173. $journalIssueNode =& CrossRefExportDom::generateJournalIssueDom($doc, $journal, $issue, $section, $article);
  174. XMLCustomWriter::appendChild($journalNode, $journalIssueNode);
  175. // Article node
  176. $journalArticleNode =& CrossRefExportDom::generateJournalArticleDom($doc, $journal, $issue, $section, $article);
  177. XMLCustomWriter::appendChild($journalNode, $journalArticleNode);
  178. // DOI data node
  179. $DOIdataNode =& CrossRefExportDom::generateDOIdataDom($doc, $article->getDOI(), Request::url(null, 'article', 'view', $article->getArticleId()));
  180. XMLCustomWriter::appendChild($journalArticleNode, $DOIdataNode);
  181. XMLCustomWriter::appendChild($bodyNode, $journalNode);
  182. }
  183. }
  184. }
  185. // dump out results
  186. if (!empty($outputFile)) {
  187. if (($h = fopen($outputFile, 'w'))===false) return false;
  188. fwrite($h, XMLCustomWriter::getXML($doc));
  189. fclose($h);
  190. } else {
  191. header("Content-Type: application/xml");
  192. header("Cache-Control: private");
  193. header("Content-Disposition: attachment; filename=\"crossref.xml\"");
  194. XMLCustomWriter::printXML($doc);
  195. }
  196. return true;
  197. }
  198. /**
  199. * Execute import/export tasks using the command-line interface.
  200. * @param $args Parameters to the plugin
  201. */
  202. function executeCLI($scriptName, &$args) {
  203. // $command = array_shift($args);
  204. $xmlFile = array_shift($args);
  205. $journalPath = array_shift($args);
  206. $journalDao =& DAORegistry::getDAO('JournalDAO');
  207. $issueDao =& DAORegistry::getDAO('IssueDAO');
  208. $sectionDao =& DAORegistry::getDAO('SectionDAO');
  209. $userDao =& DAORegistry::getDAO('UserDAO');
  210. $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
  211. $journal =& $journalDao->getJournalByPath($journalPath);
  212. if (!$journal) {
  213. if ($journalPath != '') {
  214. echo Locale::translate('plugins.importexport.crossref.cliError') . "\n";
  215. echo Locale::translate('plugins.importexport.crossref.error.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
  216. }
  217. $this->usage($scriptName);
  218. return;
  219. }
  220. if ($xmlFile != '') switch (array_shift($args)) {
  221. case 'articles':
  222. $results =& ArticleSearch::formatResults($args);
  223. if (!$this->exportArticles($journal, $results, $xmlFile)) {
  224. echo Locale::translate('plugins.importexport.crossref.cliError') . "\n";
  225. echo Locale::translate('plugins.importexport.crossref.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
  226. }
  227. return;
  228. case 'issue':
  229. $issueId = array_shift($args);
  230. $issue =& $issueDao->getIssueByBestIssueId($issueId, $journal->getJournalId());
  231. if ($issue == null) {
  232. echo Locale::translate('plugins.importexport.crossref.cliError') . "\n";
  233. echo Locale::translate('plugins.importexport.crossref.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
  234. return;
  235. }
  236. $issues = array($issue);
  237. if (!$this->exportIssues($journal, $issues, $xmlFile)) {
  238. echo Locale::translate('plugins.importexport.crossref.cliError') . "\n";
  239. echo Locale::translate('plugins.importexport.crossref.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
  240. }
  241. return;
  242. }
  243. $this->usage($scriptName);
  244. }
  245. /**
  246. * Display the command-line usage information
  247. */
  248. function usage($scriptName) {
  249. echo Locale::translate('plugins.importexport.crossref.cliUsage', array(
  250. 'scriptName' => $scriptName,
  251. 'pluginName' => $this->getName()
  252. )) . "\n";
  253. }
  254. }
  255. ?>