-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSushiService.php
1176 lines (989 loc) · 39.9 KB
/
SushiService.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
/*
**************************************************************************************************************************
** CORAL Usage Statistics Module
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
**
** CORAL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
**
** CORAL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with CORAL. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************************************************************
*/
class SushiService extends DatabaseObject
{
protected function defineRelationships()
{
}
protected function overridePrimaryKeyName()
{
}
public $startDate;
public $endDate;
private $statusLog = array();
private $detailLog = array();
public function getByPlatformID($platformID)
{
if (isset($platformID)) {
$query = "SELECT * FROM `$this->tableName` WHERE `platformID` = '$platformID'";
$result = $this->db->processQuery($query, 'assoc');
foreach (array_keys($result) as $attributeName) {
$this->addAttribute($attributeName);
$this->attributes[$attributeName] = $result[$attributeName];
}
}
}
public function getByPublisherPlatformID($publisherPlatformID)
{
if (isset($publisherPlatformID)) {
$query = "SELECT * FROM `$this->tableName` WHERE `publisherPlatformID` = '$publisherPlatformID'";
$result = $this->db->processQuery($query, 'assoc');
foreach (array_keys($result) as $attributeName) {
$this->addAttribute($attributeName);
$this->attributes[$attributeName] = $result[$attributeName];
}
}
}
//returns array of sushi service objects that need to be run on a particular day
public function getByDayOfMonth($serviceDayOfMonth)
{
//now formulate query
$query = "SELECT * FROM SushiService WHERE `serviceDayOfMonth` = '$serviceDayOfMonth';";
$result = $this->db->processQuery(stripslashes($query), 'assoc');
$objects = array();
//need to do this since it could be that there's only one request and this is how the dbservice returns result
if (isset($result['sushiServiceID'])) {
$object = new SushiService(new NamedArguments(array('primaryKey' => $result['sushiServiceID'])));
array_push($objects, $object);
} else {
foreach ($result as $row) {
$object = new SushiService(new NamedArguments(array('primaryKey' => $row['sushiServiceID'])));
array_push($objects, $object);
}
}
return $objects;
}
public function getPublisherOrPlatform()
{
if (($this->platformID != "") && ($this->platformID > 0)) {
return new Platform(new NamedArguments(array('primaryKey' => $this->platformID)));
} else {
return new PublisherPlatform(new NamedArguments(array('primaryKey' => $this->publisherPlatformID)));
}
}
public function getServiceProvider()
{
return str_replace('"', '', $this->getPublisherOrPlatform->reportDisplayName);
}
public function failedImports()
{
$query = "SELECT ipl.platformID, ss.sushiServiceID, date(il.importDateTime), details, il.importLogID
FROM ImportLog il
INNER JOIN ImportLogPlatformLink ipl USING (ImportLogID)
INNER JOIN SushiService ss ON (ss.platformID = ipl.platformID)
INNER JOIN (SELECT platformID, max(importLogID) importLogID, max(importDateTime) importDateTime FROM ImportLog mil INNER JOIN ImportLogPlatformLink mipl USING (ImportLogID) GROUP BY platformID) mil ON (mil.importLogID = il.importLogID)
WHERE ucase(details) like '%FAIL%'
ORDER BY il.importDateTime desc";
$result = $this->db->processQuery(stripslashes($query), 'assoc');
$resultArray = array();
$importArray = array();
//need to do this since it could be that there's only one result and this is how the dbservice returns result
if (isset($result['platformID'])) {
foreach (array_keys($result) as $attributeName) {
$resultArray[$attributeName] = $result[$attributeName];
}
array_push($importArray, $resultArray);
} else {
foreach ($result as $row) {
$resultArray = array();
foreach (array_keys($row) as $attributeName) {
$resultArray[$attributeName] = $row[$attributeName];
}
array_push($importArray, $resultArray);
}
}
return $importArray;
}
public function allServices()
{
$query = "SELECT ss.platformID, ss.publisherPlatformID, sushiServiceID, serviceURL, reportLayouts, releaseNumber,
if(serviceDayOfMonth > day(now()), str_to_date(concat(EXTRACT(YEAR_MONTH FROM NOW()), lpad(serviceDayOfMonth,2,'0')), '%Y%m%d'), str_to_date(concat(EXTRACT(YEAR_MONTH FROM NOW()) + 1, lpad(serviceDayOfMonth,2,'0')), '%Y%m%d') ) next_import
FROM SushiService ss
LEFT JOIN Platform p on (p.platformID = ss.platformID)
LEFT JOIN PublisherPlatform pp
INNER JOIN Publisher pub USING(publisherID)
ON (pp.publisherPlatformID = ss.publisherPlatformID)
ORDER BY p.name, pub.name";
$result = $this->db->processQuery(stripslashes($query), 'assoc');
$resultArray = array();
$importArray = array();
//need to do this since it could be that there's only one result and this is how the dbservice returns result
if (isset($result['platformID'])) {
foreach (array_keys($result) as $attributeName) {
$resultArray[$attributeName] = $result[$attributeName];
}
array_push($importArray, $resultArray);
} else {
foreach ($result as $row) {
$resultArray = array();
foreach (array_keys($row) as $attributeName) {
$resultArray[$attributeName] = $row[$attributeName];
}
array_push($importArray, $resultArray);
}
}
return $importArray;
}
/*********************************************************************************************************************
* Runners
*/
//run through ajax function on publisherplatform
public function runTest()
{
$reportLayouts = $this->reportLayouts;
$rlArray = explode(";", $reportLayouts);
//just default test import dates to just be january 1 - 31 of this year
$sDate = date_format(date_create_from_format("Ymd", date("Y") . "0101"), "Y-m-d");
$eDate = date_format(date_create_from_format("Ymd", date("Y") . "0131"), "Y-m-d");
$this->setImportDates($sDate, $eDate);
$serviceProvider = $this->getServiceProvider();
$errors = [];
foreach ($rlArray as $reportLayout) {
try {
$this->sushiTransfer($reportLayout, $serviceProvider);
} catch(Exception $e) {
$errors[$reportLayout] = $e->getMessage();
}
}
if ($reportLayouts == "") {
echo _("At least one report type must be set up!");
} elseif (!empty($errors)) {
foreach($errors as $rl => $er) {
echo '<h3> Report '.$rl.'</h3>';
echo '<p>'.$er.'</p>';
}
} else {
echo _("Connection test successful!");
}
}
//run through post or through sushi scheduler
public function runAll($overwritePlatform = TRUE)
{
$reportLayouts = $this->reportLayouts;
if ($reportLayouts == "") {
return _("No report types are set up!");
}
$detailsForOutput = array();
$rlArray = explode(";", $reportLayouts);
$serviceProvider = $this->getServiceProvider();
foreach ($rlArray as $reportLayout) {
try {
$detailsForOutput[] = implode("\n", $this->run($reportLayout, $serviceProvider, $overwritePlatform));
sleep(3);
} catch(Exception $e) {
$detailsForOutput[] = $e->getMessage();
}
}
return implode("\r\n", $detailsForOutput);
}
/**
* @throws Exception
*/
public function run($reportLayout, $serviceProvider, $overwritePlatform) {
$this->statusLog = array();
$this->detailLog = array();
// get sushi report
$response = $this->sushiTransfer($reportLayout, $serviceProvider);
$ext = $this->releaseNumber > 4 ? '.json' : '.xml';
// Save the response
$filename = $serviceProvider . '_' . $reportLayout . '_' . $this->startDate . '_' . $this->endDate;
$replace = "_";
$pattern = "/([[:alnum:]_\.-]*)/";
$file = BASE_DIR . 'counterstore/' . str_replace(str_split(preg_replace($pattern, $replace, $filename)), $replace, $filename) . $ext;
file_put_contents($file, $response);
//Test that response was saved
if (!file_get_contents($file)) {
$this->logStatus("Failed trying to open file: " . $file . ". This could be due to not having write access to the /counterstore/ directory.");
$this->saveLogAndExit($reportLayout);
}
// parse the report
if ($this->releaseNumber < 5) {
$report = $this->parseXML($file, $reportLayout, $serviceProvider, $serviceProvider);
} else {
$report = $this->parseJson($file, $reportLayout, $overwritePlatform, $serviceProvider);
}
$this->log("Finished parsing " . $this->getServiceProvider . ": $reportLayout.");
// validate and save the report
$txtOut = '';
//determine the format of the report to port to csv from the layouts.ini file
$layoutCode = $reportLayout . '_R' . $this->releaseNumber;
$layoutsArray = parse_ini_file(BASE_DIR . "layouts.ini", true);
$layoutKey = $layoutsArray['ReportTypes'][$layoutCode];
$layoutColumns = $layoutsArray[$layoutKey]['columns'];
if (count($layoutColumns) == 0 || $layoutCode == ''){
$this->logStatus("Failed determining layout: Reached report items before establishing layout. Please make sure this layout is set up in layouts.ini");
$this->saveLogAndExit($reportLayout);
}
$monthColumns = array_map(function($m) {
return $m['columnName'];
},$report['header']['months']);
$header = array_merge($layoutColumns,$monthColumns);
$txtOut .= implode("\t", $header) . "\n";
$this->log("Layout validated successfully against layouts.ini : " . $layoutCode);
// Add rows
foreach($report['rows'] as $row) {
$finalArray = array();
// Use the report's layoutcode's columns to order them properly
foreach($layoutColumns as $colName){
$finalArray[] = isset($row[$colName]) ? $row[$colName] : null;
}
foreach($row['months'] as $m) {
$finalArray[] = $m;
}
$txtOut .= implode("\t", $finalArray) . "\n";
}
#Save final text delimited "file" and log output on server
$txtFile = $filename . '_' . strtotime('now') . '.txt';
$fp = fopen(BASE_DIR . 'archive/' . $txtFile, 'w');
fwrite($fp, $txtOut);
fclose($fp);
$this->log("");
$this->log("-- Sushi report writing completed --");
$this->log("Archive/Text File Name: " . Utility::getPageURL() . 'archive/' . $txtFile);
$this->saveLogAndExit($layoutCode, $txtFile, true);
return $this->statusLog;
}
/*********************************************************************************************************************
* Requestors
*/
private function sushiTransfer($reportLayout, $serviceProvider)
{
if ($this->releaseNumber < 5) {
$response = $this->xmlTransfer($reportLayout, $serviceProvider);
} else {
$response = $this->jsonTransfer($reportLayout, $serviceProvider);
}
return $response;
}
private function jsonTransfer($reportLayout, $serviceProvider)
{
$startDate = date_create($this->startDate);
$endDate = date_create($this->endDate);
// End Date is earlier than start date
if ($this->startDate > $this->endDate) {
$this->logStatus("Invalid Dates entered. Must enter a start before end date.");
$this->saveLogAndExit($reportLayout);
}
// setup params
$params = array(
'begin_date' => date_format($startDate, 'Y-m'),
'end_date' => date_format($endDate, 'Y-m'),
'customer_id' => $this->customerID
);
if ($this->requestorID) {
$params['requestor_id'] = $this->requestorID;
}
if ($this->apiKey) {
$params['api_key'] = $this->apiKey;
}
// If a master report, need to add 'attributes_to_show'
if (in_array($reportLayout,['PR','DR','TR','IR'])) {
$params['attributes_to_show'] = 'Data_Type|Access_Method';
if ($reportLayout == 'TR') {
$params['attributes_to_show'] .= '|Access_Type|Section_Type|YOP';
}
if ($reportLayout == 'IR') {
$params['attributes_to_show'] .= '|Access_Type|YOP|Publication_Date|Authors|Article_Version';
}
}
// If an IR or IR_A1 report get the parent and component details
if (in_array($reportLayout, ['IR', 'IR_A1'])) {
// Using a boolean true causes some vendors to balk as this is converted to a 1 in the curl call
$params['include_component_details'] = 'true';
$params['include_parent_details'] = 'true';
}
if ($this->platform) {
$params['platform'] = $this->platform;
}
// setup curl client
$trailingSlash = substr($this->serviceURL, -1) == '/' ? '' : '/';
$endpoint = $this->serviceURL . $trailingSlash . 'reports/' . strtolower($reportLayout) . '?' . http_build_query($params);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
if ($this->security) {
if (preg_match("/http/i", $this->security)) {
curl_setopt($ch, CURLOPT_USERPWD, $this->login . ":" . $this->password);
}
}
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'CoralSushiService');
$this->log("Connecting to $this->serviceURL");
// try executing curl
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
$this->logStatus("Exception performing curl request with connection to $serviceProvider: $error");
$this->saveLogAndExit($reportLayout);
}
curl_close($ch);
$response = mb_convert_encoding($response, 'UTF-8', 'UTF-8');
$json = json_decode($response);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->logStatus('JSON decoding error: ' . json_last_error_msg());
$this->saveLogAndExit($reportLayout);
}
// if the json is empty
if (empty($json)) {
$this->logStatus("No data was returned from $serviceProvider using the endpoint: $endpoint");
$this->saveLogAndExit($reportLayout);
}
// If there is an error, the model will have Code and Message in the top level
if (!empty($json->Code) && !(empty($json->Message))) {
$this->logStatus("Received an error from $serviceProvider: $json->Message");
$this->saveLogAndExit($reportLayout);
}
// Also check for report exceptions
if (!empty($json->Report_Header->Exceptions)) {
//https://www.projectcounter.org/appendix-f-handling-errors-exceptions/
$reportFatalCodes = array(1000, 1010, 1020, 1030);
$reportErrorCodes = array(2000, 2010, 2020, 3000, 3010, 3020, 3030);
$errorsOrFatalCount = 0;
foreach($json->Report_Header->Exceptions as $exception) {
$aAn = 'a';
if (in_array($exception->Code, $reportFatalCodes)) {
$errorKey = 'fatal';
$errorsOrFatalCount += 1;
} elseif (in_array($exception->Code, $reportErrorCodes)) {
$errorKey = 'error';
$aAn = 'an';
$errorsOrFatalCount += 1;
} else {
$errorKey = 'warning';
}
$message = "Received $aAn $errorKey message from $serviceProvider: $exception->Message";
if (!empty($exception->Data)) {
$message .= " Additional Data: $exception->Data";
}
if (!empty($exception->Help_URL)) {
$message .= " Help Url: $exception->Help_URL";
}
$this->log($message);
}
if ($errorsOrFatalCount > 0) {
$this->logStatus($this->logStatus("Received $errorsOrFatalCount errors from $serviceProvider. Details can be found in the log."));
$this->saveLogAndExit($reportLayout);
}
}
$this->log("$reportLayout successfully retrieved from $serviceProvider for start date: $this->startDate, end date: $this->endDate");
$this->log("");
$this->log("-- Sushi Transfer completed --");
return $response;
}
private function soapConnection($wsdl, $parameters)
{
$parameters = array_merge($parameters, array(
"keep_alive" => true,
"connection_timeout" => 1000,
"trace" => 1,
"exceptions" => 1,
"cache_wsdl" => WSDL_CACHE_NONE,
"stream_context" => stream_context_create(array(
'http' => array('protocol_version' => 1.0,
'header' => 'Content-Type: application/soap+xml')))
)
);
try {
try {
$client = new SoapClient($wsdl, $parameters);
//returns soapfault
} catch (Exception $e) {
$error = $e->__toString();
//if soap fault returned version mismatch or http headers error, try again with soap 1.2
if ((preg_match('/Version/i', $error)) || (preg_match('/HTTP/i', $error))) {
$this->log("Using Soap Version 1.2");
$parameters = array_merge($parameters, array("soap_version" => SOAP_1_2));
//try connection again with 1.2
$client = new SoapClient($wsdl, $parameters);
}
}
//throws soap fault
} catch (Exception $e) {
$error = $e->getMessage();
$this->logStatus("Failed to establish soap connection: " . $error);
$this->saveLogAndExit();
}
$this->log("");
$this->log("-- Soap Connection successfully completed --");
$this->log("");
return $client;
}
private function xmlTransfer($reportLayout, $serviceProvider)
{
//if report layout is BR and Release is 3, change it to 1
if ((preg_match('/BR/i', $reportLayout)) && ($this->releaseNumber == "3")) {
$releaseNumber = '1';
} else {
$releaseNumber = $this->releaseNumber;
}
$createDate = date("Y-m-d\TH:i:s.0\Z");
$id = uniqid("CORAL:", true);
if (($this->wsdlURL == '') || (strtoupper($this->wsdlURL) == 'COUNTER')) {
if ($this->releaseNumber == "4") {
$wsdl = 'http://www.niso.org/schemas/sushi/counter_sushi4_0.wsdl';
} else {
$wsdl = 'http://www.niso.org/schemas/sushi/counter_sushi3_0.wsdl';
}
} else {
$wsdl = $this->wsdlURL;
}
// look at $Security to ses if it uses an extension
if (preg_match('/Extension=/i', $this->security)) {
$extensions = array();
$varlist = explode(";", $this->security);
foreach ($varlist as $params) {
list($extVar, $extVal) = explode("=", $params);
$extensions[$extVar] = $extVal;
if ($extVar == 'Extension') {
$extension = $extVal;
}
}
}
if (!empty($extension)) {
include BASE_DIR . 'sushiincludes/extension_' . $extension . '.inc.php';
} else {
if (preg_match("/http/i", $this->security)) {
$this->log("Using HTTP Basic authentication via login and password.");
$parameters = array(
'login' => $this->login,
'password' => $this->password,
'location' => $this->serviceURL,
);
} else {
if ((strtoupper($this->wsdlURL) != 'COUNTER') && ($this->wsdlURL != '')) {
$this->log("Using provided wsdl: $wsdl");
$parameters = array();
} else {
$this->log("Using COUNTER wsdl, connecting to $this->serviceURL");
$parameters = array('location' => $this->serviceURL);
}
}
$client = $this->soapConnection($wsdl, $parameters);
}
if (preg_match("/wsse/i", $this->security)) {
// Prepare SoapHeader parameters
$strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
$objSoapVarUser = new SoapVar($this->login, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
$objSoapVarPass = new SoapVar($this->password, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, false);
// Prepare Soap Client
try {
$client->__setSoapHeaders(array($objSoapVarWSSEHeader));
} catch (Exception $e) {
$error = $e->getMessage();
$this->logStatus("Failed to connect to $serviceProvider: " . $error);
$this->log("Tried: " . var_dump($client));
$this->saveLogAndExit($reportLayout);
}
}
$startDate = new DateTime($this->startDate);
$startDate->modify('first day of this month');
$endDate = new DateTime($this->endDate);
$endDate->modify('last day of this month');
// End Date is earlier than start date
if ($startDate > $endDate) {
$this->logStatus("Invalid Dates entered. Must enter a start before end date.");
$this->saveLogAndExit($reportLayout);
}
try {
$reportRequest = array
('Requestor' => array
('ID' => $this->requestorID,
'Name' => 'CORAL Processing',
'Email' => $this->requestorID
),
'CustomerReference' => array
('ID' => $this->customerID,
'Name' => 'CORAL Processing'
),
'ReportDefinition' => array
('Filters' => array
('UsageDateRange' => array
('Begin' => $startDate->format('Y-m-d'),
'End' => $endDate->format('Y-m-d')
)
),
'Name' => $reportLayout,
'Release' => $releaseNumber
),
'Created' => $createDate,
'ID' => $id,
'connection_timeout' => 1000
);
$dateError = FALSE;
$result = $client->GetReport($reportRequest);
} catch (Exception $e) {
$error = $e->getMessage();
$this->logStatus("Exception performing GetReport with connection to $serviceProvider: $error");
//exceptions seem to happen that don't matter, continue processing and if no data or error is found then it will quit.
//$this->saveLogAndExit($reportLayout);
}
$xml = $client->__getLastResponse();
// Check for errors
try {
$reader = XMLReader::xml($xml);
} catch (Exception $e) {
$error = $e->getMessage();
$this->logStatus("There was an error trying to parse the SUSHI report from $serviceProvider. This could be due to a malformed response from the sushi service. Error: $error");
$this->saveLogAndExit($reportLayout);
}
$message = "";
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT) {
if ($reader->localName == 'Severity') {
$reader->read();
$severity = trim($reader->value);
}
if ($reader->localName == 'Message') {
$reader->read();
$message = trim($reader->value);
}
}
}
$reader->close();
if ($message != "") {
if (($severity == "Error") || (stripos($message, "Error") !== FALSE)) {
$this->logStatus("Failed to request report from $serviceProvider: " . $message);
$this->log("Please fix the settings for this provider and try again.");
$this->saveLogAndExit($reportLayout);
} else {
$this->logStatus("$serviceProvider says: $severity: $message");
}
}
return $xml;
}
/*********************************************************************************************************************
* Parsers
*/
private function parseXML($fName, $reportLayout, $overwritePlatform, $serviceProvider)
{
//////////////////////////////////////
//PARSE XML!!
//////////////////////////////////////
// Setup report months
$reportMonths = $this->reportMonths($this->startDate, $this->endDate);
//read layouts ini file to get the available layouts
$metrics = parse_ini_file(BASE_DIR . "metrics.ini");
$string = file_get_contents($fName);
// Gets rid of all namespace references
$clean_xml = $this->stripNamespaces($string);
$xml = simplexml_load_string($clean_xml);
$report = array(
'header' => array(),
'rows' => array(),
);
//First - get report information
$data = $xml->Body->ReportResponse->Report->Report;
$report['header']['id'] = $data->attributes()->Name;
$report['header']['release'] = $data->attributes()->Version;
$report['header']['months'] = $reportMonths;
foreach ($data->Customer->ReportItems as $resource) {
//reset variables
/**
* Each $reportArray is slightly different
* JR1: Need aggregated count columns of ytd, ytdPDF, ytdHTML
* BR1: Need aggregated count columns of ytd
* DB1: Need separate rows based on activity type, but aggregated counts for those activity types
*/
$row = array();
if ($overwritePlatform) {
$row['platform'] = $serviceProvider;
} else {
$row['platform'] = $resource->ItemPlatform[0]->__toString();
}
$row['publisher'] = $resource->ItemPublisher->__toString();
$row['title'] = $resource->ItemName->__toString();
foreach ($resource->ItemIdentifier as $identifier) {
$idType = strtoupper($identifier->Type);
if (!(strrpos($idType, 'PRINT') === false) && !(strrpos($idType, 'ISSN') === false)) {
$row['issn'] = $identifier->Value->__toString();
} else if (!(strrpos($idType, 'ONLINE') === false) && !(strrpos($idType, 'ISSN') === false)) {
$row['eissn'] = $identifier->Value->__toString();
} else if (!(strpos($idType, 'PRINT') === false) && !(strpos($idType, 'ISBN') === false)) {
$row['isbn'] = $identifier->Value->__toString();
} else if (!(strpos($idType, 'ONLINE') === false) && !(strpos($idType, 'ISBN') === false)) {
$row['eisbn'] = $identifier->Value->__toString();
} else if (!(strpos($idType, 'DOI') === false)) {
$row['doi'] = $identifier->Value->__toString();
} else if (!(strpos($idType, 'PROPRIETARY') === false)) {
$row['pi'] = $identifier->Value->__toString();
}
}
// Get all possible metric types for the resource
$metricTypes = array();
foreach ($resource->ItemPerformance as $monthlyStat) {
foreach ($monthlyStat->Instance as $metricStat) {
$type = $metricStat->MetricType->__toString();
if (!in_array($type, $metricTypes)) {
$metricTypes[] = $type;
}
}
}
$stashedRow = $row;
foreach ($metricTypes as $type) {
$metricRow = $stashedRow;
$metricRow['activityType'] = empty($metrics[$type]) ? $type : $metrics[$type];
$metricRow['ytd'] = 0;
$metricRow['months'] = array();
foreach ($reportMonths as $month) {
$metricRow['months'][$month['columnName']] = 0;
foreach ($resource->ItemPerformance as $monthlyStat) {
if ($month['start'] == $monthlyStat->Period->Begin) {
foreach ($monthlyStat->Instance as $metricStat) {
if ($metricStat->MetricType->__toString() == $type) {
$metricRow['months'][$month['columnName']] = intval($metricStat->Count);
}
}
}
}
}
$metricRow['ytd'] = array_reduce($metricRow['months'], function ($carry, $item) {
$carry += $item;
return $carry;
});
$report['rows'][] = $metricRow;
}
}
return $report;
}
private function parseJson($fName, $reportLayout, $overwritePlatform, $serviceProvider)
{
// Setup report months
$reportMonths = $this->reportMonths($this->startDate, $this->endDate);
$string = file_get_contents($fName);
$data = json_decode($string, true);
$report = array(
'header' => array(),
'rows' => array(),
);
$report['header']['id'] = $data['Report_Header']['Report_ID'];
$report['header']['release'] = $data['Report_Header']['Release'];
$report['header']['months'] = $reportMonths;
foreach ($data['Report_Items'] as $resource) {
$row = array();
// platform
if ($overwritePlatform) {
$row['platform'] = $serviceProvider;
} else {
$row['platform'] = $resource['Platform'];
}
// Platform reports need to have the platform as the title
if (preg_match("/pr/i", $reportLayout)) {
$row['title'] = $row['platform'];
}
// Publisher ID
if (is_array($resource) && array_key_exists('Publisher_ID', $resource)) {
if (count($resource['Publisher_ID'])) {
$row['publisherID'] = strtolower($resource['Publisher_ID'][0]['Type']) . '=' . $resource['Publisher_ID'][0]['Value'];
}
}
// all string values
foreach (array_keys($resource) as $key) {
if (is_array($resource[$key]) || is_object($resource[$key]) || $key == 'Platform') {
continue;
}
$row[$this->r5Attr($key)] = $resource[$key];
}
// contributors (for Item reports)
if (isset($resource['Item_Contributors']) && is_array($resource['Item_Contributors'])) {
$authorsArray = array();
foreach($resource['Item_Contributors'] as $author) {
$string = $author['Name'];
if ($author['Identifier']) {
$string .= '(' . $author['Identifier'] . ')';
}
$authorsArray[] = $string;
}
$row['authors'] = implode(';', $authorsArray);
}
// publication date (for Item reports)
if (isset($resource['Item_Dates']) && is_array($resource['Item_Dates'])) {
foreach($resource['Item_Dates'] as $date) {
if($date['Type'] == 'Publication_Date') {
$row['publicationDate'] = $date['Value'];
}
}
}
// article version (for Item reports)
if (isset($resource['Item_Attributes']) && is_array($resource['Item_Attributes'])) {
foreach($resource['Item_Attributes'] as $attr) {
if($attr['Type'] == 'Article_Version') {
$row['articleVersion'] = $attr['Value'];
}
}
}
// parent (for Item reports)
if (isset($resource['Item_Parent'])) {
$parent = isset($resource['Item_Parent']['Item_Name']) ? $resource['Item_Parent'] : $resource['Item_Parent'][0];
$row['parentTitle'] = $parent['Item_Name'];
$row['parentDataType'] = isset($parent['Data_Type']) ? $parent['Data_Type'] : '';
if (isset($parent['Item_ID']) && is_array($parent['Item_ID'])) {
foreach ($parent['Item_ID'] as $id) {
$row[$this->r5Attr('Parent_'.$id['Type'])] = $id['Value'];
}
}
}
// component (for Item reports)
if (isset($resource['Item_Component'])) {
$parent = isset($resource['Item_Component']['Item_Name']) ? $resource['Item_Component'] : $resource['Item_Component'][0];
$row['componentTitle'] = $parent['Item_Name'];
$row['componentDataType'] = isset($parent['Data_Type']) ? $parent['Data_Type'] : '';
if (isset($parent['Item_ID']) && is_array($parent['Item_ID'])) {
foreach ($parent['Item_ID'] as $id) {
$row[$this->r5Attr('Component_'.$id['Type'])] = $id['Value'];
}
}
}
if (array_key_exists('Item_ID', $resource)) {
// identifiers
foreach ($resource['Item_ID'] as $id) {
$row[$this->r5Attr($id['Type'])] = $id['Value'];
}
}
// Get all possible metric types for the resource
$metricTypes = array();
foreach ($resource['Performance'] as $monthlyStat) {
foreach ($monthlyStat['Instance'] as $metricStat) {
$type = $metricStat['Metric_Type'];
if (!in_array($type, $metricTypes)) {
$metricTypes[] = $type;
}
}
}
$stashedRow = $row;
foreach ($metricTypes as $type) {
$metricRow = $stashedRow;
$metricRow['activityType'] = $type;
$metricRow['months'] = array();
foreach ($reportMonths as $month) {
$metricRow['months'][$month['columnName']] = 0;
foreach ($resource['Performance'] as $monthlyStat) {
if ($month['start'] == $monthlyStat['Period']['Begin_Date']) {
foreach ($monthlyStat['Instance'] as $metricStat) {
if ($metricStat['Metric_Type'] == $type) {
$metricRow['months'][$month['columnName']] = intval($metricStat['Count']);
}
}
}
}
}
$metricRow['ytd'] = array_reduce($metricRow['months'], function ($carry, $item) {
$carry += $item;
return $carry;
});
if ($metricRow['ytd'] > 0) {
$report['rows'][] = $metricRow;
}
}
}
return $report;
}
/*********************************************************************************************************************
* Loggers
*/
//status for storing in DB and displaying in rows
private function logStatus($logText)
{
array_push($this->statusLog, $logText);
array_push($this->detailLog, $logText);
}
//longer log for storing in log file and displaying output
private function log($logText)
{
array_push($this->detailLog, $logText);
}
//logs process to import log table and to log file
public function saveLogAndExit($reportLayout = NULL, $filename = NULL, $success = FALSE)
{
//First, delete any preexisting Failured records, these shouldn't be needed/interesting after this.
$this->log("Cleaning up prior failed import logs....");
$this->getPublisherOrPlatform->removeFailedSushiImports;
if (!$filename) {
$logFilename = strtotime("now") . '.txt';
} else {
$logFilename = $filename;
}
$logFileLocation = 'logs/' . $logFilename;
$this->log("Log File Name: $logFileLocation");
if ($success) {
$this->logStatus("Finished processing " . $this->getServiceProvider . ": $reportLayout.");
}
//save the actual log file
$fp = fopen(BASE_DIR . $logFileLocation, 'w');
fwrite($fp, implode("\n", $this->detailLog));
fclose($fp);
//save to import log!!
$importLog = new ImportLog();
$importLog->loginID = "sushi";