Repository navigation
Expand file tree
/
Copy pathmerge_study_review_columns.php
More file actions
120 lines (104 loc) · 2.89 KB
/
Copy pathmerge_study_review_columns.php
File metadata and controls
120 lines (104 loc) · 2.89 KB
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
<?php
/**
* Copy Review state and Quality control tag onto a studies CSV.
*
* Join key: studies "Study page name" = reviews "Subject".
* Usage: php merge_study_review_columns.php <studies.csv> <reviews.csv>
*/
if ( $argc !== 3 ) {
fwrite( STDERR, "Usage: php merge_study_review_columns.php <studies.csv> <reviews.csv>\n" );
exit( 1 );
}
$studiesPath = $argv[1];
$reviewsPath = $argv[2];
/**
* @return string
*/
function titleKey( string $title ): string {
return str_replace( '_', ' ', trim( $title ) );
}
/**
* @param resource $handle
* @return list<string>|null
*/
function readCsvRow( $handle ) {
$row = fgetcsv( $handle );
if ( $row === false || $row === null ) {
return null;
}
if ( $row === [ null ] ) {
return null;
}
return $row;
}
$reviewsHandle = fopen( $reviewsPath, 'r' );
if ( $reviewsHandle === false ) {
fwrite( STDERR, "Cannot read $reviewsPath\n" );
exit( 1 );
}
$reviewHeader = readCsvRow( $reviewsHandle );
if ( $reviewHeader === null ) {
fwrite( STDERR, "Reviews CSV has no header\n" );
fclose( $reviewsHandle );
exit( 1 );
}
$subjectIdx = array_search( 'Subject', $reviewHeader, true );
$stateIdx = array_search( 'Review state', $reviewHeader, true );
$qcIdx = array_search( 'Quality control tag', $reviewHeader, true );
if ( $subjectIdx === false || $stateIdx === false || $qcIdx === false ) {
fwrite( STDERR, "Reviews CSV must have Subject, Review state, Quality control tag columns\n" );
fclose( $reviewsHandle );
exit( 1 );
}
$bySubject = [];
while ( ( $row = readCsvRow( $reviewsHandle ) ) !== null ) {
$subject = titleKey( $row[$subjectIdx] ?? '' );
if ( $subject === '' ) {
continue;
}
$bySubject[$subject] = [
$row[$stateIdx] ?? '',
$row[$qcIdx] ?? '',
];
}
fclose( $reviewsHandle );
$studiesHandle = fopen( $studiesPath, 'r' );
if ( $studiesHandle === false ) {
fwrite( STDERR, "Cannot read $studiesPath\n" );
exit( 1 );
}
$studyHeader = readCsvRow( $studiesHandle );
if ( $studyHeader === null ) {
fwrite( STDERR, "Studies CSV has no header\n" );
fclose( $studiesHandle );
exit( 1 );
}
$nameIdx = array_search( 'Study page name', $studyHeader, true );
if ( $nameIdx === false ) {
fwrite( STDERR, "Studies CSV must have a Study page name column\n" );
fclose( $studiesHandle );
exit( 1 );
}
$outPath = $studiesPath . '.tmp';
$outHandle = fopen( $outPath, 'w' );
if ( $outHandle === false ) {
fwrite( STDERR, "Cannot write $outPath\n" );
fclose( $studiesHandle );
exit( 1 );
}
$studyHeader[] = 'Review state';
$studyHeader[] = 'Quality control tag';
fputcsv( $outHandle, $studyHeader );
while ( ( $row = readCsvRow( $studiesHandle ) ) !== null ) {
$key = titleKey( $row[$nameIdx] ?? '' );
$extra = $bySubject[$key] ?? [ '', '' ];
$row[] = $extra[0];
$row[] = $extra[1];
fputcsv( $outHandle, $row );
}
fclose( $studiesHandle );
fclose( $outHandle );
if ( !rename( $outPath, $studiesPath ) ) {
fwrite( STDERR, "Cannot replace $studiesPath\n" );
exit( 1 );
}