-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrun_a_batch.php
266 lines (240 loc) · 9.01 KB
/
run_a_batch.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
<?PHP
# Run a single batch
require_once ( __DIR__ . '/lib/initialize.php' ) ;
require_once ( __DIR__ . '/lib/wikidata_oauth.php' );
$pid = getmypid();
$batch_id = getenv('BATCH_ID');
if (! $batch_id) {
print("$pid - BATCH_ID environment variable missing - exiting...\n");
exit(1);
}
$token_key = getenv('TOKEN_KEY');
if (! $token_key) {
print("$pid - TOKEN_KEY environment variable missing - exiting...\n");
exit(1);
}
$token_secret = getenv('TOKEN_SECRET');
if (! $token_key) {
print("$pid - TOKEN_SECRET environment variable missing - exiting...\n");
exit(1);
}
$dbtools = new DatabaseTools($db_passwd_file);
$db_conn = $dbtools->openToolDB('authors');
$oauth = new WD_OAuth('author-disambiguator', $oauth_ini_file, $db_conn, $token_key, $token_secret);
if ($oauth->isAuthOK()) {
print ("$pid - Authorized for " . $oauth->userinfo->name . "\n") ;
} else {
print ("$pid - Authorization not working - exiting!\n");
$db_conn->close();
exit(1);
}
$eg_string = edit_groups_string($batch_id) ;
$wil = new WikidataItemList ;
$dbquery = "UPDATE batches SET process_id = $pid WHERE batch_id = '$batch_id'";
$db_conn->query($dbquery);
$edit_claims = new EditClaims($oauth);
while (1) {
$cmd_query = "SELECT ordinal, action, data FROM commands WHERE batch_id = '$batch_id' AND (status = 'READY' OR status = 'RUNNING') ORDER BY ordinal";
$results = $db_conn->query($cmd_query);
$actions_to_run = array();
while ($row = $results->fetch_row()) {
$action_data = array();
$action_data['ordinal'] = $row[0];
$action_data['action'] = $row[1];
$action_data['data'] = $row[2];
$actions_to_run[] = $action_data;
}
$results->close();
if (count($actions_to_run) == 0) {
break;
}
foreach ($actions_to_run AS $action_data) {
$ordinal = $action_data['ordinal'];
$action = $action_data['action'];
$data = $action_data['data'];
$running_cmd = "UPDATE commands SET status = 'RUNNING', run = NOW() WHERE batch_id = '$batch_id' and ordinal = $ordinal";
$db_conn->query($running_cmd);
$db_conn->close();
$error = NULL;
if ($action == 'replace_name') {
$cmd_parts = array();
if (preg_match('/^(Q\d+):(Q\d+):(\d+)/', $data, $cmd_parts)) {
$author_q = $cmd_parts[1];
$work_qid = $cmd_parts[2];
$author_num = $cmd_parts[3];
print ("$batch_id/$pid - Replacing name for $work_qid - $author_num with $author_q\n");
$result = $edit_claims->replace_name_with_author($work_qid, $author_num, $author_q, "Author Disambiguator set author [[$author_q]] $eg_string");
if (! $result) {
$error = $edit_claims->error;
}
} else if (preg_match('/^(Q\d+):(Q\d+):(.+)/', $data, $cmd_parts)) {
$author_q = $cmd_parts[1];
$work_qid = $cmd_parts[2];
$claim_id = $cmd_parts[3];
print ("$batch_id/$pid - Replacing name for $work_qid - $claim_id with $author_q\n");
$result = $edit_claims->replace_name_claim_with_author($work_qid, $claim_id, $author_q, "Author Disambiguator set author [[$author_q]] $eg_string");
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'revert_author') {
$cmd_parts = array();
if (preg_match('/^(Q\d+):(Q\d+)$/', $data, $cmd_parts)) {
$author_q = $cmd_parts[1];
$work_qid = $cmd_parts[2];
$wil->loadItems( [$author_q] );
$author_item = $wil->getItem ( $author_q ) ;
print ("$batch_id/$pid - Reverting author for $work_qid - $author_q\n");
$result = $edit_claims->revert_author( $work_qid, $author_item, "Author Disambiguator revert author for [[$author_q]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'move_author') {
$cmd_parts = array();
if (preg_match('/^(Q\d+):(Q\d+):(Q\d+)$/', $data, $cmd_parts)) {
$author_q = $cmd_parts[1];
$work_qid = $cmd_parts[2];
$new_author_q = $cmd_parts[3];
print ("$batch_id/$pid - Moving author for $work_qid - $author_q to $new_author_q\n");
$result = $edit_claims->move_author( $work_qid, $author_q, $new_author_q, "Author Disambiguator change author from [[$author_q]] to [[$new_author_q]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'merge_work') {
if (preg_match('/^(Q\d+)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
print ("$batch_id/$pid - Merging redundant author entries for $work_qid\n");
$article_item = generate_article_entries2( [$work_qid], true) [ $work_qid ];
$to_load = array() ;
$author_qid_map = array();
foreach ( $article_item->authors AS $auth_list ) {
foreach ( $auth_list AS $auth ) {
$to_load[] = $auth ;
$author_qid_map[$auth] = 1;
}
}
foreach ( $article_item->published_in AS $pub ) $to_load[] = $pub ;
foreach ( $article_item->topics AS $topic ) $to_load[] = $topic ;
$to_load = array_unique( $to_load );
$wil->loadItems ( $to_load ) ;
$author_qids = array_keys($author_qid_map);
$stated_as_names = fetch_stated_as_for_authors($author_qids, true);
$merge_candidates = $article_item->merge_candidates($wil, $stated_as_names);
$author_numbers = array() ;
foreach ( $merge_candidates AS $num => $do_merge ) {
if ($do_merge) {
$author_numbers[] = $num ;
}
}
$result = $edit_claims->merge_authors( $work_qid, $author_numbers, array(), "Author Disambiguator merge authors for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'merge_authors') {
if (preg_match('/^(Q\d+):(.*):(.*)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
$author_numbers = explode('|', $cmd_parts[2]);
if ($author_numbers[0] == NULL) {
$author_numbers = array();
}
$remove_claims = explode('|', $cmd_parts[3]);
if ($remove_claims[0] == NULL) {
$remove_claims = array();
}
print ("$batch_id/$pid - Merging author entries for $work_qid : author numbers " . implode(',', $author_numbers) . "; removing claims " . implode(',', $remove_claims) . "\n");
$result = $edit_claims->merge_authors( $work_qid, $author_numbers, $remove_claims, "Author Disambiguator merge authors for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'renumber_authors') {
if (preg_match('/^(Q\d+):(.*):(.*)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
$renumbering_pairs = explode('|', $cmd_parts[2]);
if ($renumbering_pairs[0] == NULL) {
$renumbering_pairs = array();
}
$remove_claims = explode('|', $cmd_parts[3]);
if ($remove_claims[0] == NULL) {
$remove_claims = array();
}
$claims = array();
$new_nums = array();
foreach ($renumbering_pairs AS $pair) {
$parts = explode(',', $pair);
$claims[] = $parts[0];
$new_nums[] = $parts[1];
}
$renumbering = array_combine($claims, $new_nums);
print ("$batch_id/$pid - Renumbering authors for $work_qid\n");
$result = $edit_claims->renumber_authors( $work_qid, $renumbering, $remove_claims, "Author Disambiguator renumber authors for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'match_authors') {
if (preg_match('/^(Q\d+):(.*)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
$matches = explode('|', $cmd_parts[2]);
if ($matches[0] == NULL) {
$matches = array();
}
print ("$batch_id/$pid - Matching authors for $work_qid\n");
$result = $edit_claims->match_authors( $work_qid, $matches, "Author Disambiguator matching authors for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'add_missing') {
if (preg_match('/^(Q\d+):(.*)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
$authors_to_add = explode('|', $cmd_parts[2]);
print ("$batch_id/$pid - Adding authors for $work_qid\n");
$result = $edit_claims->add_authors( $work_qid, $authors_to_add, "Author Disambiguator adding authors for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
} else if ($action == 'remove_name_strings') {
if (preg_match('/^(Q\d+)$/', $data, $cmd_parts)) {
$work_qid = $cmd_parts[1];
print ("$batch_id/$pid - Removing author name strings for $work_qid\n");
$result = $edit_claims->remove_name_strings( $work_qid, "Author Disambiguator removing author name strings for [[$work_qid]] $eg_string" ) ;
if (! $result) {
$error = $edit_claims->error;
}
} else {
$error = 'Bad data';
}
}
$db_conn = $dbtools->openToolDB('authors');
$finished_cmd = "UPDATE commands SET status = 'DONE' WHERE batch_id = '$batch_id' and ordinal = '$ordinal'";
if ($error != NULL) {
$error_str = $db_conn->real_escape_string($error);
$finished_cmd = "UPDATE commands SET status = 'ERROR', message = '$error_str' WHERE batch_id = '$batch_id' and ordinal = '$ordinal'";
}
$db_conn->query($finished_cmd);
}
}
$db_conn->close();
exit ( 0 ) ;
?>