-
Notifications
You must be signed in to change notification settings - Fork 32
/
TestIthenticate.php
492 lines (441 loc) · 14.6 KB
/
TestIthenticate.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
<?php
/**
* @file plugins/generic/plagiarism/TestIThenticate.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class TestIThenticate
*
* @brief Low-budget mock class for IThenticate -- set the config setting
* `test_mdoe` to `On` in the `config.inc.php` to log API usage
* instead of interacting with the iThenticate service.
*/
namespace APP\plugins\generic\plagiarism;
use APP\core\Application;
use APP\submission\Submission;
use Exception;
use Illuminate\Support\Str;
use PKP\author\Author;
use PKP\config\Config;
use PKP\context\Context;
use PKP\site\Site;
use PKP\user\User;
class TestIThenticate
{
/**
* @copydoc IThenticate::$eulaVersion
*/
protected ?string $eulaVersion = null;
/**
* @copydoc IThenticate::$eulaVersionDetails
*/
protected ?array $eulaVersionDetails = [
"version" => "v1beta",
"valid_from" => "2018-04-30T17:00:00Z",
"valid_until" => null,
"url" => "https://static.turnitin.com/eula/v1beta/en-us/eula.html",
"available_languages" => [
"sv-SE",
"zh-CN",
"ja-JP",
"ko-KR",
"es-MX",
"nl-NL",
"ru-RU",
"zh-TW",
"ar-SA",
"pt-BR",
"de-DE",
"el-GR",
"nb-NO",
"cs-CZ",
"da-DK",
"tr-TR",
"pl-PL",
"fi-FI",
"it-IT",
"vi-VN",
"fr-FR",
"en-US",
"ro-RO",
],
];
/**
* @copydoc IThenticate::$suppressApiRequestException
*/
protected bool $suppressApiRequestException = true;
/**
* @copydoc IThenticate::DEFAULT_EULA_VERSION
*/
public const DEFAULT_EULA_VERSION = 'latest';
/**
* @copydoc IThenticate::DEFAULT_EULA_LANGUAGE
*/
public const DEFAULT_EULA_LANGUAGE = 'en-US';
/**
* @copydoc IThenticate::DEFAULT_WEBHOOK_EVENTS
*/
public const DEFAULT_WEBHOOK_EVENTS = [
'SUBMISSION_COMPLETE',
'SIMILARITY_COMPLETE',
'SIMILARITY_UPDATED',
'PDF_STATUS',
'GROUP_ATTACHMENT_COMPLETE',
];
/**
* @copydoc IThenticate::SUBMISSION_PERMISSION_SET
*/
public const SUBMISSION_PERMISSION_SET = [
'ADMINISTRATOR',
'APPLICANT',
'EDITOR',
'INSTRUCTOR',
'LEARNER',
'UNDEFINED',
'USER',
];
/**
* The minimum value of similarity report's view_setting's `exclude_small_matches` option
* @see https://developers.turnitin.com/docs/tca#generate-similarity-report
*
* @var int
*/
public const EXCLUDE_SAMLL_MATCHES_MIN = 8;
/**
* @copydoc IThenticate::__construct()
*/
public function __construct(
string $apiUrl,
string $apiKey,
string $integrationName,
string $integrationVersion,
?string $eulaVersion = null
)
{
// These following 2 conditions are to facilitate the mock the EULA requirement
if ($eulaVersion) {
$this->eulaVersion = $eulaVersion;
}
if (!$eulaVersion) {
$this->eulaVersion = Config::getVar('ithenticate', 'test_mode_eula', true) ? 'v1beta' : null;
}
error_log(
sprintf(
"Constructing iThenticate with API URL : {$apiUrl} \n
API Key : {$apiKey} \n
Integration Name : {$integrationName} \n
Integration Version : {$integrationVersion} \n
EULA Version : {$this->eulaVersion}"
)
);
}
/**
* @copydoc IThenticate::withoutSuppressingApiRequestException()
*/
public function withoutSuppressingApiRequestException(): static
{
$this->suppressApiRequestException = false;
error_log('deactivating api request exception suppression');
return $this;
}
/**
* @copydoc IThenticate::getEnabledFeature()
*/
public function getEnabledFeature(mixed $feature = null): string|array|null
{
static $result;
$result = '{
"similarity": {
"viewer_modes": {
"match_overview": true,
"all_sources": true
},
"generation_settings": {
"search_repositories": [
"INTERNET",
"PUBLICATION",
"CROSSREF",
"CROSSREF_POSTED_CONTENT",
"SUBMITTED_WORK"
],
"submission_auto_excludes": true
},
"view_settings": {
"exclude_bibliography": true,
"exclude_quotes": true,
"exclude_abstract": true,
"exclude_methods": true,
"exclude_small_matches": true,
"exclude_internet": true,
"exclude_publications": true,
"exclude_crossref": true,
"exclude_crossref_posted_content": true,
"exclude_submitted_works": true,
"exclude_citations": true,
"exclude_preprints": true
}
},
"tenant": {
"require_eula": '.($this->eulaVersion ? "true" : "false").'
},
"product_name": "Turnitin Originality",
"access_options": [
"NATIVE",
"CORE_API",
"DRAFT_COACH"
]
}';
if (!$feature) {
error_log("iThenticate enabled feature details {$result}");
return json_decode($result, true);
}
$featureStatus = data_get(
json_decode($result, true),
$feature,
fn () => $this->suppressApiRequestException
? null
: throw new Exception("Feature details {$feature} does not exist")
);
error_log("iThenticate specific enable feature details {$featureStatus}");
return $featureStatus;
}
/**
* @copydoc IThenticate::validateAccess()
*/
public function validateAccess(mixed &$result = null): bool
{
error_log("Confirming the service access validation for given access details");
return true;
}
/**
* @copydoc IThenticate::confirmEula()
*/
public function confirmEula(User $user, Context $context): bool
{
error_log("Confirming EULA for user {$user->getId()} with language ".$this->getApplicableLocale($context->getPrimaryLocale())." for version {$this->getApplicableEulaVersion()}");
return true;
}
/**
* @copydoc IThenticate::createSubmission()
*/
public function createSubmission(
Site $site,
Submission $submission,
User $user,
Author $author,
string $authorPermission,
string $submitterPermission
): ?string
{
if (!$this->validatePermission($authorPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new Exception("in valid owner permission {$authorPermission} given");
}
if (!$this->validatePermission($submitterPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new Exception("in valid submitter permission {$submitterPermission} given");
}
error_log("Creating a new submission with id {$submission->getId()} by submitter {$user->getId()} for owner {$author->getId()} with owner permission as {$authorPermission} and submitter permission as {$submitterPermission}");
return \Illuminate\Support\Str::uuid()->__toString();
}
/**
* @copydoc IThenticate::uploadFile()
*/
public function uploadFile(string $submissionTacId, string $fileName, mixed $fileContent): bool
{
error_log("Uploading submission file named {$fileName} for submission UUID {$submissionTacId}");
return true;
}
/**
* @copydoc IThenticate::getSubmissionInfo()
*/
public function getSubmissionInfo(string $submissionUuid): ?string
{
return '{
"id": "'.$submissionUuid.'",
"owner": "a9c14691-9523-4f44-b5fc-4a673c5d4a35",
"title": "History 101 Final Esssay",
"status": "COMPLETE",
"content_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"page_count": 3,
"word_count": 145,
"character_count": 760,
"created_time": "2023-08-30T22:13:41Z",
"capabilities" : [
"INDEX",
"VIEWER",
"SIMILARITY"
]
}';
}
/**
* @copydoc IThenticate::scheduleSimilarityReportGenerationProcess()
*/
public function scheduleSimilarityReportGenerationProcess(string $submissionUuid, array $settings = []): bool
{
error_log(
sprintf(
'Scheduled similarity report generation process for submission UUID %s with similarity config %s',
$submissionUuid,
print_r($settings, true)
)
);
return true;
}
/**
* @copydoc IThenticate::getSimilarityResult()
*/
public function getSimilarityResult(string $submissionUuid): ?string
{
error_log("Similarity report result retrived for iThenticate submission id : {$submissionUuid}");
return '{
"submission_id": "'.$submissionUuid.'",
"overall_match_percentage": 15,
"internet_match_percentage": 12,
"publication_match_percentage": 10,
"submitted_works_match_percentage": 0,
"status": "COMPLETE",
"time_requested": "2017-11-06T19:14:31.828Z",
"time_generated": "2017-11-06T19:14:45.993Z",
"top_source_largest_matched_word_count": 193,
"top_matches": []
}';
}
/**
* @copydoc IThenticate::createViewerLaunchUrl()
*/
public function createViewerLaunchUrl(
string $submissionUuid,
User $user,
string $locale,
string $viewerPermission,
bool $allowUpdateInViewer
): ?string
{
error_log("Similarity report viewer launch url generated for iThenticate submission id : {$submissionUuid} with locale : {$locale}, viewer permission : {$viewerPermission} and update viewer permission : {$allowUpdateInViewer}");
return Application::get()->getRequest()->getBaseUrl();
}
/**
* @copydoc IThenticate::verifyUserEulaAcceptance()
*/
public function verifyUserEulaAcceptance(Author|User $user, string $version): bool
{
error_log("Verifying if user with id {$user->getId()} has already confirmed the given EULA version {$version}");
return true;
}
/**
* @copydoc IThenticate::validateEulaVersion()
*/
public function validateEulaVersion(string $version): bool
{
error_log("Validating/Retrieving the given EULA version {$version}");
return true;
}
/**
* @copydoc IThenticate::registerWebhook()
*/
public function registerWebhook(
string $signingSecret,
string $url,
array $events = self::DEFAULT_WEBHOOK_EVENTS
): ?string
{
error_log(
sprintf(
"Register webhook end point with singing secret : %s, url : %s and events : [%s]",
$signingSecret,
$url,
implode(', ',$events)
)
);
return Str::uuid()->__toString();
}
/**
* @copydoc IThenticate::deleteWebhook()
*/
public function deleteWebhook(string $webhookId): bool
{
error_log("ithenticate webhook with id : {$webhookId} removed");
return true;
}
/**
* @copydoc IThenticate::getEulaDetails()
*/
public function getEulaDetails(): ?array
{
return $this->eulaVersionDetails;
}
/**
* @copydoc IThenticate::getApplicableEulaVersion()
*/
public function getApplicableEulaVersion(): string
{
return $this->eulaVersion;
}
/**
* @copydoc IThenticate::setApplicableEulaVersion()
*/
public function setApplicableEulaVersion(string $version): static
{
$this->eulaVersion = $version;
return $this;
}
/**
* @copydoc IThenticate::getApplicableEulaUrl()
*/
public function getApplicableEulaUrl(string|array|null $locales = null): string
{
if (!$this->eulaVersion) {
throw new Exception('No EULA version set yet');
}
$applicableEulaLanguage = $this->getApplicableLocale($locales ?? static::DEFAULT_EULA_LANGUAGE);
$eulaUrl = $this->eulaVersionDetails['url'];
return str_replace(
strtolower(static::DEFAULT_EULA_LANGUAGE),
strtolower($applicableEulaLanguage),
$eulaUrl
);
}
/**
* @copydoc IThenticate::getApplicableLocale()
*/
public function getApplicableLocale(string|array $locales, ?string $eulaVersion = null): string
{
if (!$this->getEulaDetails() && !$this->validateEulaVersion($eulaVersion ?? $this->eulaVersion)) {
return static::DEFAULT_EULA_LANGUAGE;
}
if (is_string($locales)) {
return $this->getCorrespondingLocaleAvailable($locales) ?? static::DEFAULT_EULA_LANGUAGE;
}
foreach ($locales as $locale) {
$correspondingLocale = $this->getCorrespondingLocaleAvailable($locale);
if ($correspondingLocale) {
return $correspondingLocale;
}
}
return static::DEFAULT_EULA_LANGUAGE;
}
/**
* @copydoc IThenticate::isCorrespondingLocaleAvailable()
*/
protected function getCorrespondingLocaleAvailable(string $locale): ?string
{
$eulaLangs = $this->eulaVersionDetails['available_languages'];
$locale = str_replace("_", "-", substr($locale, 0, 5));
return in_array($locale, $eulaLangs)
? $locale
: collect($eulaLangs)
->filter(
fn(string $lang) => strtolower(
collect(explode("-", $lang))->first()
) === strtolower(collect(explode("-", $locale))->first())
)->first();
}
/**
* @copydoc IThenticate::validatePermission()
*/
protected function validatePermission(string $permission, array $permissionSet): bool
{
return in_array($permission, $permissionSet);
}
}