]> git.mxchange.org Git - friendica.git/blob - src/Core/Update.php
Moved the functions update_db and run_update_function to a Friendica\Core\Update...
[friendica.git] / src / Core / Update.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Friendica\Database\DBStructure;
6
7 class Update
8 {
9         /**
10          * Automatic database updates
11          */
12         public static function run()
13         {
14                 $build = Config::get('system', 'build');
15
16                 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
17                         $build = DB_UPDATE_VERSION - 1;
18                         Config::set('system', 'build', $build);
19                 }
20
21                 if ($build != DB_UPDATE_VERSION) {
22                         require_once 'update.php';
23
24                         $stored = intval($build);
25                         $current = intval(DB_UPDATE_VERSION);
26                         if ($stored < $current) {
27                                 Config::load('database');
28
29                                 // Compare the current structure with the defined structure
30                                 $t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
31                                 if (!is_null($t)) {
32                                         return;
33                                 }
34
35                                 // run the pre_update_nnnn functions in update.php
36                                 for ($x = $stored + 1; $x <= $current; $x++) {
37                                         $r = self::runUpdateFunction($x, 'pre_update');
38                                         if (!$r) {
39                                                 break;
40                                         }
41                                 }
42
43                                 Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
44
45                                 // update the structure in one call
46                                 $retval = DBStructure::update(false, true);
47                                 if ($retval) {
48                                         DBStructure::updateFail(
49                                                 DB_UPDATE_VERSION,
50                                                 $retval
51                                         );
52                                         return;
53                                 } else {
54                                         Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
55                                 }
56
57                                 // run the update_nnnn functions in update.php
58                                 for ($x = $stored + 1; $x <= $current; $x++) {
59                                         $r = self::runUpdateFunction($x, 'update');
60                                         if (!$r) {
61                                                 break;
62                                         }
63                                 }
64                         }
65                 }
66         }
67
68         /**
69          * Executes a specific update function
70          *
71          * @param int $x the DB version number of the function
72          * @param string $prefix the prefix of the function (update, pre_update)
73          *
74          * @return bool true, if the update function worked
75          */
76         public static function runUpdateFunction($x, $prefix)
77         {
78                 $funcname = $prefix . '_' . $x;
79
80                 if (function_exists($funcname)) {
81                         // There could be a lot of processes running or about to run.
82                         // We want exactly one process to run the update command.
83                         // So store the fact that we're taking responsibility
84                         // after first checking to see if somebody else already has.
85                         // If the update fails or times-out completely you may need to
86                         // delete the config entry to try again.
87
88                         $t = Config::get('database', $funcname);
89                         if (!is_null($t)) {
90                                 return false;
91                         }
92                         Config::set('database', $funcname, time());
93
94                         // call the specific update
95                         $retval = $funcname();
96
97                         if ($retval) {
98                                 //send the administrator an e-mail
99                                 DBStructure::updateFail(
100                                         $x,
101                                         L10n::t('Update %s failed. See error logs.', $x)
102                                 );
103                                 return false;
104                         } else {
105                                 Config::set('database', $funcname, 'success');
106
107                                 if ($prefix == 'update') {
108                                         Config::set('system', 'build', $x);
109                                 }
110
111                                 return true;
112                         }
113                 } else {
114                         Config::set('database', $funcname, 'success');
115
116                         if ($prefix == 'update') {
117                                 Config::set('system', 'build', $x);
118                         }
119
120                         return true;
121                 }
122         }
123 }