-
Notifications
You must be signed in to change notification settings - Fork 4
/
MarcLint.php
4287 lines (3983 loc) · 149 KB
/
MarcLint.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
/**
* Lint for MARC records
*
* This module is adapted from the MARC::Lint CPAN module for Perl, maintained by
* Bryan Baldus <eijabb@cpan.org> and available at http://search.cpan.org/~eijabb/
*
* Current MARC::Lint version used as basis for this module: 1.53
*
* PHP version 7
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package MARC
* @author Demian Katz <demian.katz@villanova.edu>
* @author Dan Scott <dscott@laurentian.ca>
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @copyright 2003-2019 Oy Realnode Ab, Dan Scott
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
namespace VuFind\Marc;
use VuFindCode\ISBN;
/**
* Class for testing validity of MARC records against MARC21 standard.
*
* @category VuFind
* @package MARC
* @author Demian Katz <demian.katz@villanova.edu>
* @author Dan Scott <dscott@laurentian.ca>
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class MarcLint
{
/**
* Rules used for testing records
*
* @var array
*/
protected $rules;
/**
* A Lint\CodeData object for validating codes
*
* @var Lint\CodeData
*/
protected $data;
/**
* Warnings generated during analysis
*
* @var array
*/
protected $warnings = [];
/**
* Start function
*
* Set up rules for testing MARC records.
*
* @return true
*/
public function __construct()
{
$this->parseRules();
$this->data = new Lint\CodeData();
}
/**
* Check the provided MARC record and return an array of warning messages.
*
* @param MarcReader $marc Record to check
*
* @return array
*/
public function checkRecord(MarcReader $marc): array
{
// Reset warnings:
$this->warnings = [];
$this->checkDuplicate1xx($marc);
$this->checkMissing245($marc);
$this->standardFieldChecks($marc);
return $this->warnings;
}
/**
* Add a warning.
*
* @param string $warning Warning to add
*
* @return void
*/
protected function warn(string $warning): void
{
$this->warnings[] = $warning;
}
/**
* Check for multiple 1xx fields.
*
* @param MarcReader $marc Record to check
*
* @return void
*/
protected function checkDuplicate1xx(MarcReader $marc): void
{
$count = 0;
foreach (['100', '110', '111', '130'] as $field) {
$count += count($marc->getFields($field));
}
if ($count > 1) {
$this->warn(
"1XX: Only one 1XX tag is allowed, but I found $count of them."
);
}
}
/**
* Check for missing 245 field.
*
* @param MarcReader $marc Record to check
*
* @return void
*/
protected function checkMissing245(MarcReader $marc): void
{
if (!$marc->getField('245')) {
$this->warn('245: No 245 tag.');
}
}
/**
* Check all fields against the standard rules encoded in the class.
*
* @param MarcReader $marc Record to check
*
* @return void
*/
protected function standardFieldChecks(MarcReader $marc): void
{
$fieldsSeen = [];
foreach ($marc->getAllFields() as $current) {
$tagNo = $current['tag'];
// if 880 field, inherit rules from tagno in subfield _6
if ($tagNo == 880) {
if ($sub6 = $marc->getSubfield($current, '6')) {
$tagNo = substr($sub6, 0, 3);
$tagrules = $this->rules[$tagNo] ?? null;
// 880 is repeatable, but its linked field may not be
if (isset($tagrules['repeatable'])
&& $tagrules['repeatable'] == 'NR'
&& isset($fieldsSeen['880.' . $tagNo])
) {
$this->warn("$tagNo: Field is not repeatable.");
}
$fieldsSeen['880.' . $tagNo]
= isset($fieldsSeen['880.' . $tagNo])
? $fieldsSeen['880.' . $tagNo] + 1 : 1;
} else {
$this->warn("880: No subfield 6.");
$tagrules = null;
}
} else {
// Default case -- not an 880 field:
$tagrules = $this->rules[$tagNo] ?? null;
if (isset($tagrules['repeatable']) && $tagrules['repeatable'] == 'NR'
&& isset($fieldsSeen[$tagNo])
) {
$this->warn("$tagNo: Field is not repeatable.");
}
$fieldsSeen[$tagNo] = isset($fieldsSeen[$tagNo])
? $fieldsSeen[$tagNo] + 1 : 1;
}
// Treat data fields differently from control fields:
if (intval(ltrim($tagNo, '0')) >= 10) {
if (!empty($tagrules)) {
$this->checkIndicators($tagNo, $current, $tagrules);
$this->checkSubfields($tagNo, $current, $tagrules);
}
} else {
// Control field:
if (isset($current['subfields'])) {
$this->warn(
"$tagNo: Subfields are not allowed in fields lower than 010"
);
}
}
// Check to see if a checkxxx() function exists, and call it on the
// field if it does
$method = 'check' . $tagNo;
if (method_exists($this, $method)) {
$this->$method($current, $marc);
}
}
}
/**
* Check the indicators for the provided field.
*
* @param string $tagNo Tag number being checked
* @param array $field Field to check
* @param array $rules Rules to use for checking
*
* @return void
*/
protected function checkIndicators($tagNo, $field, $rules)
{
for ($i = 1; $i <= 2; $i++) {
$ind = $field["i$i"];
if ($ind === false || $ind == ' ') {
$ind = 'b';
}
if (!strstr($rules['ind' . $i]['values'], $ind)) {
// Make indicator blank value human-readable for message:
if ($ind == 'b') {
$ind = 'blank';
}
$this->warn(
"$tagNo: Indicator $i must be "
. $rules['ind' . $i]['hr_values']
. " but it's \"$ind\""
);
}
}
}
/**
* Check the subfields for the provided field.
*
* @param string $tagNo Tag number being checked
* @param array $field Field to check
* @param array $rules Rules to use for checking
*
* @return void
*/
protected function checkSubfields($tagNo, $field, $rules)
{
$subSeen = [];
foreach ($field['subfields'] as $current) {
$code = $current['code'];
$data = $current['data'];
$subrules = $rules['sub' . $code] ?? null;
if (empty($subrules)) {
$this->warn("$tagNo: Subfield _$code is not allowed.");
} elseif ($subrules['repeatable'] == 'NR' && isset($subSeen[$code])) {
$this->warn("$tagNo: Subfield _$code is not repeatable.");
}
if (preg_match('/\r|\t|\n/', $data)) {
$this->warn(
"$tagNo: Subfield _$code has an invalid control character"
);
}
$subSeen[$code] = isset($subSeen[$code]) ? $subSeen[$code]++ : 1;
}
}
/**
* Looks at 020$a and reports errors if the check digit is wrong.
* Looks at 020$z and validates number if hyphens are present.
*
* @param array $field Field to check
* @param MarcReader $marc MARC record
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function check020(array $field, MarcReader $marc): void
{
foreach ($field['subfields'] as $current) {
$data = $current['data'];
// remove any hyphens
$isbn = str_replace('-', '', $data);
// remove nondigits
$isbn = preg_replace('/^\D*(\d{9,12}[X\d])\b.*$/', '$1', $isbn);
if ($current['code'] === 'a') {
if ((substr($data, 0, strlen($isbn)) != $isbn)) {
$this->warn("020: Subfield a may have invalid characters.");
}
// report error if no space precedes a qualifier in subfield a
if (preg_match('/\(/', $data) && !preg_match('/[X0-9] \(/', $data)) {
$this->warn(
"020: Subfield a qualifier must be preceded by space, $data."
);
}
// report error if unable to find 10-13 digit string of digits in
// subfield 'a'
if (!preg_match('/(?:^\d{10}$)|(?:^\d{13}$)|(?:^\d{9}X$)/', $isbn)) {
$this->warn(
"020: Subfield a has the wrong number of digits, $data."
);
} else {
$isbnObj = new ISBN($isbn);
if (strlen($isbn) == 10) {
if (!$isbnObj->isValid()) {
$this->warn("020: Subfield a has bad checksum, $data.");
}
} elseif (strlen($isbn) == 13) {
if (!$isbnObj->isValid()) {
$this->warn(
"020: Subfield a has bad checksum (13 digit), $data."
);
}
}
}
} elseif ($current['code'] === 'z') {
// look for valid isbn in 020$z
if (preg_match('/^ISBN/', $data)
|| preg_match('/^\d*\-\d+/', $data)
) {
// ##################################################
// ## Turned on for now--Comment to unimplement ####
// ##################################################
if (strlen($isbn) == 10) {
$isbnObj = new ISBN($isbn);
if ($isbnObj->isValid()) {
$this->warn("020: Subfield z is numerically valid.");
}
}
}
}
}
}
/**
* Warns if subfields are not evenly divisible by 3 unless second indicator is 7
* (future implementation would ensure that each subfield is exactly 3 characters
* unless ind2 is 7--since subfields are now repeatable. This is not implemented
* here due to the large number of records needing to be corrected.). Validates
* against the MARC Code List for Languages (<http://www.loc.gov/marc/>).
*
* @param array $field Field to check
* @param MarcReader $marc MARC record
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function check041(array $field, MarcReader $marc): void
{
// warn if length of each subfield is not divisible by 3 unless ind2 is 7
if ($field['i2'] != '7') {
foreach ($field['subfields'] as $sub) {
$code = $sub['code'];
$data = $sub['data'];
if (strlen($data) % 3 != 0) {
$this->warn(
"041: Subfield _$code must be evenly divisible by 3 or "
. "exactly three characters if ind2 is not 7, ($data)."
);
} else {
for ($i = 0; $i < strlen($data); $i += 3) {
$chk = substr($data, $i, 3);
if (!in_array($chk, $this->data->languageCodes)) {
$obs = $this->data->obsoleteLanguageCodes;
if (in_array($chk, $obs)) {
$this->warn(
"041: Subfield _$code, $data, may be obsolete."
);
} else {
$this->warn(
"041: Subfield _$code, $data ($chk),"
. " is not valid."
);
}
}
}
}
}
}
}
/**
* Warns if each subfield a is not exactly 7 characters. Validates each code
* against the MARC code list for Geographic Areas (<http://www.loc.gov/marc/>).
*
* @param array $field Field to check
* @param MarcReader $marc MARC record
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function check043(array $field, MarcReader $marc): void
{
foreach ($field['subfields'] as $sub) {
if ('a' !== $sub['code']) {
continue;
}
// warn if length of subfield a is not exactly 7
$data = $sub['data'];
if (strlen($data) != 7) {
$this->warn("043: Subfield _a must be exactly 7 characters, $data");
} elseif (!in_array($data, $this->data->geogAreaCodes)) {
if (in_array($data, $this->data->obsoleteGeogAreaCodes)) {
$this->warn("043: Subfield _a, $data, may be obsolete.");
} else {
$this->warn("043: Subfield _a, $data, is not valid.");
}
}
}
}
/**
* -Makes sure $a exists (and is first subfield).
* -Warns if last character of field is not a period
* --Follows LCRI 1.0C, Nov. 2003 rather than MARC21 rule
* -Verifies that $c is preceded by / (space-/)
* -Verifies that initials in $c are not spaced
* -Verifies that $b is preceded by :;= (space-colon, space-semicolon,
* space-equals)
* -Verifies that $h is not preceded by space unless it is dash-space
* -Verifies that data of $h is enclosed in square brackets
* -Verifies that $n is preceded by . (period)
* --As part of that, looks for no-space period, or dash-space-period
* (for replaced elipses)
* -Verifies that $p is preceded by , (no-space-comma) when following $n and
* . (period) when following other subfields.
* -Performs rudimentary article check of 245 2nd indicator vs. 1st word of
* 245$a (for manual verification).
*
* Article checking is done by internal checkArticle method, which should work
* for 130, 240, 245, 440, 630, 730, and 830.
*
* @param array $field Field to check
* @param MarcReader $marc MARC record
*
* @return void
*/
protected function check245(array $field, MarcReader $marc): void
{
if (!$marc->getSubfields($field, 'a')) {
$this->warn("245: Must have a subfield _a.");
}
// Convert subfields to array and set flags indicating which subfields are
// present while we're at it.
$tmp = $field['subfields'];
$hasSubfields = $subfields = [];
foreach ($tmp as $current) {
$subfields[] = $current;
$hasSubfields[$current['code']] = true;
}
// 245 must end in period (may want to make this less restrictive by allowing
// trailing spaces)
// do 2 checks--for final punctuation (MARC21 rule), and for period
// (LCRI 1.0C, Nov. 2003)
$lastChar = substr($subfields[count($subfields) - 1]['data'], -1);
if (!in_array($lastChar, ['.', '?', '!'])) {
$this->warn("245: Must end with . (period).");
} elseif ($lastChar != '.') {
$this->warn(
"245: MARC21 allows ? or ! as final punctuation but LCRI 1.0C, Nov."
. " 2003 (LCPS 1.7.1 for RDA records), requires period."
);
}
// Check for first subfield
// subfield a should be first subfield (or 2nd if subfield '6' is present)
if (isset($hasSubfields['6'])) {
// make sure there are at least 2 subfields
if (count($subfields) < 2) {
$this->warn("245: May have too few subfields.");
} else {
$first = $subfields[0]['code'];
$second = $subfields[1]['code'];
if ($first != '6') {
$this->warn("245: First subfield must be _6, but it is $first");
} elseif ($second != 'a') {
$this->warn(
"245: First subfield after subfield _6 must be _a, but it "
. "is _$second"
);
}
}
} else {
// 1st subfield must be 'a'
$first = $subfields[0]['code'];
if ($first != 'a') {
$this->warn("245: First subfield must be _a, but it is _$first");
}
}
// End check for first subfield
// subfield c, if present, must be preceded by /
// also look for space between initials
if (isset($hasSubfields['c'])) {
foreach ($subfields as $i => $current) {
// 245 subfield c must be preceded by / (space-/)
if ($current['code'] == 'c') {
if ($i > 0
&& !preg_match('/\s\/$/', $subfields[$i - 1]['data'])
) {
$this->warn("245: Subfield _c must be preceded by /");
}
// 245 subfield c initials should not have space
if (preg_match('/\b\w\. \b\w\./', $current['data'])) {
$this->warn(
"245: Subfield _c initials should not have a space."
);
}
break;
}
}
}
// each subfield b, if present, should be preceded by :;= (colon, semicolon,
// or equals sign)
if (isset($hasSubfields['b'])) {
// 245 subfield b should be preceded by space-:;= (colon, semicolon, or
// equals sign)
foreach ($subfields as $i => $current) {
if ($current['code'] == 'b' && $i > 0
&& !preg_match('/ [:;=]$/', $subfields[$i - 1]['data'])
) {
$this->warn(
"245: Subfield _b should be preceded by space-colon, "
. "space-semicolon, or space-equals sign."
);
}
}
}
// each subfield h, if present, should be preceded by non-space
if (isset($hasSubfields['h'])) {
// 245 subfield h should not be preceded by space
foreach ($subfields as $i => $current) {
// report error if subfield 'h' is preceded by space (unless
// dash-space)
if ($current['code'] == 'h') {
$prev = $subfields[$i - 1]['data'];
if ($i > 0 && !preg_match('/(\S$)|(\-\- $)/', $prev)) {
$this->warn(
"245: Subfield _h should not be preceded by space."
);
}
// report error if subfield 'h' does not start with open square
// bracket with a matching close bracket; could have check
// against list of valid values here
$data = $current['data'];
if (!preg_match('/^\[\w*\s*\w*\]/', $data)) {
$this->warn(
"245: Subfield _h must have matching square brackets,"
. " $data."
);
}
}
}
}
// each subfield n, if present, must be preceded by . (period)
if (isset($hasSubfields['n'])) {
// 245 subfield n must be preceded by . (period)
foreach ($subfields as $i => $current) {
// report error if subfield 'n' is not preceded by non-space-period
// or dash-space-period
if ($current['code'] == 'n' && $i > 0) {
$prev = $subfields[$i - 1]['data'];
if (!preg_match('/(\S\.$)|(\-\- \.$)/', $prev)) {
$this->warn(
"245: Subfield _n must be preceded by . (period)."
);
}
}
}
}
// each subfield p, if present, must be preceded by a , (no-space-comma)
// if it follows subfield n, or by . (no-space-period or
// dash-space-period) following other subfields
if (isset($hasSubfields['p'])) {
// 245 subfield p must be preceded by . (period) or , (comma)
foreach ($subfields as $i => $current) {
if ($current['code'] == 'p' && $i > 0) {
$prev = $subfields[$i - 1];
// case for subfield 'n' being field before this one (allows
// dash-space-comma)
if ($prev['code'] == 'n'
&& !preg_match('/(\S,$)|(\-\- ,$)/', $prev['data'])
) {
$this->warn(
"245: Subfield _p must be preceded by , (comma) "
. "when it follows subfield _n."
);
} elseif ($prev['code'] != 'n'
&& !preg_match('/(\S\.$)|(\-\- \.$)/', $prev['data'])
) {
$this->warn(
"245: Subfield _p must be preceded by . (period)"
. " when it follows a subfield other than _n."
);
}
}
}
}
// check for invalid 2nd indicator
$this->checkArticle($field, $marc);
}
/**
* Check of articles is based on code from Ian Hamilton. This version is more
* limited in that it focuses on English, Spanish, French, Italian and German
* articles. Certain possible articles have been removed if they are valid
* English non-articles. This version also disregards 008_language/041 codes
* and just uses the list of articles to provide warnings/suggestions.
*
* Source for articles = <http://www.loc.gov/marc/bibliographic/bdapp-e.html>
*
* Should work with fields 130, 240, 245, 440, 630, 730, and 830. Reports error
* if another field is passed in.
*
* @param array $field Field to check
* @param MarcReader $marc MARC record
*
* @return void
*/
protected function checkArticle(array $field, MarcReader $marc): void
{
// add articles here as needed
// Some omitted due to similarity with valid words (e.g. the German 'die').
static $article = [
'a' => 'eng glg hun por',
'an' => 'eng',
'das' => 'ger',
'dem' => 'ger',
'der' => 'ger',
'ein' => 'ger',
'eine' => 'ger',
'einem' => 'ger',
'einen' => 'ger',
'einer' => 'ger',
'eines' => 'ger',
'el' => 'spa',
'en' => 'cat dan nor swe',
'gl' => 'ita',
'gli' => 'ita',
'il' => 'ita mlt',
'l' => 'cat fre ita mlt',
'la' => 'cat fre ita spa',
'las' => 'spa',
'le' => 'fre ita',
'les' => 'cat fre',
'lo' => 'ita spa',
'los' => 'spa',
'os' => 'por',
'the' => 'eng',
'um' => 'por',
'uma' => 'por',
'un' => 'cat spa fre ita',
'una' => 'cat spa ita',
'une' => 'fre',
'uno' => 'ita',
];
// add exceptions here as needed
// may want to make keys lowercase
static $exceptions = [
'A & E',
'A & ',
'A-',
'A+',
'A is ',
'A isn\'t ',
'A l\'',
'A la ',
'A posteriori',
'A priori',
'A to ',
'El Nino',
'El Salvador',
'L is ',
'L-',
'La Salle',
'Las Vegas',
'Lo cual',
'Lo mein',
'Lo que',
'Los Alamos',
'Los Angeles',
];
// get tagno to determine which indicator to check and for reporting
$tagNo = $field['tag'];
// retrieve tagno from subfield 6 if 880 field
if ($tagNo == '880' && ($sub6 = $marc->getSubfield($field, '6'))) {
$tagNo = substr($sub6, 0, 3);
}
// $ind holds nonfiling character indicator value
$ind = '';
// $first_or_second holds which indicator is for nonfiling char value
$first_or_second = '';
if (in_array($tagNo, [130, 630, 730])) {
$ind = $field['i1'];
$first_or_second = '1st';
} elseif (in_array($tagNo, [240, 245, 440, 830])) {
$ind = $field['i2'];
$first_or_second = '2nd';
} else {
$this->warn(
'Internal error: ' . $tagNo
. ' is not a valid field for article checking'
);
return;
}
if (!is_numeric($ind)) {
$this->warn($tagNo . ": Non-filing indicator is non-numeric");
return;
}
// get subfield 'a' of the title field
$title = $marc->getSubfield($field, 'a');
// warn about out-of-range skip indicators (note: this feature is an
// addition to the PHP code; it is not ported directly from MARC::Lint).
if ($ind > strlen($title)) {
$this->warn($tagNo . ": Non-filing indicator is out of range");
return;
}
$char1_notalphanum = 0;
// check for apostrophe, quote, bracket, or parenthesis, before first word
// remove if found and add to non-word counter
while (preg_match('/^["\'\[\(*]/', $title)) {
$char1_notalphanum++;
$title = preg_replace('/^["\'\[\(*]/', '', $title);
}
// split title into first word + rest on space, parens, bracket, apostrophe,
// quote, or hyphen
preg_match('/^([^ \(\)\[\]\'"\-]+)([ \(\)\[\]\'"\-])?(.*)/i', $title, $hits);
$firstword = $hits[1] ?? '';
$separator = $hits[2] ?? '';
$etc = $hits[3] ?? '';
// get length of first word plus the number of chars removed above plus one
// for the separator
$nonfilingchars = strlen($firstword) + $char1_notalphanum + 1;
// check to see if first word is an exception
$isan_exception = false;
foreach ($exceptions as $current) {
if (substr($title, 0, strlen($current)) == $current) {
$isan_exception = true;
break;
}
}
// lowercase chars of $firstword for comparison with article list
$firstword = strtolower($firstword);
// see if first word is in the list of articles and not an exception
$isan_article = !$isan_exception && isset($article[$firstword]);
// if article then $nonfilingchars should match $ind
if ($isan_article) {
// account for quotes, apostrophes, parens, or brackets before 2nd word
if (strlen($separator) && preg_match('/^[ \(\)\[\]\'"\-]+/', $etc)) {
while (preg_match('/^[ "\'\[\]\(\)*]/', $etc)) {
$nonfilingchars++;
$etc = preg_replace('/^[ "\'\[\]\(\)*]/', '', $etc);
}
}
if ($nonfilingchars != $ind) {
$this->warn(
$tagNo . ": First word, $firstword, may be an article, check "
. "$first_or_second indicator ($ind)."
);
}
} else {
// not an article so warn if $ind is not 0
if ($ind != '0') {
$this->warn(
$tagNo . ": First word, $firstword, does not appear to be an "
. "article, check $first_or_second indicator ($ind)."
);
}
}
}
/**
* Support method for constructor to load MARC rules.
*
* @return void
*/
protected function parseRules()
{
// Break apart the rule data on line breaks:
$lines = explode("\n", $this->getRawRules());
// Each group of data is split by a blank line -- process one group
// at a time:
$currentGroup = [];
foreach ($lines as $currentLine) {
if (empty($currentLine)) {
if (!empty($currentGroup)) {
$this->processRuleGroup($currentGroup);
$currentGroup = [];
}
} else {
$currentGroup[] = preg_replace("/\s+/", " ", $currentLine);
}
}
// Still have unprocessed data after the loop? Handle it now:
if (!empty($currentGroup)) {
$this->processRuleGroup($currentGroup);
}
}
/**
* Support method for parseRules() -- process one group of lines representing
* a single tag.
*
* @param array $rules Rule lines to process
*
* @return void
*/
protected function processRuleGroup($rules)
{
// The first line is guaranteed to exist and gives us some basic info:
[$tag, $repeatable, $description] = explode(' ', $rules[0]);
$this->rules[$tag] = [
'repeatable' => $repeatable,
'desc' => $description
];
// We may or may not have additional details:
for ($i = 1; $i < count($rules); $i++) {
[$key, $value, $lineDesc] = explode(' ', $rules[$i] . ' ');
if (substr($key, 0, 3) == 'ind') {
// Expand ranges:
$value = str_replace('0-9', '0123456789', $value);
$this->rules[$tag][$key] = [
'values' => $value,
'hr_values' => $this->getHumanReadableIndicatorValues($value),
'desc'=> $lineDesc
];
} else {
if (strlen($key) <= 1) {
$this->rules[$tag]['sub' . $key] = [
'repeatable' => $value,
'desc' => $lineDesc
];
} elseif (strstr($key, '-')) {
[$startKey, $endKey] = explode('-', $key);
foreach (range($startKey, $endKey) as $sub) {
$this->rules[$tag]['sub' . $sub] = [
'repeatable' => $value,
'desc' => $lineDesc
];
}
}
}
}
}
/**
* Turn a set of indicator rules into a human-readable list.
*
* @param string $rules Indicator rules
*
* @return string
*/
protected function getHumanReadableIndicatorValues($rules)
{
// No processing needed for blank rule:
if ($rules == 'blank') {
return $rules;
}
// Create string:
$string = '';
$length = strlen($rules);
for ($i = 0; $i < $length; $i++) {
$current = substr($rules, $i, 1);
if ($current == 'b') {
$current = 'blank';
}
$string .= $current;
if ($length - $i == 2) {
$string .= ' or ';
} elseif ($length - $i > 2) {
$string .= ', ';
}
}
return $string;
}
/**
* Support method for parseRules() -- get the raw rules from MARC::Lint.
*
* @return string
*/
protected function getRawRules()
{
// @codingStandardsIgnoreStart
return <<<'RULES'
001 NR CONTROL NUMBER
ind1 blank Undefined
ind2 blank Undefined
NR Undefined
002 NR LOCALLY DEFINED (UNOFFICIAL)
ind1 blank Undefined
ind2 blank Undefined
NR Undefined
003 NR CONTROL NUMBER IDENTIFIER
ind1 blank Undefined
ind2 blank Undefined
NR Undefined
005 NR DATE AND TIME OF LATEST TRANSACTION
ind1 blank Undefined
ind2 blank Undefined
NR Undefined
006 R FIXED-LENGTH DATA ELEMENTS--ADDITIONAL MATERIAL CHARACTERISTICS--GENERAL INFORMATION
ind1 blank Undefined
ind2 blank Undefined
R Undefined
007 R PHYSICAL DESCRIPTION FIXED FIELD--GENERAL INFORMATION
ind1 blank Undefined
ind2 blank Undefined
R Undefined
008 NR FIXED-LENGTH DATA ELEMENTS--GENERAL INFORMATION
ind1 blank Undefined
ind2 blank Undefined
NR Undefined
010 NR LIBRARY OF CONGRESS CONTROL NUMBER
ind1 blank Undefined
ind2 blank Undefined
a NR LC control number
b R NUCMC control number
z R Canceled/invalid LC control number
8 R Field link and sequence number
013 R PATENT CONTROL INFORMATION
ind1 blank Undefined
ind2 blank Undefined
a NR Number
b NR Country
c NR Type of number
d R Date
e R Status
f R Party to document
6 NR Linkage
8 R Field link and sequence number
015 R NATIONAL BIBLIOGRAPHY NUMBER
ind1 blank Undefined
ind2 blank Undefined
a R National bibliography number
q R Qualifying information
z R Canceled/Invalid national bibliography number
2 NR Source
6 NR Linkage
8 R Field link and sequence number
016 R NATIONAL BIBLIOGRAPHIC AGENCY CONTROL NUMBER
ind1 b7 National bibliographic agency
ind2 blank Undefined
a NR Record control number
z R Canceled or invalid record control number