16 import(
'classes.plugins.DOIPubIdExportPlugin');
19 define(
'DATACITE_API_RESPONSE_OK', 201);
20 define(
'DATACITE_API_URL',
'https://mds.datacite.org/');
23 define(
'DATACITE_API_TESTPREFIX',
'10.5072');
26 define(
'DATACITE_EXPORT_FILE_XML', 0x01);
27 define(
'DATACITE_EXPORT_FILE_TAR', 0x02);
36 return 'DataciteExportPlugin';
43 return __(
'plugins.importexport.datacite.displayName');
50 return __(
'plugins.importexport.datacite.description');
57 return 'article=>datacite-xml';
64 return 'issue=>datacite-xml';
71 return 'galley=>datacite-xml';
85 return 'DataciteSettingsForm';
92 return 'DataciteExportDeployment';
100 $path = array(
'plugin', $this->
getName());
102 import(
'lib.pkp.classes.file.FileManager');
106 if (
$request->getUserVar(EXPORT_ACTION_EXPORT)) {
108 if ($result ===
true) {
109 $exportedFiles = array();
110 foreach ($objects as $object) {
112 $exportXml = $this->
exportXML($object, $filter, $context, $noValidation);
115 $objectFileNamePart = $objectsFileNamePart .
'-' . $object->getId();
117 $fileManager->writeFile($exportFileName, $exportXml);
118 $exportedFiles[] = $exportFileName;
122 assert(count($exportedFiles) >= 1);
123 if (count($exportedFiles) > 1) {
128 foreach ($exportedFiles as $exportedFile) {
129 $fileManager->deleteByPath($exportedFile);
132 $finalExportFileName = array_shift($exportedFiles);
134 $fileManager->downloadByPath($finalExportFileName);
135 $fileManager->deleteByPath($finalExportFileName);
137 if (is_array($result)) {
138 foreach($result as $error) {
139 assert(is_array($error) && count($error) >= 1);
143 NOTIFICATION_TYPE_ERROR,
144 (isset($error[1]) ? $error[1] :
null)
149 $request->redirect(
null,
null,
null, $path,
null, $tab);
151 } elseif (
$request->getUserVar(EXPORT_ACTION_DEPOSIT)) {
152 $resultErrors = array();
153 foreach ($objects as $object) {
155 $exportXml = $this->
exportXML($object, $filter, $context, $noValidation);
158 $objectFileNamePart = $objectsFileNamePart .
'-' . $object->getId();
160 $fileManager->writeFile($exportFileName, $exportXml);
162 $result = $this->
depositXML($object, $context, $exportFileName);
163 if (is_array($result)) {
164 $resultErrors[] = $result;
167 $fileManager->deleteByPath($exportFileName);
170 if (empty($resultErrors)) {
173 $this->getDepositSuccessNotificationMessageKey(),
174 NOTIFICATION_TYPE_SUCCESS
177 foreach($resultErrors as $errors) {
178 foreach ($errors as $error) {
179 assert(is_array($error) && count($error) >= 1);
183 NOTIFICATION_TYPE_ERROR,
184 (isset($error[1]) ? $error[1] :
null)
190 $request->redirect(
null,
null,
null, $path,
null, $tab);
192 return parent::executeExportAction(
$request, $objects, $filter, $tab, $objectsFileNamePart);
202 $doi = $object->getStoredPubId(
'doi');
203 assert(!empty($doi));
208 assert(!empty($url));
211 import(
'lib.pkp.classes.helpers.PKPCurlHelper');
214 curl_setopt($curlCh, CURLOPT_RETURNTRANSFER,
true);
215 curl_setopt($curlCh, CURLOPT_POST,
true);
217 $username = $this->
getSetting($context->getId(),
'username');
218 $password = $this->
getSetting($context->getId(),
'password');
219 curl_setopt($curlCh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
220 curl_setopt($curlCh, CURLOPT_USERPWD,
"$username:$password");
222 assert(is_readable($filename));
223 $payload = file_get_contents($filename);
224 assert($payload !==
false && !empty($payload));
225 curl_setopt($curlCh, CURLOPT_URL, DATACITE_API_URL .
'metadata');
226 curl_setopt($curlCh, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml;charset=UTF-8'));
227 curl_setopt($curlCh, CURLOPT_POSTFIELDS, $payload);
229 $response = curl_exec($curlCh);
230 if ($response ===
false) {
231 $result = array(array(
'plugins.importexport.common.register.error.mdsError',
"Registering DOI $doi: No response from server."));
233 $status = curl_getinfo($curlCh, CURLINFO_HTTP_CODE);
234 if ($status != DATACITE_API_RESPONSE_OK) {
235 $result = array(array(
'plugins.importexport.common.register.error.mdsError',
"Registering DOI $doi: $status - $response"));
239 if ($result ===
true) {
240 $payload =
"doi=$doi\nurl=$url";
241 curl_setopt($curlCh, CURLOPT_URL, DATACITE_API_URL .
'doi');
242 curl_setopt($curlCh, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain;charset=UTF-8'));
243 curl_setopt($curlCh, CURLOPT_POSTFIELDS, $payload);
244 $response = curl_exec($curlCh);
245 if ($response ===
false) {
246 $result = array(array(
'plugins.importexport.common.register.error.mdsError',
'Registering DOI $doi: No response from server.'));
248 $status = curl_getinfo($curlCh, CURLINFO_HTTP_CODE);
249 if ($status != DATACITE_API_RESPONSE_OK) {
250 $result = array(array(
'plugins.importexport.common.register.error.mdsError',
"Registering DOI $doi: $status - $response"));
255 if ($result ===
true) {
265 function executeCLICommand($scriptName, $command, $context, $outputFile, $objects, $filter, $objectsFileNamePart) {
266 import(
'lib.pkp.classes.file.FileManager');
271 if ($result ===
true) {
272 $exportedFiles = array();
273 foreach ($objects as $object) {
275 $exportXml = $this->
exportXML($object, $filter, $context);
278 $objectFileNamePart = $objectsFileNamePart .
'-' . $object->getId();
280 $fileManager->writeFile($exportFileName, $exportXml);
281 $exportedFiles[] = $exportFileName;
285 assert(count($exportedFiles) >= 1);
286 if (count($exportedFiles) > 1) {
289 $finalExportFileType = DATACITE_EXPORT_FILE_TAR;
292 $finalExportFileName = array_shift($exportedFiles);
293 $finalExportFileType = DATACITE_EXPORT_FILE_XML;
295 $outputFileExtension = ($finalExportFileType == DATACITE_EXPORT_FILE_TAR ?
'.tar.gz' :
'.xml');
296 if (substr($outputFile , -strlen($outputFileExtension)) != $outputFileExtension) {
297 $outputFile .= $outputFileExtension;
299 $fileManager->copyFile($finalExportFileName, $outputFile);
300 foreach ($exportedFiles as $exportedFile) {
301 $fileManager->deleteByPath($exportedFile);
303 $fileManager->deleteByPath($finalExportFileName);
305 echo __(
'plugins.importexport.common.cliError') .
"\n";
306 echo __(
'manager.plugins.tarCommandNotFound') .
"\n\n";
307 $this->
usage($scriptName);
311 $resultErrors = array();
312 foreach ($objects as $object) {
314 $exportXml = $this->
exportXML($object, $filter, $context);
317 $objectFileNamePart = $objectsFileNamePart .
'-' . $object->getId();
319 $fileManager->writeFile($exportFileName, $exportXml);
321 $result = $this->
depositXML($object, $context, $exportFileName);
322 if (is_array($result)) {
323 $resultErrors[] = $result;
326 $fileManager->deleteByPath($exportFileName);
328 if (empty($resultErrors)) {
329 echo __(
'plugins.importexport.common.register.success') .
"\n";
331 echo __(
'plugins.importexport.common.cliError') .
"\n";
332 foreach($resultErrors as $errors) {
333 foreach ($errors as $error) {
334 assert(is_array($error) && count($error) >= 1);
335 $errorMessage = __($error[0], array(
'param' => (isset($error[1]) ? $error[1] :
null)));
336 echo
"*** $errorMessage\n";
340 $this->
usage($scriptName);
353 if (empty($tarBinary) || !is_executable($tarBinary)) {
355 array(
'manager.plugins.tarCommandNotFound')
369 function _tarFiles($targetPath, $targetFile, $sourceFiles) {
372 $tarCommand =
Config::getVar(
'cli',
'tar') .
' -czf ' . escapeshellarg($targetFile);
374 $tarCommand .=
' -C ' . escapeshellarg($targetPath);
376 $tarCommand .=
' --owner 0 --group 0 --';
379 foreach($sourceFiles as $sourceFile) {
380 assert(dirname($sourceFile) .
'/' === $targetPath);
381 if (dirname($sourceFile) .
'/' !== $targetPath)
continue;
382 $tarCommand .=
' ' . escapeshellarg(basename($sourceFile));
397 if (is_a($object,
'ArticleGalley')) {
398 $articleId = $object->getSubmissionId();
400 if ($cache->isCached(
'articles', $articleId)) {
401 $article = $cache->get(
'articles', $articleId);
405 assert(is_a($article,
'Submission'));
409 case is_a($object,
'Issue'):
410 $url = $router->url(
$request, $context->getPath(),
'issue',
'view', $object->getBestIssueId(),
null,
null,
true);
412 case is_a($object,
'Submission'):
413 $url = $router->url(
$request, $context->getPath(),
'article',
'view', $object->getBestId(),
null,
null,
true);
415 case is_a($object,
'ArticleGalley'):
416 $url = $router->url(
$request, $context->getPath(),
'article',
'view', array($article->getBestId(), $object->getBestGalleyId()),
null,
null,
true);