forked from pkp/orcidProfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrcidProfilePlugin.inc.php
1194 lines (1125 loc) · 41.1 KB
/
OrcidProfilePlugin.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file plugins/generic/orcidProfile/OrcidProfilePlugin.inc.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2019 Simon Fraser University
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class OrcidProfilePlugin
* @ingroup plugins_generic_orcidProfile
*
* @brief ORCID Profile plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
define('ORCID_URL', 'https://orcid.org/');
define('ORCID_URL_SANDBOX', 'https://sandbox.orcid.org/');
define('ORCID_API_URL_PUBLIC', 'https://pub.orcid.org/');
define('ORCID_API_URL_PUBLIC_SANDBOX', 'https://pub.sandbox.orcid.org/');
define('ORCID_API_URL_MEMBER', 'https://api.orcid.org/');
define('ORCID_API_URL_MEMBER_SANDBOX', 'https://api.sandbox.orcid.org/');
define('ORCID_API_VERSION_URL', 'v2.1/');
define('ORCID_API_SCOPE_PUBLIC', '/authenticate');
define('ORCID_API_SCOPE_MEMBER', '/activities/update');
define('OAUTH_TOKEN_URL', 'oauth/token');
define('ORCID_EMPLOYMENTS_URL', 'employments');
define('ORCID_PROFILE_URL', 'person');
define('ORCID_EMAIL_URL', 'email');
define('ORCID_WORK_URL', 'work');
class OrcidProfilePlugin extends GenericPlugin {
const PUBID_TO_ORCID_EXT_ID = [ "doi" => "doi", "other::urn" => "urn"];
const USERGROUP_TO_ORCID_ROLE = [ "Author" => "AUTHOR", "Translator" => "CHAIR_OR_TRANSLATOR"];
private $submissionIdToBePublished;
private $currentContextId;
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled($mainContextId)) {
// Register callback for Smarty filters; add CSS
HookRegistry::register('TemplateManager::display', array($this, 'handleTemplateDisplay'));
// Add "Connect ORCID" button to PublicProfileForm
HookRegistry::register('User::PublicProfile::AdditionalItems', array($this, 'handleUserPublicProfileDisplay'));
// Display additional ORCID access information and checkbox to send e-mail to authors in the AuthorForm
HookRegistry::register('authorform::display', array($this, 'handleFormDisplay'));
// Send email to author, if the added checkbox was ticked
HookRegistry::register('authorform::execute', array($this, 'handleAuthorFormExecute'));
// Insert the OrcidHandler to handle ORCID redirects
HookRegistry::register('LoadHandler', array($this, 'setupCallbackHandler'));
// Handle ORCID on user registration
HookRegistry::register('registrationform::execute', array($this, 'collectUserOrcidId'));
// Send emails to authors without ORCID id upon submission
HookRegistry::register('submissionsubmitstep3form::execute', array($this, 'handleSubmissionSubmitStep3FormExecute'));
// Add more ORCiD fields to author DAO
HookRegistry::register('authordao::getAdditionalFieldNames', array($this, 'handleAdditionalFieldNames'));
// Add more ORCiD fields to UserDAO
HookRegistry::register('userdao::getAdditionalFieldNames', array($this, 'handleAdditionalFieldNames'));
// Send submission meta data upload to ORCID profiles on publication of an issue
HookRegistry::register('IssueGridHandler::publishIssue', array($this, 'handlePublishIssue'));
HookRegistry::register('issueentrypublicationmetadataform::execute', array($this, 'handleScheduleForPublication'));
// Send emails to authors without authorised ORCID access on promoting a submission to production
HookRegistry::register('EditorAction::recordDecision', array($this, 'handleEditorAction'));
}
return $success;
}
/**
* Get page handler path for this plugin.
* @return string Path to plugin's page handler
*/
function getHandlerPath() {
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'pages';
}
/**
* Hook callback: register pages for each sushi-lite method
* This URL is of the form: orcidapi/{$orcidrequest}
* @see PKPPageRouter::route()
*/
function setupCallbackHandler($hookName, $params) {
$page = $params[0];
if ($this->getEnabled() && $page == 'orcidapi') {
$this->import('pages/OrcidHandler');
define('HANDLER_CLASS', 'OrcidHandler');
return true;
}
return false;
}
/**
* Load a setting for a specific journal or load it from the config.inc.php if it is specified there.
*
* @param $contextId int The id of the journal from which the plugin settings should be loaded.
* @param $name string Name of the setting.
* @return mixed The setting value, either from the database for this context
* or from the global configuration file.
*/
function getSetting($contextId, $name)
{
switch ($name) {
case 'orcidProfileAPIPath':
$config_value = Config::getVar('orcid','api_url');
break;
case 'orcidClientId':
$config_value = Config::getVar('orcid','client_id');
break;
case 'orcidClientSecret':
$config_value = Config::getVar('orcid','client_secret');
break;
default:
return parent::getSetting($contextId, $name);
}
return $config_value ?: parent::getSetting($contextId, $name);
}
/**
* Check if there exist a valid orcid configuration section in the global config.inc.php of OJS.
* @return boolean True, if the config file has api_url, client_id and client_secret set in an [orcid] section
*/
function isGloballyConfigured() {
$apiUrl = Config::getVar('orcid','api_url');
$clientId = Config::getVar('orcid','client_id');
$clientSecret = Config::getVar('orcid','client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}
/**
* Hook callback to handle form display.
* Registers output filter for public user profile and author form.
*
* @see Form::display()
*
* @param $hookName string
* @param $args Form[]
*
* @return bool
*/
function handleFormDisplay($hookName, $args) {
$request = PKPApplication::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
switch ($hookName) {
case 'authorform::display':
$authorForm =& $args[0];
$author = $authorForm->getAuthor();
if ($author) {
$authenticated = !empty($author->getData('orcidAccessToken'));
$templateMgr->assign( array(
'orcidAccessToken' => $author->getData('orcidAccessToken'),
'orcidAccessScope' => $author->getData('orcidAccessScope'),
'orcidAccessExpiresOn' => $author->getData('orcidAccessExpiresOn'),
'orcidAccessDenied' => $author->getData('orcidAccessDenied'),
'orcidAuthenticated' => $authenticated
));
}
$templateMgr->registerFilter("output", array($this, 'authorFormFilter'));
break;
}
return false;
}
/**
* Hook callback: register output filter for user registration and article display.
*
* @see TemplateManager::display()
*
* @param $hookName string
* @param $args array
* @return bool
*/
function handleTemplateDisplay($hookName, $args) {
$templateMgr =& $args[0];
$template =& $args[1];
$request = PKPApplication::get()->getRequest();
// Assign our private stylesheet, for front and back ends.
$templateMgr->addStyleSheet(
'orcidProfile',
$request->getBaseUrl() . '/' . $this->getStyleSheet(),
array(
'contexts' => array('frontend', 'backend')
)
);
switch ($template) {
case 'frontend/pages/userRegister.tpl':
$templateMgr->registerFilter("output", array($this, 'registrationFilter'));
break;
case 'frontend/pages/article.tpl':
//$templateMgr->assign('orcidIcon', $this->getIcon());
$script = 'var orcidIconSvg = $('. json_encode($this->getIcon()) .');';
$article = $templateMgr->getTemplateVars('article');
$authors = $article->getAuthors();
foreach ($authors as $author) {
if(!empty($author->getOrcid()) && !empty($author->getData('orcidAccessToken'))) {
$script .= '$("a[href=\"'.$author->getOrcid().'\"]").prepend(orcidIconSvg);';
}
}
$templateMgr->addJavaScript('orcidIconDisplay', $script, ['inline' => true]);
break;
}
return false;
}
/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
*
* @return string
*/
function getOauthPath() {
return $this->getOrcidUrl() . 'oauth/';
}
/**
* Return the ORCID website url (prod or sandbox) based on the current API configuration
*
* @return string
*/
function getOrcidUrl() {
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$apiPath = $this->getSetting($contextId, 'orcidProfileAPIPath');
if ($apiPath == ORCID_API_URL_PUBLIC || $apiPath == ORCID_API_URL_MEMBER) {
return ORCID_URL;
} else {
return ORCID_URL_SANDBOX;
}
}
/**
* Return an ORCID OAuth authorization link with
*
* @param $handlerMethod string containting a valid method of the OrcidHandler
* @param $redirectParams Array associative array with additional request parameters for the redirect URL
*/
function buildOAuthUrl($handlerMethod, $redirectParams) {
$request = PKPApplication::get()->getRequest();
$context = $request->getContext();
// This should only ever happen within a context, never site-wide.
assert($context != null);
$contextId = $context->getId();
if ( $this->isMemberApiEnabled($contextId) ) {
$scope = ORCID_API_SCOPE_MEMBER;
}
else {
$scope = ORCID_API_SCOPE_PUBLIC;
}
// We need to construct a page url, but the request is using the component router.
// Use the Dispatcher to construct the url and set the page router.
$redirectUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'orcidapi',
$handlerMethod, null, $redirectParams);
return $this->getOauthPath() . 'authorize?' . http_build_query(array(
'client_id' => $this->getSetting($contextId, 'orcidClientId'),
'response_type' => 'code',
'scope' => $scope,
'redirect_uri' => $redirectUrl));
}
/**
* Output filter adds ORCiD interaction to registration form.
*
* @param $output string
* @param $templateMgr TemplateManager
* @return string
*/
function registrationFilter($output, $templateMgr) {
if (preg_match('/<form[^>]+id="register"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$targetOp = 'register';
$templateMgr->assign(array(
'targetOp' => $targetOp,
'orcidUrl' => $this->getOrcidUrl(),
'orcidOAuthUrl' => $this->buildOAuthUrl('orcidAuthorize', array('targetOp' => $targetOp)),
'orcidIcon' => $this->getIcon(),
));
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplateResource('orcidProfile.tpl'));
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
$templateMgr->unregisterFilter('output', array($this, 'registrationFilter'));
}
return $output;
}
/**
* Renders additional content for the PublicProfileForm.
*
* Called by @see lib/pkp/templates/user/publicProfileForm.tpl
*
* @param $output string
* @param $templateMgr TemplateManager
* @return bool
*/
function handleUserPublicProfileDisplay($hookName, $params) {
$templateMgr =& $params[1];
$output =& $params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();
$user = $request->getUser();
$contextId = ($context == null) ? 0 : $context->getId();
$targetOp = 'profile';
$templateMgr->assign(array(
'targetOp' => $targetOp,
'orcidUrl' => $this->getOrcidUrl(),
'orcidOAuthUrl' => $this->buildOAuthUrl('orcidAuthorize', array('targetOp' => $targetOp)),
'orcidClientId' => $this->getSetting($contextId, 'orcidClientId'),
'orcidIcon' => $this->getIcon(),
'orcidAuthenticated' => !empty($user->getData('orcidAccessToken')),
));
$output = $templateMgr->fetch($this->getTemplateResource('orcidProfile.tpl'));
return true;
}
/**
* Output filter adds ORCiD interaction to contributors metadata add/edit form.
*
* @param $output string
* @param $templateMgr TemplateManager
* @return string
*/
function authorFormFilter($output, $templateMgr) {
if (preg_match('/<input[^>]+name="submissionId"[^>]*>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$templateMgr->assign('orcidIcon', $this->getIcon());
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplateResource('authorFormOrcid.tpl'));
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
$templateMgr->unregisterFilter('output', array($this, 'authorFormFilter'));
}
return $output;
}
/**
* handleAuthorFormexecute sends an e-mail to the author if a specific checkbox was ticked in the author form.
*
* @see AuthorForm::execute() The function calling the hook.
*
* @param $hookname string
* @param $args AuthorForm[]
*/
function handleAuthorFormExecute($hookname, $args) {
$form =& $args[0];
$form->readUserVars(array('requestOrcidAuthorization', 'deleteOrcid'));
$requestAuthorization = $form->getData('requestOrcidAuthorization');
$deleteOrcid = $form->getData('deleteOrcid');
$author = $form->getAuthor();
if ($author && $requestAuthorization) {
$this->sendAuthorMail($author);
}
if ($author && $deleteOrcid) {
$author->setOrcid(null);
$this->removeOrcidAccessToken($author, false);
}
}
/**
* Collect the ORCID when registering a user.
*
* @param $hookName string
* @param $params array
* @return bool
*/
function collectUserOrcidId($hookName, $params) {
$form = $params[0];
$user = $form->user;
$form->readUserVars(array('orcid'));
$user->setOrcid($form->getData('orcid'));
return false;
}
/**
* Output filter adds ORCiD interaction to the 3rd step submission form.
*
* @param $output string
* @param $templateMgr TemplateManager
* @return bool
*/
function handleSubmissionSubmitStep3FormExecute($hookName, $params) {
$form = $params[0];
// Have to use global Request access because request is not passed to hook
$authors = $form->submission->getAuthors();
$request = Application::get()->getRequest();
$user = $request->getUser();
//error_log("OrcidProfilePlugin: authors[0] = " . var_export($authors[0], true));
//error_log("OrcidProfilePlugin: user = " . var_export($user, true));
if($authors[0]->getOrcid() === $user->getOrcid()) {
// if the author and user share the same ORCID id
// copy the access token from the user
error_log("OrcidProfilePlugin: user->orcidAccessToken = " . $user->getData('orcidAccessToken'));
$authors[0]->setData('orcidAccessToken', $user->getData('orcidAccessToken'));
$authors[0]->setData('orcidAccessScope', $user->getData('orcidAccessScope'));
$authors[0]->setData('orcidRefreshToken', $user->getData('orcidRefreshToken'));
$authors[0]->setData('orcidAccessExpiresOn', $user->getData('orcidAccessExpiresOn'));
$authors[0]->setData('orcidSandbox', $user->getData('orcidSandbox'));
$authorDao = DAORegistry::getDAO('AuthorDAO');
$authorDao->updateObject($authors[0]);
error_log("OrcidProfilePlugin: author = " . var_export($authors[0], true));
}
return false;
}
/**
* Add additional ORCID specific fields to the Author and User objects
*
* @param $hookName string
* @param $params array
*
* @return bool
*/
function handleAdditionalFieldNames($hookName, $params) {
$fields =& $params[1];
$fields[] = 'orcidSandbox';
$fields[] = 'orcidAccessToken';
$fields[] = 'orcidAccessScope';
$fields[] = 'orcidRefreshToken';
$fields[] = 'orcidAccessExpiresOn';
$fields[] = 'orcidAccessDenied';
if ($hookName === 'authordao::getAdditionalFieldNames') {
// holds a one time hash string generated before sending the ORCID authorization e-mail
$fields[] = 'orcidEmailToken';
// holds the id of the added work entry in the corresponding ORCID profile for updates
$fields[] = 'orcidWorkPutCode';
}
return false;
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.orcidProfile.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.orcidProfile.description');
}
/**
* @see PKPPlugin::getInstallEmailTemplatesFile()
*/
function getInstallEmailTemplatesFile() {
return ($this->getPluginPath() . '/emailTemplates.xml');
}
/**
* @see PKPPlugin::getInstallEmailTemplateDataFile()
*/
function getInstallEmailTemplateDataFile() {
return ($this->getPluginPath() . '/locale/{$installedLocale}/emailTemplates.xml');
}
/**
* Extend the {url ...} smarty to support this plugin.
*/
function smartyPluginUrl($params, $smarty) {
$path = array($this->getCategory(), $this->getName());
if (is_array($params['path'])) {
$params['path'] = array_merge($path, $params['path']);
} elseif (!empty($params['path'])) {
$params['path'] = array_merge($path, array($params['path']));
} else {
$params['path'] = $path;
}
if (!empty($params['id'])) {
$params['path'] = array_merge($params['path'], array($params['id']));
unset($params['id']);
}
return $smarty->smartyUrl($params, $smarty);
}
/**
* @see Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url(
$request,
null,
null,
'manage',
null,
array(
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic'
)
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $actionArgs)
);
}
/**
* @see Plugin::manage()
*/
function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$templateMgr = TemplateManager::getManager();
$templateMgr->registerPlugin('function', 'plugin_url', array($this, 'smartyPluginUrl'));
$apiOptions = [
ORCID_API_URL_PUBLIC => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.public',
ORCID_API_URL_PUBLIC_SANDBOX => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.publicSandbox',
ORCID_API_URL_MEMBER => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.member',
ORCID_API_URL_MEMBER_SANDBOX => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.memberSandbox'
];
$templateMgr->assign('orcidApiUrls', $apiOptions);
$templateMgr->assign('logLevelOptions', [
'ERROR' => 'plugins.generic.orcidProfile.manager.settings.logLevel.error',
'ALL' => 'plugins.generic.orcidProfile.manager.settings.logLevel.all'
]);
$this->import('OrcidProfileSettingsForm');
$form = new OrcidProfileSettingsForm($this, $contextId);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Return the location of the plugin's CSS file
*
* @return string
*/
function getStyleSheet() {
return $this->getPluginPath() . '/css/orcidProfile.css';
}
/**
* Return a string of the ORCiD SVG icon
*
* @return string
*/
function getIcon() {
$path = Core::getBaseDir() . '/' . $this->getPluginPath() . '/templates/images/orcid.svg';
return file_exists($path) ? file_get_contents($path) : '';
}
/**
* Instantiate a MailTemplate
*
* @param string $emailKey
* @param Context $context
*
* @return MailTemplate
*/
function getMailTemplate($emailKey, $context = null) {
import('lib.pkp.classes.mail.MailTemplate');
return new MailTemplate($emailKey, null, $context, false);
}
/**
* Send mail with ORCID authorization link to the e-mail address of the supplied Author object.
*
* @param Author $author
* @param bool $updateAuthor If true update the author fields in the database.
* Use this only if not called from a function, which does this anyway.
*/
public function sendAuthorMail($author, $updateAuthor = false)
{
$request = PKPApplication::get()->getRequest();
$context = $request->getContext();
// This should only ever happen within a context, never site-wide.
assert($context != null);
$contextId = $context->getId();
if ( $this->isMemberApiEnabled($contextId) ) {
$mailTemplate = 'ORCID_REQUEST_AUTHOR_AUTHORIZATION';
}
else {
$mailTemplate = 'ORCID_COLLECT_AUTHOR_ID';
}
$mail = $this->getMailTemplate($mailTemplate, $context);
$emailToken = md5(microtime().$author->getEmail());
$author->setData('orcidEmailToken', $emailToken);
$articleDao = DAORegistry::getDAO('ArticleDAO');
$article = $articleDao->getById($author->getSubmissionId());
$oauthUrl = $this->buildOAuthUrl('orcidVerify', array('token' => $emailToken, 'articleId' => $author->getSubmissionId()));
$aboutUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'orcidapi', 'about', null);
// Set From to primary journal contact
$mail->setFrom($context->getData('contactEmail'), $context->getData('contactName'));
// Send to author
$mail->setRecipients(array(array('name' => $author->getFullName(), 'email' => $author->getEmail())));
// Send the mail with parameters
$mail->sendWithParams(array(
'orcidAboutUrl' => $aboutUrl,
'authorOrcidUrl' => $oauthUrl,
'authorName' => $author->getFullName(),
'articleTitle' => $article->getLocalizedTitle(),
));
if ($updateAuthor) {
$authorDao = DAORegistry::getDAO('AuthorDAO');
$authorDao->updateLocaleFields($author);
}
}
/**
* handlePublishIssue sends all submissions for which the authors hava an ORCID and access token
* to ORCID. This hook will be called on publication of a new issue.
*
* @see
*
* @param $hookName string
* @param $args Issue[] Issue object that will be published
*
**/
public function handlePublishIssue($hookName, $args) {
$issue =& $args[0];
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$authorDao = DAORegistry::getDAO('AuthorDAO');
$publishedArticles = $publishedArticleDao->getPublishedArticles($issue->getId());
$request = PKPApplication::get()->getRequest();
$journal = $request->getContext();
foreach ($publishedArticles as $publishedArticle) {
$articleId = $publishedArticle->getId();
$this->sendSubmissionToOrcid($articleId, $request, $issue);
}
}
/**
* handleScheduleForPublication is a hook called during the "Schedule for publication" step
* from the production stage of a submission. It registers another hook, because at the time of calling this hook,
* the issue
*
* @param $hookName string The name the hook was registered as.
* @param $args array Hook arguments, $form, $request, &$returner
*
* @see IssueEntryPublicationMetadataForm::execute() The function calling the hook.
*/
public function handleScheduleForPublication($hookName, $args) {
$form = $args[0];
$submissionId = $form->getSubmission()->getId();
$this->submissionIdToBePublished = $submissionId;
HookRegistry::register('ArticleSearchIndex::articleChangesFinished',
[$this, 'handleScheduleForPublicationFinished']);
}
/**
* handleScheduleForPublicationFinished is a hook registered by handleScheduleForPublication and sends the
* submission data to orcid. The hook will be called at the end of IssueEntryPublicationMetadataForm::execute()
*
* @param $hookName string The name the hook was registered as.
* @param $args array Hook arguments
*
* @see IssueEntryPublicationMetadataForm::execute() The function calling the hook.
*/
public function handleScheduleForPublicationFinished($hookName, $args) {
if ( $this->submissionIdToBePublished ) {
$request = PKPApplication::get()->getRequest();
$this->sendSubmissionToOrcid($this->submissionIdToBePublished, $request);
$this->submissionIdToBePublished = null;
}
}
/**
* handleEditorAction handles promoting a submission to production.
*
* @param $hookName string Name the hook was registered with
* @param $args array Hook arguments, &$submission, &$editorDecision, &$result, &$recommendation.
*
* @see EditorAction::recordDecision() The function calling the hook.
*/
public function handleEditorAction($hookName, $args) {
$submission =& $args[0];
$authorDao = DAORegistry::getDAO('AuthorDAO');
$authors = $authorDao->getBySubmissionId($submission->getId());
foreach ($authors as $author) {
$orcidAccessExpiresOn = Carbon\Carbon::parse($author->getData('orcidAccessExpiresOn'));
if ( $author->getData('orcidAccessToken') == null || $orcidAccessExpiresOn->isPast()) {
$this->sendAuthorMail($author, true);
}
}
}
/**
* Check if the submission with the supplied id is actually published.
* @param int $submissionId Id of the submission/article to check.
* @return boolean True, if the article is part of a published issue. False otherwise.
*/
public function isSubmissionPublished($submissionId) {
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$article = $publishedArticleDao->getByArticleId($submissionId);
if ( $article === null ) {
return false;
}
$issue = DAORegistry::getDAO('IssueDAO')->getById($article->getIssueId());
if ( $issue === null || !$issue->getPublished()) {
return false;
}
return true;
}
/**
* sendSubmissionToOrcid posts JSON consisting of submission, journal and issue meta data
* to ORCID profiles of submission authors.
*
* @see https://github.com/ORCID/ORCID-Source/tree/master/orcid-model/src/main/resources/record_2.1
* for documentation and examples of the ORCID JSON format.
*
* @param $submissionId integer Id of the article for which the data will be sent to ORCID
* @return void
*
**/
public function sendSubmissionToOrcid($submissionId, $request, $issue = null) {
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$article = $publishedArticleDao->getByArticleId($submissionId);
if ( $article === null ) {
$this->logError("No PublishedArticle found for id $submissionId");
return false;
}
if ( $issue === null ) {
$issue = DAORegistry::getDAO('IssueDAO')->getById($article->getIssueId());
}
if ( $issue === null || !$issue->getPublished()) {
// Issue not yet published, do not send
return false;
}
$journal = $request->getContext();
$this->currentContextId = $journal->getId();
if (!$this->isMemberApiEnabled($journal->getId())) {
// Sending to ORCID only works with the member API
return false;
}
$authorDao = DAORegistry::getDAO('AuthorDAO');
$authors = $authorDao->getBySubmissionId($submissionId);
// Collect valid ORCID ids and access tokens from submission contributors
$authorsWithOrcid = [];
foreach ($authors as $author) {
if ($author->getOrcid() && $author->getData('orcidAccessToken') ) {
$orcidAccessExpiresOn = Carbon\Carbon::parse($author->getData('orcidAccessExpiresOn'));
if ($orcidAccessExpiresOn->isFuture()) {
# Extract only the ORCID from the stored ORCID uri
$orcid = basename(parse_url($author->getOrcid(), PHP_URL_PATH));
$authorsWithOrcid[$orcid] = $author;
}
else {
$this->logError("Token expired on $orcidAccessExpiresOn for author ". $author->getId() .
", deleting orcidAccessToken!");
$this->removeOrcidAccessToken($author);
}
}
}
if ( empty($authorsWithOrcid) ) {
$this->logInfo('No contributor with ORICD id or valid access token for submission ' . $submissionId);
return false;
}
$orcidWork = $this->buildOrcidWork($article, $journal, $authors, $issue, $request);
$this::logInfo("Request body (without put-code): " . json_encode($orcidWork));
$requestsSuccess = [];
foreach ($authorsWithOrcid as $orcid => $author) {
$url = $this->getSetting($journal->getId(), 'orcidProfileAPIPath') . ORCID_API_VERSION_URL . $orcid . '/'
. ORCID_WORK_URL;
$method = "POST";
if ( $putCode = $author->getData('orcidWorkPutCode')) {
// Submission has already been sent to ORCID. Use PUT to update meta data
$url .= '/' . $putCode;
$method = "PUT";
$orcidWork['put-code'] = $putCode;
}
else {
// Remove put-code from body because the work has not yet been sent
unset($orcidWork['put-code']);
}
$orcidWorkJson = json_encode($orcidWork);
$header = [
'Content-Type: application/vnd.orcid+json',
'Content-Length: ' . strlen($orcidWorkJson),
'Accept: application/json',
'Authorization: Bearer ' . $author->getData('orcidAccessToken')
];
$this->logInfo("$method $url");
$this->logInfo("Header: " . var_export($header, true));
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $orcidWorkJson,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header
]);
// Use proxy if configured
if ($httpProxyHost = Config::getVar('proxy', 'http_host')) {
curl_setopt($ch, CURLOPT_PROXY, $httpProxyHost);
curl_setopt($ch, CURLOPT_PROXYPORT, Config::getVar('proxy', 'http_port', '80'));
if ($username = Config::getVar('proxy', 'username')) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $username . ':' . Config::getVar('proxy', 'password'));
}
}
$responseHeaders = [];
// Needed to correctly process response headers.
// This function is called by curl for each received line of the header.
// Code from StackOverflow answer here: https://stackoverflow.com/a/41135574/8938233
// Thanks to StackOverflow user Geoffrey.
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$responseHeaders)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) {
// ignore invalid headers
return $len;
}
$name = strtolower(trim($header[0]));
if (!array_key_exists($name, $responseHeaders)) {
$responseHeaders[$name] = [trim($header[1])];
}
else {
$responseHeaders[$name][] = trim($header[1]);
}
return $len;
}
);
$result = curl_exec($ch);
if (curl_error($ch)) {
$this->logError('Unable to post to ORCID API, curl error: ' . curl_error($ch));
curl_close($ch);
return false;
}
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->logInfo("Response status: $httpstatus");
switch ($httpstatus) {
case 200:
// Work updated
$this->logInfo("Work updated in profile, putCode: $putCode");
$requestsSuccess[$orcid] = true;
break;
case 201:
$location = $responseHeaders['location'][0];
// Extract the ORCID work put code for updates/deletion.
$putCode = intval(basename(parse_url($location, PHP_URL_PATH)));
$this->logInfo("Work added to profile, putCode: $putCode");
$author->setData('orcidWorkPutCode', $putCode);
$authorDao->updateLocaleFields($author);
$requestsSuccess[$orcid] = true;
break;
case 401:
// invalid access token, token was revoked
$error = json_decode($result);
if ($error->error === 'invalid_token') {
$this->logError("$error->error_description, deleting orcidAccessToken from author");
$this->removeOrcidAccessToken($author);
}
$requestsSuccess[$orcid] = false;
break;
case 404:
// a work has been deleted from a ORCID record. putCode is no longer valid.
if ($method === 'PUT') {
$this->logError("Work deleted from ORCID record, deleting putCode form author");
$author->setData('orcidWorkPutCode', null);
$authorDao->updateLocaleFields($author);
$requestsSuccess[$orcid] = false;
}
else {
$this->logError("Unexpected status $httpstatus response, body: $result");
$requestsSuccess[$orcid] = false;
}
break;
case 409:
$this->logError('Work already added to profile, response body: '. $result);
$requestsSuccess[$orcid] = false;
break;
default:
$this->logError("Unexpected status $httpstatus response, body: $result");
$requestsSuccess[$orcid] = false;
}
}
if (array_product($requestsSuccess) ) {
return true;
}
else {
return $requestsSuccess;
}
}
/**
* Build an associative array with submission meta data, which can be encoded to a valid ORCID work JSON structure.
*
* @see https://github.com/ORCID/ORCID-Source/blob/master/orcid-model/src/main/resources/record_2.1/samples/write_sample/bulk-work-2.1.json
* Example of valid ORCID JSON for adding works to an ORCID record.
* @param Article $article extract data from this Article
* @param Journal $journal Journal (context) object the Article is part of
* @param Author[] $authors Array of Author objects, the contributors of the article
* @param Issue $issue Issue the Article is part of
* @param Request $request the current request
* @return array an associative array with article meta data corresponding to ORCID work JSON structure
*/
public function buildOrcidWork($article, $journal, $authors, $issue, $request) {
$articleLocale = $article->getLocale();
$titles = $article->getTitle($articleLocale);
$citationPlugin = PluginRegistry::getPlugin('generic', 'citationstylelanguageplugin');
$bibtexCitation = trim(strip_tags($citationPlugin->getCitation($request, $article, 'bibtex')));
$articleUrl = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'article', 'view', $article->getBestArticleId());
$orcidWork = [
'title' => [
'title' => [
'value' => $article->getLocalizedTitle($articleLocale)
],
'subtitle' => [
'value' => $article->getSubtitle($articleLocale)
]
],
'journal-title' => [
'value' => $journal->getName('en_US')
],
'short-description' => trim(strip_tags($article->getAbstract('en_US'))),
'type' => 'JOURNAL_ARTICLE',
'external-ids' => [ 'external-id' => $this->buildOrcidExternalIds($article, $journal, $issue, $articleUrl)],
'publication-date' => $this->buildOrcidPublicationDate($issue),
'url' => $articleUrl,
'citation' => [
'citation-type' => 'BIBTEX',
'citation-value' => $bibtexCitation
],
'language-code' => substr($articleLocale, 0, 2),
'contributors' => [ 'contributor' => $this->buildOrcidContributors($authors, $journal->getId()) ]
];
if ($articleLocale !== 'en_US') {
$orcidWork['title']['translated-title'] = [
'value' => $article->getTitle('en_US'),
'language-code' => 'en'
];
}
return $orcidWork;
}
/**
* Parse issue year and publication date and use the older on of the two as
* the publication date of the ORCID work.
*
* @return array Associative array with year, month and day or only year
*/
private function buildOrcidPublicationDate($issue) {
$issuePublicationDate = Carbon\Carbon::parse($issue->getDatePublished());
return [
'year' => [ 'value' => $issuePublicationDate->format("Y")],
'month' => [ 'value' => $issuePublicationDate->format("m")],
'day' => [ 'value' => $issuePublicationDate->format("d")]
];
}
/**
* Build the external identifiers ORCID JSON structure from article, journal and issue meta data.
*
* @see https://pub.orcid.org/v2.0/identifiers Table of valid ORCID identifier types.
*
* @param Article $article The Article object for which the external identifiers should be build.
* @param Journal $journal Journal the Article is part of.