commit | author | age
|
175a52
|
1 |
<?php |
JK |
2 |
define('RECORD_SEPERATOR', chr(0x1e)); |
|
3 |
define('UNIT_SEPERATOR', chr(0x1f)); |
|
4 |
|
|
5 |
class MARC21 { |
|
6 |
static function from_string($data) { |
|
7 |
$lead_len = 24; |
|
8 |
$lead = substr($data, 0, $lead_len); |
|
9 |
|
|
10 |
$file_length = substr($lead, 0, 5); |
|
11 |
$head_len = substr($lead, 12, 5); |
|
12 |
|
|
13 |
$cat_record_lof = substr($lead, 20, 1); |
|
14 |
$cat_record_scp = substr($lead, 21, 1); |
|
15 |
$cat_record_imp = substr($lead, 22, 1); |
|
16 |
$cat_record_len = 3 + $cat_record_lof + $cat_record_scp + $cat_record_imp; |
|
17 |
$cat_len = $head_len-$lead_len-1; |
|
18 |
|
|
19 |
$cat = substr($data, $lead_len, $cat_len); |
|
20 |
$info = substr($data, $head_len); |
|
21 |
|
|
22 |
$unit = FALSE; |
|
23 |
for($i=0; $i<$cat_len; $i += $cat_record_len) { |
|
24 |
$rec_num = substr($cat, $i, 3); |
|
25 |
|
|
26 |
if($rec_num>899) { |
|
27 |
continue; |
|
28 |
} |
|
29 |
|
|
30 |
$rec_len = substr($cat, $i+3, $cat_record_lof); |
|
31 |
$rec_start = substr($cat, $i+3+$cat_record_lof, $cat_record_scp); |
|
32 |
$rec = substr($info, $rec_start, $rec_len-1); |
|
33 |
|
|
34 |
$temp = array(); |
|
35 |
$unit = FALSE; |
|
36 |
$unit_letter = 'a'; |
|
37 |
|
|
38 |
for($j=0; $j<$rec_len; $j++) { |
|
39 |
$char = substr($rec, $j, 1); |
|
40 |
|
|
41 |
if(($j==0 || $j==1) && $rec_num>9 && $char!=' ') { |
|
42 |
if($j==0) { |
|
43 |
$temp['f0'] = $char; |
|
44 |
} |
|
45 |
elseif($j==1) { |
|
46 |
$temp['f1'] = $char; |
|
47 |
} |
|
48 |
} |
|
49 |
elseif($char == UNIT_SEPERATOR) { |
|
50 |
$unit = TRUE; |
|
51 |
} |
|
52 |
elseif($unit === TRUE) { |
|
53 |
$temp[$unit_letter] = trim($collect, ' :;,/'); |
|
54 |
$unit = FALSE; |
|
55 |
$unit_letter = $char; |
|
56 |
$collect = ''; |
|
57 |
} |
|
58 |
else |
|
59 |
{ |
|
60 |
$collect .= $char; |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
$temp[$unit_letter] = trim($collect, ' :;,/'); |
|
65 |
$collect = ''; |
|
66 |
|
|
67 |
$return[$rec_num][] = $temp; |
|
68 |
} |
|
69 |
|
|
70 |
return $return; |
|
71 |
} |
|
72 |
|
|
73 |
static function to_array($MARC) { |
|
74 |
if(!$MARC['020']) { |
|
75 |
$MARC['020'] = array(); |
|
76 |
} |
|
77 |
foreach($MARC['020'] as $value) { |
|
78 |
$value = (string)$value['a']; |
|
79 |
if(strlen($value)==9 AND strlen($value)!=13) { |
|
80 |
$value .= checksum::ISBN($value); |
|
81 |
} |
|
82 |
if( strlen($value) > strlen($ISBN) ) { |
|
83 |
$ISBN = $value; |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
if(!$MARC['022']) { |
|
88 |
$MARC['022'] = array(); |
|
89 |
} |
|
90 |
foreach($MARC['022'] as $value) { |
|
91 |
$value = (int)$value['a']; |
|
92 |
if( strlen($value) > strlen($ISSN) ) { |
|
93 |
$ISSN = $value; |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
if($MARC['100'][0]['f0']==1) { |
|
98 |
$autor = explode(', ', $MARC['100'][0]['a'], 2); |
|
99 |
$MARC['100'][0]['a'] = str_replace('.', '', $autor[1]).' '.$autor[0]; |
|
100 |
} |
|
101 |
|
|
102 |
if(empty($MARC['100'][0]['a'])) { |
|
103 |
$MARC['100'][0]['a'] = 'Praca zbiorowa'; |
|
104 |
} |
|
105 |
|
|
106 |
$MARC['260'][0]['b'] = str_replace( |
|
107 |
array( |
|
108 |
'Wydaw.', |
|
109 |
'Państ.', |
|
110 |
'Państw.', |
|
111 |
'PK', |
|
112 |
'Min.', |
|
113 |
), |
|
114 |
array( |
|
115 |
'Wydawnictwo', |
|
116 |
'Państwowy', |
|
117 |
'Państwowe', |
|
118 |
'Politechnika Krakowska', |
|
119 |
'Ministerstwa', |
|
120 |
), |
|
121 |
$MARC['260'][0]['b']); |
|
122 |
|
|
123 |
if($MARC['041'][0]['a'] == 'pol') { |
|
124 |
$MARC['041'][0]['a'] = 'polski'; |
|
125 |
} |
|
126 |
|
|
127 |
|
|
128 |
if(empty($MARC['041'][0]['a'])) { |
|
129 |
$MARC['041'][0]['a'] = 'polski'; |
|
130 |
} |
|
131 |
|
|
132 |
if(!empty($MARC['245'][0]['b'])) { |
|
133 |
$MARC['245'][0]['a'] = trim($MARC['245'][0]['a'], '().,\\/"\' ').'. '.ucfirst(trim($MARC['245'][0]['b'], '().,\\/"\' ')); |
|
134 |
} |
|
135 |
|
|
136 |
return array( |
|
137 |
'tytul' => trim($MARC['245'][0]['a'], '().,\\/"\' '), |
|
138 |
'autor' => trim($MARC['100'][0]['a'], '().,\\/"\' '), |
|
139 |
'rok' => trim($MARC['260'][0]['c'], '().,\\/"\' '), |
|
140 |
'miejsce' => trim($MARC['260'][0]['a'], '().,\\/"\' '), |
|
141 |
'wydawnictwo' => trim($MARC['260'][0]['b'], '().,\\/"\' '), |
|
142 |
'wydanie' => trim($MARC['250'][0]['a'], '().,\\/"\' '), |
|
143 |
'jezyk' => $MARC['041'][0]['a'], |
|
144 |
'ISBN' => $ISBN, |
|
145 |
'ISSN' => $ISSN, |
|
146 |
/* stan */ |
|
147 |
); |
|
148 |
} |
|
149 |
|
|
150 |
static function to_database($kod, $MARC) { |
|
151 |
if(!$MARC['020']) { |
|
152 |
$MARC['020'] = array(); |
|
153 |
} |
|
154 |
foreach($MARC['020'] as $value) { |
|
155 |
$value = (int)$value['a']; |
|
156 |
if( strlen($value) > strlen($ISBN) ) { |
|
157 |
$ISBN = $value; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
if(!$MARC['022']) { |
|
162 |
$MARC['022'] = array(); |
|
163 |
} |
|
164 |
foreach($MARC['022'] as $value) { |
|
165 |
$value = (int)$value['a']; |
|
166 |
if( strlen($value) > strlen($ISSN) ) { |
|
167 |
$ISSN = $value; |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
db2::add('ksiazki', array( |
|
172 |
'id' => $kod, |
|
173 |
'tytul' => $MARC['245'][0]['a'], |
|
174 |
'autor' => $MARC['100'][0]['a'], |
|
175 |
'rok' => $MARC['260'][0]['c'], |
|
176 |
'miejsce' => $MARC['260'][0]['a'], |
|
177 |
'wydawnictwo' => $MARC['260'][0]['b'], |
|
178 |
'wydanie' => $MARC['250'][0]['a'], |
|
179 |
'jezyk' => $MARC['041'][0]['a'], |
|
180 |
'ISBN' => $ISBN, |
|
181 |
'ISSN' => $ISSN, |
|
182 |
/* stan */ |
|
183 |
)); |
|
184 |
} |
|
185 |
} |
|
186 |
?> |