|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) { |
|
4
|
|
|
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** In this class we store things related to data processing **/ |
|
8
|
|
|
|
|
9
|
|
|
class SDImportDataParser { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @param $input string |
|
13
|
|
|
* @param $args array |
|
14
|
|
|
* @param $parser Parser |
|
15
|
|
|
* @param $frame Frame |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function processData( $input, $args, $parser, $frame ) { |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
global $wgSDImportDataPage; |
|
21
|
|
|
$pageTitle = $parser->getTitle(); |
|
22
|
|
|
$output = ""; |
|
23
|
|
|
|
|
24
|
|
|
$separator="\t"; |
|
25
|
|
|
$delimiter='"'; |
|
26
|
|
|
$fields = array(); |
|
27
|
|
|
|
|
28
|
|
|
if ( is_object( $pageTitle ) ) { |
|
29
|
|
|
|
|
30
|
|
|
$ns = $pageTitle->getNamespace(); |
|
31
|
|
|
|
|
32
|
|
|
if ( key_exists( $ns, $wgSDImportDataPage ) ) { |
|
33
|
|
|
|
|
34
|
|
|
$nsRepo = $wgSDImportDataPage[$ns]; |
|
35
|
|
|
|
|
36
|
|
|
$separator = SDImportData::getSelector( $args, $nsRepo, "separator" ); // String |
|
37
|
|
|
$delimiter = SDImportData::getSelector( $args, $nsRepo, "delimiter" ); // String |
|
38
|
|
|
$object = SDImportData::getSelector( $args, $nsRepo, "rowobject" ); // String |
|
39
|
|
|
$fields = SDImportData::getSelector( $args, $nsRepo, "rowfields" ); // Array |
|
40
|
|
|
$props = SDImportData::getSelector( $args, $nsRepo, "typefields" ); // Array |
|
|
|
|
|
|
41
|
|
|
$refs = SDImportData::getSelector( $args, $nsRepo, "ref" ); // Hash |
|
42
|
|
|
$pre = SDImportData::getSelector( $args, $nsRepo, "prefields" ); // Array |
|
43
|
|
|
$post = SDImportData::getSelector( $args, $nsRepo, "postfields" ); // Array |
|
44
|
|
|
$editable = SDImportData::getSelector( $args, $nsRepo, "edit" ); // Boolean |
|
45
|
|
|
// TODO: Add single option here maybe? |
|
46
|
|
|
|
|
47
|
|
|
// TODO: Should we add props here if they don't exist? |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$dprops = array(); |
|
51
|
|
|
|
|
52
|
|
|
if ( $refs ) { |
|
53
|
|
|
foreach ( $refs as $key => $val ) { |
|
54
|
|
|
$dprops[ $key ] = SDImportData::processWikiText( $val, $pageTitle ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Empty array |
|
59
|
|
|
$table = array(); |
|
60
|
|
|
// We not assume preprocessing here |
|
61
|
|
|
$checkstr = trim( $input ); |
|
62
|
|
|
if ( !empty( $checkstr ) ) { |
|
63
|
|
|
$table = self::getCSVData( $input, $separator, $delimiter ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// wfErrorLog( "SELF: ".print_r($table), '/tmp/my-custom-debug.log' ); |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
foreach ( $table as $row ) { |
|
70
|
|
|
$fieldcount = 0; |
|
71
|
|
|
$struct = array(); |
|
72
|
|
|
foreach ( $row as $field ) { |
|
73
|
|
|
|
|
74
|
|
|
$field = trim( $field ); |
|
75
|
|
|
|
|
76
|
|
|
if ( ! empty( $field ) ) { |
|
77
|
|
|
$pretxt = ""; |
|
78
|
|
|
if ( isset( $pre[ $fieldcount ] ) && !empty( $pre[ $fieldcount ] ) ) { |
|
79
|
|
|
$pretxt = $pre[ $fieldcount ].":"; // : for pre |
|
80
|
|
|
} |
|
81
|
|
|
$postxt = ""; |
|
82
|
|
|
if ( isset( $post[ $fieldcount ] ) && !empty( $post[ $fieldcount ] ) ) { |
|
83
|
|
|
$postxt = "@".$post[ $fieldcount ]; // @ for post |
|
84
|
|
|
} |
|
85
|
|
|
if ( array_key_exists( $fieldcount, $fields ) ) { |
|
86
|
|
|
$struct[ $fields[ $fieldcount ] ] = $pretxt.$field.$postxt; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
$fieldcount++; |
|
90
|
|
|
} |
|
91
|
|
|
foreach ( $dprops as $dpropk => $dpropv ) { |
|
92
|
|
|
$struct[ $dpropk ] = $dpropv; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
SDImportData::insertInternalObject( $parser, $pageTitle, $object, $struct ); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ( !empty( $input ) ) { |
|
103
|
|
|
$wgOut = $parser->getOutput(); |
|
104
|
|
|
|
|
105
|
|
|
// TODO: Revisit if this still needs handled this way |
|
106
|
|
|
global $wgScriptPath; |
|
107
|
|
|
$handsonpath = $wgScriptPath."/extensions/SemanticDataImport/libs/handsontable/handsontable.full.js"; |
|
108
|
|
|
$wgOut->addHeadItem( '<script src="'.$handsonpath.'"></script>' ); //Hack because of handsontable for last versions :/ |
|
109
|
|
|
$wgOut->addModules( 'ext.sdimport' ); |
|
110
|
|
|
|
|
111
|
|
|
$fieldList = ""; |
|
112
|
|
|
if ( sizeof( $fields ) > 0 ) { |
|
113
|
|
|
$fieldList = " data-cols='".implode(",", $fields)."' "; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$dataedit = ""; |
|
117
|
|
|
if ( $editable ) { |
|
|
|
|
|
|
118
|
|
|
$dataedit = "data-edit='data-edit'"; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$output = "<div class='smwdata' data-delimiter='".$delimiter."' data-separator=\"".$separator."\"".$fieldList." ".$dataedit.">".$input."</div>"; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return array( $output, 'noparse' => true, 'isHTML' => true ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $input string |
|
131
|
|
|
* @param $args array |
|
132
|
|
|
* @param $parser Parser |
|
133
|
|
|
* @param $frame Frame |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function prepareLink( $parser, $frame, $args ) { |
|
137
|
|
|
|
|
138
|
|
|
global $wgSDImportDataPage; |
|
139
|
|
|
|
|
140
|
|
|
$wgOut = $parser->getOutput(); |
|
141
|
|
|
$wgOut->addModules( 'ext.sdimport' ); |
|
142
|
|
|
|
|
143
|
|
|
$attrs_allowed = array( "title", "model", "readonly", "ref", "readOnlyfields" ); |
|
144
|
|
|
|
|
145
|
|
|
$attrs = array(); |
|
146
|
|
|
$output = ""; |
|
147
|
|
|
$model = "json"; // Let's use by default JSON model |
|
148
|
|
|
$pagetitle = null; // No page default. Do nothing |
|
149
|
|
|
$ref = null; // No ref hash by default |
|
150
|
|
|
$readOnlyfields = null; // No readonlyfields by default |
|
151
|
|
|
|
|
152
|
|
|
foreach ( $args as $arg ) { |
|
153
|
|
|
$arg_clean = trim( $frame->expand( $arg ) ); |
|
154
|
|
|
$arg_proc = explode( "=", $arg_clean, 2 ); |
|
155
|
|
|
|
|
156
|
|
|
if ( count( $arg_proc ) == 1 ){ |
|
157
|
|
|
$pagetitle = trim( $arg_proc[0] ); |
|
158
|
|
|
} else { |
|
159
|
|
|
|
|
160
|
|
|
if ( in_array( trim( $arg_proc[0] ), $attrs_allowed ) ) { |
|
161
|
|
|
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] ); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// TODO: Parse more parameters from function |
|
167
|
|
|
if ( array_key_exists( "title", $attrs ) ) { |
|
168
|
|
|
$pagetitle = $attrs['title']; |
|
169
|
|
|
} |
|
170
|
|
|
if ( array_key_exists( "model", $attrs ) ) { |
|
171
|
|
|
$model = $attrs['model']; |
|
172
|
|
|
} |
|
173
|
|
|
if ( array_key_exists( "ref", $attrs ) ) { |
|
174
|
|
|
$ref = str_replace( "[", "{", $attrs['ref'] ); |
|
175
|
|
|
$ref = str_replace( "]", "}", $ref ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if ( array_key_exists( "readOnlyfields", $attrs ) ) { |
|
179
|
|
|
$readOnlyfields = str_replace( "[", "{", $attrs['readOnlyfields'] ); |
|
180
|
|
|
$readOnlyfields = str_replace( "]", "}", $readOnlyfields ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if ( $pagetitle ) { |
|
184
|
|
|
|
|
185
|
|
|
$dataAttrsStr = ""; |
|
186
|
|
|
|
|
187
|
|
|
if ( $pagetitle ) { |
|
188
|
|
|
$dataAttrsStr.= "data-title='$pagetitle'"; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ( $ref ) { |
|
192
|
|
|
$dataAttrsStr.= " data-ref='$ref'"; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
if ( $readOnlyfields ) { |
|
196
|
|
|
$dataAttrsStr.= " data-readOnlyfields='$readOnlyfields'"; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$dataAttrsStr.= " data-model='$model'"; |
|
200
|
|
|
|
|
201
|
|
|
if ( array_key_exists( "readonly", $attrs ) ) { |
|
202
|
|
|
$dataAttrsStr.= " data-readonly='true'"; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$output = "<div class='smwdata-link' ".$dataAttrsStr."></div>"; |
|
206
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return array( $output, 'noparse' => true, 'isHTML' => true ); |
|
210
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
private static function getCSVData( $text, $separator="\t", $delimiter='"' ) { |
|
214
|
|
|
|
|
215
|
|
|
$table = array(); |
|
216
|
|
|
|
|
217
|
|
|
if ( empty( $text ) ) { |
|
218
|
|
|
return $table; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$linesCSV = explode( "\n", $text ); |
|
222
|
|
|
|
|
223
|
|
|
foreach ( $linesCSV as $lineCSV ) { |
|
224
|
|
|
if ( !empty( $lineCSV ) ) { |
|
225
|
|
|
array_push( $table, str_getcsv( $lineCSV, $separator, $delimiter ) ); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $table; |
|
230
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.