3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Admin;
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Update;
26 use Friendica\Database\DBA;
27 use Friendica\Database\DBStructure;
29 use Friendica\Module\BaseAdmin;
31 class DBSync extends BaseAdmin
33 public static function content(array $parameters = [])
35 parent::content($parameters);
39 $action = $parameters['action'] ?? '';
40 $update = $parameters['update'] ?? 0;
45 DI::config()->set('database', 'update_' . $update, 'success');
46 $curr = DI::config()->get('system', 'build');
47 if (intval($curr) == $update) {
48 DI::config()->set('system', 'build', intval($curr) + 1);
51 info(DI::l10n()->t('Update has been marked successful'));
56 // @TODO Seems like a similar logic like Update::check()
57 $retval = DBStructure::update($a->getBasePath(), false, true);
59 $o = DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
60 DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
61 DI::config()->set('database', 'last_successful_update_time', time());
63 $o = DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
68 require_once 'update.php';
70 // @TODO: Replace with parameter from router
72 $func = 'update_' . $update;
74 if (function_exists($func)) {
77 if ($retval === Update::FAILED) {
78 $o = DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
79 } elseif ($retval === Update::SUCCESS) {
80 $o = DI::l10n()->t('Update %s was successfully applied.', $func);
81 DI::config()->set('database', $func, 'success');
83 $o = DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
86 $o = DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
87 DI::config()->set('database', $func, 'success');
96 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
97 while ($config = DBA::fetch($configStmt)) {
98 $upd = intval(substr($config['k'], 7));
99 if ($upd >= 1139 && $config['v'] != 'success') {
103 DBA::close($configStmt);
105 if (!count($failed)) {
106 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
107 '$base' => DI::baseUrl()->get(true),
108 '$banner' => DI::l10n()->t('No failed updates.'),
109 '$check' => DI::l10n()->t('Check database structure'),
112 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
113 '$base' => DI::baseUrl()->get(true),
114 '$banner' => DI::l10n()->t('Failed Updates'),
115 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
116 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
117 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),
125 DI::baseUrl()->redirect('admin/dbsync');