-
Notifications
You must be signed in to change notification settings - Fork 2
/
koha_610a_686c_998a_sar.php
259 lines (236 loc) · 9.13 KB
/
koha_610a_686c_998a_sar.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
<?php
/*
* koha_610a_686c_998a_sar.php -- Field 610$a/686$c/998$a search & replace.
* Copyright (C) 2017 Andreas Roussos
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
$time_start = microtime ( true ) ;
if ( isset ( $_POST [ 'searchfor' ] ) &&
isset ( $_POST [ 'replacewith' ] ) &&
isset ( $_POST [ 'dryrun' ] ) ) {
$sf = $_POST [ 'searchfor' ] ;
$rw = $_POST [ 'replacewith' ] ;
$dr = $_POST [ 'dryrun' ] ;
if ( ( $sf != "" ) && ( $rw != "" ) ) {
if ( $sf != $rw ) {
if ( ( $_POST [ 'f610a' ] == "" ) &&
( $_POST [ 'f686c' ] == "" ) &&
( $_POST [ 'f998a' ] == "" ) ) {
echo "<pre>Please select at least one field to replace.</pre>" ;
} else {
$f610a = ( $_POST [ 'f610a' ] == "on" ) ? "on" : "off" ;
$f686c = ( $_POST [ 'f686c' ] == "on" ) ? "on" : "off" ;
$f998a = ( $_POST [ 'f998a' ] == "on" ) ? "on" : "off" ;
echo "<xmp style=\"font: normal 9pt Courier New\">"
. "Fields: "
. "610a = " . $f610a . "\n "
. "686c = " . $f686c . "\n "
. "998a = " . $f998a . "\n\n"
. "Searching for: \"" . $sf
. "\" and replacing with: \"" . $rw . "\"\n" ;
search_and_replace (
$sf,
$rw,
$f610a,
$f686c,
$f998a,
$dr ) ;
}
} elseif ( $sf === $rw )
echo "<pre>Both fields contain the same value.</pre>" ;
} else
echo "<pre>Please fill in both fields.</pre>" ;
} else {
?>
<form action="koha_610a_686c_998a_sar.php" method="post">
<table>
<tr>
<td align="right">Search for:</td>
<td><input size="80" type="text" name="searchfor"></td>
</tr>
<tr>
<td align="right">Replace with:</td>
<td><input size="80" type="text" name="replacewith"></td>
</tr>
<tr>
<td align="right" valign="top">Fields to replace:</td>
<td rowspan=3><input type="checkbox" name="f610a" checked="checked">610a<br>
<input type="checkbox" name="f686c">686c<br>
<input type="checkbox" name="f998a">998a</td>
</tr>
</table>
<input type="hidden" name="dryrun" value="1"></td>
<p><input type="submit"></p>
</form>
<?php
}
function search_and_replace (
$searchfor,
$replacewith,
$field610a,
$field686c,
$field998a,
$dryrun ) {
$conn = mysqli_connect (
'', // hostname
'', // username
'', // password
'' ) ; // database
if ( mysqli_connect_errno ( $conn ) ) {
printf ( "Connect failed: %s\n", mysqli_connect_error ( $conn ) ) ;
exit ;
}
if ( ! mysqli_set_charset ( $conn, "utf8" ) ) {
printf (
"Error loading character set utf8: %s\n",
mysqli_error ( $conn ) ) ;
exit ;
}
$query =
"SELECT
biblionumber,
metadata AS marcxml
FROM
biblio_metadata
WHERE
format = 'marcxml'" ;
if ( ! $res = mysqli_query ( $conn, $query ) ) {
printf ( "mysqli_query failed: %s\n", mysqli_error ( $conn ) ) ;
exit ;
}
if ( mysqli_num_rows ( $res ) != 0 ) {
$count = 0 ;
while ( $row = mysqli_fetch_assoc ( $res ) ) {
$found = 0 ;
$marcxmlbefore = $row [ 'marcxml' ] ;
$record = simplexml_load_string ( $row [ 'marcxml' ] ) ;
$record -> registerXPathNamespace (
"ns",
"http://www.loc.gov/MARC21/slim" ) ;
if ( $field610a == "on" ) {
$subfield = $record -> xpath(
'//ns:record/ns:datafield[@tag="610"]/ns:subfield') ;
foreach ( $subfield as $key => $value ) {
if ( $subfield [ $key ] == $searchfor ) {
$subfield [ $key ] [ 0 ] = $replacewith ;
$found = 1 ;
$count ++ ;
}
}
}
if ( $field686c == "on" ) {
$subfield = $record -> xpath(
'//ns:record/ns:datafield[@tag="686"]/ns:subfield') ;
foreach ( $subfield as $key => $value ) {
if ( $subfield [ $key ] == $searchfor ) {
$subfield [ $key ] [ 0 ] = $replacewith ;
$found = 1 ;
$count ++ ;
}
}
}
if ( $field998a == "on" ) {
$subfield = $record -> xpath(
'//ns:record/ns:datafield[@tag="998"]/ns:subfield') ;
foreach ( $subfield as $key => $value ) {
if ( $subfield [ $key ] == $searchfor ) {
$subfield [ $key ] [ 0 ] = $replacewith ;
$found = 1 ;
$count ++ ;
}
}
}
if ( $found == 1 ) {
$str = implode (
"\n",
array_slice ( explode ( "\n", $record -> asXML ( ) ), 2 ) ) ;
$marcxmlafter =
'<?xml version="1.0" encoding="UTF-8"?>
<record
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
xmlns="http://www.loc.gov/MARC21/slim">
' . $str ;
if ( $dryrun == 1 ) {
echo "\nI will update biblio " . $row [ 'biblionumber' ]
. " as follows:\n\n" ;
echo xdiff_string_diff (
str_replace ( "\r", "", $marcxmlbefore ),
$marcxmlafter ) ;
// echo xdiff_string_diff ( $marcxmlbefore, $marcxmlafter ) ;
} elseif ( $dryrun == 0 ) {
$qry =
"UPDATE
biblio_metadata
SET
metadata = \"" . mysqli_real_escape_string (
$conn, $marcxmlafter ) . "\"
WHERE
biblionumber = " . $row [ 'biblionumber' ] ;
// echo "<pre>" . $qry . "</pre>" ;
if ( ! mysqli_query ( $conn, $qry ) ) {
printf (
"mysqli_query failed: %s\n",
mysqli_error ( $conn ) ) ;
exit ;
} else {
echo "\nUpdated biblio " . $row [ 'biblionumber' ] ;
}
$qry = "INSERT INTO
zebraqueue (
biblio_auth_number,
operation,
server,
done )
VALUES ( "
. $row [ 'biblionumber' ] . ", "
. "\"specialUpdate\", "
. "\"biblioserver\", "
. "0 )" ;
// echo $qry . "\n" ;
if ( ! mysqli_query ( $conn, $qry ) ) {
printf (
"mysqli_query failed: %s\n",
mysqli_error ( $conn ) ) ;
exit ;
}
}
}
}
} else
exit ;
mysqli_close ( $conn ) ;
if ( $dryrun == 1 ) {
print "\n$count change(s) to be committed.\n</xmp>\n" ;
?>
<form action="koha_610a_686c_998a_sar.php" method="post">
<input type="hidden" name="searchfor" value="<?php echo $searchfor ; ?>">
<input type="hidden" name="replacewith" value="<?php echo $replacewith ; ?>">
<input type="hidden" name="dryrun" value="0">
<input type="hidden" name="f610a" value="<?php echo $field610a ; ?>">
<input type="hidden" name="f686c" value="<?php echo $field686c ; ?>">
<input type="hidden" name="f998a" value="<?php echo $field998a ; ?>">
<input type="submit" value="Commit changes" style="color: #ff0000">
</form>
<?php
} elseif ( $dryrun == 0 ) {
print "\n\n$count change(s) have been committed.\n</xmp>\n" ;
print "<a href=\"koha_610a_686c_998a_sar.php\">Start again</a><br><br>" ;
}
}
$time_end = microtime ( true ) ;
$time = $time_end - $time_start ;
printf ( "\nProcess time: %.3f seconds", $time ) ;
?>