-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateDOIs.php
188 lines (136 loc) · 4.37 KB
/
updateDOIs.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
<?php
if(php_sapi_name() != "cli")
die();
?>
Welcome to DOI-Changer!
<?php
############################ Config
// Run with Prod = true if you are sure all is working!
$prod = false;
// DataCite Username ans password
$USER = "username";
$PASS = "password";
// Proxy. set to null if you are not using it!
// Format: IP/Hostname:Port
$PROXY = null;
// Whats the old and new domain?
$OLDDOMAIN = "http://my.old.domain";
$NEWDOMAIN = "https://my.new.domain.also.with.https";
// Should I ignore some DOIs? comma separated.
// Set to null if no DOIs should be ignored.
$IGNOREDOI = array('12.3456/DOIUPDATER');
// Do you want to specify a custom DOI-list? Do it here ;)
$doiList = array(
// '12.3456/DOI',
);
############################# Main
$result['skipped'] = array();
$result['domainproblem'] = array();
$result['updateproblem'] = array();
if(!empty($doiList)) {
echo "Got manual DOIList!".PHP_EOL;
} else {
$c = curl_init();
if(!is_null($PROXY)) {
curl_setopt($c, CURLOPT_PROXY, $PROXY);
}
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($c, CURLOPT_URL, "https://mds.datacite.org/doi");
curl_setopt($c,CURLOPT_USERAGENT,'doi_url_updater (https://github.com/Commifreak/doi_url_updater)');
$out = curl_exec($c);
$info = curl_getinfo($c);
curl_close($c);
if(empty($out))
die("Got no list!");
echo "Got List!".PHP_EOL;
$doiList = explode(PHP_EOL, $out);
}
echo "Got ".count($doiList)." DOI's!".PHP_EOL;
$c = curl_init();
$i=0;
foreach($doiList as $doi) {
$i++;
echo $i." / ".count($doiList).": =====================================> ".$doi." <===================================== ".PHP_EOL.PHP_EOL;
$skipThisDOI = false;
if(!is_null($IGNOREDOI)) {
foreach($IGNOREDOI as $ignore) {
if(strpos($doi, $ignore) !== false) {
echo "This DOI (or a part of it) should be ignored! (".$doi." <=> ".$ignore."), skipping...".PHP_EOL;
$result['skipped'][] = $doi;
#sleep(5);
$skipThisDOI = true;
}
}
}
if($skipThisDOI)
continue;
echo "Getting info ... ";
if(!is_null($PROXY)) {
curl_setopt($c, CURLOPT_PROXY, $PROXY);
}
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($c, CURLOPT_URL, "https://mds.datacite.org/doi/$doi");
curl_setopt($c,CURLOPT_USERAGENT,'doi_url_updater (https://github.com/Commifreak/doi_url_updater)');
$out = curl_exec($c);
if(empty($out))
die("Got no data!");
echo "Got data!".PHP_EOL;
if(strpos($out, $OLDDOMAIN) === false) {
echo "This DOI has not the wanted OLD domain! (".$out.") Ignoring...".PHP_EOL;
$result['domainproblem'][$doi] = $out;
#sleep(5);
continue;
} else {
echo "This DOI is candidate for updating the URL!".PHP_EOL;
$doiNewUrl = str_replace($OLDDOMAIN, $NEWDOMAIN, $out);
echo "* Updating the URL from ".$out." to ".$doiNewUrl." ... ";
if($prod) {
$newDOIData = array(
"doi" => $doi,
"url" => $doiNewUrl
);
$c2 = curl_init();
if(!is_null($PROXY)) {
curl_setopt($c, CURLOPT_PROXY, $PROXY);
}
curl_setopt($c2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c2, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($c2, CURLOPT_POST, true);
curl_setopt($c2, CURLOPT_POSTFIELDS, $newDOIData);
curl_setopt($c2, CURLOPT_URL, "https://mds.datacite.org/doi");
curl_setopt($c2,CURLOPT_USERAGENT,'doi_url_updater (https://github.com/Commifreak/doi_url_updater)');
$out2 = curl_exec($c2);
$info = curl_getinfo($c2);
curl_close($c2);
if($info["http_code"] != 201) {
echo "Something went wrong! HTTP-Code is not 201!".PHP_EOL."===> ".$out2;
$result['updateproblem'][$doi] = $out2;
} else {
echo "Success!".PHP_EOL;
}
} else {
echo PHP_EOL."TESTMODE! Not doing anything!".PHP_EOL;
}
}
#var_dump($out.PHP_EOL);
#if($i >= 2)
#break;
#echo PHP_EOL."=================================".PHP_EOL;
echo PHP_EOL;
}
curl_close($c);
echo "End!".PHP_EOL;
echo "========================= Results =========================".PHP_EOL;
echo "\tDOIs: ".count($doiList).PHP_EOL;
echo "\tskipped/Ignored: ".count($result['skipped']).PHP_EOL;
print_r($result['skipped']);
echo PHP_EOL;
echo "\tDomainproblem: ".count($result['domainproblem']).PHP_EOL;
print_r($result['domainproblem']);
echo PHP_EOL;
echo "\tUpdateproblem: ".count($result['updateproblem']).PHP_EOL;
print_r($result['updateproblem']);
echo PHP_EOL;
?>