1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\install\console\command\update;
15
16use phpbb\install\exception\installer_exception;
17use phpbb\install\helper\install_helper;
18use phpbb\install\helper\iohandler\cli_iohandler;
19use phpbb\install\helper\iohandler\factory;
20use phpbb\install\installer;
21use phpbb\install\updater_configuration;
22use phpbb\language\language;
23use Symfony\Component\Config\Definition\Exception\Exception;
24use Symfony\Component\Config\Definition\Processor;
25use Symfony\Component\Console\Input\InputArgument;
26use Symfony\Component\Console\Input\InputInterface;
27use Symfony\Component\Console\Output\OutputInterface;
28use Symfony\Component\Console\Style\SymfonyStyle;
29use Symfony\Component\Yaml\Exception\ParseException;
30use Symfony\Component\Yaml\Yaml;
31
32class update extends \phpbb\console\command\command
33{
34	/**
35	 * @var factory
36	 */
37	protected $iohandler_factory;
38
39	/**
40	 * @var installer
41	 */
42	protected $installer;
43
44	/**
45	 * @var install_helper
46	 */
47	protected $install_helper;
48
49	/**
50	 * @var language
51	 */
52	protected $language;
53
54	/**
55	 * Constructor
56	 *
57	 * @param language			$language
58	 * @param factory			$factory
59	 * @param installer			$installer
60	 * @param install_helper	$install_helper
61	 */
62	public function __construct(language $language, factory $factory, installer $installer, install_helper $install_helper)
63	{
64		$this->iohandler_factory = $factory;
65		$this->installer = $installer;
66		$this->language = $language;
67		$this->install_helper = $install_helper;
68
69		parent::__construct(new \phpbb\user($language, 'datetime'));
70	}
71
72	/**
73	 * {@inheritdoc}
74	 */
75	protected function configure()
76	{
77		$this
78			->setName('update')
79			->addArgument(
80				'config-file',
81				InputArgument::REQUIRED,
82				$this->language->lang('CLI_CONFIG_FILE'))
83			->setDescription($this->language->lang('CLI_UPDATE_BOARD'))
84		;
85	}
86
87	/**
88	 * Executes the command update.
89	 *
90	 * Update the board
91	 *
92	 * @param InputInterface  $input  An InputInterface instance
93	 * @param OutputInterface $output An OutputInterface instance
94	 *
95	 * @return int
96	 */
97	protected function execute(InputInterface $input, OutputInterface $output)
98	{
99		$this->iohandler_factory->set_environment('cli');
100
101		/** @var cli_iohandler $iohandler */
102		$iohandler = $this->iohandler_factory->get();
103		$style = new SymfonyStyle($input, $output);
104		$iohandler->set_style($style, $output);
105
106		$this->installer->set_iohandler($iohandler);
107
108		$config_file = $input->getArgument('config-file');
109
110		if (!$this->install_helper->is_phpbb_installed())
111		{
112			$iohandler->add_error_message('INSTALL_PHPBB_NOT_INSTALLED');
113
114			return 1;
115		}
116
117		if (!is_file($config_file))
118		{
119			$iohandler->add_error_message(array('MISSING_FILE', $config_file));
120
121			return 1;
122		}
123
124		try
125		{
126			$config = Yaml::parse(file_get_contents($config_file), true, false);
127		}
128		catch (ParseException $e)
129		{
130			$iohandler->add_error_message(array('INVALID_YAML_FILE', $config_file));
131
132			return 1;
133		}
134
135		$processor = new Processor();
136		$configuration = new updater_configuration();
137
138		try
139		{
140			$config = $processor->processConfiguration($configuration, $config);
141		}
142		catch (Exception $e)
143		{
144			$iohandler->add_error_message('INVALID_CONFIGURATION', $e->getMessage());
145
146			return 1;
147		}
148
149		$this->register_configuration($iohandler, $config);
150
151		try
152		{
153			$this->installer->run();
154			return 0;
155		}
156		catch (installer_exception $e)
157		{
158			$iohandler->add_error_message($e->getMessage());
159			return 1;
160		}
161	}
162
163	/**
164	 * Register the configuration to simulate the forms.
165	 *
166	 * @param cli_iohandler $iohandler
167	 * @param array $config
168	 */
169	private function register_configuration(cli_iohandler $iohandler, $config)
170	{
171		$iohandler->set_input('update_type', $config['type']);
172		$iohandler->set_input('submit_update', 'submit');
173
174		$iohandler->set_input('compression_method', '.tar');
175		$iohandler->set_input('method', 'direct_file');
176		$iohandler->set_input('submit_update_file', 'submit');
177
178		$iohandler->set_input('submit_continue_file_update', 'submit');
179
180		$iohandler->set_input('update-extensions', $config['extensions']);
181	}
182}
183