]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/DBSync.php
Added parameters
[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\Module\BaseAdminModule;
12
13 class DBSync extends BaseAdminModule
14 {
15         public static function content($parameters)
16         {
17                 parent::content($parameters);
18
19                 $a = self::getApp();
20
21                 $o = '';
22
23                 if ($a->argc > 3 && $a->argv[2] === 'mark') {
24                         // @TODO: Replace with parameter from router
25                         $update = intval($a->argv[3]);
26                         if ($update) {
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);
31                                 }
32                                 info(L10n::t('Update has been marked successful') . EOL);
33                         }
34                         $a->internalRedirect('admin/dbsync');
35                 }
36
37                 if ($a->argc > 2) {
38                         if ($a->argv[2] === 'check') {
39                                 // @TODO Seems like a similar logic like Update::check()
40                                 $retval = DBStructure::update($a->getBasePath(), false, true);
41                                 if ($retval === '') {
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());
45                                 } else {
46                                         $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
47                                 }
48                                 if ($a->argv[2] === 'check') {
49                                         return $o;
50                                 }
51                         } elseif (intval($a->argv[2])) {
52                                 require_once 'update.php';
53
54                                 // @TODO: Replace with parameter from router
55                                 $update = intval($a->argv[2]);
56
57                                 $func = 'update_' . $update;
58
59                                 if (function_exists($func)) {
60                                         $retval = $func();
61
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');
67                                         } else {
68                                                 $o .= L10n::t('Update %s did not return a status. Unknown if it succeeded.', $func);
69                                         }
70                                 } else {
71                                         $o .= L10n::t('There was no additional update function %s that needed to be called.', $func) . "<br />";
72                                         Config::set('database', $func, 'success');
73                                 }
74
75                                 return $o;
76                         }
77                 }
78
79                 $failed = [];
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') {
84                                 $failed[] = $upd;
85                         }
86                 }
87
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'),
93                         ]);
94                 } else {
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'),
101                                 '$failed' => $failed
102                         ]);
103                 }
104
105                 return $o;
106         }
107 }