AppDataRepairStep::copyConfig()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
c 0
b 0
f 0
ccs 0
cts 16
cp 0
rs 9.8666
cc 4
nc 5
nop 0
crap 20
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Migration;
26
27
use OCA\CMSPico\Service\ConfigService;
28
use OCA\CMSPico\Service\FileService;
29
use OCA\CMSPico\Service\MiscService;
30
use OCA\CMSPico\Service\PicoService;
31
use OCA\CMSPico\Service\PluginsService;
32
use OCA\CMSPico\Service\TemplatesService;
33
use OCA\CMSPico\Service\ThemesService;
34
use OCA\CMSPico\Service\WebsitesService;
35
use OCP\Files\NotFoundException;
36
use OCP\IGroupManager;
37
use OCP\ILogger;
38
use OCP\Migration\IOutput;
39
use OCP\Migration\IRepairStep;
40
41
class AppDataRepairStep implements IRepairStep
42
{
43
	use MigrationTrait;
44
45
	/** @var IGroupManager */
46
	private $groupManager;
47
48
	/** @var WebsitesService */
49
	private $websitesService;
50
51
	/** @var ConfigService */
52
	private $configService;
53
54
	/** @var TemplatesService */
55
	private $templatesService;
56
57
	/** @var ThemesService */
58
	private $themesService;
59
60
	/** @var PluginsService */
61
	private $pluginsService;
62
63
	/** @var FileService */
64
	private $fileService;
65
66
	/** @var MiscService */
67
	private $miscService;
68
69
	/** @var bool */
70
	private $locked = false;
71
72
	/**
73
	 * AppDataRepairStep constructor.
74
	 *
75
	 * @param ILogger          $logger
76
	 * @param IGroupManager    $groupManager
77
	 * @param WebsitesService  $websitesService
78
	 * @param ConfigService    $configService
79
	 * @param TemplatesService $templatesService
80
	 * @param ThemesService    $themesService
81
	 * @param PluginsService   $pluginsService
82
	 * @param FileService      $fileService
83
	 * @param MiscService      $miscService
84
	 */
85
	public function __construct(
86
		ILogger $logger,
87
		IGroupManager $groupManager,
88
		WebsitesService $websitesService,
89
		ConfigService $configService,
90
		TemplatesService $templatesService,
91
		ThemesService $themesService,
92
		PluginsService $pluginsService,
93
		FileService $fileService,
94
		MiscService $miscService
95
	) {
96
		$this->setLogger($logger);
97
98
		$this->groupManager = $groupManager;
99
		$this->websitesService = $websitesService;
100
		$this->configService = $configService;
101
		$this->templatesService = $templatesService;
102
		$this->themesService = $themesService;
103
		$this->pluginsService = $pluginsService;
104
		$this->fileService = $fileService;
105
		$this->miscService = $miscService;
106
	}
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getName(): string
112
	{
113
		return 'Preparing app data of Pico CMS for Nextcloud';
114
	}
115
116
	/**
117
	 * @param IOutput $output
118
	 */
119
	public function run(IOutput $output): void
120
	{
121
		$this->setOutput($output);
122
123
		// never run AppDataRepairStep multiple times for the same session
124
		// this might happen if you update and enable the app at the same time
125
		if ($this->locked) {
126
			$this->logInfo('Pico CMS\' app data has been prepared already, skipping …');
127
			return;
128
		}
129
130
		$this->locked = true;
131
132
		$this->logInfo('Checking Pico CMS requirements …');
133
		$this->miscService->checkComposer();
134
		$this->miscService->checkPublicFolder();
135
136
		$this->logInfo('Rebuilding Pico CMS app settings …');
137
		$this->rebuildAppSettings();
138
139
		$this->logInfo('Syncing Pico CMS app data folder …');
140
		$this->syncAppDataFolder();
141
142
		$this->logInfo('Copying Pico CMS config …');
143
		$this->copyConfig();
144
145
		$this->logInfo('Registering Pico CMS templates …');
146
		$this->registerTemplates();
147
148
		$this->logInfo('Publishing Pico CMS themes …');
149
		$this->publishThemes();
150
151
		$this->logInfo('Publishing Pico CMS plugins …');
152
		$this->publishPlugins();
153
	}
154
155
	/**
156
	 * @return void
157
	 */
158
	private function rebuildAppSettings(): void
159
	{
160
		$limitGroups = $this->websitesService->getLimitGroups();
161
		$limitGroups = array_values(array_filter($limitGroups, [ $this->groupManager, 'groupExists' ]));
162
		$this->websitesService->setLimitGroups($limitGroups);
163
	}
164
165
	/**
166
	 * @return void
167
	 */
168
	private function syncAppDataFolder(): void
169
	{
170
		$this->fileService->syncAppDataFolder();
171
	}
172
173
	/**
174
	 * @return void
175
	 */
176
	private function copyConfig(): void
177
	{
178
		$appDataConfigFolder = $this->fileService->getAppDataFolder(PicoService::DIR_CONFIG);
179
		$systemConfigFolder = $this->fileService->getSystemFolder(PicoService::DIR_CONFIG);
180
181
		foreach ($systemConfigFolder as $configFile) {
182
			$configFileName = $configFile->getName();
183
184
			if (!$configFile->isFile()) {
185
				continue;
186
			}
187
188
			try {
189
				$appDataConfigFolder->getFile($configFileName)->delete();
190
				$this->logWarning('Replacing Pico CMS config file "%s"', $configFileName);
191
			} catch (NotFoundException $e) {
192
				$this->logInfo('Adding Pico CMS config file "%s"', $configFileName);
193
			}
194
195
			$configFile->copy($appDataConfigFolder);
196
		}
197
	}
198
199
	/**
200
	 * @return void
201
	 */
202
	private function registerTemplates(): void
203
	{
204
		$this->registerSystemTemplates();
205
		$this->registerCustomTemplates();
206
	}
207
208
	/**
209
	 * @return void
210
	 */
211
	private function registerSystemTemplates(): void
212
	{
213
		$systemTemplatesFolder = $this->fileService->getSystemFolder(PicoService::DIR_TEMPLATES);
214
215
		$oldSystemTemplates = $this->templatesService->getSystemTemplates();
216
		$this->configService->deleteAppValue(ConfigService::SYSTEM_TEMPLATES);
217
218
		foreach ($systemTemplatesFolder as $templateFolder) {
219
			$templateName = $templateFolder->getName();
220
			if ($templateFolder->isFolder()) {
221
				$this->templatesService->registerSystemTemplate($templateName);
222
			}
223
		}
224
225
		$oldSystemTemplates = array_keys($oldSystemTemplates);
226
		$newSystemTemplates = array_keys($this->templatesService->getSystemTemplates());
227
		$this->logChanges('Pico CMS system template', $newSystemTemplates, $oldSystemTemplates, true);
228
	}
229
230
	/**
231
	 * @return void
232
	 */
233
	private function registerCustomTemplates(): void
234
	{
235
		$appDataTemplatesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_TEMPLATES);
236
237
		$oldCustomTemplates = $this->templatesService->getCustomTemplates();
238
		$this->configService->deleteAppValue(ConfigService::CUSTOM_TEMPLATES);
239
240
		$systemTemplates = $this->templatesService->getSystemTemplates();
241
		foreach ($appDataTemplatesFolder as $templateFolder) {
242
			$templateName = $templateFolder->getName();
243
			if ($templateFolder->isFolder()) {
244
				if (isset($oldCustomTemplates[$templateName]) && !isset($systemTemplates[$templateName])) {
245
					$this->templatesService->registerCustomTemplate($templateName);
246
				}
247
			}
248
		}
249
250
		$oldCustomTemplates = array_keys($oldCustomTemplates);
251
		$newCustomTemplates = array_keys($this->templatesService->getCustomTemplates());
252
		$this->logChanges('Pico CMS custom template', $newCustomTemplates, $oldCustomTemplates);
253
	}
254
255
	/**
256
	 * @return void
257
	 */
258
	private function publishThemes(): void
259
	{
260
		$publicThemesFolder = $this->fileService->getPublicFolder(PicoService::DIR_THEMES);
261
		$publicThemesFolder->truncate();
262
263
		$this->configService->deleteAppValue(ConfigService::THEMES_ETAG);
264
265
		$this->publishSystemThemes();
266
		$this->publishCustomThemes();
267
	}
268
269
	/**
270
	 * @return void
271
	 */
272
	private function publishSystemThemes(): void
273
	{
274
		$systemThemesFolder = $this->fileService->getSystemFolder(PicoService::DIR_THEMES);
275
276
		$oldSystemThemes = $this->themesService->getSystemThemes();
277
		$this->configService->deleteAppValue(ConfigService::SYSTEM_THEMES);
278
279
		foreach ($systemThemesFolder as $themeFolder) {
280
			$themeName = $themeFolder->getName();
281
			if ($themeFolder->isFolder()) {
282
				$this->themesService->publishSystemTheme($themeName);
283
			}
284
		}
285
286
		$oldSystemThemes = array_keys($oldSystemThemes);
287
		$newSystemThemes = array_keys($this->themesService->getSystemThemes());
288
		$this->logChanges('Pico CMS system theme', $newSystemThemes, $oldSystemThemes, true);
289
	}
290
291
	/**
292
	 * @return void
293
	 */
294
	private function publishCustomThemes(): void
295
	{
296
		$appDataThemesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_THEMES);
297
298
		$oldCustomThemes = $this->themesService->getCustomThemes();
299
		$this->configService->deleteAppValue(ConfigService::CUSTOM_THEMES);
300
301
		$systemThemes = $this->themesService->getSystemThemes();
302
		foreach ($appDataThemesFolder as $themeFolder) {
303
			$themeName = $themeFolder->getName();
304
			if ($themeFolder->isFolder()) {
305
				if (isset($oldCustomThemes[$themeName]) && !isset($systemThemes[$themeName])) {
306
					$this->themesService->publishCustomTheme($themeName);
307
				}
308
			}
309
		}
310
311
		$oldCustomThemes = array_keys($oldCustomThemes);
312
		$newCustomThemes = array_keys($this->themesService->getCustomThemes());
313
		$this->logChanges('Pico CMS custom theme', $newCustomThemes, $oldCustomThemes);
314
	}
315
316
	/**
317
	 * @return void
318
	 */
319
	private function publishPlugins(): void
320
	{
321
		$publicPluginsFolder = $this->fileService->getPublicFolder(PicoService::DIR_PLUGINS);
322
		$publicPluginsFolder->truncate();
323
324
		$this->configService->deleteAppValue(ConfigService::PLUGINS_ETAG);
325
326
		$this->publishSystemPlugins();
327
		$this->publishCustomPlugins();
328
	}
329
330
	/**
331
	 * @return void
332
	 */
333
	private function publishSystemPlugins(): void
334
	{
335
		$systemPluginsFolder = $this->fileService->getSystemFolder(PicoService::DIR_PLUGINS);
336
337
		$oldSystemPlugins = $this->pluginsService->getSystemPlugins();
338
		$this->configService->deleteAppValue(ConfigService::SYSTEM_PLUGINS);
339
340
		foreach ($systemPluginsFolder as $pluginFolder) {
341
			$pluginName = $pluginFolder->getName();
342
			if ($pluginFolder->isFolder()) {
343
				$this->pluginsService->publishSystemPlugin($pluginName);
344
			}
345
		}
346
347
		$oldSystemPlugins = array_keys($oldSystemPlugins);
348
		$newSystemPlugins = array_keys($this->pluginsService->getSystemPlugins());
349
		$this->logChanges('Pico CMS system plugin', $newSystemPlugins, $oldSystemPlugins, true);
350
	}
351
352
	/**
353
	 * @return void
354
	 */
355
	private function publishCustomPlugins(): void
356
	{
357
		$appDataPluginsFolder = $this->fileService->getAppDataFolder(PicoService::DIR_PLUGINS);
358
359
		$oldCustomPlugins = $this->pluginsService->getCustomPlugins();
360
		$this->configService->deleteAppValue(ConfigService::CUSTOM_PLUGINS);
361
362
		$systemPlugins = $this->pluginsService->getSystemPlugins();
363
		foreach ($appDataPluginsFolder as $pluginFolder) {
364
			$pluginName = $pluginFolder->getName();
365
			if ($pluginFolder->isFolder()) {
366
				if (isset($oldCustomPlugins[$pluginName]) && !isset($systemPlugins[$pluginName])) {
367
					$this->pluginsService->publishCustomPlugin($pluginName);
368
				}
369
			}
370
		}
371
372
		$oldCustomPlugins = array_keys($oldCustomPlugins);
373
		$newCustomPlugins = array_keys($this->pluginsService->getCustomPlugins());
374
		$this->logChanges('Pico CMS custom plugin', $newCustomPlugins, $oldCustomPlugins);
375
	}
376
}
377