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);
41 if ($a->argc > 3 && $a->argv[2] === 'mark') {
42 // @TODO: Replace with parameter from router
43 $update = intval($a->argv[3]);
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);
50 info(DI::l10n()->t('Update has been marked successful') . EOL);
52 DI::baseUrl()->redirect('admin/dbsync');
56 if ($a->argv[2] === 'check') {
57 // @TODO Seems like a similar logic like Update::check()
58 $retval = DBStructure::update($a->getBasePath(), false, true);
60 $o .= DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
61 DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
62 DI::config()->set('database', 'last_successful_update_time', time());
64 $o .= DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
66 if ($a->argv[2] === 'check') {
69 } elseif (intval($a->argv[2])) {
70 require_once 'update.php';
72 // @TODO: Replace with parameter from router
73 $update = intval($a->argv[2]);
75 $func = 'update_' . $update;
77 if (function_exists($func)) {
80 if ($retval === Update::FAILED) {
81 $o .= DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
82 } elseif ($retval === Update::SUCCESS) {
83 $o .= DI::l10n()->t('Update %s was successfully applied.', $func);
84 DI::config()->set('database', $func, 'success');
86 $o .= DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
89 $o .= DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
90 DI::config()->set('database', $func, 'success');
98 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
99 while ($config = DBA::fetch($configStmt)) {
100 $upd = intval(substr($config['k'], 7));
101 if ($upd >= 1139 && $config['v'] != 'success') {
106 if (!count($failed)) {
107 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
108 '$base' => DI::baseUrl()->get(true),
109 '$banner' => DI::l10n()->t('No failed updates.'),
110 '$check' => DI::l10n()->t('Check database structure'),
113 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
114 '$base' => DI::baseUrl()->get(true),
115 '$banner' => DI::l10n()->t('Failed Updates'),
116 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
117 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
118 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),