]> git.mxchange.org Git - friendica.git/blob - src/Core/Update.php
d4bcd04bba192d03ddd450801dc1bb44c875b5ef
[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         const SUCCESS = 0;
10         const FAILED  = 1;
11
12         /**
13          * Automatic database updates
14          */
15         public static function run()
16         {
17                 $build = Config::get('system', 'build');
18
19                 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
20                         $build = DB_UPDATE_VERSION - 1;
21                         Config::set('system', 'build', $build);
22                 }
23
24                 if ($build != DB_UPDATE_VERSION) {
25                         require_once 'update.php';
26
27                         $stored = intval($build);
28                         $current = intval(DB_UPDATE_VERSION);
29                         if ($stored < $current) {
30                                 Config::load('database');
31
32                                 // Compare the current structure with the defined structure
33                                 if (Lock::acquire('dbupdate')) {
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                                         // update the structure in one call
44                                         $retval = DBStructure::update(false, true);
45                                         if ($retval) {
46                                                 DBStructure::updateFail(
47                                                         DB_UPDATE_VERSION,
48                                                         $retval
49                                                 );
50                                                 Lock::release('dbupdate');
51                                                 return;
52                                         } else {
53                                                 Config::set('database', 'last_successful_update', $current);
54                                                 Config::set('database', 'last_successful_update_time', time());
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                                         Lock::release('dbupdate');
66                                 }
67                         }
68                 }
69         }
70
71         /**
72          * Executes a specific update function
73          *
74          * @param int $x the DB version number of the function
75          * @param string $prefix the prefix of the function (update, pre_update)
76          *
77          * @return bool true, if the update function worked
78          */
79         public static function runUpdateFunction($x, $prefix)
80         {
81                 $funcname = $prefix . '_' . $x;
82
83                 if (function_exists($funcname)) {
84                         // There could be a lot of processes running or about to run.
85                         // We want exactly one process to run the update command.
86                         // So store the fact that we're taking responsibility
87                         // after first checking to see if somebody else already has.
88                         // If the update fails or times-out completely you may need to
89                         // delete the config entry to try again.
90
91                         if (Lock::acquire('dbupdate_function')) {
92
93                                 // call the specific update
94                                 $retval = $funcname();
95
96                                 if ($retval) {
97                                         //send the administrator an e-mail
98                                         DBStructure::updateFail(
99                                                 $x,
100                                                 L10n::t('Update %s failed. See error logs.', $x)
101                                         );
102                                         Lock::release('dbupdate_function');
103                                         return false;
104                                 } else {
105                                         Config::set('database', 'last_successful_update_function', $funcname);
106                                         Config::set('database', 'last_successful_update_function_time', time());
107
108                                         if ($prefix == 'update') {
109                                                 Config::set('system', 'build', $x);
110                                         }
111
112                                         Lock::release('dbupdate_function');
113                                         return true;
114                                 }
115                         }
116                 } else {
117                         logger('Skipping \'' . $funcname . '\' without executing', LOGGER_DEBUG);
118
119                         Config::set('database', 'last_successful_update_function', $funcname);
120                         Config::set('database', 'last_successful_update_function_time', time());
121
122                         if ($prefix == 'update') {
123                                 Config::set('system', 'build', $x);
124                         }
125
126                         return true;
127                 }
128         }
129 }