aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/command/update/check.php
blob: 4cd7d2155c1544a0846b74d506ba54b3b2e27e57 (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\console\command\update;

use phpbb\config\config;
use phpbb\exception\exception_interface;
use phpbb\language\language;
use phpbb\user;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;

class check extends \phpbb\console\command\command
{
	/** @var \phpbb\config\config */
	protected $config;

	/** @var \Symfony\Component\DependencyInjection\ContainerBuilder */
	protected $phpbb_container;
	/**
	 * @var language
	 */
	private $language;

	/**
	* Construct method
	*/
	public function __construct(user $user, config $config, ContainerInterface $phpbb_container, language $language)
	{
		$this->config = $config;
		$this->phpbb_container = $phpbb_container;
		$this->language = $language;

		$this->language->add_lang(array('acp/common', 'acp/extensions'));

		parent::__construct($user);
	}

	/**
	* Configures the service.
	*
	* Sets the name and description of the command.
	*
	* @return null
	*/
	protected function configure()
	{
		$this
			->setName('update:check')
			->setDescription($this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK'))
			->addArgument('ext-name', InputArgument::OPTIONAL, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_ARGUMENT_1'))
			->addOption('stability', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_OPTION_STABILITY'))
			->addOption('cache', 'c', InputOption::VALUE_NONE, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_OPTION_CACHE'))
		;
	}

	/**
	* Executes the command.
	*
	* Checks if an update is available.
	* If at least one is available, a message is printed and if verbose mode is set the list of possible updates is printed.
	* If their is none, nothing is printed unless verbose mode is set.
	*
	* @param InputInterface $input Input stream, used to get the options.
	* @param OutputInterface $output Output stream, used to print messages.
	* @return int 0 if the board is up to date, 1 if it is not and 2 if an error occurred.
	* @throws \RuntimeException
	*/
	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$io = new SymfonyStyle($input, $output);

		$recheck = true;
		if ($input->getOption('cache'))
		{
			$recheck = false;
		}

		$stability = null;
		if ($input->getOption('stability'))
		{
			$stability = $input->getOption('stability');
			if (!($stability == 'stable') && !($stability == 'unstable'))
			{
				$io->error($this->language->lang('CLI_ERROR_INVALID_STABILITY', $stability));
				return 3;
			}
		}

		$ext_name = $input->getArgument('ext-name');
		if ($ext_name != null)
		{
			if ($ext_name == 'all')
			{
				return $this->check_all_ext($io, $stability, $recheck);
			}
			else
			{
				return $this->check_ext($input, $io, $stability, $recheck, $ext_name);
			}
		}
		else
		{
			return $this->check_core($input, $io, $stability, $recheck);
		}
	}

	/**
	 * Check if a given extension is up to date
	 *
	 * @param InputInterface	$input		Input stream, used to get the options.
	 * @param SymfonyStyle		$io			IO handler, for formatted and unified IO
	 * @param string			$stability	Force a given stability
	 * @param bool				$recheck	Disallow the use of the cache
	 * @param string			$ext_name	The extension name
	 * @return int
	 */
	protected function check_ext(InputInterface $input, SymfonyStyle $io, $stability, $recheck, $ext_name)
	{
		try
		{
			$ext_manager = $this->phpbb_container->get('ext.manager');
			$md_manager = $ext_manager->create_extension_metadata_manager($ext_name);
			$updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability);

			$metadata = $md_manager->get_metadata('all');
			if ($input->getOption('verbose'))
			{
				$io->title($md_manager->get_metadata('display-name'));

				$io->note($this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $metadata['version']);
			}

			if (!empty($updates_available))
			{
				if ($input->getOption('verbose'))
				{
					$io->caution($this->language->lang('NOT_UP_TO_DATE', $metadata['name']));

					$this->display_versions($io, $updates_available);
				}

				return 1;
			}
			else
			{
				if ($input->getOption('verbose'))
				{
					$io->success($this->language->lang('UPDATE_NOT_NEEDED'));
				}

				return 0;
			}
		}
		catch (\RuntimeException $e)
		{
			$io->error($this->language->lang('EXTENSION_NOT_INSTALLED', $ext_name));

			return 1;
		}
	}

	/**
	 * Check if the core is up to date
	 *
	 * @param InputInterface	$input		Input stream, used to get the options.
	 * @param SymfonyStyle		$io			IO handler, for formatted and unified IO
	 * @param string			$stability	Force a given stability
	 * @param bool				$recheck	Disallow the use of the cache
	 * @return int
	 */
	protected function check_core(InputInterface $input, SymfonyStyle $io, $stability, $recheck)
	{
		$version_helper = $this->phpbb_container->get('version_helper');
		$version_helper->force_stability($stability);

		$updates_available = $version_helper->get_suggested_updates($recheck);

		if ($input->getOption('verbose'))
		{
			$io->title('phpBB core');

			$io->note( $this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $this->config['version']);
		}

		if (!empty($updates_available))
		{
			$io->caution($this->language->lang('UPDATE_NEEDED'));

			if ($input->getOption('verbose'))
			{
				$this->display_versions($io, $updates_available);
			}

			return 1;
		}
		else
		{
			if ($input->getOption('verbose'))
			{
				$io->success($this->language->lang('UPDATE_NOT_NEEDED'));
			}

			return 0;
		}
	}

	/**
	* Check if all the available extensions are up to date
	*
	* @param SymfonyStyle	$io			IO handler, for formatted and unified IO
	* @param string			$stability	Stability specifier string
	* @param bool			$recheck	Disallow the use of the cache
	* @return int
	*/
	protected function check_all_ext(SymfonyStyle $io, $stability, $recheck)
	{
		/** @var \phpbb\extension\manager $ext_manager */
		$ext_manager = $this->phpbb_container->get('ext.manager');

		$rows = [];

		foreach ($ext_manager->all_available() as $ext_name => $ext_path)
		{
			$row = [];
			$row[] = sprintf("<info>%s</info>", $ext_name);
			$md_manager = $ext_manager->create_extension_metadata_manager($ext_name);
			try
			{
				$metadata = $md_manager->get_metadata('all');
				if (isset($metadata['extra']['version-check']))
				{
					try {
						$updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability);
						if (!empty($updates_available))
						{
							$versions = array_map(function($entry)
							{
								return $entry['current'];
							}, $updates_available);

							$row[] = sprintf("<comment>%s</comment>", $metadata['version']);
							$row[] = implode(', ', $versions);
						}
						else
						{
							$row[] = sprintf("<info>%s</info>", $metadata['version']);
							$row[] = '';
						}
					} catch (\RuntimeException $e) {
						$row[] = $metadata['version'];
						$row[] = '';
					}
				}
				else
				{
					$row[] = $metadata['version'];
					$row[] = '';
				}
			}
			catch (exception_interface $e)
			{
				$exception_message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
				$row[] = '<error>' . $exception_message . '</error>';
			}
			catch (\RuntimeException $e)
			{
				$row[] = '<error>' . $e->getMessage() . '</error>';
			}

			$rows[] = $row;
		}

		$io->table([
			$this->language->lang('EXTENSION_NAME'),
			$this->language->lang('CURRENT_VERSION'),
			$this->language->lang('LATEST_VERSION'),
		], $rows);

		return 0;
	}

	/**
	* Display the details of the available updates
	*
	* @param SymfonyStyle	$io					IO handler, for formatted and unified IO
	* @param array			$updates_available	The list of the available updates
	*/
	protected function display_versions(SymfonyStyle $io, $updates_available)
	{
		$io->section($this->language->lang('UPDATES_AVAILABLE'));

		$rows = [];
		foreach ($updates_available as $version_data)
		{
			$row = ['', '', ''];
			$row[0] = $version_data['current'];

			if (isset($version_data['announcement']))
			{
				$row[1] = $version_data['announcement'];
			}

			if (isset($version_data['download']))
			{
				$row[2] = $version_data['download'];
			}

			$rows[] = $row;
		}

		$io->table([
			$this->language->lang('VERSION'),
			$this->language->lang('ANNOUNCEMENT_TOPIC'),
			$this->language->lang('DOWNLOAD_LATEST'),
		], $rows);
	}
}