friendica/src/Module/Admin/DBSync.php

107 lines
3.4 KiB
PHP
Raw Normal View History

2019-05-02 00:01:43 -04:00
<?php
namespace Friendica\Module\Admin;
use Friendica\Core\Renderer;
use Friendica\Core\Update;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
2019-05-02 00:01:43 -04:00
use Friendica\Module\BaseAdminModule;
class DBSync extends BaseAdminModule
{
public static function content(array $parameters = [])
2019-05-02 00:01:43 -04:00
{
2019-11-05 15:22:54 -05:00
parent::content($parameters);
2019-05-02 00:01:43 -04:00
$a = DI::app();
2019-05-02 00:01:43 -04:00
$o = '';
if ($a->argc > 3 && $a->argv[2] === 'mark') {
// @TODO: Replace with parameter from router
$update = intval($a->argv[3]);
if ($update) {
DI::config()->set('database', 'update_' . $update, 'success');
$curr = DI::config()->get('system', 'build');
2019-05-02 00:01:43 -04:00
if (intval($curr) == $update) {
DI::config()->set('system', 'build', intval($curr) + 1);
2019-05-02 00:01:43 -04:00
}
info(DI::l10n()->t('Update has been marked successful') . EOL);
2019-05-02 00:01:43 -04:00
}
DI::baseUrl()->redirect('admin/dbsync');
2019-05-02 00:01:43 -04:00
}
if ($a->argc > 2) {
if ($a->argv[2] === 'check') {
// @TODO Seems like a similar logic like Update::check()
$retval = DBStructure::update($a->getBasePath(), false, true);
if ($retval === '') {
$o .= DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
DI::config()->set('database', 'last_successful_update_time', time());
2019-05-02 00:01:43 -04:00
} else {
$o .= DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
2019-05-02 00:01:43 -04:00
}
if ($a->argv[2] === 'check') {
return $o;
}
} elseif (intval($a->argv[2])) {
require_once 'update.php';
// @TODO: Replace with parameter from router
$update = intval($a->argv[2]);
$func = 'update_' . $update;
if (function_exists($func)) {
$retval = $func();
if ($retval === Update::FAILED) {
$o .= DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
2019-05-02 00:01:43 -04:00
} elseif ($retval === Update::SUCCESS) {
$o .= DI::l10n()->t('Update %s was successfully applied.', $func);
DI::config()->set('database', $func, 'success');
2019-05-02 00:01:43 -04:00
} else {
$o .= DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
2019-05-02 00:01:43 -04:00
}
} else {
$o .= DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
DI::config()->set('database', $func, 'success');
2019-05-02 00:01:43 -04:00
}
return $o;
}
}
$failed = [];
$configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
while ($config = DBA::fetch($configStmt)) {
$upd = intval(substr($config['k'], 7));
if ($upd >= 1139 && $config['v'] != 'success') {
$failed[] = $upd;
}
}
if (!count($failed)) {
$o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
'$base' => DI::baseUrl()->get(true),
'$banner' => DI::l10n()->t('No failed updates.'),
'$check' => DI::l10n()->t('Check database structure'),
2019-05-02 00:01:43 -04:00
]);
} else {
$o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
'$base' => DI::baseUrl()->get(true),
'$banner' => DI::l10n()->t('Failed Updates'),
'$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
'$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
'$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),
2019-05-02 00:01:43 -04:00
'$failed' => $failed
]);
}
return $o;
}
}