-
Notifications
You must be signed in to change notification settings - Fork 1
/
InnopacWeb.php
1195 lines (940 loc) · 32.2 KB
/
InnopacWeb.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
/**
* Get bibliographic data, item status, course reserves, and patron information from an Innovative
* Interfaces Catalog. Conforms to the DLF ILS Discovery Interface Task Group (ILS-DI) Technical
* Recommendation 1.0.
*
* @author David Walker
* @copyright 2008 The California State University
* @link http://xerxes.calstate.edu
* @version 2.0
* @package Mango
* @license http://www.gnu.org/licenses/
*/
class InnopacWeb
{
private $strServer = ""; // server address
private $strIndex = ""; // search index
private $strPhrase = ""; // search phrase
private $strNormalized = ""; // search phrase encoded for url
private $iTotal = 0; // total number of hits in a search
private $iScope = null; // limit: numeric scope
private $arrLimit = array ( ); // limits
private $arrURL = array ( ); // list of url's accessed, mostly for debugging
private $bolInnReach = false; // whether this is an innreach system
private $strMarcNS = "http://www.loc.gov/MARC21/slim"; // marc namespace
/**
* Constructor
*
* @param string $strServer server address
*/
public function __construct($strServer)
{
if ( substr( $strServer, strlen( $strServer ) - 1, 1 ) == "/" )
{
$strServer = substr( $strServer, 0, strlen( $strServer ) - 1 );
}
$this->strServer = $strServer;
}
### HARVESTING FUNCTIONS
public function harvestBibliographicRecords()
{
throw new InnopacWeb_NotSupportedException("harvestBibliographicRecords not currently supported");
}
public function harvestExpandedRecords()
{
throw new InnopacWeb_NotSupportedException("harvestExpandedRecords not currently supported");
}
public function harvestAuthorityRecords()
{
throw new InnopacWeb_NotSupportedException("harvestAuthorityRecords not currently supported");
}
public function harvestHoldingsRecords()
{
throw new InnopacWeb_NotSupportedException("harvestHoldingsRecords not currently supported");
}
### REAL-TIME SEARCH FUNCTIONS
/**
* Given a set of bibliographic or item identifiers, returns a list with availability of the
* items associated with the identifiers.
*
* @param string $id list of either bibliographic or item identifiers, seperate multiple ids by space
* @param string $id_type the type of record identifier being used in $id, currently only 'bibliographic'
* @return array a list of item availability objects that represent all the availability of the
* items associated with the requested bibliographic / item identifiers
*/
public function getAvailability($id, $id_type)
{
$arrResults = array ( ); // results array
$x = 0; // counter to keep track of found records
$arrIDs = explode( " ", $id ); // ids as array
if ( $id_type != "bibliographic")
{
throw new InnopacWeb_UnsupportedTypeException("currently only supports availability look-up by bibliographic id");
}
foreach ( $arrIDs as $strBibId )
{
// get marc page
$strResponse = $this->getRecordMarcPage($strBibId);
if ($strResponse != null )
{
// extract availability info and add to list
$arrItems = $this->extractHoldings($strResponse);
$arrResults[$strBibId] = $arrItems;
// mark that we got it
$x++;
}
else
{
$arrResults[$strBibId] = null;
}
}
if ( $x == 0)
{
return null;
}
else
{
return $arrResults;
}
}
/**
* Given a list of record identifiers, returns a list of record objects that contain
* bibliographic information, as well as associated holdings and item information.
*
* @param string $id one or more id numbers, seperate multiple ids by space
* @param string $schema defines the metadata schema in which the records are returned, currently only marc-xml
* @return array a list of record objects that represents the bibliographic record, as well as
* associated holdings and item information for each requested bibliographic identifier
*/
public function getRecords($id, $schema = 'marc-xml')
{
$arrResults = array ( ); // results array
$x = 0; // counter to keep track of found records
$arrIDs = explode( " ", $id ); // ids as array
if ( $schema != "marc-xml")
{
throw new InnopacWeb_UnsupportedSchemaException("marc-xml is the only currently supported schema");
}
foreach ( $arrIDs as $strBibId )
{
$strResponse = $this->getRecordMarcPage($strBibId);
if ( $strResponse != null )
{
// get record and add to list
$objRecord = $this->extractRecord( $strResponse );
$arrResults[$strBibId] = $objRecord;
// mark it as found
$x++;
}
else
{
$arrResults[$strBibId] = null;
}
}
if ( $x == 0 )
{
$this->iTotal = 0;
return null;
}
else
{
$this->iTotal = 1;
return $arrResults;
}
}
/**
* Returns a list of records in the ILS matching a search query
*
* @param string $index one letter index to search on, this will differ depending on catalog
* @param string $query search phrase
* @param string $schema [optional] schema for bibliographic records, only option now is marc-xml
* @param string $recordType [optional] fullness of the record response, only option now is 'full'
* @param int $offset [optional] starting record in the set to return, defaults to 1
* @param int $max [optional] maximum number of records to return, defaults to 10
* @param string $sort [optional] sort order for keyword searches, defaults to date, acceptable values:
* - 'D' date
* - 'R' rank
* - 'A' alphabetical
* @return array array of Record objects with attached Item objects
*/
public function search($index, $query, $schema = "marc-xml", $recordType = "full", $offset = 1, $max = 10, $sort = "D")
{
// type check
if ( ! is_int( $offset ) )
{
throw new Exception( "param 4 (offset) must be of type int" );
}
if ( ! is_int( $max ) )
{
throw new Exception( "param 5 (max) must be of type int" );
}
if ( $schema != "marc-xml")
{
throw new InnopacWeb_UnsupportedSchemaException("marc-xml is the only currently supported schema");
}
if ( $recordType != "full")
{
throw new InnopacWeb_UnsupportedTypeException("'full' is the only supported record type");
}
// search and retrieve records
$this->iTotal = $this->doSearch( $index, $query );
return $this->retrieve( $offset, $max, $sort );
}
public function scan()
{
throw new InnopacWeb_NotSupportedException("scan not currently supported");
}
public function getAuthorityRecords()
{
throw new InnopacWeb_NotSupportedException("getAuthorityRecords not currently supported");
}
public function searchCourseReserves()
{
throw new InnopacWeb_NotSupportedException("searchCourseReserves not currently supported");
}
public function explain()
{
throw new InnopacWeb_NotSupportedException("explain not currently supported");
}
### PATRON FUNCTIONS ###
public function lookupPatron()
{
throw new InnopacWeb_NotSupportedException("lookupPatron not currently supported");
}
public function authenticatePatron()
{
throw new InnopacWeb_NotSupportedException("authenticatePatron not currently supported");
}
public function getPatronInfo()
{
throw new InnopacWeb_NotSupportedException("getPatronInfo not currently supported");
}
public function getPatronStatus()
{
throw new InnopacWeb_NotSupportedException("getPatronStatus not currently supported");
}
public function getServices()
{
throw new InnopacWeb_NotSupportedException("getServices not currently supported");
}
public function renewLoan()
{
throw new InnopacWeb_NotSupportedException("renewLoan not currently supported");
}
public function holdTitle()
{
throw new InnopacWeb_NotSupportedException("holdTitle not currently supported");
}
public function holdItem()
{
throw new InnopacWeb_NotSupportedException("holdItem not currently supported");
}
public function cancelHold()
{
throw new InnopacWeb_NotSupportedException("cancelHold not currently supported");
}
public function recallItem()
{
throw new InnopacWeb_NotSupportedException("recallItem not currently supported");
}
public function cancelRecall()
{
throw new InnopacWeb_NotSupportedException("cancelRecall not currently supported");
}
### OPAC INTERACTION FUNCTIONS ###
public function goToBibliographicRequestPage($bibid)
{
return $this->strServer . "/record=" . $bibid;
}
public function outputRewritablePage()
{
throw new InnopacWeb_NotSupportedException("outputRewritablePage not currently supported");
/*
$strResponse = file_get_contents($this->strServer . "/record=" . $bibid);
$strResponse = str_ireplace("<base ", "<base href=\"" . $this->strServer . "/\" ", $strResponse);
return $strResponse;
*/
}
public function outputIntermediateFormat()
{
throw new InnopacWeb_NotSupportedException("outputIntermediateFormat not currently supported");
}
### PRIVATE FUNCTIONS ###
/**
* Return the MARC record page
*
* @param string $strBibId bib id
* @return string URI path
*/
private function getRecordMarcPage($strBibId)
{
$strId = substr( $strBibId, 1 );
$strQuery = "/search/.$strBibId/.$strBibId/1,1,1,B/detlmarc~$strId&FF=&1,0,";
$strResponse = file_get_contents( $this->strServer . $strQuery );
array_push( $this->arrURL, $this->strServer . $strQuery );
if ( ! stristr($strResponse, "<pre>") )
{
// didn't find a record
return null;
}
else
{
return $strResponse;
}
}
/**
* Initiate a search using the supplied query
*
* @param string $strIndex one letter index to search on, this will differ depending on catalog
* @param string $strPhrase search phrase
* @return int number of records found
*/
private function doSearch($strIndex, $strPhrase)
{
$strQuery = ""; // query part of url
$strResponse = ""; // html response from the catalog
$arrMatches = array ( ); // regex match holding array
// set the values into the object property
$this->strIndex = $strIndex;
$this->strPhrase = $strPhrase;
// normalize the search phrase for urls
$this->strNormalized = strtolower( $strPhrase );
// browse (i.e., non-keyword) searches require a special set
// of normalization rules
if ( $strIndex != "X" && $strIndex != "Y" )
{
// replace all non-indexed punctuation with space;
// indexed punctuation includes: @ # $ + |
// special handling below for: ' & -
$this->strNormalized = preg_replace( "/[^a-zA-Z0-9 \@\#\$\+\|\&\-\']/", " ", $this->strNormalized );
// special cases
$this->strNormalized = str_replace( "'", "", $this->strNormalized );
$this->strNormalized = str_replace( " & ", " and ", $this->strNormalized );
// standard numbers should have the dash removed completely
if ( $strIndex == "i" )
{
$this->strNormalized = str_replace( "-", "", $this->strNormalized );
}
else
{
$this->strNormalized = str_replace( "-", " ", $this->strNormalized );
}
// remove leading article for title searches
if ( substr( $this->strNormalized, 0, 2 ) == "a " )
{
$this->strNormalized = substr( $this->strNormalized, 2 );
}
elseif ( substr( $this->strNormalized, 0, 3 ) == "an " )
{
$this->strNormalized = substr( $this->strNormalized, 3 );
}
elseif ( substr( $this->strNormalized, 0, 4 ) == "the " )
{
$this->strNormalized = substr( $this->strNormalized, 4 );
}
// remove double-spaces
while ( strstr( $this->strNormalized, " " ) )
{
$this->strNormalized = str_replace( " ", " ", $this->strNormalized );
}
// convert all accented characters to their latin equivalent
$this->strNormalized = strtr( $this->strNormalized, "¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy" );
// pad certain occurances of numbers with added spaces:
// +++++++9 if the search started with a number-space
// ++++9 for any space-number in the search not at the beginning
$this->strNormalized = preg_replace( "/ ([0-9]{1})/", "++++$1", $this->strNormalized );
$this->strNormalized = preg_replace( "/^([0-9]{1}) /", "+++++++$1", $this->strNormalized );
}
$this->strNormalized = urlencode( $this->strNormalized );
// build base url query
$strQuery = "/search/" . $strIndex . "?" . urlencode( $strPhrase );
// add limits
$strQuery .= $this->appendLimits();
// get initial search results page
$strResponse = file_get_contents( $this->strServer . $strQuery );
array_push( $this->arrURL, $this->strServer . $strQuery );
// extract the total number of hits in the results page;
if ( preg_match( "/Entries<br \/> ([0-9]{1,10}) Found/", $strResponse, $arrMatches ) != 0 )
{
return $arrMatches[1]; // browse search
}
elseif ( preg_match( "/\(1-[0-9]{1,2} of ([0-9]{1,10})\)/", $strResponse, $arrMatches ) != 0 )
{
return $arrMatches[1]; // keyword search
}
elseif ( ! stristr( $strResponse, "No matches found" ) && ! stristr( $strResponse, "NO ENTRIES FOUND" ) )
{
return 1; // only found one response, catalog jumped right to full display
}
else
{
return 0;
}
}
/**
* Retrieve records from a previously initiated search
*
* @param int $iStart starting record in the set to return, defaults to 1
* @param int $iLimit maximum number of records to return, defaults to 10
* @param string $strSort [optional] sort order for keyword searches, defaults to date, acceptable values:
* - 'D' date
* - 'R' rank
* - 'A' alphabetical
* @return array array of DOMDocuments as MARC-XML
*/
private function retrieve($iStart, $iLimit, $strSort = "D")
{
// type check
if ( ! is_int( $iStart ) )
{
throw new Exception( "param 1 (start record) must be of type int" );
}
if ( ! is_int( $iLimit ) )
{
throw new Exception( "param 2 (limit) must be of type int" );
}
$arrResults = array ( ); // results array
if ( $this->iTotal > 0 )
{
// set the end point in the range
$iEnd = $iStart + ($iLimit - 1);
if ( $iEnd > $this->iTotal )
{
$iEnd = $this->iTotal;
}
// some adjustments for keyword searches
if ( $this->strIndex == "X" )
{
$this->strNormalized = "(" . $this->strNormalized . ")";
}
// cycle through the results, pulling back each
// record and adding it to the response
while ( $iStart <= $iEnd )
{
$strRecord = "";
$strResponse = "";
$objRecord = new DOMDocument( );
// build the url query for each individual record
$strRecord = "/search?/" . $this->strIndex . $this->strNormalized . "&SORT=" . $strSort . $this->appendLimits() . "/" . $this->strIndex . $this->strNormalized . "&SORT=" . $strSort . $this->appendLimits() . "/" . $iStart . "," . $this->iTotal . "," . $this->iTotal . ",B/detlmarc&FF=" . $this->strIndex . $this->strNormalized . "&SORT=" . $strSort . $this->appendLimits() . "&" . $iStart . "," . $iStart . ",";
// get marc display html page from the server
// and convert it to XML
$strResponse = file_get_contents( $this->strServer . $strRecord );
array_push( $this->arrURL, $this->strServer . $strRecord );
$objRecord = $this->extractRecord( $strResponse );
// add the document to the results array
$arrResults[$iStart] = $objRecord;
$iStart ++;
}
}
return $arrResults;
}
/**
* Append the param limits set in properties below
*
* @return string URL params
*/
private function appendLimits()
{
$strReturn = "";
if ( $this->iScope != null )
{
$strReturn .= "&searchscope=" . $this->iScope;
}
foreach ( $this->arrLimit as $key => $value )
{
$strReturn .= "&$key=" . urlencode( $value );
}
return $strReturn;
}
/**
* Convenience class to get both the marc record and the item objects
*
* @param string $strResponse HTML MARC display page from catalog
* @return DOMDocument MARC-XML response
*/
private function extractRecord($strResponse)
{
$objXml = $this->extractMarc( $strResponse );
$arrHoldings = $this->extractHoldings( $strResponse );
$objRecord = new InnopacWeb_Record( );
$objRecord->bibliographicRecord = $objXml;
$objRecord->items = $arrHoldings;
return $objRecord;
}
/**
* Extracts data from the holdings table in the HTML response
*
* @param string $strHtml html response from catalog
* @param bool $bolRecursive [optional] whether this is a recursive call from inside the function
* @return array array of Item objects
*/
private function extractHoldings($strHtml, $bolRecursive = false)
{
$bolOrderNote = false; // whether holdings table shows an ordered noted
$bolFieldNotes = false; // whether holdings table includes comments indicating item record marc fields
// see if there is more than one page of holdings, in which case get the
// expanded (holdings) page instead; performance hit, but gotta do it
if ( stristr($strHtml, "additional copies") )
{
if ( $bolRecursive == true )
{
throw new Exception("recursion error getting additional copies");
}
$strHoldingsUrl = ""; // uri to full holdings list
$arrMatches = array(); // matches from the regex below
// get all the post form actions, one of them will be for the
// function that gets the additional items (holdings) page
if ( preg_match_all("/<form method=\"post\" action=\"([^\"]*)\"/", $strHtml, $arrMatches, PREG_PATTERN_ORDER))
{
foreach($arrMatches[1] as $strPostUrl)
{
if ( stristr($strPostUrl, "/holdings") )
{
$strHoldingsUrl = $this->strServer . $strPostUrl;
break;
}
}
}
// get the full response page now and redo the function call
$strResponse = file_get_contents($strHoldingsUrl);
return $this->extractHoldings($strResponse, true);
}
$strTable = ""; // characters that mark the start of the holding table
$arrHoldings = array ( ); // master array we'll used to hold the holdings
// check to see if there are holdings as well as item records
if ( strpos( $strHtml, "class=\"bibHoldings\">" ) !== false )
{
// do something!
return null;
}
// look to see which template this is and adjust the
// parser to accomadate the html structure
if ( strpos( $strHtml, "class=\"bibItems\">" ) !== false )
{
// most library innopac systems
$strTable = "class=\"bibItems\">";
}
elseif ( strpos( $strHtml, "BGCOLOR=#DDEEFF>" ) !== false )
{
// old innreach system
$strTable = "BGCOLOR=#DDEEFF>";
}
elseif ( strpos( $strHtml, "centralHolding" ) !== false )
{
// newer innreach system
$strTable = "centralHolding";
}
elseif ( strpos( $strHtml, "class=\"bibOrder" ) !== false )
{
// this is just a note saying the item has been ordered
$strTable = "class=\"bibOrder";
$bolOrderNote = true;
}
elseif ( strpos( $strHtml, "class=\"bibHoldings" ) !== false )
{
$strTable = "class=\"bibHoldings";
}
elseif ( strpos( $strHtml, "class=\"bibDetail" ) !== false )
{
$strTable = "class=\"bibDetail";
}
else
{
return $arrHoldings;
}
// narrow the page initially to the holdings table
$strHtml = $this->removeLeft( $strHtml, $strTable );
$strHtml = $this->removeRight( $strHtml, "</table>" );
// we'll use the table row as the delimiter of each holding
while ( strstr( $strHtml, "<tr" ) )
{
$arrItem = array ( );
$strItem = "";
// remove everything left of the table row, dump the row content into a
// local variable, while removing it from the master variable so
// our loop above continues to cycle thru the results
$strHtml = $this->removeLeft( $strHtml, "<tr" );
$strItem = "<tr" . $this->removeRight( $strHtml, "</tr>" );
$strHtml = $this->removeLeft( $strHtml, "</tr>" );
// make sure this isn't the header row
if ( strpos( $strItem, "<th" ) === false )
{
// extract any url in item, especially for InnReach
$strUrl = null;
$arrUrl = array ( );
if ( preg_match( "/<a href=\"([^\"]{1,})\">/", $strItem, $arrUrl ) )
{
$strUrl = $arrUrl[1];
}
// replace the item record marc field comments with place holders
// so we can grab them later after removing the html tags
$strItem = preg_replace("/<\!-- field (.{1}) -->/", "+~\$1~", $strItem);
// strip out tags and non-breaking spaces
$strItem = strip_tags( $strItem );
$strItem = str_replace( " ", "", $strItem );
// normalize spaces
while ( strstr( $strItem, " " ) )
{
$strItem = str_replace( " ", " ", $strItem );
}
$strItem = trim( $strItem );
// now split the remaining data out into an array
$arrItem = array();
// the display included the item record field comments, in which
// case we will use these over a general column-based approach
// since it is more precise; this should be the case on all local systems
if ( strstr($strItem, "+~"))
{
$bolFieldNotes = true;
$arrItemTemp = explode( "+~", $strItem );
foreach ($arrItemTemp as $strItemTemp)
{
if ( strstr($strItemTemp, "~") )
{
$arrItemField = explode("~", $strItemTemp);
$strFieldKey = trim($arrItemField[0]);
$strFieldValue = trim($arrItemField[1]);
$arrItem[$strFieldKey] = $strFieldValue;
}
}
}
else
{
$arrItem = explode( "\n", $strItem );
// add url back into the array
if ( $strUrl != null )
{
array_push( $arrItem, $strUrl );
}
}
// final clean-up, assignment
$objItem = new InnopacWeb_Item();
if ( $bolFieldNotes == true )
{
foreach ( $arrItem as $key => $strData )
{
switch ( $key )
{
case "1" :
$objItem->location = $strData;
break;
case "C" :
$objItem->call_number = $strData;
break;
case "v" :
$objItem->volume = $strData;
break;
case "%" :
$objItem->status = $strData;
break;
}
}
}
else
{
for ( $x = 0 ; $x < count( $arrItem ) ; $x ++ )
{
$strData = trim( $arrItem[$x] );
if ( $bolOrderNote == true )
{
$objItem->status = "ON ORDER";
$objItem->note = $strData;
}
elseif ( $this->bolInnReach == true )
{
switch ( $x )
{
case 0 :
$objItem->institution = $strData;
break;
case 1 :
$objItem->location = $strData;
break;
case 2 :
// note for accessing item online
$objItem->note = $strData;
break;
case 3 :
// this is a link if the second position had an
// online access note
if ( $objItem->note != "" )
{
$objItem->link = $strData;
}
else
{
$objItem->call_number = $strData;
}
break;
case 4 :
$objItem->status = $strData;
break;
}
}
else
{
switch ( $x )
{
case 0 :
$objItem->location = $strData;
break;
case 2 :
$objItem->call_number = $strData;
break;
case 3 :
$objItem->status = $strData;
break;
}
}
}
}
$arrMatches = array();
if ( preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2})/", $objItem->status, $arrMatches) )
{
$objDate = new DateTime($arrMatches[1] . "/" . $arrMatches[2] . "/" . $arrMatches[3]);
$objItem->dateAvailable = $objDate;
}
array_push( $arrHoldings, $objItem );
}
}
return $arrHoldings;
}
/**
* Extracts the MARC data from the HTML response and converts it to MARC-XML
*
* @param string $strMarc marc data as string
* @return DOMDocument marc-xml document
*/
private function extractMarc($strResponse)
{
$objXml = new DOMDocument( );
$objXml->recover = true;
$strMarc = ""; // marc data as text
$arrTags = array ( ); // array to hold each MARC tag
// parse out MARC data
$strMarc = $this->removeLeft( $strResponse, "<pre>" );
$strMarc = $this->removeRight( $strMarc, "</pre>" );
// remove break-tabs for easier parsing
$strMarc = str_replace( "\n ", "", $strMarc );
$strMarc = trim( $strMarc );
// assign the marc values to the array based on Unix LF as delimiter
$arrTags = explode( "\n", $strMarc );
// begin building the XML response
$objXml->loadXML( "<record xmlns=\"http://www.loc.gov/MARC21/slim\" />" );
foreach ( $arrTags as $strTag )
{
// assign tag # and identifiers
$strTagNumber = substr( $strTag, 0, 3 );
$strId1 = substr( $strTag, 4, 1 );
$strId2 = substr( $strTag, 5, 1 );
// assign data and clean it up
$strData = substr( $strTag, 7 );
$strData = utf8_encode( $strData );
$strData = $this->escapeXml( $strData );
$strData = trim( $strData );
if ( $strTagNumber == "LEA" )
{
// leader
$objLeader = $objXml->createElementNS( $this->strMarcNS, "leader", $strData );
$objXml->documentElement->appendChild( $objLeader );
}
elseif ( $strTagNumber == "REC" )
{
// Pseudo-MARC "REC" data field to store the INNOPAC
// bibliographic record number in subfield a.
$objRecNum = $objXml->createElementNS( $this->strMarcNS, "datafield" );
$objRecNum->setAttribute( "tag", "REC" );
$objRecNum->setAttribute( "ind1", ' ' );
$objRecNum->setAttribute( "ind2", ' ' );
$objRecNumSub = $objXml->createElementNS( $this->strMarcNS, "subfield", strtolower( $strData ) );
$objRecNumSub->setAttribute( "code", 'a' );
$objRecNum->appendChild( $objRecNumSub );
$objXml->documentElement->appendChild( $objRecNum );
}
elseif ( ( int ) $strTagNumber <= 8 )
{
// control fields
$objControlField = $objXml->createElementNS( $this->strMarcNS, "controlfield", $strData );
$objControlField->setAttribute( "tag", $strTagNumber );
$objXml->documentElement->appendChild( $objControlField );
}
else
{
// data fields
$objDataField = $objXml->createElementNS( $this->strMarcNS, "datafield" );
$objDataField->setAttribute( "tag", $strTagNumber );
$objDataField->setAttribute( "ind1", $strId1 );
$objDataField->setAttribute( "ind2", $strId2 );
// if first character is not a pipe symbol, then this is the default |a subfield
// so make that explicit for the array
if ( substr( $strData, 0, 1 ) != "|" )
{
$strData = "|a " . $strData;
}
// split the subfield data on the pipe and add them in using the first
// character after the delimiter as the subfield code
$arrSubFields = explode( "|", $strData );
foreach ( $arrSubFields as $strSubField )
{
if ( $strSubField != "" )