Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
update
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 4
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 1
240
 get_step_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_task_lang_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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\module\update_database\task;
15
16use phpbb\db\migration\exception;
17use phpbb\db\output_handler\installer_migrator_output_handler;
18use phpbb\db\output_handler\log_wrapper_migrator_output_handler;
19use phpbb\install\exception\resource_limit_reached_exception;
20use phpbb\install\exception\user_interaction_required_exception;
21use phpbb\install\task_base;
22
23/**
24 * Database updater task
25 */
26class update extends task_base
27{
28    /**
29     * @var \phpbb\cache\driver\driver_interface
30     */
31    protected $cache;
32
33    /**
34     * @var \phpbb\config\config
35     */
36    protected $config;
37
38    /**
39     * @var \phpbb\extension\manager
40     */
41    protected $extension_manager;
42
43    /**
44     * @var \phpbb\filesystem\filesystem
45     */
46    protected $filesystem;
47
48    /**
49     * @var \phpbb\install\helper\config
50     */
51    protected $installer_config;
52
53    /**
54     * @var \phpbb\install\helper\iohandler\iohandler_interface
55     */
56    protected $iohandler;
57
58    /**
59     * @var \phpbb\language\language
60     */
61    protected $language;
62
63    /**
64     * @var \phpbb\log\log
65     */
66    protected $log;
67
68    /**
69     * @var \phpbb\db\migrator
70     */
71    protected $migrator;
72
73    /**
74     * @var \phpbb\user
75     */
76    protected $user;
77
78    /**
79     * @var string
80     */
81    protected $phpbb_root_path;
82
83    /**
84     * Constructor
85     *
86     * @param \phpbb\install\helper\container_factory                $container
87     * @param \phpbb\filesystem\filesystem                            $filesystem
88     * @param \phpbb\install\helper\config                            $installer_config
89     * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler
90     * @param \phpbb\language\language                                $language
91     * @param string                                                $phpbb_root_path
92     */
93    public function __construct(\phpbb\install\helper\container_factory $container, \phpbb\filesystem\filesystem $filesystem, \phpbb\install\helper\config $installer_config, \phpbb\install\helper\iohandler\iohandler_interface $iohandler, \phpbb\language\language $language, $phpbb_root_path)
94    {
95        $this->filesystem            = $filesystem;
96        $this->installer_config        = $installer_config;
97        $this->iohandler            = $iohandler;
98        $this->language                = $language;
99        $this->phpbb_root_path        = $phpbb_root_path;
100
101        $this->cache                = $container->get('cache.driver');
102        $this->config                = $container->get('config');
103        $this->extension_manager    = $container->get('ext.manager');
104        $this->log                    = $container->get('log');
105        $this->migrator                = $container->get('migrator');
106        $this->user                    = $container->get('user');
107
108        parent::__construct(true);
109    }
110
111    /**
112     * {@inheritdoc}
113     */
114    public function run()
115    {
116        $this->language->add_lang('migrator');
117
118        if (!isset($this->config['version_update_from']))
119        {
120            $this->config->set('version_update_from', $this->config['version']);
121        }
122
123        $original_version = $this->config['version_update_from'];
124
125        $this->migrator->set_output_handler(
126            new log_wrapper_migrator_output_handler(
127                $this->language,
128                new installer_migrator_output_handler($this->iohandler),
129                $this->phpbb_root_path . 'store/migrations_' . time() . '.log',
130                $this->filesystem
131            )
132        );
133
134        $this->migrator->create_migrations_table();
135
136        $migrations = $this->extension_manager
137            ->get_finder()
138            ->core_path('phpbb/db/migration/data/')
139            ->extension_directory('/migrations')
140            ->get_classes();
141
142        $this->migrator->set_migrations($migrations);
143
144        $migration_step_count = $this->installer_config->get('database_update_migration_steps', -1);
145        if ($migration_step_count < 0)
146        {
147            $migration_step_count = count($this->migrator->get_installable_migrations()) * 2;
148            $this->installer_config->set('database_update_migration_steps', $migration_step_count);
149        }
150
151        $progress_count = $this->installer_config->get('database_update_count', 0);
152        $restart_progress_bar = ($progress_count === 0); // Only "restart" when the update runs for the first time
153        $this->iohandler->set_task_count($migration_step_count, $restart_progress_bar);
154        $this->installer_config->set_task_progress_count($migration_step_count);
155
156        while (!$this->migrator->finished())
157        {
158            try
159            {
160                $this->migrator->update();
161                $progress_count++;
162
163                $last_run_migration = $this->migrator->get_last_run_migration();
164                if (isset($last_run_migration['effectively_installed']) && $last_run_migration['effectively_installed'])
165                {
166                    // We skipped two step, so increment $progress_count by another one
167                    $progress_count++;
168                }
169                else if (($last_run_migration['task'] === 'process_schema_step' && !$last_run_migration['state']['migration_schema_done']) ||
170                    ($last_run_migration['task'] === 'process_data_step' && !$last_run_migration['state']['migration_data_done']))
171                {
172                    // We just run a step that wasn't counted yet so make it count
173                    $migration_step_count++;
174                }
175
176                $this->iohandler->set_task_count($migration_step_count);
177                $this->installer_config->set_task_progress_count($migration_step_count);
178                $this->iohandler->set_progress('STAGE_UPDATE_DATABASE', $progress_count);
179            }
180            catch (exception $e)
181            {
182                $msg = $e->getParameters();
183                array_unshift($msg, $e->getMessage());
184
185                $this->iohandler->add_error_message($msg);
186                throw new user_interaction_required_exception();
187            }
188
189            if ($this->installer_config->get_time_remaining() <= 0 || $this->installer_config->get_memory_remaining() <= 0)
190            {
191                $this->installer_config->set('database_update_count', $progress_count);
192                $this->installer_config->set('database_update_migration_steps', $migration_step_count);
193                throw new resource_limit_reached_exception();
194            }
195        }
196
197        if ($original_version !== $this->config['version'])
198        {
199            $this->log->add(
200                'admin',
201                (isset($this->user->data['user_id'])) ? $this->user->data['user_id'] : ANONYMOUS,
202                $this->user->ip,
203                'LOG_UPDATE_DATABASE',
204                false,
205                array(
206                    $original_version,
207                    $this->config['version']
208                )
209            );
210        }
211
212        $this->iohandler->add_success_message('INLINE_UPDATE_SUCCESSFUL');
213
214        $this->cache->purge();
215
216        $this->config->increment('assets_version', 1);
217    }
218
219    /**
220     * {@inheritdoc}
221     */
222    public static function get_step_count()
223    {
224        return 0;
225    }
226
227    /**
228     * {@inheritdoc}
229     */
230    public function get_task_lang_name()
231    {
232        return '';
233    }
234}