-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_refine_xml_clean_up_cambodia_batch_2v2.php
168 lines (118 loc) · 4.23 KB
/
open_refine_xml_clean_up_cambodia_batch_2v2.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
/*
Script to clean up text file from open refine.
This was used with the second Cambodian manuscripts hard drive.
1/19/2021
*/
<?php
$title_array = array();
$csv = array_map('str_getcsv', file('title_names.csv'));
foreach ($csv as $row) {
array_push($title_array, $row[0]);
}
print "type a file path: ";
$dir = fgets(STDIN);
$dir = trim($dir);
if (!is_dir($dir)) {
print "The directory $dir does not exist.\n";
print "Exiting program.\n";
} else {
dirToArray($dir, $title_array);
}
function dirToArray($dir, &$title_array) {
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value,array(".",".."))) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
dirToArray($dir . DIRECTORY_SEPARATOR . $value, $title_array);
} else {
$val_exten = substr(strrchr($value, '.'), 1);
$val_exten = strtolower($val_exten);
if ($val_exten === "txt") {
write_file($value, $title_array);
}
}
}
}
}
function write_file($value, &$title_array) {
$delim = "*";
$lines = file($value);
$arr_count = 0;
$file_name = pathinfo($value, PATHINFO_FILENAME);
$new_file = $file_name . "_" . $title_array[$arr_count] . ".xml";
$file = fopen($new_file, 'w');
foreach($lines as $line) {
$next_line = next($lines);
if (trim($delim) === trim($line)) {
$arr_count += 1;
fclose($file);
clean_xml_null($new_file);
if(trim($next_line) != '') {
$file_name = pathinfo($value, PATHINFO_FILENAME);
$new_file = $file_name . "_" . $title_array[$arr_count] . ".xml";
$file = fopen($new_file, 'w');
}
} else {
$line = preg_replace('~(?<=>)(")|(")(?=<)~', '', $line);
fwrite($file, $line);
}
}
}
function clean_xml_null($new_file) {
$xml = simplexml_load_file($new_file, null, LIBXML_NOBLANKS);
$remove = $xml->xpath("//mods:titleInfo[mods:title='null']");
foreach ( $remove as $item ) {
unset($item[0]);
}
if ($remove2 = $xml->xpath("//mods:name[mods:namePart='null']")) {
$remove3 = $xml->xpath("//mods:name[@type='personal']");
foreach ( $remove3 as $item ) {
unset($item[0]);
}
}
$remove4 = $xml->xpath("//mods:genre[text()='null']");
foreach ( $remove4 as $item ) {
unset($item[0]);
}
$remove5 = $xml->xpath("///mods:dateCreated[text()='null']");
foreach ( $remove5 as $item ) {
unset($item[0][0]);
}
$remove6 = $xml->xpath("///mods:form[text()='null']");
foreach ( $remove6 as $item ) {
unset($item[0][0]);
}
$remove7 = $xml->xpath("//mods:note[text()='null']");
foreach ( $remove7 as $item ) {
unset($item[0]);
}
$remove8 = $xml->xpath("//mods:identifier[text()='null']");
foreach ( $remove8 as $item ) {
unset($item[0]);
}
$remove9 = $xml->xpath("//mods:subject[mods:topic='null']");
foreach ( $remove9 as $item ) {
unset($item[0]);
}
$remove10 = $xml->xpath("//mods:subject[mods:geographic='null']");
foreach ( $remove10 as $item ) {
unset($item[0]);
}
$domDocument = dom_import_simplexml($xml)->ownerDocument;
$domDocument->formatOutput = true;
$domDocument->save($new_file);
$config = array(
'indent' => true,
'indent-spaces' => 16,
'indent-with-tabs' => true,
'tab-size' => 16,
'clean' => true,
'input-xml' => true,
'output-xml' => true,
'wrap' => false
);
$tidy = new Tidy();
$repaired = $tidy->repairfile($new_file, $config);
file_put_contents($new_file, $repaired);
}
?>