]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/DBSync.php
Replace Module::init() with Constructors
[friendica.git] / src / Module / Admin / DBSync.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Update;
26 use Friendica\Database\DBA;
27 use Friendica\Database\DBStructure;
28 use Friendica\DI;
29 use Friendica\Module\BaseAdmin;
30
31 class DBSync extends BaseAdmin
32 {
33         public function content(): string
34         {
35                 parent::content();
36
37                 $a = DI::app();
38
39                 $action = $this->parameters['action'] ?? '';
40                 $update = $this->parameters['update'] ?? 0;
41
42                 switch ($action) {
43                         case 'mark':
44                                 if ($update) {
45                                         DI::config()->set('database', 'update_' . $update, 'success');
46                                         $curr = DI::config()->get('system', 'build');
47                                         if (intval($curr) == $update) {
48                                                 DI::config()->set('system', 'build', intval($curr) + 1);
49                                         }
50
51                                         info(DI::l10n()->t('Update has been marked successful'));
52                                 }
53
54                                 break;
55                         case 'check':
56                                 // @TODO Seems like a similar logic like Update::check()
57                                 $retval = DBStructure::performUpdate();
58                                 if ($retval === '') {
59                                         $o = DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
60                                 } else {
61                                         $o = DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
62                                 }
63
64                                 return $o;
65                         case 'update':
66                                 require_once 'update.php';
67
68                                 // @TODO: Replace with parameter from router
69                                 if ($update) {
70                                         $func = 'update_' . $update;
71
72                                         if (function_exists($func)) {
73                                                 $retval = $func();
74
75                                                 if ($retval === Update::FAILED) {
76                                                         $o = DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
77                                                 } elseif ($retval === Update::SUCCESS) {
78                                                         $o = DI::l10n()->t('Update %s was successfully applied.', $func);
79                                                         DI::config()->set('database', $func, 'success');
80                                                 } else {
81                                                         $o = DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
82                                                 }
83                                         } else {
84                                                 $o = DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
85                                                 DI::config()->set('database', $func, 'success');
86                                         }
87
88                                         return $o;
89                                 }
90
91                                 break;
92                         default:
93                                 $failed = [];
94                                 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
95                                 while ($config = DBA::fetch($configStmt)) {
96                                         $upd = intval(substr($config['k'], 7));
97                                         if ($upd >= 1139 && $config['v'] != 'success') {
98                                                 $failed[] = $upd;
99                                         }
100                                 }
101                                 DBA::close($configStmt);
102
103                                 if (!count($failed)) {
104                                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
105                                                 '$base' => DI::baseUrl()->get(true),
106                                                 '$banner' => DI::l10n()->t('No failed updates.'),
107                                                 '$check' => DI::l10n()->t('Check database structure'),
108                                         ]);
109                                 } else {
110                                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
111                                                 '$base' => DI::baseUrl()->get(true),
112                                                 '$banner' => DI::l10n()->t('Failed Updates'),
113                                                 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
114                                                 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
115                                                 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),
116                                                 '$failed' => $failed
117                                         ]);
118                                 }
119
120                                 return $o;
121                 }
122
123                 DI::baseUrl()->redirect('admin/dbsync');
124                 return '';
125         }
126 }