plugins/importexport/native/NativeImportExportPlugin.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00015 //$Id$
00016 
00017 import('classes.plugins.ImportExportPlugin');
00018 
00019 import('xml.XMLCustomWriter');
00020 
00021 define('NATIVE_DTD_URL', 'http://pkp.sfu.ca/ocs/dtds/native.dtd');
00022 define('NATIVE_DTD_ID', '-//PKP//OCS Papers XML//EN');
00023 
00024 class NativeImportExportPlugin extends ImportExportPlugin {
00031    function register($category, $path) {
00032       $success = parent::register($category, $path);
00033       $this->addLocaleData();
00034       return $success;
00035    }
00036 
00042    function getName() {
00043       return 'NativeImportExportPlugin';
00044    }
00045 
00046    function getDisplayName() {
00047       return __('plugins.importexport.native.displayName');
00048    }
00049 
00050    function getDescription() {
00051       return __('plugins.importexport.native.description');
00052    }
00053 
00054    function display(&$args) {
00055       $templateMgr =& TemplateManager::getManager();
00056       parent::display($args);
00057 
00058       $conference =& Request::getConference();
00059       $schedConf =& Request::getSchedConf();
00060 
00061       switch (array_shift($args)) {
00062          case 'exportPaper':
00063             $paperIds = array(array_shift($args));
00064             $result = array_shift(PaperSearch::formatResults($paperIds));
00065             $this->exportPaper($schedConf, $result['track'], $result['publishedPaper']);
00066             break;
00067          case 'exportPapers':
00068             $paperIds = Request::getUserVar('paperId');
00069             if (!isset($paperIds)) $paperIds = array();
00070             $results =& PaperSearch::formatResults($paperIds);
00071             $this->exportPapers($results);
00072             break;
00073          case 'papers':
00074             // Display a list of papers for export
00075             $this->setBreadcrumbs(array(), true);
00076             $publishedPaperDao =& DAORegistry::getDAO('PublishedPaperDAO');
00077             $rangeInfo = Handler::getRangeInfo('papers');
00078             $paperIds = $publishedPaperDao->getPublishedPaperIdsAlphabetizedBySchedConf($conference->getId(), $schedConf->getId());
00079             $totalPapers = count($paperIds);
00080             if ($rangeInfo->isValid()) $paperIds = array_slice($paperIds, $rangeInfo->getCount() * ($rangeInfo->getPage()-1), $rangeInfo->getCount());
00081             import('core.VirtualArrayIterator');
00082             $iterator = new VirtualArrayIterator(PaperSearch::formatResults($paperIds), $totalPapers, $rangeInfo->getPage(), $rangeInfo->getCount());
00083             $templateMgr->assign_by_ref('papers', $iterator);
00084             $templateMgr->display($this->getTemplatePath() . 'papers.tpl');
00085             break;
00086          case 'import':
00087             import('file.TemporaryFileManager');
00088             $trackDao =& DAORegistry::getDAO('TrackDAO');
00089             $user =& Request::getUser();
00090             $temporaryFileManager = new TemporaryFileManager();
00091 
00092             if (($existingFileId = Request::getUserVar('temporaryFileId'))) {
00093                // The user has just entered more context. Fetch an existing file.
00094                $temporaryFile = TemporaryFileManager::getFile($existingFileId, $user->getId());
00095             } else {
00096                $temporaryFile = $temporaryFileManager->handleUpload('importFile', $user->getId());
00097             }
00098 
00099             $context = array(
00100                'conference' => $conference,
00101                'schedConf' => $schedConf,
00102                'user' => $user
00103             );
00104 
00105             if (($trackId = Request::getUserVar('trackId'))) {
00106                $context['track'] = $trackDao->getTrack($trackId);
00107             }
00108 
00109             if (!$temporaryFile) {
00110                $templateMgr->assign('error', 'plugins.importexport.native.error.uploadFailed');
00111                return $templateMgr->display($this->getTemplatePath() . 'importError.tpl');
00112             }
00113  
00114             $doc =& $this->getDocument($temporaryFile->getFilePath());
00115 
00116             if (substr($this->getRootNodeName($doc), 0, 5) === 'paper') {
00117                // Ensure the user has supplied enough valid information to
00118                // import papers within an appropriate context. If not,
00119                // prompt them for the.
00120                if (!isset($context['track'])) {
00121                   AppLocale::requireComponents(array(LOCALE_COMPONENT_OCS_AUTHOR));
00122                   $templateMgr->assign('trackOptions', array('0' => __('author.submit.selectTrack')) + $trackDao->getTrackTitles($schedConf->getId(), false));
00123                   $templateMgr->assign('temporaryFileId', $temporaryFile->getId());
00124                   return $templateMgr->display($this->getTemplatePath() . 'paperContext.tpl');
00125                }
00126             }
00127 
00128             @set_time_limit(0);
00129 
00130             if ($this->handleImport($context, $doc, $errors, $papers, false)) {
00131                $templateMgr->assign_by_ref('papers', $papers);
00132                return $templateMgr->display($this->getTemplatePath() . 'importSuccess.tpl');
00133             } else {
00134                $templateMgr->assign_by_ref('errors', $errors);
00135                return $templateMgr->display($this->getTemplatePath() . 'importError.tpl');
00136             }
00137             break;
00138          default:
00139             $this->setBreadcrumbs();
00140             $templateMgr->display($this->getTemplatePath() . 'index.tpl');
00141       }
00142    }
00143 
00144    function exportPaper(&$schedConf, &$track, &$paper, $outputFile = null) {
00145       $this->import('NativeExportDom');
00146       $doc =& XMLCustomWriter::createDocument('paper', NATIVE_DTD_ID, NATIVE_DTD_URL);
00147       $paperNode =& NativeExportDom::generatePaperDom($doc, $schedConf, $track, $paper);
00148       XMLCustomWriter::appendChild($doc, $paperNode);
00149 
00150       if (!empty($outputFile)) {
00151          if (($h = fopen($outputFile, 'w'))===false) return false;
00152          fwrite($h, XMLCustomWriter::getXML($doc));
00153          fclose($h);
00154       } else {
00155          header("Content-Type: application/xml");
00156          header("Cache-Control: private");
00157          header("Content-Disposition: attachment; filename=\"paper-" . $paper->getId() . ".xml\"");
00158          XMLCustomWriter::printXML($doc);
00159       }
00160       return true;
00161    }
00162 
00163    function exportPapers(&$results, $outputFile = null) {
00164       $this->import('NativeExportDom');
00165       $doc =& XMLCustomWriter::createDocument('papers', NATIVE_DTD_ID, NATIVE_DTD_URL);
00166       $papersNode =& XMLCustomWriter::createElement($doc, 'papers');
00167       XMLCustomWriter::appendChild($doc, $papersNode);
00168 
00169       foreach ($results as $result) {
00170          $paper =& $result['publishedPaper'];
00171          $track =& $result['track'];
00172          $conference =& $result['conference'];
00173          $schedConf =& $result['schedConf'];
00174          $paperNode =& NativeExportDom::generatePaperDom($doc, $schedConf, $track, $paper);
00175          XMLCustomWriter::appendChild($papersNode, $paperNode);
00176       }
00177 
00178       if (!empty($outputFile)) {
00179          if (($h = fopen($outputFile, 'w'))===false) return false;
00180          fwrite($h, XMLCustomWriter::getXML($doc));
00181          fclose($h);
00182       } else {
00183          header("Content-Type: application/xml");
00184          header("Cache-Control: private");
00185          header("Content-Disposition: attachment; filename=\"papers.xml\"");
00186          XMLCustomWriter::printXML($doc);
00187       }
00188       return true;
00189    }
00190 
00191    function &getDocument($fileName) {
00192       $parser = new XMLParser();
00193       $returner =& $parser->parse($fileName);
00194       return $returner;
00195    }
00196 
00197    function getRootNodeName(&$doc) {
00198       return $doc->name;
00199    }
00200 
00201    function handleImport(&$context, &$doc, &$errors, &$papers, $isCommandLine) {
00202       $errors = array();
00203       $papers = array();
00204 
00205       $user =& $context['user'];
00206       $conference =& $context['conference'];
00207       $schedConf =& $context['schedConf'];
00208 
00209       $rootNodeName = $this->getRootNodeName($doc);
00210 
00211       $this->import('NativeImportDom');
00212 
00213       switch ($rootNodeName) {
00214          case 'papers':
00215             $track =& $context['track'];
00216             return NativeImportDom::importPapers($conference, $schedConf, $doc->children, $track, $papers, $errors, $user, $isCommandLine);
00217             break;
00218          case 'paper':
00219             $track =& $context['track'];
00220             $result = NativeImportDom::importPaper($conference, $schedConf, $doc, $track, $paper, $errors, $user, $isCommandLine);
00221             if ($result) $papers = array($paper);
00222             return $result;
00223             break;
00224          default:
00225             $errors[] = array('plugins.importexport.native.import.error.unsupportedRoot', array('rootName' => $rootNodeName));
00226             return false;
00227             break;
00228       }
00229    }
00230 
00235    function executeCLI($scriptName, &$args) {
00236       $command = array_shift($args);
00237       $xmlFile = array_shift($args);
00238       $conferencePath = array_shift($args);
00239       $schedConfPath = array_shift($args);
00240 
00241       $conferenceDao =& DAORegistry::getDAO('ConferenceDAO');
00242       $schedConfDao =& DAORegistry::getDAO('SchedConfDAO');
00243       $trackDao =& DAORegistry::getDAO('TrackDAO');
00244       $userDao =& DAORegistry::getDAO('UserDAO');
00245       $publishedPaperDao =& DAORegistry::getDAO('PublishedPaperDAO');
00246 
00247       $conference =& $conferenceDao->getConferenceByPath($conferencePath);
00248       if ($conference) $schedConf =& $schedConfDao->getSchedConfByPath($schedConfPath, $conference->getId());
00249 
00250       if (!$conference || !$schedConfPath) {
00251          if ($conferencePath != '') {
00252             echo __('plugins.importexport.native.cliError') . "\n";
00253             echo __('plugins.importexport.native.error.unknownConference', array('conferencePath' => $conferencePath, 'schedConfPath' => $schedConfPath)) . "\n\n";
00254          }
00255          $this->usage($scriptName);
00256          return;
00257       }
00258 
00259       $this->import('NativeImportDom');
00260       if ($xmlFile && NativeImportDom::isRelativePath($xmlFile)) {
00261          $xmlFile = PWD . '/' . $xmlFile;
00262       }
00263 
00264       switch ($command) {
00265          case 'import':
00266             $userName = array_shift($args);
00267             $user =& $userDao->getUserByUsername($userName);
00268 
00269             if (!$user) {
00270                if ($userName != '') {
00271                   echo __('plugins.importexport.native.cliError') . "\n";
00272                   echo __('plugins.importexport.native.error.unknownUser', array('userName' => $userName)) . "\n\n";
00273                }
00274                $this->usage($scriptName);
00275                return;
00276             }
00277 
00278             $doc =& $this->getDocument($xmlFile);
00279 
00280             $context = array(
00281                'user' => $user,
00282                'conference' => $conference,
00283                'schedConf' => $schedConf
00284             );
00285 
00286             switch ($this->getRootNodeName($doc)) {
00287                case 'paper':
00288                case 'papers':
00289                   // Determine the extra context information required
00290                   // for importing papers.
00291                   switch (array_shift($args)) {
00292                      case 'track_id':
00293                         $track =& $trackDao->getTrack(($trackIdentifier = array_shift($args)));
00294                         break;
00295                      case 'track_name':
00296                         $track =& $trackDao->getTrackByTitle(($trackIdentifier = array_shift($args)), $schedConf->getId());
00297                         break;
00298                      case 'track_abbrev':
00299                         $track =& $trackDao->getTrackByAbbrev(($trackIdentifier = array_shift($args)), $schedConf->getId());
00300                         break;
00301                      default:
00302                         return $this->usage($scriptName);
00303                   }
00304 
00305                   if (!$track) {
00306                      echo __('plugins.importexport.native.cliError') . "\n";
00307                      echo __('plugins.importexport.native.export.error.trackNotFound', array('trackIdentifier' => $trackIdentifier)) . "\n\n";
00308                      return;
00309                   }
00310                   $context['track'] =& $track;
00311             }
00312 
00313             $result = $this->handleImport($context, $doc, $errors, $papers, true);
00314             if ($result) {
00315                echo __('plugins.importexport.native.import.success.description') . "\n\n";
00316                if (!empty($papers)) echo __('paper.papers') . ":\n";
00317                foreach ($papers as $paper) {
00318                   echo "\t" . $paper->getLocalizedTitle() . "\n";
00319                }
00320             } else {
00321                echo __('plugins.importexport.native.cliError') . "\n";
00322                foreach ($errors as $error) {
00323                   echo "\t" . __($error[0], $error[1]) . "\n";
00324                }
00325             }
00326             return;
00327             break;
00328          case 'export':
00329             if ($xmlFile != '') switch (array_shift($args)) {
00330                case 'paper':
00331                   $paperId = array_shift($args);
00332                   $publishedPaper =& $publishedPaperDao->getPublishedPaperByBestPaperId($schedConf->getId(), $paperId);
00333                   if ($publishedPaper == null) {
00334                      echo __('plugins.importexport.native.cliError') . "\n";
00335                      echo __('plugins.importexport.native.export.error.paperNotFound', array('paperId' => $paperId)) . "\n\n";
00336                      return;
00337                   }
00338 
00339                   $trackDao =& DAORegistry::getDAO('TrackDAO');
00340                   $track =& $trackDao->getTrack($publishedPaper->getTrackId());
00341 
00342                   if (!$this->exportPaper($schedConf, $track, $publishedPaper, $xmlFile)) {
00343                      echo __('plugins.importexport.native.cliError') . "\n";
00344                      echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
00345                   }
00346                   return;
00347                case 'papers':
00348                   $results =& PaperSearch::formatResults($args);
00349                   if (!$this->exportPapers($results, $xmlFile)) {
00350                      echo __('plugins.importexport.native.cliError') . "\n";
00351                      echo __('plugins.importexport.native.export.error.couldNotWrite', array('fileName' => $xmlFile)) . "\n\n";
00352                   }
00353                   return;
00354             }
00355             break;
00356       }
00357       $this->usage($scriptName);
00358    }
00359 
00363    function usage($scriptName) {
00364       echo __('plugins.importexport.native.cliUsage', array(
00365          'scriptName' => $scriptName,
00366          'pluginName' => $this->getName()
00367       )) . "\n";
00368    }
00369 }
00370 
00371 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7