]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/DBSync.php
Merge pull request #8272 from MrPetovan/bug/8254-regex-url-img
[friendica.git] / src / Module / Admin / DBSync.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 static function content(array $parameters = [])
34         {
35                 parent::content($parameters);
36
37                 $a = DI::app();
38
39                 $o = '';
40
41                 if ($a->argc > 3 && $a->argv[2] === 'mark') {
42                         // @TODO: Replace with parameter from router
43                         $update = intval($a->argv[3]);
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                                 info(DI::l10n()->t('Update has been marked successful') . EOL);
51                         }
52                         DI::baseUrl()->redirect('admin/dbsync');
53                 }
54
55                 if ($a->argc > 2) {
56                         if ($a->argv[2] === 'check') {
57                                 // @TODO Seems like a similar logic like Update::check()
58                                 $retval = DBStructure::update($a->getBasePath(), false, true);
59                                 if ($retval === '') {
60                                         $o .= DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
61                                         DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
62                                         DI::config()->set('database', 'last_successful_update_time', time());
63                                 } else {
64                                         $o .= DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
65                                 }
66                                 if ($a->argv[2] === 'check') {
67                                         return $o;
68                                 }
69                         } elseif (intval($a->argv[2])) {
70                                 require_once 'update.php';
71
72                                 // @TODO: Replace with parameter from router
73                                 $update = intval($a->argv[2]);
74
75                                 $func = 'update_' . $update;
76
77                                 if (function_exists($func)) {
78                                         $retval = $func();
79
80                                         if ($retval === Update::FAILED) {
81                                                 $o .= DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
82                                         } elseif ($retval === Update::SUCCESS) {
83                                                 $o .= DI::l10n()->t('Update %s was successfully applied.', $func);
84                                                 DI::config()->set('database', $func, 'success');
85                                         } else {
86                                                 $o .= DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
87                                         }
88                                 } else {
89                                         $o .= DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
90                                         DI::config()->set('database', $func, 'success');
91                                 }
92
93                                 return $o;
94                         }
95                 }
96
97                 $failed = [];
98                 $configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
99                 while ($config = DBA::fetch($configStmt)) {
100                         $upd = intval(substr($config['k'], 7));
101                         if ($upd >= 1139 && $config['v'] != 'success') {
102                                 $failed[] = $upd;
103                         }
104                 }
105
106                 if (!count($failed)) {
107                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/structure_check.tpl'), [
108                                 '$base' => DI::baseUrl()->get(true),
109                                 '$banner' => DI::l10n()->t('No failed updates.'),
110                                 '$check' => DI::l10n()->t('Check database structure'),
111                         ]);
112                 } else {
113                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/dbsync/failed_updates.tpl'), [
114                                 '$base' => DI::baseUrl()->get(true),
115                                 '$banner' => DI::l10n()->t('Failed Updates'),
116                                 '$desc' => DI::l10n()->t('This does not include updates prior to 1139, which did not return a status.'),
117                                 '$mark' => DI::l10n()->t("Mark success \x28if update was manually applied\x29"),
118                                 '$apply' => DI::l10n()->t('Attempt to execute this update step automatically'),
119                                 '$failed' => $failed
120                         ]);
121                 }
122
123                 return $o;
124         }
125 }