-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbatchAction.php
174 lines (126 loc) · 3.75 KB
/
batchAction.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
<?php
require_once( __DIR__ . '/vendor/autoload.php' );
require_once( __DIR__ . '/lib/resolve.php' );
require_once( __DIR__ . '/lib/action.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;
// Detect commandline args
$conffile = 'config.json';
$csvfile = 'list.csv';
$taskname = null; // If no task given, exit
$resolve = true; // If we allow wikipedia resolving
$delimiter = "\t"; // Default separator
$enclosure = "\""; // Default delimiter
$offset = 1; // Default offset
if ( count( $argv ) > 1 ) {
$conffile = $argv[1];
}
if ( count( $argv ) > 2 ) {
$csvfile = $argv[2];
}
if ( count( $argv ) > 3 ) {
$taskname = $argv[3];
}
// 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( "tasks", $confjson ) ) {
$tasksConf = $confjson["tasks"];
}
if ( array_key_exists( "resolve", $confjson ) ) {
$resolve = $confjson["resolve"];
}
if ( array_key_exists( "delimiter", $confjson ) ) {
$delimiter = $confjson["delimiter"];
}
if ( array_key_exists( "enclosure", $confjson ) ) {
$enclosure = $confjson["enclosure"];
}
if ( array_key_exists( "offset", $confjson ) ) {
$offset = $confjson["offset"];
}
$tasks = array_keys( $tasksConf );
$props = null;
if ( count( $tasks ) < 1 ) {
// No task, exit
exit;
}
if ( ! $taskname ) {
echo "No task specified!";
exit;
} else {
if ( in_array( $taskname, $tasks ) ) {
$props = $tasksConf[ $taskname ];
} else {
// Some error here. Stop it
exit;
}
}
$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()
);
$reader = Reader::createFromPath( $csvfile );
$reader->setOffset($offset);
$reader->setDelimiter( $delimiter );
$reader->setEnclosure( $enclosure );
$results = $reader->fetch();
foreach ( $results as $row ) {
$wdid = null;
# If smaller size, continue
if ( count( $row ) < 1 ) {
continue;
}
if ( substr( $row[0], 0, 1 ) === "#" ) {
# Skip if # -> Handling errors, etc.
continue;
}
echo $row[0]."\n";
// Do we resolve WikiData from Wikipedia?
if ( $resolve ) {
$wdid = retrieveWikidataId( $row[0], $wikiconfig, $wikidataconfig );
}
if ( $wdid ) {
// $wdid = "Q13406268"; // Dummy, for testing purposes. Must be changed
// Add statement and ref
echo $wdid."\n"; // Only considers id -> ACTION done via configuration
performAction( $wbFactory, $wdid, $row, $props, $wikiconfig );
performHeaderAction( $wbFactory, $wdid, $row, $props );
sleep( 5 ); // Delay 5 seconds
} else {
echo "- Missing ".$row[0]."\n";
$wdid = createItem( $wbFactory, $row, $props );
if ( $wdid ) {
performAction( $wbFactory, $wdid, $row, $props, $wikiconfig );
sleep( 5 ); // Delay 5 seconds
} else {
echo "- Could not create ".$row[0]."\n";
}
}
}
$api->logout();