/**
  * @see WebService::call()
  * @param $webServiceRequest WebServiceRequest
  * @return DOMDocument|string the result of the web service or null in case of an error.
  */
 function &call(&$webServiceRequest)
 {
     // Call the web service
     $xmlResult = parent::call($webServiceRequest);
     // Catch web service errors
     if (is_null($xmlResult)) {
         return $xmlResult;
     }
     if ($this->_lastResponseStatus >= 400 && $this->_lastResponseStatus <= 599) {
         $nullVar = null;
         return $nullVar;
     }
     switch ($this->_returnType) {
         case XSL_TRANSFORMER_DOCTYPE_DOM:
             // Create DOM document
             $resultDOM = new DOMDocument('1.0', Config::getVar('i18n', 'client_charset'));
             // Try to handle non-well-formed responses
             $resultDOM->recover = true;
             $resultDOM->loadXML($xmlResult);
             return $resultDOM;
         case XSL_TRANSFORMER_DOCTYPE_STRING:
             return $xmlResult;
         default:
             assert(false);
     }
 }
 /**
  * @see WebService::call()
  * @param $webServiceRequest WebServiceRequest
  * @return DOMDocument|string the result of the web service or null in case of an error.
  */
 function &call(&$webServiceRequest)
 {
     // Call the web service
     $xmlResult = parent::call($webServiceRequest);
     if (Config::getVar('debug', 'log_web_service_info')) {
         error_log('Time: ' . date('c') . "\nRequest: " . print_r($webServiceRequest, true) . "\nResponse: " . print_r($xmlResult, true) . "\nLast response status: " . $this->_lastResponseStatus . "\n");
     }
     // Catch web service errors
     if (is_null($xmlResult)) {
         return $xmlResult;
     }
     switch ($this->_returnType) {
         case XSL_TRANSFORMER_DOCTYPE_DOM:
             // Create DOM document
             $resultDOM = new DOMDocument('1.0', Config::getVar('i18n', 'client_charset'));
             // Try to handle non-well-formed responses
             $resultDOM->recover = true;
             $resultDOM->loadXML($xmlResult);
             return $resultDOM;
         case XSL_TRANSFORMER_DOCTYPE_STRING:
             return $xmlResult;
         default:
             assert(false);
     }
 }
 /**
  * @see WebService::call()
  * @param $webServiceRequest WebServiceRequest
  * @return array The result of the web service or null in case of an error.
  */
 function &call(&$webServiceRequest)
 {
     // Call the web service
     $jsonResult = parent::call($webServiceRequest);
     // Catch web service errors
     if (is_null($jsonResult)) {
         return $jsonResult;
     }
     $resultArray = json_decode($jsonResult, true);
     // Catch decoding errors.
     if (!is_array($resultArray)) {
         return null;
     }
     return $resultArray;
 }
 /**
  *
  * @param $journal Journal
  * @param $articles array
  * @return boolean
  */
 function _exportArticles($journal, &$articles)
 {
     if (!isset($articles) || count($articles) == 0) {
         return false;
     }
     $plugin = $this->_plugin;
     $journalPath = $journal->getPath();
     $journalId = $journal->getId();
     $payload = '';
     foreach ($articles as $article) {
         $doi = $article->getPubId('doi');
         $publishedDate = date('Y-m-d', strtotime($article->getDatePublished()));
         $title = preg_replace('/s+/', ' ', $article->getLocalizedTitle());
         if ($doi && $publishedDate && $title) {
             $payload .= "{$doi} {$publishedDate} {$title}\n";
         }
     }
     $depositUrl = $this->_depositUrl;
     $apiKey = $plugin->getSetting($journalId, 'apiKey');
     if (!$apiKey) {
         $this->addExecutionLogEntry(__('plugins.generic.alm.senderTask.warning.noApiKey', array('path' => $journalPath)), SCHEDULED_TASK_MESSAGE_TYPE_WARNING);
     }
     $params = array('api_key' => $apiKey, 'payload' => $payload);
     $jsonManager = new JSONManager();
     if ($payload && $depositUrl && $apiKey) {
         $webServiceRequest = new WebServiceRequest($depositUrl, $params, 'POST');
         // Configure and call the web service
         $webService = new WebService();
         $result = $webService->call($webServiceRequest);
         if ($result) {
             $resultDecoded = $jsonManager->decode($result);
         }
         if (is_null($result)) {
             $this->addExecutionLogEntry(__('plugins.generic.alm.senderTask.error.noServerResponse', array('path' => $journalPath)), SCHEDULED_TASK_MESSAGE_TYPE_ERROR);
         }
         if ($resultDecoded && isset($resultDecoded->success) && isset($resultDecoded->count) && $resultDecoded->count == count($articles)) {
             return true;
         } else {
             $this->addExecutionLogEntry(__('plugins.generic.alm.senderTask.error.returnError', array('error' => $result, 'articlesNumber' => count($articles), 'payload' => $payload)), SCHEDULED_TASK_MESSAGE_TYPE_ERROR);
         }
     }
     return false;
 }
 /**
  * @see DOIExportPlugin::registerDoi()
  */
 function registerDoi(&$request, &$journal, &$object, $filename)
 {
     $shoulder = $this->getSetting($journal->getId(), 'shoulder');
     // Transmit CrossRef XML metadata.
     assert(is_readable($filename));
     $payload = file_get_contents($filename);
     assert($payload !== false && !empty($payload));
     // we only consider articles and issues here
     $result = true;
     if (is_a($object, 'PublishedArticle') || is_a($object, 'Issue')) {
         $input = "_profile: crossref" . PHP_EOL;
         $input .= "crossref: " . $this->_doiMetadataEscape($payload) . PHP_EOL;
         // TODO: SHOW BOTH DATACITE METADATA AS WELL
         //5 required datacite fields:
         $input .= "datacite.creator: ";
         if (is_a($object, 'PublishedArticle')) {
             foreach ($object->getAuthors() as $author) {
                 $input .= $author->getLastName() . ", " . $author->getFirstName() . " " . $author->getMiddleName() . "; ";
             }
         }
         $input .= PHP_EOL;
         $input .= "datacite.title: " . $object->getLocalizedTitle() . PHP_EOL;
         $input .= "datacite.publisher: " . $journal->getSetting('publisherInstitution') . PHP_EOL;
         $input .= "datacite.publicationyear: " . date('Y', strtotime($object->getDatePublished())) . PHP_EOL;
         $input .= "datacite.resourcetype: " . $object->getLocalizedData('type') . PHP_EOL;
         if ($object->getData('ezid::registeredDoi')) {
             $webServiceRequest = new WebServiceRequest(EZID_API_CRUD_URL . $object->getData('ezid::registeredDoi'), $input, 'POST');
             $expectedResponse = EZID_API_RESPONSE_OK;
         } else {
             $webServiceRequest = new WebServiceRequest(EZID_API_MINT_URL . $shoulder, $input, 'POST');
             $expectedResponse = EZID_API_RESPONSE_CREATED;
         }
         $webServiceRequest->setHeader('Content-Type', 'text/plain; charset=UTF-8');
         $webServiceRequest->setHeader('Content-Length', strlen($input));
         $webService = new WebService();
         $username = $this->getSetting($journal->getId(), 'username');
         $password = $this->getSetting($journal->getId(), 'password');
         $webService->setAuthUsername($username);
         $webService->setAuthPassword($password);
         $response =& $webService->call($webServiceRequest);
         if ($response === false) {
             $result = array(array('plugins.importexport.common.register.error.mdsError', __('plugins.importexport.ezid.error.webserviceNoResponse')));
         } else {
             if ($response === NULL) {
                 $result = array(array('plugins.importexport.ezid.error.webserviceInvalidRequest'));
             } else {
                 $status = $webService->getLastResponseStatus();
                 if ($status != $expectedResponse) {
                     $result = array(array('plugins.importexport.common.register.error.mdsError', "{$status} - " . htmlentities($response)));
                 }
             }
         }
     } else {
         return false;
     }
     if ($result === true) {
         # trim off "success: doi:"
         $trimmed_body = preg_replace('/(success: doi:)/', '', $response);
         if (strstr($trimmed_body, ' | ark:') !== FALSE) {
             list($doi, $ark) = explode(' | ark:', $trimmed_body, 2);
             $ark = 'ark:' . $ark;
         } else {
             $doi = $trimmed_body;
             $ark = '';
         }
         if (is_a($object, 'Issue')) {
             $dao =& DAORegistry::getDAO('IssueDAO');
             $dao->changePubId($object->getId(), 'doi', $doi);
             $object->setStoredPubId('doi', $doi);
         } elseif (is_a($object, 'Article')) {
             $dao =& DAORegistry::getDAO('ArticleDAO');
             $dao->changePubId($object->getId(), 'doi', $doi);
             $object->setStoredPubId('doi', $doi);
         }
         // Mark the object as registered.
         $this->markRegistered($request, $object, $shoulder);
     }
     return $result;
 }
Example #6
0
 /**
  * Call web service with the given parameters
  * @param $url string
  * @param $params array GET or POST parameters
  * @param $method string (optional)
  * @return JSON or null in case of error
  */
 function &_callWebService($url, &$params, $method = 'GET')
 {
     // Create a request
     if (!is_array($params)) {
         $params = array();
     }
     $params['api_key'] = $this->_apiKey;
     $webServiceRequest = new WebServiceRequest($url, $params, $method);
     // Can't strip slashes from the result, we have a JSON
     // response with escaped characters.
     $webServiceRequest->setCleanResult(false);
     // Configure and call the web service
     $webService = new WebService();
     $result =& $webService->call($webServiceRequest);
     return $result;
 }