-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xmarc2sql.php
435 lines (420 loc) · 16.7 KB
/
Xmarc2sql.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
<?php
$f = dirname(__FILE__)."/conf.php";
if (!file_exists( $f )) die("
Pas de fichier de configuration, renommez _conf.php en conf.php.
");
$conf = include( $f );
Xmarc2sql::connect( $conf['sqlite'] );
Xmarc2sql::glob( $conf['srcglob'] );
class Xmarc2sql {
/** Table de prénoms (pour reconnaître le sexe), chargée depuis given.php */
static $given;
/** Table de caractères pour mise à l’ASCII, chargée depuis frtr.php */
static $frtr;
/** connection */
static private $_pdo;
/** où sommes nous ? */
static private $_marc;
/** texte */
static private $_text;
/** parseur */
static $file;
/** colonnes */
static private $_cols = array(
"document" => array(
"ark",
"title",
"date",
"year",
"place",
"publisher",
"dewey",
"lang",
"type",
"description",
"pages",
"size",
"byline",
"bysort",
"person",
"birthyear",
"deathyear",
"posthum",
"hasgall",
"id",
),
"contribution" => array(
"document",
"person",
"role",
"writes",
),
"person" => array(
"family",
"given",
"sort",
"gender",
"date",
"birthyear",
"deathyear",
"id",
),
"gallica" => array(
"document",
"ark",
"title",
"id",
)
);
/** queries */
static private $_q;
/** enregistrements */
static private $_rec;
static function glob( $glob )
{
self::$given = include(dirname(__FILE__).'/lib/given.php');
self::$frtr = include( dirname( __FILE__ )."/lib/frtr.php" );
self::$frtr[' ']='';
self::$_q['document'] = self::$_pdo->prepare(
"INSERT INTO document (".implode(", ", self::$_cols['document'] ).") VALUES (".rtrim(str_repeat("?, ", count( self::$_cols['document'] )), ", ").");"
);
self::$_q['title'] = self::$_pdo->prepare( "INSERT INTO title ( docid, text ) VALUES ( ?, ? )");
/*
<mxc:datafield tag="700" ind1=" " ind2=" ">
<mxc:subfield code="3">11900134</mxc:subfield>
<mxc:subfield code="w">0 b.....</mxc:subfield>
<mxc:subfield code="a">Diderot</mxc:subfield>
<mxc:subfield code="m">Denis</mxc:subfield>
<mxc:subfield code="d">1713-1784</mxc:subfield>
<mxc:subfield code="4">0360</mxc:subfield>
</mxc:datafield>
*/
self::$_q['contribution'] = self::$_pdo->prepare(
"INSERT INTO contribution (".implode(", ", self::$_cols['contribution'] ).") VALUES (".rtrim(str_repeat("?, ", count( self::$_cols['contribution'] )), ", ").");"
);
self::$_q['person'] = self::$_pdo->prepare(
"INSERT OR REPLACE INTO person (".implode(", ", self::$_cols['person'] ).") VALUES (".rtrim(str_repeat("?, ", count( self::$_cols['person'] )), ", ").");"
);
self::$_q['gallica'] = self::$_pdo->prepare(
"INSERT INTO gallica (".implode(", ", self::$_cols['gallica'] ).") VALUES (".rtrim(str_repeat("?, ", count( self::$_cols['gallica'] )), ", ").");"
);
self::$_q['perstest'] = self::$_pdo->prepare( "SELECT * FROM person WHERE id = ?" );
self::$_pdo->beginTransaction();
$i = 0;
foreach ( glob( $glob ) as $file ) {
$i++;
// echo "$i \r";
self::$file = $file;
self::_file( $file );
}
self::$_pdo->commit();
self::$_pdo->exec( "
-- date des contributions (optimisation)
UPDATE contribution SET
date=( SELECT year FROM document WHERE document=document.id )
;
-- nombre de documents
UPDATE person SET
docs=( SELECT count(*) FROM contribution WHERE person=person.id AND writes = 1 )
;
UPDATE person SET writes=1 WHERE docs > 0;
-- Nombre de volumes Gallica
UPDATE document SET
volumes=( SELECT count(*) FROM gallica WHERE document=document.id )
;
-- les morts
UPDATE person SET
posthum=( SELECT count(*) FROM contribution WHERE person=person.id AND writes = 1 AND date > deathyear+1 ),
anthum=( SELECT count(*) FROM contribution WHERE person=person.id AND writes = 1 AND date <= deathyear+1 )
WHERE writes=1 AND deathyear > 0
;
-- Homère
UPDATE person SET
posthum=( SELECT count(*) FROM contribution WHERE person=person.id AND writes = 1 )
WHERE birthyear IS NULL AND deathyear IS NULL
;
-- les vivants
UPDATE person SET
anthum=( SELECT count(*) FROM contribution WHERE person=person.id AND writes = 1 )
WHERE birthyear > 0 AND deathyear IS NULL
;
-- nombre de pages
UPDATE person SET
pages=( SELECT sum(document.pages) FROM contribution, document WHERE contribution.document=document.id AND contribution.person = person.id )
;
-- date d’édition après mort de l’auteur principal ?
UPDATE document SET
posthum=( year > deathyear+1 )
;
" );
}
private static function _file( $file )
{
$data = file_get_contents($file);
$parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
xml_set_element_handler( $parser, "Xmarc2sql::_open", "Xmarc2sql::_close" );
xml_set_character_data_handler( $parser, "Xmarc2sql::_text" );
if ( !xml_parse( $parser, $data, true ) ) {
die( sprintf(
"XML error: %s at line %d",
xml_error_string( xml_get_error_code( $parser ) ),
xml_get_current_line_number( $parser ))
);
}
xml_parser_free( $parser );
}
private static function _open( $parser, $name, $atts )
{
self::$_text = "";
if ( $name == "mxc:record" ) {
self::$_rec['document'] = array_fill_keys ( self::$_cols['document'], null );
// id="ark:/12148/cb30089185p"
$ark = substr( $atts['id'], 11);
self::$_rec['document']['ark'] = $ark;
self::$_rec['document']['id'] = substr($ark, 2, -1);
}
if ( $name == "mxc:controlfield" ) {
self::$_marc = array( $atts['tag'] );
}
if ( $name == "mxc:datafield" ) {
self::$_marc = array();
self::$_marc[0] = $atts['tag'];
if ( self::$_marc[0] == 937 ) {
self::$_rec['gallica'] = array_fill_keys ( self::$_cols['gallica'], null );
self::$_rec['gallica']['document'] = self::$_rec['document']['id'];
}
if ( self::$_marc[0] == 100 || self::$_marc[0] == 700 ) {
self::$_rec['person'] = array_fill_keys ( self::$_cols['person'], null );
self::$_rec['contribution'] = array_fill_keys ( self::$_cols['contribution'], null );
self::$_rec['contribution']['document'] = self::$_rec['document']['id'];
}
if ( self::$_marc[0] == 110 || self::$_marc[0] == 710 ) {
self::$_rec['org'] = array();
}
}
else if ( $name == "mxc:subfield" ) {
self::$_marc[1] = $atts['code'];
}
}
private static function _close( $parser, $name )
{
if ( $name == 'mxc:subfield' ) {
if ( self::$_marc == array( 41, "a" ) ) {
self::$_rec['document']['lang'] = self::$_text;
}
if ( self::$_marc == array( 100, "3" ) || self::$_marc == array( 700, "3" ) ) {
self::$_rec['person']['id'] = self::$_text;
self::$_rec['contribution']['person'] = self::$_text;
}
// <mxc:subfield code="4">0440</mxc:subfield> http://data.bnf.fr/vocabulary/roles/
if ( self::$_marc == array( 100, "4" ) || self::$_marc == array( 700, "4" ) ) {
$role = 0+self::$_text;
self::$_rec['contribution']['role'] = $role;
if ( $role == 70 || $role == 71 || $role == 72 || $role == 73 || $role == 980 || $role == 990 ) self::$_rec['contribution']['writes'] = 1;
}
if ( self::$_marc == array( 100, "a" ) || self::$_marc == array( 700, "a" ) ) {
self::$_rec['person']['family'] = self::$_text;
}
if ( self::$_marc == array( 100, "d" ) || self::$_marc == array( 700, "d" ) ) {
self::$_rec['person']['date'] = self::$_text;
$moins = "";
if ( strpos( self::$_text, "av." ) ) $moins="-";
if(preg_match("@(\d+)\??-@", self::$_text, $matches)) {
self::$_rec['person']['birthyear'] = $moins.$matches[1];
}
if(preg_match("@-(\d\d\d\d)@", self::$_text, $matches)) {
self::$_rec['person']['deathyear'] = $moins.$matches[1];
}
}
if ( self::$_marc == array( 100, "m" ) || self::$_marc == array( 700, "m" ) ) {
self::$_rec['person']['given'] = self::$_text;
}
if ( self::$_marc == array( 110, "3" ) || self::$_marc == array( 710, "3" ) ) {
self::$_rec['org']['id'] = self::$_text;
}
if ( self::$_marc == array( 110, "4" ) || self::$_marc == array( 710, "4" ) ) {
self::$_rec['org']['role'] = self::$_text;
}
if (
self::$_marc == array( 110, "a" ) || self::$_marc == array( 710, "a" )
|| self::$_marc == array( 110, "c" ) || self::$_marc == array( 710, "c" )
) {
if (!isset( self::$_rec['org']['name'] ) ) self::$_rec['org']['name']= self::$_text;
else self::$_rec['org']['name'] = ", ".self::$_text;
}
if ( self::$_marc == array( 245, "a" ) ) {
self::$_rec['document']['title'] = self::$_text;
}
if ( self::$_marc == array( 245, "d" ) ) {
self::$_rec['document']['type'] = self::$_text;
}
if ( self::$_marc == array( 260, "a" ) ) {
self::$_rec['document']['place'] = self::$_text;
}
if ( self::$_marc == array( 260, "c" ) ) {
self::$_rec['document']['publisher'] = self::$_text;
}
if ( self::$_marc == array( 260, "d" ) ) {
self::$_rec['document']['date'] = self::$_text;
if ( self::$_rec['document']['year']);
else if ( preg_match( '@(\d\d\d\d)@', self::$_text, $matches) ) {
self::$_rec['document']['year'] = $matches[0];
}
else if ( strpos( strtolower( self::$_text ), "s. d." ) !== false || strpos( strtolower(self::$_text), "s.d." ) !== false );
else if ( preg_match( '@[Aa]n ([IVX]+)@', self::$_text, $matches ) ) {
$rep = array( "I"=>1793, "II"=>1794, "III"=>1795, "IV"=>1796, "V"=>1797, "VI"=>1798, "VII"=>1799, "VIII"=>1800, "IX"=>1801, "X"=>1802, "XI"=>1803, "XII"=>1804, "XIII"=>1805, "XIV"=>1805 );
if ( isset($rep[$matches[1]]) ) self::$_rec['document']['year'] = $rep[$matches[1]];
}
else {
echo self::$file.' 260$d date ? '.self::$_text."\n";
}
}
if ( self::$_marc == array( 280, "a" ) ) {
self::$_rec['document']['description'] = self::$_text;
preg_match_all( '/([0-9]+)(-[0-9IVXLC]+)? [pf]+\./', self::$_text, $matches, PREG_PATTERN_ORDER );
if ( count($matches[1]) > 0 ) {
self::$_rec['document']['pages'] = 0;
foreach( $matches[1] as $p ) self::$_rec['document']['pages'] += $p;
}
else if ( preg_match( "/ pièce /u", self::$_text ) ) {
self::$_rec['document']['pages'] = 1;
}
}
if ( self::$_marc == array( 680, "a" ) ) self::$_rec['document']['dewey'] = self::$_text;
if ( self::$_marc == array( 937, "j" ) ) {
// ark:/12148/bpt6k5655164n
if (preg_match( '@(bpt6k([0-9]+).)@', self::$_text, $matches) ) {
self::$_rec['gallica']['ark'] = $matches[1];
self::$_rec['gallica']['id'] = $matches[2];
}
// cartes et plans ? pas de texte ?
else if( strrpos( self::$_text, "ark:/12148/btv") === 0);
else {
echo self::$file.' 937$j Lien gallica ? '.self::$_text."\n";
}
}
if ( self::$_marc == array( 937, "k" ) ) self::$_rec['gallica']['title'] = self::$_text;
}
if ( $name == 'mxc:controlfield' ) {
if ( self::$_marc[0] == 8 ) {
if (preg_match( '@ (\d\d\d\d) @', self::$_text, $matches ) ) self::$_rec['document']['year'] = $matches[1];
if (preg_match( '@ ([a-z][a-z])([a-z][a-z][a-z]) @', self::$_text, $matches ) ) self::$_rec['document']['lang'] = $matches[2];
}
}
if ( $name == 'mxc:datafield' ) {
if ( self::$_marc[0] == 100 || self::$_marc[0] == 700 ) {
if ( self::$_rec['person']['date'] && strpos(self::$_rec['person']['date'], '.') === false && !self::$_rec['person']['birthyear'] ) { // peu de cas
// print_r( self::$_rec['person'] );
}
// auteur principal ?
if ( !self::$_rec['document']['byline'] ) {
self::$_rec['document']['person'] = self::$_rec['person']['id'];
self::$_rec['document']['birthyear'] = self::$_rec['person']['birthyear'];
self::$_rec['document']['deathyear'] = self::$_rec['person']['deathyear'];
}
// ajouter à la ligne auteur
if ( self::$_rec['document']['byline'] ) self::$_rec['document']['byline'] .= " ; ";
self::$_rec['document']['byline'] .= self::$_rec['person']['family'];
if ( self::$_rec['person']['given'] ) self::$_rec['document']['byline'] .= ", ".self::$_rec['person']['given'];
if ( self::$_rec['person']['given'] ) {
$key = mb_strtolower( self::$_rec['person']['given'] );
if ( isset( self::$given[$key] ) ) self::$_rec['person']['gender'] = self::$given[$key];
}
self::$_rec['person']['sort'] = strtr( self::$_rec['person']['family'].self::$_rec['person']['given'], self::$frtr );
// record
self::$_q['perstest']->execute( array( self::$_rec['person']['id'] ) );
$pers = self::$_q['perstest']->fetch();
// enregistrer l'auteur collecté si rien, ou si mieux (avec dates)
if ( !$pers || (!$pers['date'] && self::$_rec['person']['date']) ) {
self::$_q['person']->execute( array_values( self::$_rec['person'] ) );
}
try {
self::$_q['contribution']->execute( array_values( self::$_rec['contribution'] ) );
}
catch ( Exception $e ) {
/* Doublon d’auteur
// ark:/12148/cb31495695f
<mxc:datafield tag="100" ind1=" " ind2=" ">
<mxc:subfield code="3">10656495</mxc:subfield>
<mxc:subfield code="w">0 b</mxc:subfield>
<mxc:subfield code="a">Trigant Gautier</mxc:subfield>
<mxc:subfield code="m">Jean-Pierre</mxc:subfield>
<mxc:subfield code="d">1766-1844</mxc:subfield>
<mxc:subfield code="4">0070</mxc:subfield>
</mxc:datafield>
<mxc:datafield tag="700" ind1=" " ind2=" ">
<mxc:subfield code="3">10656495</mxc:subfield>
<mxc:subfield code="w">0 b</mxc:subfield>
<mxc:subfield code="a">Trigant Gautier</mxc:subfield>
<mxc:subfield code="m">Jean-Pierre</mxc:subfield>
<mxc:subfield code="d">1766-1844</mxc:subfield>
<mxc:subfield code="4">0070</mxc:subfield>
</mxc:datafield>
*/
}
}
if ( self::$_marc[0] == 110 || self::$_marc[0] == 710 ) {
// ajouter à la ligne auteur
if ( self::$_rec['document']['byline'] ) self::$_rec['document']['byline'] .= " ; ";
self::$_rec['document']['byline'] .= self::$_rec['org']['name'];
}
if ( self::$_marc[0] == 937 ) {
// il ya des champs 937 sans lien gallica
if( self::$_rec['gallica']['id'] ) {
self::$_rec['document']['hasgall'] = 1;
try {
self::$_q['gallica']->execute( array_values( self::$_rec['gallica'] ) );
}
catch ( Exception $e ) {
// pb de doublons dans les notices.
}
}
}
}
if ( $name == 'mxc:record' ) {
// (S. l. n. d.)
// if ( !self::$_rec['document']['year'] && !self::$_rec['document']['date'] ) print_r(self::$_rec['document']);
if ( self::$_rec['document']['byline'] ) self::$_rec['document']['bysort'] = strtr( self::$_rec['document']['byline'], self::$frtr );
// 656 notices n'ont pas de version numérisée (vérifié au catalogue BNF)
if ( self::$_rec['document']['hasgall'] ) {
self::$_q['document']->execute( array_values( self::$_rec['document'] ) );
self::$_q['title']->execute( array( self::$_rec['document']['id'], self::$_rec['document']['title'] ) );
}
}
}
private static function _text( $parser, $data )
{
self::$_text .= $data;
}
/**
* Connexion à la base de données
*/
static function connect( $sqlfile, $create=false )
{
$dsn = "sqlite:" . $sqlfile;
if($create && file_exists($sqlfile) ) unlink( $sqlfile );
// create database
if (!file_exists($sqlfile)) { // if base do no exists, create it
if (!file_exists($dir = dirname($sqlfile))) {
mkdir($dir, 0775, true);
@chmod($dir, 0775); // let @, if www-data is not owner but allowed to write
}
self::$_pdo = new PDO($dsn);
self::$_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
@chmod($sqlfile, 0775);
self::$_pdo->exec( file_get_contents( dirname(__FILE__)."/marc.sql" ) );
self::$_pdo->exec( file_get_contents( dirname(__FILE__)."/dewey.sql" ) );
self::$_pdo->exec( file_get_contents( dirname(__FILE__)."/role.sql" ) );
return;
}
else {
// echo getcwd() . ' ' . $dsn . "\n";
// absolute path needed ?
self::$_pdo = new PDO($dsn);
self::$_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
}
}
?>