Completed
Push — master ( 858dca...453367 )
by Henry
06:29
created

Installer::rawMigrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript;
3
4
use PDOException;
5
use function file_get_contents;
6
use function method_exists;
7
use function str_replace;
8
use function ucfirst;
9
10
/**
11
 * parent class to install the database
12
 *
13
 * @since 2.4.0
14
 *
15
 * @category Installer
16
 * @package Redaxscript
17
 * @author Henry Ruhs
18
 */
19
20
class Installer
21
{
22
	/**
23
	 * instance of the registry class
24
	 *
25
	 * @var Registry
26
	 */
27
28
	protected $_registry;
29
30
	/**
31
	 * instance of the request class
32
	 *
33
	 * @var Request
34
	 */
35
36
	protected $_request;
37
38
	/**
39
	 * instance of the language class
40
	 *
41
	 * @var Language
42
	 */
43
44
	protected $_language;
45
46
	/**
47
	 * instance of the config class
48
	 *
49
	 * @var Config
50
	 */
51
52
	protected $_config;
53
54
	/**
55
	 * name of the directory
56
	 *
57
	 * @var string
58
	 */
59
60
	protected $_directory;
61
62
	/**
63
	 * placeholder for the prefix
64
	 *
65
	 * @var string
66
	 */
67
68
	protected $_prefixPlaceholder = '/* %PREFIX% */';
69
70
	/**
71
	 * constructor of the class
72
	 *
73
	 * @since 3.0.0
74
	 *
75
	 * @param Registry $registry instance of the registry class
76
	 * @param Request $request instance of the request class
77
	 * @param Language $language instance of the language class
78
	 * @param Config $config instance of the config class
79
	 */
80 3
81
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
82 3
	{
83 3
		$this->_registry = $registry;
84 3
		$this->_request = $request;
85 3
		$this->_language = $language;
86 3
		$this->_config = $config;
87
	}
88
89
	/**
90
	 * init the class
91
	 *
92
	 * @since 2.6.0
93
	 *
94
	 * @param string $directory name of the directory
95
	 */
96 3
97
	public function init(string $directory = 'database') : void
98 3
	{
99 3
		$this->_directory = $directory;
100
	}
101
102
	/**
103
	 * create from sql
104
	 *
105
	 * @since 2.4.0
106
	 *
107 1
	 * @return bool
108
	 */
109 1
110 1
	public function rawCreate() : bool
111
	{
112
		return $this->_rawExecute('create', $this->_config->get('dbType'));
113
	}
114
115
	/**
116
	 * drop from sql
117
	 *
118 1
	 * @since 2.4.0
119
	 *
120 1
	 * @return bool
121 1
	 */
122
123
	public function rawDrop() : bool
124
	{
125
		return $this->_rawExecute('drop', $this->_config->get('dbType'));
126
	}
127
128
	/**
129
	 * migrate from sql
130
	 *
131 1
	 * @since 4.4.0
132
	 *
133 1
	 * @return bool
134 1
	 */
135 1
136 1
	public function rawMigrate() : bool
137 1
	{
138 1
		return $this->_rawExecute('migrate', $this->_config->get('dbType'));
139 1
	}
140 1
141 1
	/**
142
	 * insert the data
143
	 *
144
	 * @since 3.1.0
145
	 *
146
	 * @param array $optionArray options of the installation
147
	 */
148
149
	public function insertData(array $optionArray = []) : void
150
	{
151 1
		$this->insertCategories($optionArray);
152
		$this->insertArticles($optionArray);
153 1
		$this->insertExtras($optionArray);
154 1
		$this->insertComments($optionArray);
155 1
		$this->insertGroups();
156 1
		$this->insertUsers($optionArray);
157
		$this->insertModules();
158 1
		$this->insertSettings($optionArray);
159 1
	}
160 1
161 1
	/**
162 1
	 * insert the categories
163
	 *
164 1
	 * @since 3.1.0
165 1
	 *
166
	 * @param array $optionArray options of the installation
167
	 */
168
169
	public function insertCategories(array $optionArray = []) : void
170
	{
171
		$now = $this->_registry->get('now');
172
		Db::forTablePrefix('categories')
173
			->create()
174
			->set(
175 1
			[
176
				'title' => 'Home',
177 1
				'alias' => 'home',
178 1
				'author' => $optionArray['adminUser'],
179 1
				'rank' => 1,
180 1
				'date' => $now
181
			])
182 1
			->save();
183 1
	}
184 1
185 1
	/**
186 1
	 * insert the articles
187 1
	 *
188 1
	 * @since 3.1.0
189 1
	 *
190
	 * @param array $optionArray options of the installation
191 1
	 */
192 1
193
	public function insertArticles(array $optionArray = []) : void
194
	{
195
		$now = $this->_registry->get('now');
196
		Db::forTablePrefix('articles')
197
			->create()
198
			->set(
199
			[
200
				'title' => 'Welcome',
201
				'alias' => 'welcome',
202 1
				'author' => $optionArray['adminUser'],
203
				'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'articles' . DIRECTORY_SEPARATOR . 'welcome.phtml'),
204 1
				'category' => 1,
205
				'comments' => 1,
206
				'rank' => 1,
207 1
				'date' => $now
208
			])
209
			->save();
210
	}
211
212
	/**
213
	 * insert the extras
214
	 *
215
	 * @since 3.1.0
216
	 *
217
	 * @param array $optionArray options of the installation
218
	 */
219
220
	public function insertExtras(array $optionArray = []) : void
221
	{
222
		$now = $this->_registry->get('now');
223
		$extrasArray =
224
		[
225
			'categories' =>
226
			[
227
				'category' => null,
228
				'headline' => 1,
229
				'status' => 1
230
			],
231
			'articles' =>
232
			[
233
				'category' => null,
234
				'headline' => 1,
235
				'status' => 1
236
			],
237
			'comments' =>
238
			[
239
				'category' => null,
240
				'headline' => 1,
241
				'status' => 1
242
			],
243
			'languages' =>
244 1
			[
245
				'category' => null,
246
				'headline' => 1,
247
				'status' => 0
248 1
			],
249
			'templates' =>
250 1
			[
251 1
				'category' => null,
252 1
				'headline' => 1,
253
				'status' => 0
254 1
			],
255 1
			'teaser' =>
256 1
			[
257 1
				'category' => 1,
258 1
				'headline' => 0,
259 1
				'status' => 0
260 1
			]
261 1
		];
262 1
		$extrasRank = 0;
263
264 1
		/* process extras */
265
266 1
		foreach ($extrasArray as $key => $value)
267
		{
268
			Db::forTablePrefix('extras')
269
				->create()
270
				->set(
271
				[
272
					'title' => ucfirst($key),
273
					'alias' => $key,
274
					'author' => $optionArray['adminUser'],
275
					'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'extras' . DIRECTORY_SEPARATOR . $key . '.phtml'),
276 1
					'category' => $value['category'],
277
					'headline' => $value['headline'],
278 1
					'status' => $value['status'],
279 1
					'rank' => ++$extrasRank,
280 1
					'date' => $now
281 1
				])
282
				->save();
283 1
		}
284 1
	}
285 1
286 1
	/**
287 1
	 * insert the comments
288 1
	 *
289
	 * @since 3.1.0
290 1
	 *
291 1
	 * @param array $optionArray options of the installation
292
	 */
293
294
	public function insertComments(array $optionArray = []) : void
295
	{
296
		$now = $this->_registry->get('now');
297
		Db::forTablePrefix('comments')
298
			->create()
299 1
			->set(
300
			[
301 1
				'author' => $optionArray['adminUser'],
302 1
				'email' => $optionArray['adminEmail'],
303 1
				'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'comments' . DIRECTORY_SEPARATOR . 'hello.phtml'),
304
				'article' => 1,
305 1
				'rank' => 1,
306
				'date' => $now
307
			])
308
			->save();
309
	}
310
311
	/**
312
	 * insert the groups
313
	 *
314
	 * @since 3.1.0
315
	 */
316
317
	public function insertGroups() : void
318 1
	{
319 1
		Db::forTablePrefix('groups')
320 1
			->create()
321 1
			->set(
322
			[
323 1
				'name' => 'Administrators',
324
				'alias' => 'administrators',
325
				'description' => 'Unlimited access',
326
				'categories' => '[1, 2, 3]',
327 1
				'articles' => '[1, 2, 3]',
328 1
				'extras' => '[1, 2, 3]',
329
				'comments' => '[1, 2, 3]',
330
				'groups' => '[1, 2, 3]',
331
				'users' => '[1, 2, 3]',
332
				'modules' => '[1, 2, 3]',
333
				'settings' => 1,
334
				'filter' => 0
335
			])
336
			->save();
337
		Db::forTablePrefix('groups')
338 1
			->create()
339
			->set(
340 1
			[
341 1
				'name' => 'Members',
342 1
				'alias' => 'members',
343 1
				'description' => 'Default members group'
344 1
			])
345
			->save();
346 1
	}
347 1
348 1
	/**
349 1
	 * insert the users
350 1
	 *
351 1
	 * @since 3.1.0
352
	 *
353 1
	 * @param array $optionArray options of the installation
354 1
	 */
355
356
	public function insertUsers(array $optionArray = []) : void
357
	{
358
		$passwordHash = new Hash();
359
		$passwordHash->init($optionArray['adminPassword']);
360
		Db::forTablePrefix('users')
361
			->create()
362 1
			->set(
363
			[
364
				'name' => $optionArray['adminName'],
365
				'user' => $optionArray['adminUser'],
366 1
				'password' => $passwordHash->getHash(),
367
				'email' => $optionArray['adminEmail'],
368
				'description' => 'God admin',
369
				'groups' => '[1]'
370
			])
371
			->save();
372
	}
373
374
	/**
375
	 * insert the modules
376
	 *
377
	 * @since 3.1.0
378 1
	 */
379
380 1
	public function insertModules() : void
381
	{
382
		$moduleArray =
383
		[
384 1
			'AliasGenerator',
385
			'CallHome',
386 1
			'ConfirmAction',
387 1
			'FormValidator',
388
			'RankSorter',
389
			'TextareaResizer',
390 1
			'UnmaskPassword',
391
			'VisualEditor'
392
		];
393
394
		/* process modules */
395
396
		foreach ($moduleArray as $alias)
397
		{
398
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
399
400 1
			/* install */
401
402
			if (method_exists($moduleClass, 'install'))
403
			{
404 1
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
405
				$module->install();
406 1
			}
407 1
		}
408
	}
409 1
410
	/**
411 1
	 * insert the settings
412 1
	 *
413 1
	 * @since 3.1.0
414 1
	 *
415 1
	 * @param array $optionArray options of the installation
416 1
	 */
417 1
418 1
	public function insertSettings(array $optionArray = []) : void
419 1
	{
420 1
		$settingArray =
421 1
		[
422 1
			'language' => null,
423 1
			'template' => null,
424 1
			'title' => $this->_language->get('_package')['name'],
425 1
			'author' => $optionArray['adminName'],
426 1
			'copyright' => null,
427 1
			'description' => $this->_language->get('_package')['description'],
428 1
			'keywords' => null,
429 1
			'robots' => 1,
430
			'email' => $optionArray['adminEmail'],
431
			'subject' => $this->_language->get('_package')['name'],
432
			'notification' => 0,
433
			'charset' => 'UTF-8',
434 1
			'divider' => ' - ',
435
			'zone' => 'Europe/Berlin',
436 1
			'time' => 'H:i',
437 1
			'date' => 'd.m.Y',
438 1
			'homepage' => 0,
439
			'limit' => 10,
440 1
			'order' => 'asc',
441 1
			'pagination' => 1,
442
			'registration' => 0,
443 1
			'verification' => 0,
444
			'recovery' => 1,
445 1
			'moderation' => 0,
446
			'captcha' => 0,
447
			'version' => null
448
		];
449
450
		/* process settings */
451
452
		foreach ($settingArray as $name => $value)
453
		{
454
			Db::forTablePrefix('settings')
455
				->create()
456 2
				->set(
457
				[
458 2
					'name' => $name,
459 2
					'value' => $value
460 2
				])
461
				->save();
462
		}
463
	}
464 2
465
	/**
466 2
	 * execute from sql
467 2
	 *
468
	 * @since 2.4.0
469 2
	 *
470
	 * @param string $action action to process
471 2
	 * @param string $type type of the database
472
	 *
473 2
	 * @return bool
474
	 */
475
476 2
	protected function _rawExecute(string $action = null, string $type = null) : bool
477
	{
478
		$actionFilesystem = new Filesystem\File();
479
		$actionFilesystem->init($this->_directory . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $action);
480
		$actionFilesystemArray = $actionFilesystem->getSortArray();
481
		$status = true;
482
483
		/* process filesystem */
484
485
		foreach ($actionFilesystemArray as $file)
486
		{
487
			$query = $actionFilesystem->readFile($file);
488
			if ($query)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $query of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
489
			{
490
				if ($this->_config->get('dbPrefix'))
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $this->_config->get('dbPrefix') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
491
				{
492
					$query = str_replace($this->_prefixPlaceholder, $this->_config->get('dbPrefix'), $query);
493
				}
494
				try
495
				{
496
					Db::rawExecute($query);
497
				}
498
				catch (PDOException $exception)
499
				{
500
					$status = false;
501
				}
502
			}
503
		}
504
		return $status;
505
	}
506
}
507