]> git.mxchange.org Git - friendica.git/blob - src/Core/Update.php
Using Locks for Updating and writing last success to config
[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                                 if (Lock::acquire('dbupdate')) {
31
32                                         // run the pre_update_nnnn functions in update.php
33                                         for ($x = $stored + 1; $x <= $current; $x++) {
34                                                 $r = self::runUpdateFunction($x, 'pre_update');
35                                                 if (!$r) {
36                                                         break;
37                                                 }
38                                         }
39
40                                         // update the structure in one call
41                                         $retval = DBStructure::update(false, true);
42                                         if ($retval) {
43                                                 DBStructure::updateFail(
44                                                         DB_UPDATE_VERSION,
45                                                         $retval
46                                                 );
47                                                 Lock::release('dbupdate');
48                                                 return;
49                                         } else {
50                                                 Config::set('database', 'last_successful_update', time());
51                                         }
52
53                                         // run the update_nnnn functions in update.php
54                                         for ($x = $stored + 1; $x <= $current; $x++) {
55                                                 $r = self::runUpdateFunction($x, 'update');
56                                                 if (!$r) {
57                                                         break;
58                                                 }
59                                         }
60
61                                         Lock::release('dbupdate');
62                                 }
63                         }
64                 }
65         }
66
67         /**
68          * Executes a specific update function
69          *
70          * @param int $x the DB version number of the function
71          * @param string $prefix the prefix of the function (update, pre_update)
72          *
73          * @return bool true, if the update function worked
74          */
75         public static function runUpdateFunction($x, $prefix)
76         {
77                 $funcname = $prefix . '_' . $x;
78
79                 if (function_exists($funcname)) {
80                         // There could be a lot of processes running or about to run.
81                         // We want exactly one process to run the update command.
82                         // So store the fact that we're taking responsibility
83                         // after first checking to see if somebody else already has.
84                         // If the update fails or times-out completely you may need to
85                         // delete the config entry to try again.
86
87                         if (Lock::acquire('dbupdate_function')) {
88
89                                 // call the specific update
90                                 $retval = $funcname();
91
92                                 if ($retval) {
93                                         //send the administrator an e-mail
94                                         DBStructure::updateFail(
95                                                 $x,
96                                                 L10n::t('Update %s failed. See error logs.', $x)
97                                         );
98                                         Lock::release('dbupdate_function');
99                                         return false;
100                                 } else {
101                                         Config::set('database', 'last_successful_update_function', $funcname);
102                                         Config::set('database', 'last_successful_update_function_time', time());
103
104                                         if ($prefix == 'update') {
105                                                 Config::set('system', 'build', $x);
106                                         }
107
108                                         Lock::release('dbupdate_function');
109                                         return true;
110                                 }
111                         }
112                 } else {
113                         logger('Skipping \'' . $funcname . '\' without executing', LOGGER_DEBUG);
114
115                         Config::set('database', 'last_successful_update_function', $funcname);
116                         Config::set('database', 'last_successful_update_function_time', time());
117
118                         if ($prefix == 'update') {
119                                 Config::set('system', 'build', $x);
120                         }
121
122                         return true;
123                 }
124         }
125 }