3 namespace Friendica\Module\Admin;
5 use Friendica\Core\Config;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\Update;
9 use Friendica\Database\DBA;
10 use Friendica\Database\DBStructure;
11 use Friendica\Module\BaseAdminModule;
13 class DBSync extends BaseAdminModule
15 public static function content()
23 if ($a->argc > 3 && $a->argv[2] === 'mark') {
24 // @TODO: Replace with parameter from router
25 $update = intval($a->argv[3]);
27 Config::set('database', 'update_' . $update, 'success');
28 $curr = Config::get('system', 'build');
29 if (intval($curr) == $update) {
30 Config::set('system', 'build', intval($curr) + 1);
32 info(L10n::t('Update has been marked successful') . EOL);
34 $a->internalRedirect('admin/dbsync');
38 if ($a->argv[2] === 'check') {
39 // @TODO Seems like a similar logic like Update::check()
40 $retval = DBStructure::update($a->getBasePath(), false, true);
42 $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
43 Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);
44 Config::set('database', 'last_successful_update_time', time());
46 $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
48 if ($a->argv[2] === 'check') {
51 } elseif (intval($a->argv[2])) {
52 require_once 'update.php';
54 // @TODO: Replace with parameter from router
55 $update = intval($a->argv[2]);
57 $func = 'update_' . $update;
59 if (function_exists($func)) {
62 if ($retval === Update::FAILED) {
63 $o .= L10n::t("Executing %s failed with error: %s", $func, $retval);
64 } elseif ($retval === Update::SUCCESS) {
65 $o .= L10n::t('Update %s was successfully applied.', $func);
66 Config::set('database', $func, 'success');
68 $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);
71 $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";
72 Config::set('database', $func, 'success');
80 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
81 while ($config = DBA::fetch($configStmt)) {
82 $upd = intval(substr($config['k'], 7));
83 if ($upd >= 1139 && $config['v'] != 'success') {
88 if (!count($failed)) {
89 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
90 '$base' => $a->getBaseURL(true),
91 '$banner' => L10n::t('No failed updates.'),
92 '$check' => L10n::t('Check database structure'),
95 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
96 '$base' => $a->getBaseURL(true),
97 '$banner' => L10n::t('Failed Updates'),
98 '$desc' => L10n::t('This does not include updates prior to 1139, which did not return a status.'),
99 '$mark' => L10n::t("Mark success \x28if update was manually applied\x29"),
100 '$apply' => L10n::t('Attempt to execute this update step automatically'),