-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.abstract_ext_update.php
265 lines (234 loc) · 8.4 KB
/
class.abstract_ext_update.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Class for updating the db.
*
* @author Hannes Bochmann <hannes.bochmann@dmk-ebusiness.de>
*/
abstract class abstract_ext_update
{
/**
* @var \TYPO3\CMS\Core\Charset\CharsetConverter or t3lib_cs
*/
private $csconv = false;
/**
* Main function, returning the HTML content of the update module.
*
* @return string HTML
*/
public function main()
{
$fieldsets = [];
$fieldsets['Character encoding'] = $this->getDestEncodingSelect();
$fieldsets['Update Static Info Tables'] = $this->handleUpdateStaticInfoTables();
$content = '';
$content .= '<form action="'.htmlspecialchars(\Sys25\RnBase\Utility\Link::linkThisScript()).'" method="post">';
foreach ($fieldsets as $legend => $fieldset) {
$content .= '<fieldset>';
if ($legend && !is_numeric($legend)) {
$content .= '<legend><strong> '.$legend.' </strong></legend>';
}
$content .= $fieldset;
$content .= '</fieldset>';
$content .= '<p><br /></p>';
}
$content .= '<p><input type="submit" /></p>';
$content .= '</form>';
return $content;
}
/**
* Erzeugt die Selectbox für das encoding.
*
* @return string
*/
private function getDestEncodingSelect()
{
require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('static_info_tables', 'class.tx_staticinfotables_encoding.php');
$content = '';
$content .= '<label>Destination character encoding:</label>';
$content .= tx_staticinfotables_encoding::getEncodingSelect('dest_encoding', '', 'utf-8');
$content .= '<p>(The character encoding must match the encoding of the existing tables data. By default this is UTF-8.)</p>';
if ($destEncoding = $this->getDestEncoding()) {
$content .= '<p>Current encoding: '.htmlspecialchars($destEncoding).'</p>';
}
return $content;
}
/**
* @return string
*/
private function getDestEncoding()
{
return \Sys25\RnBase\Frontend\Request\Parameters::getPostOrGetParameter('dest_encoding');
}
/**
* @TODO prüfen ob der import bereits durchgeführt wurde.
*
* @return string
*/
private function handleUpdateStaticInfoTables()
{
$updateKey = $this->getStatementKey();
$content = '';
$content .= $this->getInfoMsg();
if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('static_info_tables')) {
$content .= '<p><strong>The extension static_info_tables needs to be installed first!</strong></p>';
} else {
if (\Sys25\RnBase\Frontend\Request\Parameters::getPostOrGetParameter($updateKey)) {
if (true === ($ret = $this->queryDB($updateKey))) {
$content .= $this->getSuccessMsg();
} else {
$content .= '<p><big><strong>'.$ret.'</strong></big></p>';
}
} else {
$content .= '<p><br /><input type="checkbox" name="'.$updateKey.'" id="'.$updateKey.'" /> <label for="'.$updateKey.'">'.$this->getCheckboxLabel().'</label></p>';
}
}
return $content;
}
/**
* Liefert das Label für die Checkbox
* Enter description here ...
*/
protected function getCheckboxLabel()
{
return 'import static info tables';
}
private function queryDB($updateKey)
{
$file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->getExtensionName(), $this->getSqlFileName());
$fileContent = explode("\n", \Sys25\RnBase\Utility\Network::getUrl($file));
if (!$fileContent) {
return $this->getSqlFileName().' not found! ('.$file.')';
}
$destEncoding = $this->getDestEncoding();
$querys = [];
$keyQuery = 0;
foreach ($fileContent as $line) {
$line = trim($line);
// nach dem ende des update keys suchen
if ($keyQuery && \Sys25\RnBase\Utility\Strings::isFirstPartOfStr($line, '#'.$updateKey)) {
$keyQuery = 2;
break; // alle satements gefunden schleife nicht mehr durchlaufen
}
// nach dem anfang des update keys suchen
if (!$keyQuery && \Sys25\RnBase\Utility\Strings::isFirstPartOfStr($line, '#'.$updateKey)) {
$keyQuery = 1; // key gefunden, jetzt folgen die statements
continue;
}
// der update key wurde noch nicht erreicht
if (!$keyQuery) {
continue;
}
if ($line && \Sys25\RnBase\Utility\Strings::isFirstPartOfStr($line, $this->getSqlMode())) {
// ggf. das encoding ändern
$querys[] = $this->getUpdateEncoded($line, $destEncoding);
}
}
switch ($keyQuery) {
case 0:
return 'No '.strtolower($this->getSqlMode()).' key not found. ('.$updateKey.')';
case 1:
return 'End key not found. ('.$updateKey.')';
case 2:
// alles ok
}
if (0 === count($querys)) {
return 'No queries found. ('.$updateKey.')';
}
foreach ($querys as $query) {
\Sys25\RnBase\Database\Connection::getInstance()->doQuery($query);
}
return true;
}
/**
* Sollen Updates, Inserts ausgeführt werden?
*
* @return string
*/
protected function getSqlMode()
{
return 'UPDATE';
}
/**
* @return \TYPO3\CMS\Core\Charset\CharsetConverter or t3lib_cs
*/
private function getCharsetsConversion()
{
if (!$this->csconv) {
$charsetConverterClass = '\TYPO3\CMS\Core\Charset\CharsetConverter';
$this->csconv = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($charsetConverterClass);
}
return $this->csconv;
}
/**
* Convert the values of a SQL update statement to a different encoding than UTF-8.
*
* @param string $query Update statement like: UPDATE static_countries SET zipcode_rule='2', zipcode_length='5' WHERE cn_iso_2='DE';
* @param string $destEncoding Destination encoding
*
* @return string Converted update statement
*/
private function getUpdateEncoded($query, $destEncoding)
{
if (!('utf-8' === $destEncoding)) {
$queryElements = explode('WHERE', $query);
$where = preg_replace('#;$#', '', trim($queryElements[1]));
$queryElements = explode('SET', $queryElements[0]);
$queryFields = $queryElements[1];
$queryElements = \Sys25\RnBase\Utility\Strings::trimExplode('UPDATE', $queryElements[0], 1);
$table = $queryElements[0];
$fields_values = [];
$queryFieldsArray = \Sys25\RnBase\Utility\Strings::trimExplode(',', $queryFields, 1);
foreach ($queryFieldsArray as $fieldsSet) {
$col = \Sys25\RnBase\Utility\Strings::trimExplode('=', $fieldsSet, 1);
$value = stripslashes(substr($col[1], 1, strlen($col[1]) - 2));
$value = $this->getCharsetsConversion()->conv($value, 'utf-8', $destEncoding);
$fields_values[$col[0]] = $value;
}
$query = \Sys25\RnBase\Database\Connection::getInstance()->doUpdate(
$table,
$where,
$fields_values,
['sqlonly' => true]
);
}
return $query;
}
public function access()
{
return true;
}
/**
* Liefert den Namen der Datei, welche die Update Statements beinhaltet.
*
* @return string
*/
protected function getSqlFileName()
{
return 'ext_tables_static_update.sql';
}
/**
* @return string
*/
protected function getStatementKey()
{
return 'importStaticInfoTables';
}
/**
* Liefert den Namen der Extension für die.
*
* @return string
*/
abstract protected function getExtensionName();
/**
* Liefert die Nachricht, was gemacht werden soll.
*
* @return string
*/
abstract protected function getInfoMsg();
/**
* Liefert die Nachricht für den Erfolgsfall.
*
* @return string
*/
abstract protected function getSuccessMsg();
}