/** * Clear cache */ protected static function clearCache() { fn_clear_cache(); fn_clear_template_cache(); if (function_exists('opcache_reset')) { opcache_reset(); } }
protected function installUpgradePackage($package_id, $request) { $result = true; $information_schema = $this->getSchema($package_id, false); $logger = Log::instance($package_id); $logger->drawHeader()->add(array(sprintf('Starting installation of the "%s" upgrade package', $package_id), sprintf('Upgrading version %s to %s', $information_schema['from_version'], $information_schema['to_version']), sprintf('Running as user "%s"', fn_get_process_owner_name()))); Output::steps(5); // Validators, Backups (database/files), Copying Files, Migrations, Languages Output::display(__('uc_title_validators'), __('uc_upgrade_progress'), false); $logger->add('Executing pre-upgrade validators'); $validators = $this->getValidators(); $schema = $this->getSchema($package_id, true); $package_validators = $this->getPackageValidators($package_id, $schema); $logger->add(sprintf('Found %u validators at package', sizeof($package_validators))); if (!empty($package_validators)) { $validators = array_merge($package_validators, $validators); } foreach ($validators as $validator) { $logger->add(sprintf('Executing "%s" validator', $validator->getName())); Output::display(__('uc_execute_validator', array('[validator]' => $validator->getName())), '', false); list($result, $data) = $validator->check($schema, $request); if (!$result) { break; } } if (!$result) { $logger->add(sprintf('Upgrade stopped: awaiting resolving "%s" validator errors', $validator->getName())); return array($result, array($validator->getName() => $data)); } else { $result = self::PACKAGE_INSTALL_RESULT_SUCCESS; if ($this->perform_backup) { $backup_filename = "upg_{$package_id}_{$information_schema['from_version']}-{$information_schema['to_version']}_" . date('dMY_His', TIME); $logger->add(sprintf('Backup filename is "%s"', $backup_filename)); // Prepare restore.php file. Paste necessary data and access information $restore_preparation_result = $this->prepareRestore($package_id, $schema, $information_schema, $backup_filename . '.zip'); if (!$restore_preparation_result) { $logger->add('Upgrade stopped: unable to prepare restore file.'); return array(false, array(__('restore') => __('upgrade_center.error_unable_to_prepare_restore'))); } list($restore_key, $restore_file_path, $restore_http_path) = $restore_preparation_result; } else { $logger->add('Files and database backup skipped'); } $content_path = $this->getPackagesDir() . $package_id . '/content/'; // Run pre script if (!empty($schema['scripts']['pre'])) { $pre_script_file_path = $content_path . 'scripts/' . $schema['scripts']['pre']; $logger->add(sprintf('Executing pre-upgrade script "%s"', $pre_script_file_path)); include_once $pre_script_file_path; $logger->add('Pre-upgrade script executed successfully'); } $logger->add('Closing storefront'); $this->closeStore(); // Collect email recipients for notifications $email_recipients = array(); $user_data = fn_get_user_short_info(\Tygh::$app['session']['auth']['user_id']); if (!empty($user_data['email'])) { $email_recipients[] = $user_data['email']; } $user_is_root_admin = isset(\Tygh::$app['session']['auth']['is_root']) && \Tygh::$app['session']['auth']['is_root'] == 'Y'; if (!$user_is_root_admin) { $root_admin_id = db_get_field("SELECT user_id FROM ?:users WHERE company_id = 0 AND is_root = 'Y' AND user_type = 'A'"); $root_admin_data = fn_get_user_short_info($root_admin_id); if (!empty($root_admin_data['email'])) { $email_recipients[] = $root_admin_data['email']; } } if ($this->perform_backup) { $logger->add('Backing up files and database'); Output::display(__('backup_data'), '', true); $backup_file = DataKeeper::backup(array('pack_name' => $backup_filename, 'compress' => 'zip', 'set_comet_steps' => false, 'move_progress' => false, 'extra_folders' => array('var/langs'))); if (empty($backup_file) || !file_exists($backup_file)) { $logger->add('Upgrade stopped: failed to backup DB/files'); return array(false, array(__('backup') => __('text_uc_failed_to_backup_tables'))); } $logger->add(sprintf('Backup created at "%s"', $backup_file)); $email_data = array('backup_file' => $backup_file, 'settings_section_url' => fn_url('settings.manage'), 'restore_link' => "{$restore_http_path}?uak={$restore_key}"); } else { $email_data = array('settings_section_url' => fn_url('settings.manage')); } // Send mail to admin e-mail with information about backup $logger->add(sprintf('Sending upgrade information e-mail to: %s', implode(', ', $email_recipients))); $mail_sent = Mailer::sendMail(array('to' => $email_recipients, 'from' => 'default_company_site_administrator', 'data' => $email_data, 'tpl' => 'upgrade/backup_info.tpl'), 'A', Registry::get('settings.Appearance.backend_default_language')); if ($mail_sent) { $logger->add('E-mail was successfully sent'); } else { $logger->add('Failed to send e-mail'); return array(false, array()); } Output::display(__('uc_copy_files'), '', true); // Move files from package $logger->add('Copying package files'); $this->applyPackageFiles($content_path . 'package', $this->config['dir']['root']); $logger->add('Deleting files removed at new version'); $this->cleanupOldFiles($schema); // Copy files from themes_repository to design folder $logger->add('Processing themes files'); $this->processThemesFiles($schema); Output::display(__('uc_run_migrations'), '', true); // Run migrations if (empty($schema['migrations'])) { $logger->add('No migrations found at package'); } else { $logger->add(sprintf('Executing %u migrations found at package', sizeof($schema['migrations']))); $minimal_date = 0; foreach ($schema['migrations'] as $migration) { preg_match('/^[0-9]+/', $migration, $matches); if (!empty($matches[0])) { $date = $matches[0]; if ($date < $minimal_date || empty($minimal_date)) { $minimal_date = $date; } } } $config = array('migration_dir' => realpath($content_path . 'migrations/'), 'package_id' => $package_id); try { $migration_succeed = Migration::instance($config)->migrate($minimal_date); } catch (DatabaseException $e) { // Find out which migration caused an exception using its trace $failed_migration_file = null; // DatabaseException could be thrown as a replacement of original exception, // in this case we should look through original's exception trace $exception_with_trace = $e->getPrevious() ?: $e; foreach ($exception_with_trace->getTrace() as $trace) { if (isset($trace['file']) && strpos($trace['file'], $config['migration_dir']) === 0) { $failed_migration_file = basename($trace['file']); break; } } $this->setNotification('E', __('error'), __('uc_migration_failed', array('[migration]' => $failed_migration_file))); $migration_succeed = false; $logger->add((string) $e); } if ($migration_succeed) { $logger->add('Migrations were executed successfully'); } else { $result = self::PACKAGE_INSTALL_RESULT_WITH_ERRORS; $logger->add('Failed to execute migrations'); } } Output::display(__('uc_install_languages'), '', true); list($lang_codes_to_install, $failed_lang_codes) = $this->installLanguages($schema, $logger, $content_path); if (!empty($lang_codes_to_install) && !empty($failed_lang_codes)) { $result = self::PACKAGE_INSTALL_RESULT_WITH_ERRORS; $logger->add(sprintf('Failed to install languages: %s', implode(', ', $failed_lang_codes))); } } $upgrade_schema = $this->getSchema($package_id); // Run post script if (!empty($schema['scripts']['post'])) { $post_script_file_path = $content_path . 'scripts/' . $schema['scripts']['post']; $logger->add(sprintf('Executing post-upgrade script "%s"', $post_script_file_path)); $upgrade_notes = array(); include_once $post_script_file_path; $logger->add('Post-upgrade script executed successfully'); $upgrade_notification_text = ''; foreach ($upgrade_notes as $note) { $delim = false; if (!empty($note['title'])) { $upgrade_notification_text .= "<h3>{$note['title']}</h3>"; $delim = true; } if (!empty($note['message'])) { $upgrade_notification_text .= "<div>{$note['message']}</div>"; $delim = true; } if ($delim) { $upgrade_notification_text .= "<hr>"; } } if ($upgrade_notification_text) { $upgrade_notification_title = __('upgrade_notification_title', array('[product]' => PRODUCT_NAME, '[version]' => $upgrade_schema['to_version'])); $logger->add(sprintf('Sending upgrade information e-mail to: %s', implode(', ', $email_recipients))); $mail_sent = Mailer::sendMail(array('to' => $email_recipients, 'from' => 'default_company_site_administrator', 'data' => array(), 'subj' => $upgrade_notification_title, 'body' => $upgrade_notification_text), 'A', Registry::get('settings.Appearance.backend_default_language')); if ($mail_sent) { $logger->add('Upgrade information e-mail was successfully sent'); } else { $logger->add('Failed to send e-mail'); } fn_set_notification('I', $upgrade_notification_title, __('upgrade_notification_message') . $upgrade_notification_text, 'S'); } } // Clear obsolete files $logger->add('Cleaning cache'); fn_clear_cache(); fn_clear_template_cache(); // Add information to "Installed upgrades section" $logger->add('Saving upgrade information to DB'); $this->storeInstalledUpgrade($upgrade_schema); // Collect statistic data $logger->add('Sending statistics'); Http::get(Registry::get('config.resources.updates_server') . '/index.php?dispatch=product_updates.updated', $this->getStatsData($package_id), array('timeout' => 10)); $this->onSuccessPackageInstall($package_id, $schema, $information_schema); $logger->add('Deleting package contents'); $this->deletePackage($package_id); Output::display(__('text_uc_upgrade_completed'), '', true); $logger->add('Upgrade completed!'); return array($result, array()); }