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->getNsText(); |
31
|
|
|
|
32
|
|
|
# Handle main namespace with _ |
33
|
|
|
if ( $ns == "" ) { |
34
|
|
|
$ns = "_"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( key_exists( $ns, $wgSDImportDataPage ) ) { |
38
|
|
|
|
39
|
|
|
$nsRepo = $wgSDImportDataPage[$ns]; |
40
|
|
|
|
41
|
|
|
$separator = SDImportData::getSelector( $args, $nsRepo, "separator" ); // String |
42
|
|
|
$delimiter = SDImportData::getSelector( $args, $nsRepo, "delimiter" ); // String |
43
|
|
|
$object = SDImportData::getSelector( $args, $nsRepo, "rowobject" ); // String |
44
|
|
|
$fields = SDImportData::getSelector( $args, $nsRepo, "rowfields" ); // Array |
45
|
|
|
$props = SDImportData::getSelector( $args, $nsRepo, "typefields" ); // Array |
|
|
|
|
46
|
|
|
$refs = SDImportData::getSelector( $args, $nsRepo, "ref" ); // Hash |
47
|
|
|
$pre = SDImportData::getSelector( $args, $nsRepo, "prefields" ); // Array |
48
|
|
|
$post = SDImportData::getSelector( $args, $nsRepo, "postfields" ); // Array |
49
|
|
|
$editable = SDImportData::getSelector( $args, $nsRepo, "edit" ); // Boolean |
50
|
|
|
// TODO: Add single option here maybe? |
51
|
|
|
|
52
|
|
|
// TODO: Should we add props here if they don't exist? |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
$dprops = array(); |
56
|
|
|
|
57
|
|
|
if ( $refs ) { |
58
|
|
|
foreach ( $refs as $key => $val ) { |
59
|
|
|
$dprops[ $key ] = SDImportData::processWikiText( $val, $pageTitle ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Empty array |
64
|
|
|
$table = array(); |
65
|
|
|
// We not assume preprocessing here |
66
|
|
|
$checkstr = trim( $input ); |
67
|
|
|
if ( !empty( $checkstr ) ) { |
68
|
|
|
$table = self::getCSVData( $input, $separator, $delimiter ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// wfErrorLog( "SELF: ".print_r($table), '/tmp/my-custom-debug.log' ); |
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
foreach ( $table as $row ) { |
75
|
|
|
$fieldcount = 0; |
76
|
|
|
$struct = array(); |
77
|
|
|
foreach ( $row as $field ) { |
78
|
|
|
|
79
|
|
|
$field = trim( $field ); |
80
|
|
|
|
81
|
|
|
if ( ! empty( $field ) ) { |
82
|
|
|
$pretxt = ""; |
83
|
|
|
if ( isset( $pre[ $fieldcount ] ) && !empty( $pre[ $fieldcount ] ) ) { |
84
|
|
|
$pretxt = $pre[ $fieldcount ].":"; // : for pre |
85
|
|
|
} |
86
|
|
|
$postxt = ""; |
87
|
|
|
if ( isset( $post[ $fieldcount ] ) && !empty( $post[ $fieldcount ] ) ) { |
88
|
|
|
$postxt = "@".$post[ $fieldcount ]; // @ for post |
89
|
|
|
} |
90
|
|
|
if ( array_key_exists( $fieldcount, $fields ) ) { |
91
|
|
|
$struct[ $fields[ $fieldcount ] ] = $pretxt.$field.$postxt; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
$fieldcount++; |
95
|
|
|
} |
96
|
|
|
foreach ( $dprops as $dpropk => $dpropv ) { |
97
|
|
|
$struct[ $dpropk ] = $dpropv; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
SDImportData::insertInternalObject( $parser, $pageTitle, $object, $struct ); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( !empty( $input ) ) { |
108
|
|
|
$wgOut = $parser->getOutput(); |
109
|
|
|
|
110
|
|
|
global $wgScriptPath; |
|
|
|
|
111
|
|
|
$handsonpath = $wgScriptPath."/extensions/SemanticDataImport/libs/handsontable/handsontable.full.js"; |
112
|
|
|
$wgOut->addHeadItem( '<script src="'.$handsonpath.'"></script>' ); //Hack because of handsontable for last versions :/ |
113
|
|
|
$wgOut->addModules( 'ext.sdimport' ); |
114
|
|
|
|
115
|
|
|
$fieldList = ""; |
116
|
|
|
if ( sizeof( $fields ) > 0 ) { |
117
|
|
|
$fieldList = " data-cols='".implode(",", $fields)."' "; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$dataedit = ""; |
121
|
|
|
if ( $editable ) { |
|
|
|
|
122
|
|
|
$dataedit = "data-edit='data-edit'"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$output = "<div class='smwdata' data-delimiter='".$delimiter."' data-separator=\"".$separator."\"".$fieldList." ".$dataedit.">".$input."</div>"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return array( $output, 'noparse' => true, 'isHTML' => true ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $input string |
135
|
|
|
* @param $args array |
136
|
|
|
* @param $parser Parser |
137
|
|
|
* @param $frame Frame |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public static function prepareLink( $parser, $frame, $args ) { |
141
|
|
|
|
142
|
|
|
global $wgSDImportDataPage; |
|
|
|
|
143
|
|
|
|
144
|
|
|
$wgOut = $parser->getOutput(); |
145
|
|
|
$wgOut->addModules( 'ext.sdimport' ); |
146
|
|
|
|
147
|
|
|
$attrs_allowed = array( "title", "model", "readonly", "ref", "readOnlyfields" ); |
148
|
|
|
|
149
|
|
|
$attrs = array(); |
150
|
|
|
$output = ""; |
151
|
|
|
$model = "json"; // Let's use by default JSON model |
152
|
|
|
$pagetitle = null; // No page default. Do nothing |
153
|
|
|
$ref = null; // No ref hash by default |
154
|
|
|
$readOnlyfields = null; // No readonlyfields by default |
155
|
|
|
|
156
|
|
|
foreach ( $args as $arg ) { |
157
|
|
|
$arg_clean = trim( $frame->expand( $arg ) ); |
158
|
|
|
$arg_proc = explode( "=", $arg_clean, 2 ); |
159
|
|
|
|
160
|
|
|
if ( count( $arg_proc ) == 1 ){ |
161
|
|
|
$pagetitle = trim( $arg_proc[0] ); |
162
|
|
|
} else { |
163
|
|
|
|
164
|
|
|
if ( in_array( trim( $arg_proc[0] ), $attrs_allowed ) ) { |
165
|
|
|
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] ); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// TODO: Parse more parameters from function |
171
|
|
|
if ( array_key_exists( "title", $attrs ) ) { |
172
|
|
|
$pagetitle = $attrs['title']; |
173
|
|
|
} |
174
|
|
|
if ( array_key_exists( "model", $attrs ) ) { |
175
|
|
|
$model = $attrs['model']; |
176
|
|
|
} |
177
|
|
|
if ( array_key_exists( "ref", $attrs ) ) { |
178
|
|
|
$ref = str_replace( "[", "{", $attrs['ref'] ); |
179
|
|
|
$ref = str_replace( "]", "}", $ref ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ( array_key_exists( "readOnlyfields", $attrs ) ) { |
183
|
|
|
$readOnlyfields = str_replace( "[", "{", $attrs['readOnlyfields'] ); |
184
|
|
|
$readOnlyfields = str_replace( "]", "}", $readOnlyfields ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ( $pagetitle ) { |
188
|
|
|
|
189
|
|
|
$dataAttrsStr = ""; |
190
|
|
|
|
191
|
|
|
if ( $pagetitle ) { |
192
|
|
|
$dataAttrsStr.= "data-title='$pagetitle'"; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ( $ref ) { |
196
|
|
|
$dataAttrsStr.= " data-ref='$ref'"; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ( $readOnlyfields ) { |
200
|
|
|
$dataAttrsStr.= " data-readOnlyfields='$readOnlyfields'"; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$dataAttrsStr.= " data-model='$model'"; |
204
|
|
|
|
205
|
|
|
if ( array_key_exists( "readonly", $attrs ) ) { |
206
|
|
|
$dataAttrsStr.= " data-readonly='true'"; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$output = "<div class='smwdata-link' ".$dataAttrsStr."></div>"; |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return array( $output, 'noparse' => true, 'isHTML' => true ); |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private static function getCSVData( $text, $separator="\t", $delimiter='"' ) { |
218
|
|
|
|
219
|
|
|
$table = array(); |
220
|
|
|
|
221
|
|
|
if ( empty( $text ) ) { |
222
|
|
|
return $table; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$linesCSV = explode( "\n", $text ); |
226
|
|
|
|
227
|
|
|
foreach ( $linesCSV as $lineCSV ) { |
228
|
|
|
if ( !empty( $lineCSV ) ) { |
229
|
|
|
array_push( $table, str_getcsv( $lineCSV, $separator, $delimiter ) ); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $table; |
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.