00001 <?php
00002
00015
00016
00017
00018 import('classes.plugins.ImportExportPlugin');
00019
00020 class PubMedExportPlugin extends ImportExportPlugin {
00027 function register($category, $path) {
00028 $success = parent::register($category, $path);
00029 $this->addLocaleData();
00030 return $success;
00031 }
00032
00038 function getName() {
00039 return 'PubMedExportPlugin';
00040 }
00041
00042 function getDisplayName() {
00043 return Locale::translate('plugins.importexport.pubmed.displayName');
00044 }
00045
00046 function getDescription() {
00047 return Locale::translate('plugins.importexport.pubmed.description');
00048 }
00049
00050 function display(&$args) {
00051 $templateMgr = &TemplateManager::getManager();
00052 parent::display($args);
00053
00054 $issueDao = &DAORegistry::getDAO('IssueDAO');
00055
00056 $journal = &Request::getJournal();
00057
00058 switch (array_shift($args)) {
00059 case 'exportIssues':
00060 $issueIds = Request::getUserVar('issueId');
00061 if (!isset($issueIds)) $issueIds = array();
00062 $issues = array();
00063 foreach ($issueIds as $issueId) {
00064 $issue = &$issueDao->getIssueById($issueId);
00065 if (!$issue) Request::redirect();
00066 $issues[] = &$issue;
00067 }
00068 $this->exportIssues($journal, $issues);
00069 break;
00070 case 'exportIssue':
00071 $issueId = array_shift($args);
00072 $issue = &$issueDao->getIssueById($issueId);
00073 if (!$issue) Request::redirect();
00074 $issues = array($issue);
00075 $this->exportIssues($journal, $issues);
00076 break;
00077 case 'exportArticle':
00078 $articleIds = array(array_shift($args));
00079 $result = ArticleSearch::formatResults($articleIds);
00080 $this->exportArticles($result);
00081 break;
00082 case 'exportArticles':
00083 $articleIds = Request::getUserVar('articleId');
00084 if (!isset($articleIds)) $articleIds = array();
00085 $results = &ArticleSearch::formatResults($articleIds);
00086 $this->exportArticles($results);
00087 break;
00088 case 'issues':
00089
00090 $this->setBreadcrumbs(array(), true);
00091 $issueDao = &DAORegistry::getDAO('IssueDAO');
00092 $issues = &$issueDao->getIssues($journal->getJournalId(), Handler::getRangeInfo('issues'));
00093
00094 $templateMgr->assign_by_ref('issues', $issues);
00095 $templateMgr->display($this->getTemplatePath() . 'issues.tpl');
00096 break;
00097 case 'articles':
00098
00099 $this->setBreadcrumbs(array(), true);
00100 $publishedArticleDao = &DAORegistry::getDAO('PublishedArticleDAO');
00101 $rangeInfo = Handler::getRangeInfo('articles');
00102 $articleIds = $publishedArticleDao->getPublishedArticleIdsByJournal($journal->getJournalId(), false);
00103 $totalArticles = count($articleIds);
00104 if ($rangeInfo->isValid()) $articleIds = array_slice($articleIds, $rangeInfo->getCount() * ($rangeInfo->getPage()-1), $rangeInfo->getCount());
00105 $iterator = &new VirtualArrayIterator(ArticleSearch::formatResults($articleIds), $totalArticles, $rangeInfo->getPage(), $rangeInfo->getCount());
00106 $templateMgr->assign_by_ref('articles', $iterator);
00107 $templateMgr->display($this->getTemplatePath() . 'articles.tpl');
00108 break;
00109 default:
00110 $this->setBreadcrumbs();
00111 $templateMgr->display($this->getTemplatePath() . 'index.tpl');
00112 }
00113 }
00114
00115 function exportArticles(&$results, $outputFile = null) {
00116 $this->import('PubMedExportDom');
00117 $doc = &PubMedExportDom::generatePubMedDom();
00118 $articleSetNode = &PubMedExportDom::generateArticleSetDom($doc);
00119
00120 foreach ($results as $result) {
00121 $journal = &$result['journal'];
00122 $issue = &$result['issue'];
00123 $section = &$result['section'];
00124 $article = &$result['publishedArticle'];
00125
00126 $articleNode = &PubMedExportDom::generateArticleDom($doc, $journal, $issue, $section, $article);
00127 XMLCustomWriter::appendChild($articleSetNode, $articleNode);
00128 }
00129
00130 if (!empty($outputFile)) {
00131 if (($h = fopen($outputFile, 'w'))===false) return false;
00132 fwrite($h, XMLCustomWriter::getXML($doc));
00133 fclose($h);
00134 } else {
00135 header("Content-Type: application/xml");
00136 header("Cache-Control: private");
00137 header("Content-Disposition: attachment; filename=\"pubmed.xml\"");
00138 XMLCustomWriter::printXML($doc);
00139 }
00140 return true;
00141 }
00142
00143 function exportIssues(&$journal, &$issues, $outputFile = null) {
00144 $this->import('PubMedExportDom');
00145 $doc = &PubMedExportDom::generatePubMedDom();
00146 $articleSetNode = &PubMedExportDom::generateArticleSetDom($doc);
00147
00148 $sectionDao = &DAORegistry::getDAO('SectionDAO');
00149 $publishedArticleDao = &DAORegistry::getDAO('PublishedArticleDAO');
00150
00151 foreach ($issues as $issue) {
00152 foreach ($sectionDao->getSectionsForIssue($issue->getIssueId()) as $section) {
00153 foreach ($publishedArticleDao->getPublishedArticlesBySectionId($section->getSectionId(), $issue->getIssueId()) as $article) {
00154 $articleNode = &PubMedExportDom::generateArticleDom($doc, $journal, $issue, $section, $article);
00155 XMLCustomWriter::appendChild($articleSetNode, $articleNode);
00156 }
00157 }
00158 }
00159
00160 if (!empty($outputFile)) {
00161 if (($h = fopen($outputFile, 'w'))===false) return false;
00162 fwrite($h, XMLCustomWriter::getXML($doc));
00163 fclose($h);
00164 } else {
00165 header("Content-Type: application/xml");
00166 header("Cache-Control: private");
00167 header("Content-Disposition: attachment; filename=\"pubmed.xml\"");
00168 XMLCustomWriter::printXML($doc);
00169 }
00170 return true;
00171 }
00172
00177 function executeCLI($scriptName, &$args) {
00178
00179 $xmlFile = array_shift($args);
00180 $journalPath = array_shift($args);
00181
00182 $journalDao = &DAORegistry::getDAO('JournalDAO');
00183 $issueDao = &DAORegistry::getDAO('IssueDAO');
00184 $sectionDao = &DAORegistry::getDAO('SectionDAO');
00185 $userDao = &DAORegistry::getDAO('UserDAO');
00186 $publishedArticleDao = &DAORegistry::getDAO('PublishedArticleDAO');
00187
00188 $journal = &$journalDao->getJournalByPath($journalPath);
00189
00190 if (!$journal) {
00191 if ($journalPath != '') {
00192 echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
00193 echo Locale::translate('plugins.importexport.pubmed.error.unknownJournal', array('journalPath' => $journalPath)) . "\n\n";
00194 }
00195 $this->usage($scriptName);
00196 return;
00197 }
00198
00199 if ($xmlFile != '') switch (array_shift($args)) {
00200 case 'articles':
00201 $results = &ArticleSearch::formatResults($args);
00202 if (!$this->exportArticles($results, $xmlFile)) {
00203 echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
00204 echo Locale::translate('plugins.importexport.pubmed.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
00205 }
00206 return;
00207 case 'issue':
00208 $issueId = array_shift($args);
00209 $issue = &$issueDao->getIssueByBestIssueId($issueId, $journal->getJournalId());
00210 if ($issue == null) {
00211 echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
00212 echo Locale::translate('plugins.importexport.pubmed.export.error.issueNotFound', array('issueId' => $issueId)) . "\n\n";
00213 return;
00214 }
00215 $issues = array($issue);
00216 if (!$this->exportIssues($journal, $issues, $xmlFile)) {
00217 echo Locale::translate('plugins.importexport.pubmed.cliError') . "\n";
00218 echo Locale::translate('plugins.importexport.pubmed.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
00219 }
00220 return;
00221 }
00222 $this->usage($scriptName);
00223
00224 }
00225
00229 function usage($scriptName) {
00230 echo Locale::translate('plugins.importexport.pubmed.cliUsage', array(
00231 'scriptName' => $scriptName,
00232 'pluginName' => $this->getName()
00233 )) . "\n";
00234 }
00235 }
00236
00237 ?>