114 global $ui; |
114 global $ui; |
115 $ui->show_footer(); |
115 $ui->show_footer(); |
116 exit; |
116 exit; |
117 } |
117 } |
118 |
118 |
|
119 function enano_perform_upgrade($target_branch) |
|
120 { |
|
121 global $db, $session, $paths, $template, $plugins; // Common objects |
|
122 // Import version info |
|
123 global $enano_versions; |
|
124 // Import UI functions |
|
125 global $ui; |
|
126 // This is needed for upgrade abstraction |
|
127 global $dbdriver; |
|
128 // Main upgrade stage |
|
129 |
|
130 // Init vars |
|
131 list($major_version, $minor_version) = explode('.', installer_enano_version()); |
|
132 $installer_branch = "$major_version.$minor_version"; |
|
133 |
|
134 $version_flipped = array_flip($enano_versions[$target_branch]); |
|
135 $version_curr = enano_version(); |
|
136 // Change this to be the last version in the current branch. |
|
137 // If we're just upgrading within this branch, use the version the installer library |
|
138 // reports to us. Else, use the latest in the old (current target) branch. |
|
139 // $version_target = installer_enano_version(); |
|
140 $version_target = ( $target_branch === $installer_branch ) ? installer_enano_version() : $enano_versions[$target_branch][ count($enano_versions[$target_branch]) - 1 ]; |
|
141 |
|
142 // Calculate which scripts to run |
|
143 if ( !isset($version_flipped[$version_curr]) ) |
|
144 { |
|
145 echo '<p>ERROR: Unsupported version</p>'; |
|
146 $ui->show_footer(); |
|
147 exit; |
|
148 } |
|
149 if ( !isset($version_flipped[$version_target]) ) |
|
150 { |
|
151 echo '<p>ERROR: Upgrader doesn\'t support its own version</p>'; |
|
152 $ui->show_footer(); |
|
153 exit; |
|
154 } |
|
155 $upg_queue = array(); |
|
156 for ( $i = $version_flipped[$version_curr]; $i < $version_flipped[$version_target]; $i++ ) |
|
157 { |
|
158 if ( !isset($enano_versions[$target_branch][$i + 1]) ) |
|
159 { |
|
160 echo '<p>ERROR: Unsupported intermediate version</p>'; |
|
161 $ui->show_footer(); |
|
162 exit; |
|
163 } |
|
164 $ver_this = $enano_versions[$target_branch][$i]; |
|
165 $ver_next = $enano_versions[$target_branch][$i + 1]; |
|
166 $upg_queue[] = array($ver_this, $ver_next); |
|
167 } |
|
168 |
|
169 // Verify that all upgrade scripts are usable |
|
170 foreach ( $upg_queue as $verset ) |
|
171 { |
|
172 $file = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}-$dbdriver.sql"; |
|
173 if ( !file_exists($file) ) |
|
174 { |
|
175 echo "<p>ERROR: Couldn't find required schema file: $file</p>"; |
|
176 $ui->show_footer(); |
|
177 exit; |
|
178 } |
|
179 } |
|
180 // Perform upgrade |
|
181 foreach ( $upg_queue as $verset ) |
|
182 { |
|
183 $file = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}-$dbdriver.sql"; |
|
184 try |
|
185 { |
|
186 $parser = new SQL_Parser($file); |
|
187 } |
|
188 catch(Exception $e) |
|
189 { |
|
190 die("<pre>$e</pre>"); |
|
191 } |
|
192 |
|
193 $parser->assign_vars(array( |
|
194 'TABLE_PREFIX' => table_prefix |
|
195 )); |
|
196 |
|
197 $sql_list = $parser->parse(); |
|
198 // Check for empty schema file |
|
199 if ( $sql_list[0] === ';' && count($sql_list) == 1 ) |
|
200 { |
|
201 // It's empty, report success for this version |
|
202 // See below for explanation of why setConfig() is called here |
|
203 setConfig('enano_version', $verset[1]); |
|
204 continue; |
|
205 } |
|
206 |
|
207 foreach ( $sql_list as $sql ) |
|
208 { |
|
209 // check for '@' operator on query |
|
210 if ( substr($sql, 0, 1) == '@' ) |
|
211 { |
|
212 // Yes - perform query but don't check for errors |
|
213 $db->sql_query($sql); |
|
214 } |
|
215 else |
|
216 { |
|
217 // Perform as normal |
|
218 if ( !$db->sql_query($sql) ) |
|
219 $db->_die(); |
|
220 } |
|
221 } |
|
222 |
|
223 // Is there an additional script (logic) to be run after the schema? |
|
224 $postscript = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}.php"; |
|
225 if ( file_exists($postscript) ) |
|
226 @include($postscript); |
|
227 |
|
228 // The advantage of calling setConfig on the system version here? |
|
229 // Simple. If the upgrade fails, it will pick up from the last |
|
230 // version, not try to start again from the beginning. This will |
|
231 // still cause errors in most cases though. Eventually we probably |
|
232 // need some sort of query-numbering system that tracks in-progress |
|
233 // upgrades. |
|
234 |
|
235 setConfig('enano_version', $verset[1]); |
|
236 } |
|
237 } |
|
238 |
119 ?> |
239 ?> |