1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\Command;
9
10use Symfony\Component\Console\Command\Command;
11use Symfony\Component\Console\Input\InputArgument;
12use Symfony\Component\Console\Input\InputInterface;
13use Symfony\Component\Console\Input\InputOption;
14use Symfony\Component\Console\Output\OutputInterface;
15use Tiki_Profile;
16
17class ProfileForgetCommand extends Command
18{
19	protected function configure()
20	{
21		$this
22			->setName('profile:forget')
23			->setDescription('Forget a profile installation')
24			->addArgument(
25				'profile',
26				InputArgument::REQUIRED,
27				'Profile name'
28			)
29			->addArgument(
30				'repository',
31				InputArgument::OPTIONAL,
32				'Repository',
33				'profiles.tiki.org'
34			)->addOption(
35				'revert',
36				null,
37				InputOption::VALUE_NONE,
38				'Rollback profile changes'
39			);
40	}
41
42	protected function execute(InputInterface $input, OutputInterface $output)
43	{
44		$profileName = $input->getArgument('profile');
45		$repository = $input->getArgument('repository');
46
47		$profile = \Tiki_Profile::fromNames($repository, $profileName);
48
49		if (! $profile) {
50			$output->writeln('<error>Profile not found.</error>');
51			return;
52		}
53
54		$tikilib = \TikiLib::lib('tiki');
55
56		$installer = new \Tiki_Profile_Installer;
57		$isInstalled = $installer->isInstalled($profile);
58
59		if ($isInstalled) {
60			$transaction = $tikilib->begin();
61
62			if ($input->getOption('revert')) {
63				$query = "SELECT * FROM tiki_actionlog where action = 'profile apply' and object=? ORDER BY actionId DESC LIMIT 1";
64				$result = \TikiLib::lib('logs')->query($query, [$profileName]);
65				if ($logResult = $result->fetchRow()) {
66					$revertInfo = unserialize($logResult['log']);
67					if (! isset($revertInfo['reverted'])) {
68						\TikiLib::lib('logs')->revert_action($logResult['actionId'], $logResult['object'], 'profiles', $revertInfo);
69						$installer->revert($profile, $revertInfo);
70					}
71				} else {
72					$output->writeln('No changes were found in logs to revert.');
73				}
74			}
75
76			$installer->forget($profile);
77			$transaction->commit();
78			$output->writeln('Profile forgotten.');
79		} else {
80			$output->writeln('<info>Profile was not installed or did not create any objects.</info>');
81		}
82	}
83}
84