-
Notifications
You must be signed in to change notification settings - Fork 3
/
TabularImporter.php
178 lines (145 loc) · 6.23 KB
/
TabularImporter.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
<?php
/**
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
require_once("Importer.php");
/**
* Tabular CSV Importer
*
* @category OntoWiki
* @package Extensions
* @subpackage Csvimport
* @copyright Copyright (c) 2010, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
class TabularImporter extends Importer
{
public function parseFile() {
require_once 'CsvParser.php';
$separator = $this->parserInstructions['separator'];
$headlineDetection = $this->parserInstructions['headlineDetection'];
$parser = new CsvParser($this->uploadedFile, $separator, $headlineDetection);
$this->parsedFile = array_filter($parser->getParsedFile());
}
public function importData() {
$ontowiki = OntoWiki::getInstance();
$model = $ontowiki->selectedModel;
$modelUri = $model->getModelUri();
$prefixes = $model->getNamespaces();
#Extraction of Definitions and parsed file
$fragment = "";
if(!empty($this->configuration['fragment'])) {
$fragment = $this->configuration['fragment'];
}
$i=0;
foreach($this->parsedFile as $entity){
$resourceUri = $modelUri . $fragment . $i++;
$predicates= array();
$counter=-1;
foreach ($entity as $key => $element) {
$type = 'literal';
$counter++;
if (!empty($this->configuration['dimensions'][$counter])) {
$elementConf = $this->configuration['dimensions'][$counter];
#---------------------------------------------------------------
#RDF-Object generieren
if(!empty($elementConf['is_url'])) {
if(!empty($elementConf['contains_prefix'])) {
$grepedPrefix = substr($element, 0, strpos($element, ":"));
$namespace = "";
foreach ($prefixes as $ns => $prefix) {
$namespace = (strtolower($prefix) == strtolower($grepedPrefix)) ? $ns : "" ;
}
if(!empty($namespace)){
$element = str_replace($grepedPrefix.":",$namespace, $element);
}
}
if (parse_url($element) != FALSE) {
$type = 'uri';
}
}
}
$object = array(
'type' => $type,
'value' => $element
);
#---------------------------------------------------------------
#RDF-predicate generieren
$predicateUri = $modelUri . "p/" . urlencode($key);
if(!empty($elementConf['property'])) {
$predicateUri = $elementConf['property'];
}
if (!empty($element)){
$predicates[$predicateUri][] = $object ;
}
#---------------------------------------------------------------
#chaning Resource Uri if necessary
if(!empty($elementConf['contains_id'])) {
$resourceUri = $modelUri . $fragment . $element;
}
}
$statements = array($resourceUri => $predicates);
$model->addMultipleStatements($statements);
}
/* $config = array();
$headers = array_keys($this->parsedFile[0]);
$i = 0;
foreach($headers as $element){
$config[$element] = $this->configuration['dimensions'][$i++];
}
$i = 0;
$url = $this->componentConfig->item->base . "/".hash("md5", serialize($this->parsedFile))."/";
$elements = array();
foreach($this->parsedFile as $line){
foreach($line as $key => $value){
$element = array();
$element[$url.$i] = array(
$config[$key] => array(
array(
'type' => 'uri',
'value' => $value
)
)
);
$elements[] = $element;
//echo "someuri".$i." + ".$config[$key]." + ".$value."\n";
}
$i++;
}
$ontowiki = OntoWiki::getInstance();
foreach ($elements as $elem) {
//print_r($elem);
$ontowiki->selectedModel->addMultipleStatements($elem);
}
*/ }
public function createConfigurationView($urlBase) {
$ontowiki = OntoWiki::getInstance();
$model = $ontowiki->selectedModel;
$this->view->placeholder('main.window.title')->append('Import CSV Data');
$this->view->actionUrl = $urlBase . 'csvimport/mapping';
$this->view->salt = hash("md5", serialize($this->parsedFile));
$this->view->modelUri = (string)$model;
$nav = $ontowiki->getNavigation();
$nav->disableNavigation();
$toolbar = $ontowiki->toolbar;
$toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Extract Triples', 'id' => 'extract'))
->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Cancel'));
$this->view->placeholder('main.window.toolbar')->set($toolbar);
$headers = array_keys($this->parsedFile[0]);
$data = array();
foreach ($headers as $element) {
$data[] = array($element);
}
$this->view->table = $this->view->partial('importer/tabular.phtml', array(
'data' => $data,
'script' => $this->componentConfig->urlBase . 'extensions/csvimport/scripts/autosuggest.js',
'tableClass' => 'csvimport',
'examples' => $this->parsedFile[0],
'baseUri' => $model->getModelUri(),
'prefixes' => $model->getNamespaces()
));
}
}