-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpreProcessCSV.php
168 lines (117 loc) · 3.72 KB
/
preProcessCSV.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
<?php
require_once( __DIR__ . '/vendor/autoload.php' );
require_once( __DIR__ . '/lib/resolve.php' );
use \Mediawiki\Api as MwApi;
use \Wikibase\Api as WbApi;
use \Mediawiki\DataModel as MwDM;
use \Wikibase\DataModel as WbDM;
use League\Csv\Reader;
use League\Csv\Writer;
// Detect commandline args
$conffile = 'config.json';
$csvfile = 'list.csv';
$delimiter = "\t"; // Default separator
$enclosure = "\""; // Default delimiter
$positions = array(); // Resolve positions
$dpositions = array(); // Resolve date positions
$dschema = array(); // Resolve date positions
$dschemaout = array(); // Resolve date positions
$offset = 1;
if ( count( $argv ) > 1 ) {
$conffile = $argv[1];
}
if ( count( $argv ) > 2 ) {
$csvfile = $argv[2];
}
// Detect if files
if ( ! file_exists( $conffile ) || ! file_exists( $csvfile ) ) {
die( "Files needed" );
}
$confjson = json_decode( file_get_contents( $conffile ), 1 );
$wikiconfig = null;
$wikidataconfig = null;
if ( array_key_exists( "wikipedia", $confjson ) ) {
$wikiconfig = $confjson["wikipedia"];
}
if ( array_key_exists( "wikidata", $confjson ) ) {
$wikidataconfig = $confjson["wikidata"];
}
if ( array_key_exists( "delimiter", $confjson ) ) {
$delimiter = $confjson["delimiter"];
}
if ( array_key_exists( "enclosure", $confjson ) ) {
$enclosure = $confjson["enclosure"];
}
if ( array_key_exists( "positions", $confjson ) ) {
$positions = $confjson["positions"];
}
if ( array_key_exists( "dpositions", $confjson ) ) {
$dpositions = $confjson["dpositions"];
}
if ( array_key_exists( "dschema", $confjson ) ) {
$dschema = $confjson["dschema"];
}
if ( array_key_exists( "dschemaout", $confjson ) ) {
$dschemaout = $confjson["dschemaout"];
}
if ( array_key_exists( "offset", $confjson ) ) {
$offset = $confjson["offset"];
}
$api = new MwApi\MediawikiApi( $wikidataconfig['url'] );
$api->login( new MwApi\ApiUser( $wikidataconfig['user'], $wikidataconfig['password'] ) );
$dataValueClasses = array(
'unknown' => 'DataValues\UnknownValue',
'string' => 'DataValues\StringValue',
'boolean' => 'DataValues\BooleanValue',
'number' => 'DataValues\NumberValue',
'time' => 'DataValues\TimeValue',
'globecoordinate' => 'DataValues\Geo\Values\GlobeCoordinateValue',
'wikibase-entityid' => 'Wikibase\DataModel\Entity\EntityIdValue',
);
$wbFactory = new WbApi\WikibaseFactory(
$api,
new DataValues\Deserializers\DataValueDeserializer( $dataValueClasses ),
new DataValues\Serializers\DataValueSerializer()
);
// Cache variable to store queries
$cache = array();
$oresults = array();
// If positions to resolve...
if ( count( $positions ) > 0 ) {
$reader = Reader::createFromPath( $csvfile );
$reader->setOffset($offset);
$reader->setDelimiter( $delimiter );
$reader->setEnclosure( $enclosure );
$results = $reader->fetch();
$count = 0;
foreach ( $results as $row ) {
$orow = $row;
foreach ( $positions as $pos ) {
// We assume pos is int
if ( array_key_exists( $pos, $orow ) ) {
$orow[$pos] = resolveValue( $orow[$pos], $cache, $wikiconfig, $wikidataconfig );
}
}
foreach ( $dpositions as $dpos ) {
// We assume pos is int
if ( array_key_exists( $dpos, $orow ) ) {
$schema = null;
$schemaout = null;
if ( $dschema && $dschema[ $dpos ] ) {
$schema = $dschema[ $dpos ];
}
if ( $dschemaout && $dschemaout[ $dpos ] ) {
$schemaout = $dschemaout[ $dpos ];
}
$orow[$dpos] = resolveDate( $orow[$dpos], $schema, $schemaout );
}
}
array_push( $oresults, $orow );
}
}
if ( count( $argv ) > 3 ) {
$writer = Writer::createFromPath( $argv[3], 'w+' );
$writer->setDelimiter( $delimiter );
$writer->setEnclosure( $enclosure );
$writer->insertAll( $oresults ); //using an array
}