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;
12 use Friendica\Module\BaseAdminModule;
14 class DBSync extends BaseAdminModule
16 public static function content(array $parameters = [])
18 parent::content($parameters);
24 if ($a->argc > 3 && $a->argv[2] === 'mark') {
25 // @TODO: Replace with parameter from router
26 $update = intval($a->argv[3]);
28 Config::set('database', 'update_' . $update, 'success');
29 $curr = Config::get('system', 'build');
30 if (intval($curr) == $update) {
31 Config::set('system', 'build', intval($curr) + 1);
33 info(L10n::t('Update has been marked successful') . EOL);
35 DI::baseUrl()->redirect('admin/dbsync');
39 if ($a->argv[2] === 'check') {
40 // @TODO Seems like a similar logic like Update::check()
41 $retval = DBStructure::update($a->getBasePath(), false, true);
43 $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
44 Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);
45 Config::set('database', 'last_successful_update_time', time());
47 $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
49 if ($a->argv[2] === 'check') {
52 } elseif (intval($a->argv[2])) {
53 require_once 'update.php';
55 // @TODO: Replace with parameter from router
56 $update = intval($a->argv[2]);
58 $func = 'update_' . $update;
60 if (function_exists($func)) {
63 if ($retval === Update::FAILED) {
64 $o .= L10n::t("Executing %s failed with error: %s", $func, $retval);
65 } elseif ($retval === Update::SUCCESS) {
66 $o .= L10n::t('Update %s was successfully applied.', $func);
67 Config::set('database', $func, 'success');
69 $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);
72 $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";
73 Config::set('database', $func, 'success');
81 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
82 while ($config = DBA::fetch($configStmt)) {
83 $upd = intval(substr($config['k'], 7));
84 if ($upd >= 1139 && $config['v'] != 'success') {
89 if (!count($failed)) {
90 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
91 '$base' => $a->getBaseURL(true),
92 '$banner' => L10n::t('No failed updates.'),
93 '$check' => L10n::t('Check database structure'),
96 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
97 '$base' => $a->getBaseURL(true),
98 '$banner' => L10n::t('Failed Updates'),
99 '$desc' => L10n::t('This does not include updates prior to 1139, which did not return a status.'),
100 '$mark' => L10n::t("Mark success \x28if update was manually applied\x29"),
101 '$apply' => L10n::t('Attempt to execute this update step automatically'),