-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecialIngestDataset.php
218 lines (203 loc) · 6.56 KB
/
SpecialIngestDataset.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
<?php
/**
* IngestDataset special page.
* Lets a data provider upload and/or update one or more datasets
* to the primary sources tool back end.
*
* @file
* @ingroup Extensions
* @author Marco Fossati - User:Hjfocs
* @author Tommaso Montefusco - User:Kiailandi
* @version 2.0
* @license GPL-3.0-or-later
*/
class SpecialIngestDataset extends SpecialPage {
// Back-end API endpoints
const BASE_URI = 'https://pst.wmflabs.org/v2/';
const DATASETS_SERVICE = self::BASE_URI . 'datasets';
const UPLOAD_SERVICE = self::BASE_URI . 'upload';
const UPDATE_SERVICE = self::BASE_URI . 'update';
/**
* Initialize this special page.
*/
public function __construct() {
parent::__construct( 'IngestDataset' );
}
/**
* Show this special page to the user.
*
* @param string $sub The subpage string argument (if any).
*/
public function execute( $sub ) {
$out = $this->getOutput();
$user = $this->getUser();
// JSON keys of the /datasets service response
$userKey = 'user';
$datasetKey = 'dataset';
$this->setHeaders();
// According to https://www.mediawiki.org/wiki/Manual:Special_pages#The_localisation_file
// setHeaders() should also add the '-summary' message located in the i18n folder.
// This does not seem to work, so add it here to the output page
$out->addWikiText(
'Send your dataset to the primary sources tool back end.
Please remember it must comply with the
[[:mw:Wikibase/Indexing/RDF_Dump_Format#Data_model | Wikidata RDF data model]].'
);
if ( $user->isLoggedIn() ) {
$datasets = json_decode( file_get_contents( self::DATASETS_SERVICE ) );
$datasetCount = count( $datasets );
$userDatasets = [];
for ( $i = 0; $i < $datasetCount; $i++ ) {
preg_match( '/User:([^\/]+)/', $datasets[$i]->$userKey, $re );
if ( $re[1] == $user->getName() ) {
array_push( $userDatasets, $datasets[$i]->$datasetKey );
}
}
$userDatasetCount = count( $userDatasets );
// Enable update only if the user has uploaded at least a dataset
if ( $userDatasetCount > 0 ) {
$out->addHTML( '<script>
function swap(){
if($("#swap").text() == "I want to update a dataset"){
$("#uploadForm").hide();
$("#updateForm").show();
$("#swap").text("I want to upload a dataset");
}
else{
$("#updateForm").hide();
$("#uploadForm").show();
$("#swap").text("I want to update a dataset");
}
}
</script>'
);
$out->addHTML(
'<button id="swap" onClick="swap()">I want to update a dataset</button>
<br /><br />'
);
$updateHtml =
'<form id="updateForm" action="' . self::UPDATE_SERVICE . '"
method="post" enctype="multipart/form-data" style="display:none">
<input type="hidden" name="user" value="' . $user->getName() . ' /">
<fieldset>
<legend>Update</legend>
<table><tbody>
<tr class="mw-htmlform-field-UpdateSourceField">
<td class="mw-label">
<label for="datasetToUpdate">Dataset name to update:</label>
</td>
<td class="mw-input">
<select id="datasetToUpdate" name="dataset">';
for ( $i = 0; $i < $userDatasetCount; $i++ ) {
$updateHtml .=
'<option value="' . $userDatasets[$i] . '">'
. explode( '/', $userDatasets[$i] )[2] .
'</option>';
}
$updateHtml .=
'</select>
</td>
</tr>
<tr class="mw-htmlform-field-UpdateSourceField">
<td class="mw-label">
<label for="datasetToRemove">Dataset file to remove:</label>
</td>
<td class="mw-input">
<input id="datasetToRemove" name="remove" type="file" />
</td>
</tr>
<tr class="mw-htmlform-field-UpdateSourceField">
<td class="mw-label">
<label for="datasetToAdd">Dataset file to add:</label>
</td>
<td class="mw-input">
<input id="datasetToAdd" name="add" type="file" />
</td>
</tr>
<tr>
<td colspan="2" class="htmlform-tip">Maximum file size: 250 MB</td>
</tr>
<tr>
<td colspan="2" class="htmlform-tip">File format allowed: RDF</td>
</tr>
</tbody></table>
</fieldset>
<span class="mw-htmlform-submit-buttons">
<input type="button" onclick="
if (
$(\'#datasetToRemove\').get(0).files.length === 0 ||
$(\'#datasetToAdd\').get(0).files.length === 0
) {
alert(\'Please select a file for both inputs\')
} else {
submit()
}"
title="Update your dataset" value="Submit" />
</span>
</form>';
$out->addHTML( $updateHtml );
}
$out->addHTML(
'<form id="uploadForm" action="' . self::UPLOAD_SERVICE . '"
method="post" enctype="multipart/form-data">
<input type="hidden" name="user" value="' . $user->getName() . '" />
<fieldset>
<legend>Upload</legend>
<table><tbody>
<tr class="mw-htmlform-field-HTMLTextField">
<td class="mw-label">
<label for="datasetName">Dataset name:</label>
</td>
<td class="mw-input">
<input id="datasetName" type="text" name="name" />
</td>
</tr>
<tr class="mw-htmlform-field-HTMLTextField">
<td class="mw-label">
<label for="datasetDescription">Dataset description (optional):</label>
</td>
<td class="mw-input">
<textarea id="datasetDescription" name="description"></textarea>
</td>
</tr>
<tr class="mw-htmlform-field-UploadSourceField">
<td class="mw-label">
<label for="datasetFiles">Dataset files:</label>
</td>
<td class="mw-input">
<input id="datasetFiles" type="file" name="dataset" multiple />
</td>
</tr>
<tr>
<td colspan="2" class="htmlform-tip">Maximum file size: 250 MB</td>
</tr>
<tr>
<td colspan="2" class="htmlform-tip">File format allowed: RDF</td>
</tr>
</tbody></table>
</fieldset>
<span class="mw-htmlform-submit-buttons">
<input type="button" onclick="
if ( $( \'#datasetFiles\' ).get( 0 ).files.length === 0 ) {
alert( \'Please select a file\' )
} else {
submit()
}"
title="Upload your dataset" value="Submit" />
</span>
</form>'
);
} else {
$out->addWikiText( "<big>'''Please log in to use this feature.'''</big>" );
}
}
/**
* Make this special page appear on Special:SpecialPages under the
* 'Primary sources tool' section.
*
* @return string
*/
protected function getGroupName() {
return 'primarysources';
}
}