forked from TomLous/solr-schema-dataimport-generator
/
generateDataimport.php
214 lines (155 loc) · 7.14 KB
/
generateDataimport.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
<?php
/**
* @author Tom Lous <tomlous@gmail.com>
* @copyright 2015 Tom Lous
* @package package
* Datetime: 16/06/15 11:25
*/
print PHP_EOL . "------------------------------------" . PHP_EOL . "Generating dataimport-config.xml" . PHP_EOL;
include_once('utils.php');
// Make sure to escape all slashes in Json eg"pattern": "([\.,;:-_])", => "pattern": "([\\.,;:-_])",
$config = json_decode(file_get_contents("config/config.json"), true);
if (json_last_error()) {
print "json decode error: " . json_last_error_msg();
exit();
}
$dataimportDoc = new DOMDocument("1.0", "UTF-8");
$dataimportDoc->preserveWhiteSpace = true;
$dataimportDoc->formatOutput = true;
$comment = new DOMComment("Generated ".date('Y-m-d')." with Solr Dataimport Generator (https://github.com/TomLous/solr-schema-dataimport-generator)");
$dataimportDoc->appendChild($comment);
$dataConfig = $dataimportDoc->createElement('dataConfig');
$mainDataSource = null;
foreach ($config['dataSources'] as $name => $data) {
$dataSource = $dataimportDoc->createElement('dataSource');
$dataSource->setAttribute('name', $name);
if ($mainDataSource === null) {
$mainDataSource = $name;
}
foreach ($data as $key => $val) {
$dataSource->setAttribute($key, $val);
}
$dataConfig->appendChild($dataSource);
}
$dependencyVariables = array();
if (isset($config['dependencyVariables'])) {
$dependencyVariables = $config['dependencyVariables'];
}
$document = $dataimportDoc->createElement('document');
$dataConfig->appendChild($document);
$fields = $config['fields'];
$entityQueries = $config['entityQueries'];
// fields generation
createQueriesConfig($dataimportDoc, $document, $fields, $entityQueries, $dependencyVariables);
//$dataSource->appendChild($document);
// parsing XML creating readable dataimport.xml
$dataimportDoc->appendChild($dataConfig);
$xml = $dataimportDoc->saveXML();
$xml = preg_replace("/\s+<!--NEWLINE-->/is", "\n", $xml);
$xml = preg_replace("/ /is", "\n", $xml);
$xml = preg_replace("/	/is", "\t", $xml);
//print $xml;
file_put_contents('target/dataimport-config.xml', $xml);
function createQueriesConfig(&$doc, &$el, $fields, $entityQueries, $dependencyVariables)
{
// select
$selectFields = array();
$primaryKeys = array();
$importFields = array();
foreach ($fields as $fieldName => $fieldInfo) {
$dependancyArray = createDependencyArray($fieldInfo, $dependencyVariables);
foreach ($dependancyArray as $pos => $dependancyData) {
// $sourceFieldName = $fieldName;
$currentFieldName = dependencyParser($fieldName, $dependancyData);
if (isset($fieldInfo['dataSourceEntity'])) {
if (isset($fieldInfo['uniqueKey']) && $fieldInfo['uniqueKey']) {
$primaryKeys[$fieldInfo['dataSourceEntity']] = $currentFieldName;
}
if (!isset($selectFields[$fieldInfo['dataSourceEntity']])) {
$selectFields[$fieldInfo['dataSourceEntity']] = array();
$importFields[$fieldInfo['dataSourceEntity']] = array();
}
$statement = isset($fieldInfo['dataSourceStatement']) ? $fieldInfo['dataSourceStatement'] : "`{$currentFieldName}`";
if (isset($fieldInfo['dataSourceStatementDependencyMapping'])) {
$mapping = array();
foreach ($dependancyData as $key => $val) {
if (isset($fieldInfo['dataSourceStatementDependencyMapping'][$key]) && isset($fieldInfo['dataSourceStatementDependencyMapping'][$key][$val])) {
$mapping[$key . '_map'] = $fieldInfo['dataSourceStatementDependencyMapping'][$key][$val];
}
}
$statement = dependencyParser($statement, $mapping);
}
if (isset($fieldInfo['multiValued']) && $fieldInfo['multiValued']) {
$statement .= " as `{$currentFieldName}`";
} else {
$statement .= " as `{$currentFieldName}`";
}
$selectFields[$fieldInfo['dataSourceEntity']][$currentFieldName] = $statement;
if (isset($fieldInfo['dataSourceMultivaluedSeperator'])){
$importFields[$fieldInfo['dataSourceEntity']][$currentFieldName] = array('column' => $currentFieldName, 'sourceColName' => $currentFieldName, 'splitBy'=>$fieldInfo['dataSourceMultivaluedSeperator']);
} else {
$importFields[$fieldInfo['dataSourceEntity']][$currentFieldName] = array('column' => $currentFieldName, 'name' => $currentFieldName);
}
}
}
}
// from
$entitiesXML = array();
$tabs = str_repeat("\t", 15);
$tabs2 = str_repeat("\t", 14);
$tabs3 = str_repeat("\t", 13);
foreach ($entityQueries as $entityName => $queryInfo) {
$entity = $doc->createElement('entity');
$entity->setAttribute('name', $entityName);
if (isset($queryInfo['dataSource'])) {
$entity->setAttribute('dataSource', $queryInfo['dataSource']);
}
if (isset($queryInfo['transformers'])) {
$entity->setAttribute('transformer', implode(",", $queryInfo['transformers']));
}
if(isset($queryInfo['pk'])){
$entity->setAttribute('pk', $queryInfo['pk']);
}elseif(isset($primaryKeys[$entityName])){
$entity->setAttribute('pk', $primaryKeys[$entityName]);
}
foreach ($queryInfo['queries'] as $queryType => $queryTypeData) {
$query = "SELECT ";
$select = array();
if(isset($queryTypeData['fields'])){
foreach($queryTypeData['fields'] as $fieldName){
$select[] = $selectFields[$entityName][$fieldName];
}
}else{
$select = $selectFields[$entityName];
}
$query .= "\n{$tabs}".implode(",\n{$tabs}", $select);
foreach($queryTypeData['tables'] as $tableGroup){
$query .= " \n{$tabs2}".implode(" \n{$tabs}",$queryInfo['tables'][$tableGroup]);
}
$query .= " \n{$tabs2}".$queryTypeData['filter'].";\n{$tabs3}";
$entity->setAttribute($queryType, $query);
}
$entitiesXML[$entityName] = array('element'=>$entity);
if(isset($queryInfo['parentEntity'])){
$entitiesXML[$entityName]['parent'] = $queryInfo['parentEntity'];
}
}
// fields
foreach($importFields as $entityName => $data){
$parentEl = $entitiesXML[$entityName]['element'];
foreach($data as $fieldName=>$attributes){
$field = $doc->createElement('field');
foreach($attributes as $attr=>$val){
$field->setAttribute($attr, $val);
}
$parentEl->appendChild($field);
}
}
foreach($entitiesXML as $entityName=>$entityData){
if(!isset($entityData['parent'])){
$el->appendChild($entityData['element']);
}else{
$entitiesXML[$entityData['parent']]['element']->appendChild($entityData['element']);
}
}
}