3 * @copyright Copyright (C) 2010-2023, the Friendica project
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 protected function content(array $request = []): string
39 $action = $this->parameters['action'] ?? '';
40 $update = $this->parameters['update'] ?? 0;
45 DI::keyValue()->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 DI::sysmsg()->addInfo(DI::l10n()->t('Update has been marked successful'));
56 // @TODO Seems like a similar logic like Update::check()
57 $retval = DBStructure::performUpdate();
59 $o = DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
61 $o = DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
66 require_once 'update.php';
68 // @TODO: Replace with parameter from router
70 $func = 'update_' . $update;
72 if (function_exists($func)) {
75 if ($retval === Update::FAILED) {
76 $o = DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
77 } elseif ($retval === Update::SUCCESS) {
78 $o = DI::l10n()->t('Update %s was successfully applied.', $func);
79 DI::keyValue()->set(sprintf('database_%s', $func), 'success');
81 $o = DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
84 $o = DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
85 DI::keyValue()->set(sprintf('database_%s', $func), 'success');
94 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
95 while ($config = DBA::fetch($configStmt)) {
96 $upd = intval(substr($config['k'], 7));
97 if ($upd >= 1139 && $config['v'] != 'success') {
101 DBA::close($configStmt);
103 if (!count($failed)) {
104 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
105 '$banner' => DI::l10n()->t('No failed updates.'),
106 '$check' => DI::l10n()->t('Check database structure'),
109 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
110 '$banner' => DI::l10n()->t('Failed Updates'),
111 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
112 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
113 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),
121 DI::baseUrl()->redirect('admin/dbsync');