]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/DBSync.php
Remove get_server() in favor of Search::getGlobalDirectory()
[friendica.git] / src / Module / Admin / DBSync.php
1 <?php
2
3 namespace Friendica\Module\Admin;
4
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\DI;
12 use Friendica\Module\BaseAdminModule;
13
14 class DBSync extends BaseAdminModule
15 {
16         public static function content(array $parameters = [])
17         {
18                 parent::content($parameters);
19
20                 $a = DI::app();
21
22                 $o = '';
23
24                 if ($a->argc > 3 && $a->argv[2] === 'mark') {
25                         // @TODO: Replace with parameter from router
26                         $update = intval($a->argv[3]);
27                         if ($update) {
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);
32                                 }
33                                 info(L10n::t('Update has been marked successful') . EOL);
34                         }
35                         DI::baseUrl()->redirect('admin/dbsync');
36                 }
37
38                 if ($a->argc > 2) {
39                         if ($a->argv[2] === 'check') {
40                                 // @TODO Seems like a similar logic like Update::check()
41                                 $retval = DBStructure::update($a->getBasePath(), false, true);
42                                 if ($retval === '') {
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());
46                                 } else {
47                                         $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
48                                 }
49                                 if ($a->argv[2] === 'check') {
50                                         return $o;
51                                 }
52                         } elseif (intval($a->argv[2])) {
53                                 require_once 'update.php';
54
55                                 // @TODO: Replace with parameter from router
56                                 $update = intval($a->argv[2]);
57
58                                 $func = 'update_' . $update;
59
60                                 if (function_exists($func)) {
61                                         $retval = $func();
62
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');
68                                         } else {
69                                                 $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);
70                                         }
71                                 } else {
72                                         $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";
73                                         Config::set('database', $func, 'success');
74                                 }
75
76                                 return $o;
77                         }
78                 }
79
80                 $failed = [];
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') {
85                                 $failed[] = $upd;
86                         }
87                 }
88
89                 if (!count($failed)) {
90                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
91                                 '$base' => DI::baseUrl()->get(true),
92                                 '$banner' => L10n::t('No failed updates.'),
93                                 '$check' => L10n::t('Check database structure'),
94                         ]);
95                 } else {
96                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
97                                 '$base' => DI::baseUrl()->get(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'),
102                                 '$failed' => $failed
103                         ]);
104                 }
105
106                 return $o;
107         }
108 }