3 namespace Friendica\Module\Admin;
5 use Friendica\Core\Renderer;
6 use Friendica\Core\Update;
7 use Friendica\Database\DBA;
8 use Friendica\Database\DBStructure;
10 use Friendica\Module\BaseAdminModule;
12 class DBSync extends BaseAdminModule
14 public static function content(array $parameters = [])
16 parent::content($parameters);
22 if ($a->argc > 3 && $a->argv[2] === 'mark') {
23 // @TODO: Replace with parameter from router
24 $update = intval($a->argv[3]);
26 DI::config()->set('database', 'update_' . $update, 'success');
27 $curr = DI::config()->get('system', 'build');
28 if (intval($curr) == $update) {
29 DI::config()->set('system', 'build', intval($curr) + 1);
31 info(DI::l10n()->t('Update has been marked successful') . EOL);
33 DI::baseUrl()->redirect('admin/dbsync');
37 if ($a->argv[2] === 'check') {
38 // @TODO Seems like a similar logic like Update::check()
39 $retval = DBStructure::update($a->getBasePath(), false, true);
41 $o .= DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
42 DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
43 DI::config()->set('database', 'last_successful_update_time', time());
45 $o .= DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
47 if ($a->argv[2] === 'check') {
50 } elseif (intval($a->argv[2])) {
51 require_once 'update.php';
53 // @TODO: Replace with parameter from router
54 $update = intval($a->argv[2]);
56 $func = 'update_' . $update;
58 if (function_exists($func)) {
61 if ($retval === Update::FAILED) {
62 $o .= DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
63 } elseif ($retval === Update::SUCCESS) {
64 $o .= DI::l10n()->t('Update %s was successfully applied.', $func);
65 DI::config()->set('database', $func, 'success');
67 $o .= DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
70 $o .= DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
71 DI::config()->set('database', $func, 'success');
79 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
80 while ($config = DBA::fetch($configStmt)) {
81 $upd = intval(substr($config['k'], 7));
82 if ($upd >= 1139 && $config['v'] != 'success') {
87 if (!count($failed)) {
88 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
89 '$base' => DI::baseUrl()->get(true),
90 '$banner' => DI::l10n()->t('No failed updates.'),
91 '$check' => DI::l10n()->t('Check database structure'),
94 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
95 '$base' => DI::baseUrl()->get(true),
96 '$banner' => DI::l10n()->t('Failed Updates'),
97 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
98 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
99 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),