/
Setup.php
273 lines (216 loc) · 8.26 KB
/
Setup.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
266
267
268
269
270
271
272
273
<?php
/**
* @license GPLv3, http://www.gnu.org/copyleft/gpl.html
* @copyright Aimeos (aimeos.org), 2014-2016
* @package TYPO3
*/
namespace Aimeos\Aimeos;
use \Symfony\Component\Console\Output\OutputInterface;
use \TYPO3\CMS\Core\Package\Event\AfterPackageActivationEvent;
use \TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent;
use \TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
use \TYPO3\CMS\Install\Updates\RepeatableInterface;
use \TYPO3\CMS\Install\Updates\ChattyInterface;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Aimeos setup class.
*
* @package TYPO3
*/
class Setup implements UpgradeWizardInterface, RepeatableInterface, ChattyInterface
{
private $output;
/**
* Return the identifier for this wizard
* This should be the same string as used in the ext_localconf class registration
*
* @return string
*/
public function getIdentifier() : string
{
return 'aimeos';
}
/**
* Return the speaking name of this wizard
*
* @return string
*/
public function getTitle() : string
{
return 'Aimeos database migration';
}
/**
* Return the description for this wizard
*
* @return string
*/
public function getDescription() : string
{
return 'Updates the Aimeos database tables and migrates data if necessary';
}
/**
* Execute the update
*
* @return bool
*/
public function executeUpdate() : bool
{
try {
ob_start();
$exectimeStart = microtime(true);
self::execute();
$this->output->writeln(ob_get_clean());
$this->output->writeln(sprintf('Setup process lasted %1$f sec', (microtime(true) - $exectimeStart)));
} catch(\Throwable $t) {
$this->output->writeln(ob_get_clean());
$this->output->writeln($t->getMessage());
$this->output->writeln($t->getTraceAsString());
return false;
}
return true;
}
/**
* Returns the classes the upgrade wizard depends on
*
* @return string[]
*/
public function getPrerequisites() : array
{
return [];
}
/**
* Setter injection for output into upgrade wizards
*
* @param OutputInterface $output
*/
public function setOutput(OutputInterface $output) : void
{
$this->output = $output;
}
/**
* Checks if update is necessary
*
* @return bool Whether an update is required (TRUE) or not (FALSE)
*/
public function updateNecessary() : bool
{
return true;
}
/**
* Executes the setup tasks for updating the database.
*/
public static function execute()
{
ini_set('max_execution_time', 0);
$extconf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class);
$demo = $extconf->get('aimeos', 'useDemoData') ?: false;
\Aimeos\MShop::cache(false);
\Aimeos\MAdmin::cache(false);
$site = $extconf->get('aimeos', 'siteCode') ?: 'default';
$template = $extconf->get('aimeos', 'siteTpl') ?: 'default';
$codelen = $extconf->get('aimeos', 'codeLength') ?: 64;
$boostrap = \Aimeos\Aimeos\Base::aimeos();
$ctx = self::context(['setup' => ['default' => ['demo' => (string) $demo]]])->setEditor('setup');
\Aimeos\Setup::use($boostrap, ['codelength' => $codelen])->verbose('vvv')
->context($ctx->setEditor('aimeos:setup'))
->up($site, $template);
$extconf->set('aimeos', array_merge($extconf->get('aimeos'), ['useDemoData' => '']));
}
/**
* Returns the current schema for the install tool
*
* @param array $list List of SQL statements
* @return array SQL statements required for the install tool
*/
public static function schema(array $list) : array
{
$ctx = self::context();
$connectionNames = array_keys($ctx->config()->get( 'resource'));
$connectionNames = array_filter($connectionNames, fn (string $key): bool => str_starts_with($key, 'db'));
foreach ($connectionNames as $connectionName) {
$conn = $ctx->db($connectionName);
$adapter = $ctx->config('resource/' . $connectionName . '/adapter');
$tables = [];
foreach(['fe_users_', 'madmin_', 'mshop_'] as $prefix) {
switch ($adapter) {
case "pgsql": $sql = 'SELECT tablename FROM pg_catalog.pg_tables where tablename ~ \'^' . $prefix . '\''; break;
default: $sql = 'SHOW TABLES like \'' . $prefix . '%\'';
}
$result = $conn->create($sql)->execute();
while(($row = $result->fetch(\Aimeos\Base\DB\Result\Base::FETCH_NUM)) !== null) {
$tables[] = $row[0];
}
}
foreach($tables as $table) {
switch ($adapter) {
case "pgsql": $sql = 'CREATE TABLE IF NOT EXISTS ' . $table . '()'; break;
default: $sql = 'SHOW CREATE TABLE `' . $table . '`';
}
$result = $conn->create($sql)->execute();
while(($row = $result->fetch(\Aimeos\Base\DB\Result\Base::FETCH_NUM)) !== null) {
$str = $row[1];
$str = str_replace('"', '`', $str);
$str = preg_replace('/CONSTRAINT `[a-zA-Z0-9_-]+` /', '', $str);
$str = preg_replace('/ DEFAULT CHARSET=[^ ;]+/', '', $str);
$str = preg_replace('/ COLLATE=[^ ;]+/', '', $str);
$list[] = $str . ";\n";
}
}
}
return ['sqlString' => $list];
}
/**
* Alter schema to avoid TYPO3 dropping Aimeos tables
*
* @param AlterTableDefinitionStatementsEvent $event Event object
*/
public function schemaEvent(AlterTableDefinitionStatementsEvent $event)
{
$list = self::schema([]);
foreach ($list['sqlString'] ?? [] as $sql) {
$event->addSqlData($sql);
}
}
/**
* Update schema if extension is installed
*
* @param AfterPackageActivationEvent $event Event object
*/
public function setupEvent(AfterPackageActivationEvent $event)
{
if ($event->getPackageKey() === 'aimeos' && \Aimeos\Aimeos\Base::getExtConfig('autoSetup', true)) {
self::execute();
}
}
/**
* Returns a new context object.
*
* @param array $config Nested array of configuration settings
* @return \Aimeos\MShop\ContextIface Context object
*/
protected static function context(array $config = []) : \Aimeos\MShop\ContextIface
{
$aimeosExtPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('aimeos');
if (file_exists($aimeosExtPath . '/Resources/Libraries/autoload.php') === true) {
require_once $aimeosExtPath . '/Resources/Libraries/autoload.php';
}
$ctx = new \Aimeos\MShop\Context();
$conf = \Aimeos\Aimeos\Base::config($config);
$ctx->setConfig($conf);
$ctx->setDatabaseManager(new \Aimeos\Base\DB\Manager\Standard($conf->get('resource', []), 'DBAL'));
$ctx->setFilesystemManager(new \Aimeos\Base\Filesystem\Manager\Standard($conf->get('resource', [])));
$ctx->setLogger(new \Aimeos\Base\Logger\Errorlog(\Aimeos\Base\Logger\Iface::INFO));
$ctx->setSession(new \Aimeos\Base\Session\None());
$ctx->setCache(new \Aimeos\Base\Cache\None());
// Reset before child processes are spawned to avoid lost DB connections afterwards (TYPO3 9.4 and above)
if (php_sapi_name() === 'cli' && class_exists('\TYPO3\CMS\Core\Database\ConnectionPool')
&& method_exists('\TYPO3\CMS\Core\Database\ConnectionPool', 'resetConnections')
) {
$ctx->setProcess(new \Aimeos\Base\Process\Pcntl(\Aimeos\Aimeos\Base::getExtConfig('pcntlMax', 4)));
} else {
$ctx->setProcess(new \Aimeos\Base\Process\None());
}
$factory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory');
return $ctx->setPassword(new \Aimeos\Base\Password\Typo3($factory->getDefaultHashInstance('FE')));
}
}