PageRenderTime 64ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/ojs/ojs-2.2/plugins/importexport/pubmed/PubMedExportPlugin.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 236 lines | 175 code | 27 blank | 34 comment | 22 complexity | 5209905656b251b798db5ece6b0e4f98 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file PubMedExportPlugin.inc.php
  4. *
  5. * Copyright (c) 2003-2007 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package plugins.importexport.pubmed
  9. * @class PubMedExportPlugin
  10. *
  11. * PubMed/MEDLINE XML metadata export plugin
  12. *
  13. * $Id: PubMedExportPlugin.inc.php,v 1.9 2007/11/07 21:54:03 asmecher Exp $
  14. */
  15. import('classes.plugins.ImportExportPlugin');
  16. class PubMedExportPlugin extends ImportExportPlugin {
  17. /**
  18. * Called as a plugin is registered to the registry
  19. * @param @category String Name of category plugin was registered to
  20. * @return boolean True if plugin initialized successfully; if false,
  21. * the plugin will not be registered.
  22. */
  23. function register($category, $path) {
  24. $success = parent::register($category, $path);
  25. $this->addLocaleData();
  26. return $success;
  27. }
  28. /**
  29. * Get the name of this plugin. The name must be unique within
  30. * its category.
  31. * @return String name of plugin
  32. */
  33. function getName() {
  34. return 'PubMedExportPlugin';
  35. }
  36. function getDisplayName() {
  37. return Locale::translate('plugins.importexport.pubmed.displayName');
  38. }
  39. function getDescription() {
  40. return Locale::translate('plugins.importexport.pubmed.description');
  41. }
  42. function display(&$args) {
  43. $templateMgr = &TemplateManager::getManager();
  44. parent::display($args);
  45. $issueDao = &DAORegistry::getDAO('IssueDAO');
  46. $journal = &Request::getJournal();
  47. switch (array_shift($args)) {
  48. case 'exportIssues':
  49. $issueIds = Request::getUserVar('issueId');
  50. if (!isset($issueIds)) $issueIds = array();
  51. $issues = array();
  52. foreach ($issueIds as $issueId) {
  53. $issue = &$issueDao->getIssueById($issueId);
  54. if (!$issue) Request::redirect();
  55. $issues[] = &$issue;
  56. }
  57. $this->exportIssues($journal, $issues);
  58. break;
  59. case 'exportIssue':
  60. $issueId = array_shift($args);
  61. $issue = &$issueDao->getIssueById($issueId);
  62. if (!$issue) Request::redirect();
  63. $issues = array($issue);
  64. $this->exportIssues($journal, $issues);
  65. break;
  66. case 'exportArticle':
  67. $articleIds = array(array_shift($args));
  68. $result = ArticleSearch::formatResults($articleIds);
  69. $this->exportArticles($result);
  70. break;
  71. case 'exportArticles':
  72. $articleIds = Request::getUserVar('articleId');
  73. if (!isset($articleIds)) $articleIds = array();
  74. $results = &ArticleSearch::formatResults($articleIds);
  75. $this->exportArticles($results);
  76. break;
  77. case 'issues':
  78. // Display a list of issues for export
  79. $this->setBreadcrumbs(array(), true);
  80. $issueDao = &DAORegistry::getDAO('IssueDAO');
  81. $issues = &$issueDao->getIssues($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. $iterator = &new VirtualArrayIterator(ArticleSearch::formatResults($articleIds), $totalArticles, $rangeInfo->getPage(), $rangeInfo->getCount());
  94. $templateMgr->assign_by_ref('articles', $iterator);
  95. $templateMgr->display($this->getTemplatePath() . 'articles.tpl');
  96. break;
  97. default:
  98. $this->setBreadcrumbs();
  99. $templateMgr->display($this->getTemplatePath() . 'index.tpl');
  100. }
  101. }
  102. function exportArticles(&$results, $outputFile = null) {
  103. $this->import('PubMedExportDom');
  104. $doc = &PubMedExportDom::generatePubMedDom();
  105. $articleSetNode = &PubMedExportDom::generateArticleSetDom($doc);
  106. foreach ($results as $result) {
  107. $journal = &$result['journal'];
  108. $issue = &$result['issue'];
  109. $section = &$result['section'];
  110. $article = &$result['publishedArticle'];
  111. $articleNode = &PubMedExportDom::generateArticleDom($doc, $journal, $issue, $section, $article);
  112. XMLCustomWriter::appendChild($articleSetNode, $articleNode);
  113. }
  114. if (!empty($outputFile)) {
  115. if (($h = fopen($outputFile, 'w'))===false) return false;
  116. fwrite($h, XMLCustomWriter::getXML($doc));
  117. fclose($h);
  118. } else {
  119. header("Content-Type: application/xml");
  120. header("Cache-Control: private");
  121. header("Content-Disposition: attachment; filename=\"pubmed.xml\"");
  122. XMLCustomWriter::printXML($doc);
  123. }
  124. return true;
  125. }
  126. function exportIssues(&$journal, &$issues, $outputFile = null) {
  127. $this->import('PubMedExportDom');
  128. $doc = &PubMedExportDom::generatePubMedDom();
  129. $articleSetNode = &PubMedExportDom::generateArticleSetDom($doc);
  130. $sectionDao = &DAORegistry::getDAO('SectionDAO');
  131. $publishedArticleDao = &DAORegistry::getDAO('PublishedArticleDAO');
  132. foreach ($issues as $issue) {
  133. foreach ($sectionDao->getSectionsForIssue($issue->getIssueId()) as $section) {
  134. foreach ($publishedArticleDao->getPublishedArticlesBySectionId($section->getSectionId(), $issue->getIssueId()) as $article) {
  135. $articleNode = &PubMedExportDom::generateArticleDom($doc, $journal, $issue, $section, $article);
  136. XMLCustomWriter::appendChild($articleSetNode, $articleNode);
  137. }
  138. }
  139. }
  140. if (!empty($outputFile)) {
  141. if (($h = fopen($outputFile, 'w'))===false) return false;
  142. fwrite($h, XMLCustomWriter::getXML($doc));
  143. fclose($h);
  144. } else {
  145. header("Content-Type: application/xml");
  146. header("Cache-Control: private");
  147. header("Content-Disposition: attachment; filename=\"pubmed.xml\"");
  148. XMLCustomWriter::printXML($doc);
  149. }
  150. return true;
  151. }
  152. /**
  153. * Execute import/export tasks using the command-line interface.
  154. * @param $args Parameters to the plugin
  155. */
  156. function executeCLI($scriptName, &$args) {
  157. // $command = array_shift($args);
  158. $xmlFile = array_shift($args);
  159. $journalPath = array_shift($args);
  160. $journalDao = &DAORegistry::getDAO('JournalDAO');
  161. $issueDao = &DAORegistry::getDAO('IssueDAO');
  162. $sectionDao = &DAORegistry::getDAO('SectionDAO');
  163. $userDao = &DAORegistry::getDAO('UserDAO');
  164. $publishedArticleDao = &DAORegistry::getDAO('PublishedArticleDAO');
  165. $journal = &$journalDao->getJournalByPath($journalPath);
  166. if (!$journal) {
  167. if ($journalPath != '') {
  168. echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
  169. echo Locale::translate('plugins.importexport.pubmed.error.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
  170. }
  171. $this->usage($scriptName);
  172. return;
  173. }
  174. if ($xmlFile != '') switch (array_shift($args)) {
  175. case 'articles':
  176. $results = &ArticleSearch::formatResults($args);
  177. if (!$this->exportArticles($results, $xmlFile)) {
  178. echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
  179. echo Locale::translate('plugins.importexport.pubmed.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
  180. }
  181. return;
  182. case 'issue':
  183. $issueId = array_shift($args);
  184. $issue = &$issueDao->getIssueByBestIssueId($issueId, $journal->getJournalId());
  185. if ($issue == null) {
  186. echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
  187. echo Locale::translate('plugins.importexport.pubmed.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
  188. return;
  189. }
  190. $issues = array($issue);
  191. if (!$this->exportIssues($journal, $issues, $xmlFile)) {
  192. echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
  193. echo Locale::translate('plugins.importexport.pubmed.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
  194. }
  195. return;
  196. }
  197. $this->usage($scriptName);
  198. }
  199. /**
  200. * Display the command-line usage information
  201. */
  202. function usage($scriptName) {
  203. echo Locale::translate('plugins.importexport.pubmed.cliUsage', array(
  204. 'scriptName' => $scriptName,
  205. 'pluginName' => $this->getName()
  206. )) . "\n";
  207. }
  208. }
  209. ?>