1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - Setup * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * -------------------------------------------- * |
---|
6 | * This file written by Miles Lott <milos@groupwhere.org> * |
---|
7 | * Originally written for phpGroupWare. * |
---|
8 | * (C) 2001-2004 Miles Lott * |
---|
9 | * -------------------------------------------- * |
---|
10 | * This program is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU General Public License as published by the * |
---|
12 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
13 | * option) any later version. * |
---|
14 | \**************************************************************************/ |
---|
15 | |
---|
16 | /* app status values: |
---|
17 | U Upgrade required/available |
---|
18 | R upgrade in pRogress |
---|
19 | C upgrade Completed successfully |
---|
20 | D Dependency failure |
---|
21 | F upgrade Failed |
---|
22 | V Version mismatch at end of upgrade (Not used, proposed only) |
---|
23 | M Missing files at start of upgrade (Not used, proposed only) |
---|
24 | */ |
---|
25 | |
---|
26 | class setup_process |
---|
27 | { |
---|
28 | var $oProc; |
---|
29 | var $tables; |
---|
30 | var $updateincluded = array(); |
---|
31 | var $translation; |
---|
32 | |
---|
33 | function setup_process() |
---|
34 | { |
---|
35 | $this->translation = CreateObject('phpgwapi.setup_translation'); |
---|
36 | } |
---|
37 | |
---|
38 | /*! |
---|
39 | @function init_process |
---|
40 | @abstract create schema_proc object |
---|
41 | @param none |
---|
42 | */ |
---|
43 | function init_process() |
---|
44 | { |
---|
45 | $ConfigDomain = get_var('ConfigDomain',array('COOKIE','POST')); |
---|
46 | $phpgw_domain = $GLOBALS['phpgw_domain']; |
---|
47 | |
---|
48 | $GLOBALS['phpgw_setup']->oProc = CreateObject('phpgwapi.schema_proc',$phpgw_domain[$ConfigDomain]['db_type']); |
---|
49 | $GLOBALS['phpgw_setup']->oProc->m_odb = $GLOBALS['phpgw_setup']->db; |
---|
50 | $GLOBALS['phpgw_setup']->oProc->m_odb->Host = $phpgw_domain[$ConfigDomain]['db_host']; |
---|
51 | $GLOBALS['phpgw_setup']->oProc->m_odb->Database = $phpgw_domain[$ConfigDomain]['db_name']; |
---|
52 | $GLOBALS['phpgw_setup']->oProc->m_odb->User = $phpgw_domain[$ConfigDomain]['db_user']; |
---|
53 | $GLOBALS['phpgw_setup']->oProc->m_odb->Password = $phpgw_domain[$ConfigDomain]['db_pass']; |
---|
54 | $GLOBALS['phpgw_setup']->oProc->m_odb->Halt_On_Error = 'report'; |
---|
55 | $GLOBALS['phpgw_setup']->oProc->m_odb->connect(); |
---|
56 | } |
---|
57 | |
---|
58 | /*! |
---|
59 | @function pass |
---|
60 | @abstract the mother of all multipass upgrade parental loop functions |
---|
61 | @param $setup_info array of application info from setup.inc.php files |
---|
62 | @param $type optional, defaults to new(install), could also be 'upgrade' |
---|
63 | @param $DEBUG optional, print debugging info |
---|
64 | @param $force_en optional, install english language files |
---|
65 | */ |
---|
66 | function pass($setup_info,$method='new',$DEBUG=False,$force_en=False) |
---|
67 | { |
---|
68 | if(!$method) |
---|
69 | { |
---|
70 | return False; |
---|
71 | } |
---|
72 | // Place api first |
---|
73 | $pass['phpgwapi'] = $setup_info['phpgwapi']; |
---|
74 | $pass['admin'] = $setup_info['admin']; |
---|
75 | $pass['preferences'] = $setup_info['preferences']; |
---|
76 | @reset($setup_info); |
---|
77 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_versions($setup_info); |
---|
78 | @reset($setup_info); |
---|
79 | |
---|
80 | $i = 1; |
---|
81 | $passed = array(); |
---|
82 | $passing = array(); |
---|
83 | $pass_string = implode (':', $pass); |
---|
84 | $passing_string = implode (':', $passing); |
---|
85 | while($pass_string != $passing_string) |
---|
86 | { |
---|
87 | $passing = array(); |
---|
88 | if($DEBUG) { echo '<br>process->pass(): #' . $i . ' for ' . $method . ' processing' . "\n"; } |
---|
89 | /* Check current versions and dependencies */ |
---|
90 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_db_versions($setup_info); |
---|
91 | $setup_info = $GLOBALS['phpgw_setup']->detection->compare_versions($setup_info); |
---|
92 | //_debug_array($setup_info);exit; |
---|
93 | $setup_info = $GLOBALS['phpgw_setup']->detection->check_depends($setup_info); |
---|
94 | //if($i==2) { _debug_array($passed);exit; } |
---|
95 | |
---|
96 | /* stuff the rest of the apps, but only those with available upgrades */ |
---|
97 | while(list($key,$value) = @each($setup_info)) |
---|
98 | { |
---|
99 | if(($value['name'] != 'phpgwapi') && ($value['status'] == 'U')) |
---|
100 | { |
---|
101 | if(($passed[$value['name']]['status'] != 'F') && ($passed[$value['name']]['status'] != 'C')) |
---|
102 | { |
---|
103 | $pass[$value['name']] = $setup_info[$value['name']]; |
---|
104 | } |
---|
105 | } |
---|
106 | /* |
---|
107 | Now if we are on the 2nd or more passes, add api in |
---|
108 | if (!$pass['phpgwapi']) |
---|
109 | { |
---|
110 | $pass['phpgwapi'] = $setup_info['phpgwapi']; |
---|
111 | } |
---|
112 | */ |
---|
113 | } |
---|
114 | |
---|
115 | switch($method) |
---|
116 | { |
---|
117 | case 'new': |
---|
118 | /* Create tables and insert new records for each app in this list */ |
---|
119 | $passing = $this->current($pass,$DEBUG); |
---|
120 | $passing = $this->default_records($passing,$DEBUG); |
---|
121 | $passing = $this->add_langs($passing,$DEBUG,array(get_var('ConfigLang',Array('POST','COOKIE')),'en')); |
---|
122 | $this->save_minimal_config(); |
---|
123 | break; |
---|
124 | case 'upgrade': |
---|
125 | /* Run upgrade scripts on each app in the list */ |
---|
126 | $passing = $this->upgrade($pass,$DEBUG); |
---|
127 | $passing = $this->upgrade_langs($passing,$DEBUG); |
---|
128 | //_debug_array($pass);exit; |
---|
129 | break; |
---|
130 | default: |
---|
131 | /* What the heck are you doing? */ |
---|
132 | return False; |
---|
133 | break; |
---|
134 | } |
---|
135 | |
---|
136 | $pass = array(); |
---|
137 | @reset($passing); |
---|
138 | while(list($key,$value) = @each($passing)) |
---|
139 | { |
---|
140 | if($value['status'] == 'C') |
---|
141 | { |
---|
142 | $passed[$value['name']] = $passing[$value['name']]; |
---|
143 | if($DEBUG) { echo '<br>process->pass(): '.$passed[$value['name']]['name'] . ' install completed'."\n"; } |
---|
144 | } |
---|
145 | elseif($value['status'] == 'F') |
---|
146 | { |
---|
147 | $setup_info[$value['name']] = $passing[$value['name']]; |
---|
148 | if($DEBUG) { echo '<br>process->pass(): '.$setup_info[$value['name']]['name'] . ' install failed'."\n"; } |
---|
149 | } |
---|
150 | elseif($value['status'] == 'D') |
---|
151 | { |
---|
152 | $pass[$value['name']] = $setup_info[$value['name']]; |
---|
153 | if($DEBUG) { echo '<br>process->pass(): '.$pass[$value['name']]['name'] . ' fails dependency check on this pass'."\n"; } |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | $tmp = $passing[$value['name']]['name']; |
---|
158 | if($DEBUG) { echo '<br>process->pass(): '.$tmp . ' skipped on this pass'."\n"; } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | $i++; |
---|
163 | if($i == 20) /* Then oops it broke */ |
---|
164 | { |
---|
165 | echo '<br>Setup failure: excess looping in process->pass():'."\n"; |
---|
166 | echo '<br>Pass:<br>'."\n"; |
---|
167 | _debug_array($pass); |
---|
168 | echo '<br>Passed:<br>'."\n"; |
---|
169 | _debug_array($passed); |
---|
170 | exit; |
---|
171 | } |
---|
172 | $pass_string = implode (':', $pass); |
---|
173 | $passing_string = implode (':', $passing); |
---|
174 | } |
---|
175 | |
---|
176 | /* now return the list */ |
---|
177 | @reset($passed); |
---|
178 | while(list($key,$value) = @each($passed)) |
---|
179 | { |
---|
180 | $setup_info[$value['name']] = $passed[$value['name']]; |
---|
181 | } |
---|
182 | |
---|
183 | return ($setup_info); |
---|
184 | } |
---|
185 | |
---|
186 | /*! |
---|
187 | @function save_minimal_config |
---|
188 | @abstract saves a minimal default config, so you get a running install without entering and saveing Step #2 config |
---|
189 | */ |
---|
190 | function save_minimal_config() |
---|
191 | { |
---|
192 | $GLOBALS['current_config']['site_title'] = 'eGroupWare'; |
---|
193 | $GLOBALS['current_config']['hostname'] = $_SERVER['HTTP_HOST']; |
---|
194 | // files-dir is not longer allowed in document root, for security reasons !!! |
---|
195 | $GLOBALS['current_config']['files_dir'] = '/outside/webserver/docroot'; |
---|
196 | |
---|
197 | if(@is_dir('/tmp')) |
---|
198 | { |
---|
199 | $GLOBALS['current_config']['temp_dir'] = '/tmp'; |
---|
200 | } |
---|
201 | elseif(@is_dir('c:\\temp')) |
---|
202 | { |
---|
203 | $GLOBALS['current_config']['temp_dir'] = 'c:\\temp'; |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | $GLOBALS['current_config']['temp_dir'] = '/path/to/temp/dir'; |
---|
208 | } |
---|
209 | // guessing the phpGW url |
---|
210 | $parts = explode('/',$_SERVER['PHP_SELF']); |
---|
211 | array_pop($parts); // remove config.php |
---|
212 | array_pop($parts); // remove setup |
---|
213 | $GLOBALS['current_config']['webserver_url'] = implode('/',$parts); |
---|
214 | |
---|
215 | $datetime = CreateObject('phpgwapi.date_time'); |
---|
216 | $GLOBALS['current_config']['tz_offset'] = $datetime->getbestguess(); |
---|
217 | unset($datetime); |
---|
218 | |
---|
219 | // RalfBecker: php.net recommend this for security reasons, it should be our default too |
---|
220 | $GLOBALS['current_config']['usecookies'] = 'True'; |
---|
221 | |
---|
222 | foreach($GLOBALS['current_config'] as $setting => $value) |
---|
223 | { |
---|
224 | $setting = $GLOBALS['phpgw_setup']->db->db_addslashes($setting); |
---|
225 | $value = $GLOBALS['phpgw_setup']->db->db_addslashes($value); |
---|
226 | @$GLOBALS['phpgw_setup']->db->query("DELETE FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='$setting'",__LINE__,__FILE__); |
---|
227 | $GLOBALS['phpgw_setup']->db->query("INSERT INTO phpgw_config (config_app,config_name, config_value) VALUES ('phpgwapi','$setting','$value')"); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | /*! |
---|
232 | @function droptables |
---|
233 | @abstract drop tables per application, check that they are in the db first |
---|
234 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
235 | */ |
---|
236 | function droptables($setup_info,$DEBUG=False) |
---|
237 | { |
---|
238 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
239 | { |
---|
240 | $this->init_process(); |
---|
241 | } |
---|
242 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
243 | |
---|
244 | /* The following is built so below we won't try to drop a table that isn't there. */ |
---|
245 | $tablenames = $GLOBALS['phpgw_setup']->db->table_names(); |
---|
246 | if (!is_array($setup_info) || !is_array($tablenames)) |
---|
247 | { |
---|
248 | return $setup_info; // nothing to do |
---|
249 | } |
---|
250 | $tables = array(); |
---|
251 | foreach($tablenames as $data) |
---|
252 | { |
---|
253 | $tables[] = $data['table_name']; |
---|
254 | } |
---|
255 | |
---|
256 | if (!is_array($setup_info)) |
---|
257 | { |
---|
258 | return $setup_info; |
---|
259 | } |
---|
260 | foreach($setup_info as $app_name => $data) |
---|
261 | { |
---|
262 | if(is_array($data['tables'])) |
---|
263 | { |
---|
264 | foreach($data['tables'] as $table) |
---|
265 | { |
---|
266 | //echo $table; |
---|
267 | if(in_array($table,$tables)) |
---|
268 | { |
---|
269 | if($DEBUG){ echo '<br>process->droptables(): Dropping :'. $app_name . ' table: ' . $table; } |
---|
270 | $GLOBALS['phpgw_setup']->oProc->DropTable($table); |
---|
271 | // Update the array values for return below |
---|
272 | $setup_info[$app_name]['status'] = 'U'; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | /* Done, return current status */ |
---|
279 | return $setup_info; |
---|
280 | } |
---|
281 | |
---|
282 | /*! |
---|
283 | @function current |
---|
284 | @abstract process current table setup in each application/setup dir |
---|
285 | @param $appinfo array of application info from setup.inc.php files, etc. |
---|
286 | @discussion This duplicates the old newtables behavior, using schema_proc |
---|
287 | */ |
---|
288 | function current($setup_info,$DEBUG=False) |
---|
289 | { |
---|
290 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
291 | { |
---|
292 | $this->init_process(); |
---|
293 | } |
---|
294 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
295 | |
---|
296 | @reset($setup_info); |
---|
297 | while(list($key,$null) = @each($setup_info)) |
---|
298 | { |
---|
299 | $enabled = False; |
---|
300 | $appname = $setup_info[$key]['name']; |
---|
301 | $apptitle = $setup_info[$key]['title']; |
---|
302 | |
---|
303 | if($DEBUG) { echo '<br>process->current(): Incoming status: ' . $appname . ',status: '. $setup_info[$key]['status']; } |
---|
304 | |
---|
305 | $appdir = PHPGW_SERVER_ROOT . SEP . $appname . SEP . 'setup' . SEP; |
---|
306 | |
---|
307 | if($setup_info[$key]['tables'] && file_exists($appdir.'tables_current.inc.php')) |
---|
308 | { |
---|
309 | if($DEBUG) { echo '<br>process->current(): Including: ' . $appdir.'tables_current.inc.php'; } |
---|
310 | include ($appdir.'tables_current.inc.php'); |
---|
311 | $ret = $this->post_process($phpgw_baseline,$DEBUG); |
---|
312 | if($ret) |
---|
313 | { |
---|
314 | if($GLOBALS['phpgw_setup']->app_registered($appname)) |
---|
315 | { |
---|
316 | $GLOBALS['phpgw_setup']->update_app($appname); |
---|
317 | $GLOBALS['phpgw_setup']->update_hooks($appname); |
---|
318 | } |
---|
319 | else |
---|
320 | { |
---|
321 | $GLOBALS['phpgw_setup']->register_app($appname); |
---|
322 | $GLOBALS['phpgw_setup']->register_hooks($appname); |
---|
323 | } |
---|
324 | // Update the array values for return below |
---|
325 | $setup_info[$key]['status'] = 'C'; |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | /* script processing failed */ |
---|
330 | if($DEBUG) { echo '<br>process->current(): Failed for ' . $appname . ',status: '. $setup_info[$key]['status']; } |
---|
331 | $setup_info[$key]['status'] = 'F'; |
---|
332 | } |
---|
333 | } |
---|
334 | else |
---|
335 | { |
---|
336 | if($DEBUG) { echo '<br>process->current(): No current tables for ' . $apptitle . "\n"; } |
---|
337 | /* |
---|
338 | Add the app, but disable it if it has tables defined. |
---|
339 | A manual sql script install is needed, but we do add the hooks |
---|
340 | */ |
---|
341 | $enabled = 99; |
---|
342 | if($setup_info[$key]['tables'][0] != '') |
---|
343 | { |
---|
344 | $enabled = False; |
---|
345 | } |
---|
346 | if($GLOBALS['phpgw_setup']->app_registered($appname)) |
---|
347 | { |
---|
348 | $GLOBALS['phpgw_setup']->update_app($appname); |
---|
349 | $GLOBALS['phpgw_setup']->update_hooks($appname); |
---|
350 | } |
---|
351 | else |
---|
352 | { |
---|
353 | $GLOBALS['phpgw_setup']->register_app($appname,$enabled); |
---|
354 | $GLOBALS['phpgw_setup']->register_hooks($appname); |
---|
355 | } |
---|
356 | $setup_info[$key]['status'] = 'C'; |
---|
357 | } |
---|
358 | if($DEBUG) { echo '<br>process->current(): Outgoing status: ' . $appname . ',status: '. $setup_info[$key]['status']; } |
---|
359 | } |
---|
360 | |
---|
361 | /* Done, return current status */ |
---|
362 | return ($setup_info); |
---|
363 | } |
---|
364 | |
---|
365 | /*! |
---|
366 | @function default_records |
---|
367 | @abstract process default_records.inc.php in each application/setup dir |
---|
368 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
369 | */ |
---|
370 | function default_records($setup_info,$DEBUG=False) |
---|
371 | { |
---|
372 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
373 | { |
---|
374 | $this->init_process(); |
---|
375 | } |
---|
376 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
377 | // $oProc = $GLOBALS['phpgw_setup']->oProc; |
---|
378 | |
---|
379 | @reset($setup_info); |
---|
380 | while(list($key,$null) = @each($setup_info)) |
---|
381 | { |
---|
382 | $appname = $setup_info[$key]['name']; |
---|
383 | $appdir = PHPGW_SERVER_ROOT . SEP . $appname . SEP . 'setup' . SEP; |
---|
384 | |
---|
385 | if(file_exists($appdir.'default_records.inc.php')) |
---|
386 | { |
---|
387 | if($DEBUG) |
---|
388 | { |
---|
389 | echo '<br>process->default_records(): Including default records for ' . $appname . "\n"; |
---|
390 | } |
---|
391 | $GLOBALS['phpgw_setup']->oProc->m_odb->transaction_begin(); |
---|
392 | $oProc = &$GLOBALS['phpgw_setup']->oProc; // to be compatible with old apps |
---|
393 | include ($appdir.'default_records.inc.php'); |
---|
394 | $GLOBALS['phpgw_setup']->oProc->m_odb->transaction_commit(); |
---|
395 | } |
---|
396 | /* $setup_info[$key]['status'] = 'C'; */ |
---|
397 | } |
---|
398 | |
---|
399 | /* Done, return current status */ |
---|
400 | return ($setup_info); |
---|
401 | } |
---|
402 | |
---|
403 | /*! |
---|
404 | @function add_langs |
---|
405 | @abstract process application lang files and uninstall |
---|
406 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
407 | */ |
---|
408 | function add_langs($setup_info,$DEBUG=False,$force_langs=False) |
---|
409 | { |
---|
410 | @reset($setup_info); |
---|
411 | while(list($key,$null) = @each($setup_info)) |
---|
412 | { |
---|
413 | $appname = $setup_info[$key]['name']; |
---|
414 | $this->translation->add_langs($appname,$DEBUG,$force_langs); |
---|
415 | if($DEBUG) |
---|
416 | { |
---|
417 | echo '<br>process->add_langs(): Translations added for ' . $appname . "\n"; |
---|
418 | } |
---|
419 | } |
---|
420 | /* Done, return current status */ |
---|
421 | return ($setup_info); |
---|
422 | } |
---|
423 | |
---|
424 | /*! |
---|
425 | @function drop_langs |
---|
426 | @abstract process application lang files and install |
---|
427 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
428 | */ |
---|
429 | function drop_langs($setup_info,$DEBUG=False) |
---|
430 | { |
---|
431 | @reset($setup_info); |
---|
432 | while(list($key,$null) = @each($setup_info)) |
---|
433 | { |
---|
434 | $appname = $setup_info[$key]['name']; |
---|
435 | $this->translation->drop_langs($appname,$DEBUG); |
---|
436 | if($DEBUG) |
---|
437 | { |
---|
438 | echo '<br>process->drop_langs(): Translations removed for ' . $appname . "\n"; |
---|
439 | } |
---|
440 | } |
---|
441 | /* Done, return current status */ |
---|
442 | return ($setup_info); |
---|
443 | } |
---|
444 | |
---|
445 | /*! |
---|
446 | @function upgrade_langs |
---|
447 | @abstract process application lang files and reinstall |
---|
448 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
449 | */ |
---|
450 | function upgrade_langs($setup_info,$DEBUG=False) |
---|
451 | { |
---|
452 | @reset($setup_info); |
---|
453 | while(list($key,$null) = @each($setup_info)) |
---|
454 | { |
---|
455 | /* Don't upgrade lang files in the middle of an upgrade */ |
---|
456 | if($setup_info[$key]['status'] == 'R') |
---|
457 | { |
---|
458 | continue; |
---|
459 | } |
---|
460 | $appname = $setup_info[$key]['name']; |
---|
461 | $this->translation->drop_langs($appname,$DEBUG); |
---|
462 | $this->translation->add_langs($appname,$DEBUG); |
---|
463 | if($DEBUG) |
---|
464 | { |
---|
465 | echo '<br>process->upgrade_langs(): Translations reinstalled for ' . $appname . "\n"; |
---|
466 | } |
---|
467 | } |
---|
468 | /* Done, return current status */ |
---|
469 | return ($setup_info); |
---|
470 | } |
---|
471 | |
---|
472 | /*! |
---|
473 | @function test_data |
---|
474 | @abstract process test_data.inc.php in each application/setup dir for developer tests |
---|
475 | This data should work with the baseline tables |
---|
476 | @param $setup_info array of application info from setup.inc.php files, etc. |
---|
477 | */ |
---|
478 | function test_data($setup_info,$DEBUG=False) |
---|
479 | { |
---|
480 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
481 | { |
---|
482 | $this->init_process(); |
---|
483 | } |
---|
484 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
485 | // $oProc = $GLOBALS['phpgw_setup']->oProc; |
---|
486 | |
---|
487 | @reset($setup_info); |
---|
488 | while(list($key,$null) = @each($setup_info)) |
---|
489 | { |
---|
490 | $appname = $setup_info[$key]['name']; |
---|
491 | $appdir = PHPGW_SERVER_ROOT . SEP . $appname . SEP . 'setup' . SEP; |
---|
492 | |
---|
493 | if(file_exists($appdir.'test_data.inc.php')) |
---|
494 | { |
---|
495 | if($DEBUG) |
---|
496 | { |
---|
497 | echo '<br>process->test_data(): Including baseline test data for ' . $appname . "\n"; |
---|
498 | } |
---|
499 | $GLOBALS['phpgw_setup']->oProc->m_odb->transaction_begin(); |
---|
500 | include ($appdir.'test_data.inc.php'); |
---|
501 | $GLOBALS['phpgw_setup']->oProc->m_odb->transaction_commit(); |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | /* Done, return current status */ |
---|
506 | return ($setup_info); |
---|
507 | } |
---|
508 | |
---|
509 | /*! |
---|
510 | @function baseline |
---|
511 | @abstract process baseline table setup in each application/setup dir |
---|
512 | @param $appinfo array of application info from setup.inc.php files, etc. |
---|
513 | */ |
---|
514 | function baseline($setup_info,$DEBUG=False) |
---|
515 | { |
---|
516 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
517 | { |
---|
518 | $this->init_process(); |
---|
519 | } |
---|
520 | |
---|
521 | @reset($setup_info); |
---|
522 | while(list($key,$null) = @each($setup_info)) |
---|
523 | { |
---|
524 | $appname = $setup_info[$key]['name']; |
---|
525 | $appdir = PHPGW_SERVER_ROOT . SEP . $appname . SEP . 'setup' . SEP; |
---|
526 | |
---|
527 | if(file_exists($appdir.'tables_baseline.inc.php')) |
---|
528 | { |
---|
529 | if($DEBUG) |
---|
530 | { |
---|
531 | echo '<br>process->baseline(): Including baseline tables for ' . $appname . "\n"; |
---|
532 | } |
---|
533 | include ($appdir.'tables_baseline.inc.php'); |
---|
534 | $GLOBALS['phpgw_setup']->oProc->GenerateScripts($phpgw_baseline, $DEBUG); |
---|
535 | $this->post_process($phpgw_baseline,$DEBUG); |
---|
536 | |
---|
537 | /* Update the array values for return below */ |
---|
538 | /* $setup_info[$key]['status'] = 'R'; */ |
---|
539 | } |
---|
540 | else |
---|
541 | { |
---|
542 | if($DEBUG) |
---|
543 | { |
---|
544 | echo '<br>process->baseline(): No baseline tables for ' . $appname . "\n"; |
---|
545 | } |
---|
546 | //$setup_info[$key]['status'] = 'C'; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | /* Done, return current status */ |
---|
551 | return ($setup_info); |
---|
552 | } |
---|
553 | |
---|
554 | /*! |
---|
555 | @function upgrade |
---|
556 | @abstract process available upgrades in each application/setup dir |
---|
557 | @param $appinfo array of application info from setup.inc.php files, etc. |
---|
558 | */ |
---|
559 | function upgrade($setup_info,$DEBUG=False) |
---|
560 | { |
---|
561 | if(!@$GLOBALS['phpgw_setup']->oProc) |
---|
562 | { |
---|
563 | $this->init_process(); |
---|
564 | } |
---|
565 | $GLOBALS['phpgw_setup']->oProc->m_odb->HaltOnError = 'no'; |
---|
566 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = True; |
---|
567 | |
---|
568 | @reset($setup_info); |
---|
569 | while(list($key,$null) = @each($setup_info)) |
---|
570 | { |
---|
571 | /* Don't try to upgrade an app that is not installed */ |
---|
572 | if(!$GLOBALS['phpgw_setup']->app_registered($setup_info[$key]['name'])) |
---|
573 | { |
---|
574 | if($DEBUG) |
---|
575 | { |
---|
576 | echo '<br>process->upgrade(): Application not installed: ' . $appname . "\n"; |
---|
577 | } |
---|
578 | unset($setup_info[$key]); |
---|
579 | continue; |
---|
580 | } |
---|
581 | |
---|
582 | /* if upgrade required, or if we are running again after an upgrade or dependency failure */ |
---|
583 | if($DEBUG) { echo '<br>process->upgrade(): Incoming : appname: '.$setup_info[$key]['name'] . ' status: ' . $setup_info[$key]['status']; } |
---|
584 | if($setup_info[$key]['status'] == 'U' || |
---|
585 | $setup_info[$key]['status'] == 'D' || |
---|
586 | $setup_info[$key]['status'] == 'V' || |
---|
587 | $setup_info[$key]['status'] == '') // TODO this is not getting set for api upgrade, sometimes ??? |
---|
588 | { |
---|
589 | $appname = $setup_info[$key]['name']; |
---|
590 | $apptitle = $setup_info[$key]['title']; |
---|
591 | $currentver = $setup_info[$key]['currentver']; |
---|
592 | $targetver = $setup_info[$key]['version']; // The version we need to match when done |
---|
593 | $appdir = PHPGW_SERVER_ROOT . SEP . $appname . SEP . 'setup' . SEP; |
---|
594 | |
---|
595 | $test = array(); |
---|
596 | $GLOBALS['phpgw_setup']->oProc->m_aTables = $phpgw_baseline = array(); |
---|
597 | /* |
---|
598 | $phpgw_baseline = array(); |
---|
599 | |
---|
600 | $tmpapp = array(); |
---|
601 | $tmpapp[] = $setup_info[$key]; |
---|
602 | $this->baseline($tmpapp,$DEBUG); |
---|
603 | $GLOBALS['phpgw_setup']->oProc->m_aTables = $phpgw_baseline; |
---|
604 | // So far, including the baseline file is not helping. |
---|
605 | // Only AlterColumn/RenameColumn seem to be failing silently. |
---|
606 | // This is because we are not keeping up with table changes, so a table in baseline |
---|
607 | // either does not exist anymore, or the baseline is being lost. |
---|
608 | // 10-18-2003 milosch - The baseline file is definitely needed for any app with and update file. |
---|
609 | */ |
---|
610 | if($setup_info[$key]['tables'] && file_exists($appdir.'tables_baseline.inc.php')) |
---|
611 | { |
---|
612 | if($DEBUG) |
---|
613 | { |
---|
614 | echo '<br>process->baseline(): Including baseline tables for ' . $appname . "\n"; |
---|
615 | } |
---|
616 | include ($appdir.'tables_baseline.inc.php'); |
---|
617 | $GLOBALS['phpgw_setup']->oProc->m_aTables = $phpgw_baseline; |
---|
618 | /* $GLOBALS['phpgw_setup']->oProc->GenerateScripts($phpgw_baseline, $DEBUG); */ |
---|
619 | } |
---|
620 | else |
---|
621 | { |
---|
622 | if($DEBUG) |
---|
623 | { |
---|
624 | echo '<br>process->baseline(): No baseline tables for ' . $appname . "\n"; |
---|
625 | } |
---|
626 | /* This should be a break with a status setting, or not at all |
---|
627 | break; |
---|
628 | */ |
---|
629 | } |
---|
630 | if(file_exists($appdir . 'tables_update.inc.php') && !@$this->updateincluded[$appname]) |
---|
631 | { |
---|
632 | include ($appdir . 'tables_update.inc.php'); |
---|
633 | $this->updateincluded[$appname] = True; |
---|
634 | |
---|
635 | /* $test array comes from update file. It is a list of available upgrade functions */ |
---|
636 | @reset($test); |
---|
637 | |
---|
638 | while(list($x,$value) = @each($test)) |
---|
639 | { |
---|
640 | $this->currentversion = $currentver = $setup_info[$key]['currentver']; |
---|
641 | |
---|
642 | /* build upgrade function name */ |
---|
643 | $function = $appname . '_upgrade' . str_replace("-", '', str_replace(".", '_', $value)); |
---|
644 | |
---|
645 | if($DEBUG) |
---|
646 | { |
---|
647 | echo '<br>process->upgrade(): appname: ' . $appname; |
---|
648 | echo '<br>process->upgrade(): currentver: ' . $currentver; |
---|
649 | echo '<br>process->upgrade(): targetver: ' . $targetver; |
---|
650 | echo '<br>process->upgrade(): status: ' . $setup_info[$key]['status']; |
---|
651 | echo '<br>process->upgrade(): checking: ' . $value; |
---|
652 | echo '<br>process->upgrade(): function: ' . $function; |
---|
653 | } |
---|
654 | |
---|
655 | if($value == $targetver) |
---|
656 | { |
---|
657 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
658 | /* Done upgrading */ |
---|
659 | if($DEBUG) |
---|
660 | { |
---|
661 | echo '<br>process->upgrade(): Upgrade of ' . $appname . ' to ' . $targetver . ' is completed.' . "\n"; |
---|
662 | } |
---|
663 | $appstatus = 'C'; |
---|
664 | $setup_info[$key]['status'] = $appstatus; |
---|
665 | $setup_info[$key]['currentver'] = $targetver; |
---|
666 | if($GLOBALS['phpgw_setup']->app_registered($appname)) |
---|
667 | { |
---|
668 | $GLOBALS['phpgw_setup']->update_app($appname); |
---|
669 | $GLOBALS['phpgw_setup']->update_hooks($appname); |
---|
670 | } |
---|
671 | else |
---|
672 | { |
---|
673 | $GLOBALS['phpgw_setup']->register_app($appname); |
---|
674 | $GLOBALS['phpgw_setup']->register_hooks($appname); |
---|
675 | } |
---|
676 | //break; |
---|
677 | } |
---|
678 | elseif(($value == $currentver) || !$currentver) |
---|
679 | { |
---|
680 | /* start upgrading db in addition to baseline */ |
---|
681 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = False; |
---|
682 | if($DEBUG) { echo '<br>process->upgrade(): running ' . $function; } |
---|
683 | /* run upgrade function */ |
---|
684 | $success = $function(); |
---|
685 | if($success != False) |
---|
686 | { |
---|
687 | $setup_info[$key]['currentver'] = $success; |
---|
688 | if($DEBUG) |
---|
689 | { |
---|
690 | echo '<br>process->upgrade(): Upgrade of ' . $appname |
---|
691 | . ' from ' . $value |
---|
692 | . ' to ' . $setup_info[$key]['currentver'] |
---|
693 | . ' is completed.' . "\n"; |
---|
694 | } |
---|
695 | $appstatus = 'R'; |
---|
696 | $setup_info[$key]['status'] = $appstatus; |
---|
697 | if($GLOBALS['phpgw_setup']->app_registered($appname)) |
---|
698 | { |
---|
699 | if($DEBUG) |
---|
700 | { |
---|
701 | echo '<br>process->upgrade(): Updating registration of ' . $appname . ', new version: ' . $setup_info[$key]['currentver']; |
---|
702 | } |
---|
703 | $GLOBALS['phpgw_setup']->update_app($appname); |
---|
704 | $GLOBALS['phpgw_setup']->update_hooks($appname); |
---|
705 | } |
---|
706 | else |
---|
707 | { |
---|
708 | if($DEBUG) |
---|
709 | { |
---|
710 | echo '<br>process->upgrade(): Registering ' . $appname . ', version: ' . $setup_info[$key]['currentver']; |
---|
711 | } |
---|
712 | $GLOBALS['phpgw_setup']->register_app($appname); |
---|
713 | $GLOBALS['phpgw_setup']->register_hooks($appname); |
---|
714 | } |
---|
715 | // is the next update the one we need? |
---|
716 | if ($success && $test[$x+1] != $success && |
---|
717 | ($num = array_search($success,$test)) !== False && $num !== null) |
---|
718 | { |
---|
719 | // do we have the needed update somewhere else in the row? |
---|
720 | // if yes, position the array-pointer just before that update and continue |
---|
721 | reset($test); |
---|
722 | while((list($x,$value) = each($test)) && $x < $num-1); |
---|
723 | continue; |
---|
724 | } |
---|
725 | } |
---|
726 | else |
---|
727 | { |
---|
728 | if($DEBUG) |
---|
729 | { |
---|
730 | echo '<br>process->upgrade(): Upgrade of ' . $appname |
---|
731 | . ' from ' . $currentver |
---|
732 | . ' to ' . $value |
---|
733 | . ' failed!!!' . "\n"; |
---|
734 | } |
---|
735 | $appstatus = 'F'; |
---|
736 | break; |
---|
737 | } |
---|
738 | } |
---|
739 | elseif ($GLOBALS['phpgw_setup']->alessthanb($value,$currentver)) |
---|
740 | { |
---|
741 | if($DEBUG) { echo '<br>process->upgrade(): running baseline delta only: ' . $function . '...'; } |
---|
742 | $GLOBALS['phpgw_setup']->oProc->m_bDeltaOnly = True; |
---|
743 | $success = $function(); |
---|
744 | |
---|
745 | // is the next update the one we need? |
---|
746 | if ($success && $test[$x+1] != $success && |
---|
747 | ($num = array_search($success,$test)) !== False && $num !== null) |
---|
748 | { |
---|
749 | // do we have the needed update somewhere else in the row? |
---|
750 | // if yes, position the array-pointer just before that update and continue |
---|
751 | reset($test); |
---|
752 | while((list($x,$value) = each($test)) && $x < $num-1); |
---|
753 | continue; |
---|
754 | } |
---|
755 | } |
---|
756 | else |
---|
757 | { |
---|
758 | continue; |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|
762 | else |
---|
763 | { |
---|
764 | if($setup_info[$appname]['tables']) |
---|
765 | { |
---|
766 | $appstatus = 'F'; |
---|
767 | |
---|
768 | if($DEBUG) |
---|
769 | { |
---|
770 | echo '<br>process->upgrade(): No table upgrade available for ' . $appname . "\n"; |
---|
771 | } |
---|
772 | } |
---|
773 | else |
---|
774 | { |
---|
775 | $setup_info[$key]['currentver'] == $targetver; |
---|
776 | $appstatus = 'C'; |
---|
777 | if($GLOBALS['phpgw_setup']->app_registered($appname)) |
---|
778 | { |
---|
779 | $GLOBALS['phpgw_setup']->update_app($appname); |
---|
780 | $GLOBALS['phpgw_setup']->update_hooks($appname); |
---|
781 | } |
---|
782 | else |
---|
783 | { |
---|
784 | $GLOBALS['phpgw_setup']->register_app($appname); |
---|
785 | $GLOBALS['phpgw_setup']->register_hooks($appname); |
---|
786 | } |
---|
787 | |
---|
788 | if($DEBUG) |
---|
789 | { |
---|
790 | echo '<br>process->upgrade(): No table upgrade required for ' . $appname . "\n"; |
---|
791 | } |
---|
792 | } |
---|
793 | } |
---|
794 | } |
---|
795 | else |
---|
796 | { |
---|
797 | $appstatus = 'C'; |
---|
798 | if($DEBUG) |
---|
799 | { |
---|
800 | echo '<br>process->upgrade(): No upgrade required for ' . $appname . "\n"; |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | /* Done with this app, update status */ |
---|
805 | $setup_info[$key]['status'] = $appstatus; |
---|
806 | if($DEBUG) |
---|
807 | { |
---|
808 | echo '<br>process->upgrade(): Outgoing : appname: '.$setup_info[$key]['name'] . ' status: ' . $setup_info[$key]['status']; |
---|
809 | } |
---|
810 | } |
---|
811 | |
---|
812 | /* Done, return current status */ |
---|
813 | return ($setup_info); |
---|
814 | } |
---|
815 | |
---|
816 | /*! |
---|
817 | @function post_process |
---|
818 | @abstract commit above processing to the db |
---|
819 | */ |
---|
820 | function post_process($tables,$DEBUG=False) |
---|
821 | { |
---|
822 | if(!$tables) |
---|
823 | { |
---|
824 | return False; |
---|
825 | } |
---|
826 | |
---|
827 | $ret = $GLOBALS['phpgw_setup']->oProc->GenerateScripts($tables,$DEBUG); |
---|
828 | if($ret) |
---|
829 | { |
---|
830 | $oret = $GLOBALS['phpgw_setup']->oProc->ExecuteScripts($tables,$DEBUG); |
---|
831 | if($oret) |
---|
832 | { |
---|
833 | return True; |
---|
834 | } |
---|
835 | else |
---|
836 | { |
---|
837 | return False; |
---|
838 | } |
---|
839 | } |
---|
840 | else |
---|
841 | { |
---|
842 | return False; |
---|
843 | } |
---|
844 | } |
---|
845 | |
---|
846 | /*! |
---|
847 | @function sql_to_array |
---|
848 | @abstract send this a table name, returns printable column spec and keys for the table from schema_proc |
---|
849 | @param $tablename table whose array you want to see |
---|
850 | */ |
---|
851 | function sql_to_array($tablename='') |
---|
852 | { |
---|
853 | if(!$tablename) |
---|
854 | { |
---|
855 | return False; |
---|
856 | } |
---|
857 | |
---|
858 | if(!$GLOBALS['phpgw_setup']->oProc) |
---|
859 | { |
---|
860 | $this->init_process(); |
---|
861 | } |
---|
862 | |
---|
863 | $GLOBALS['phpgw_setup']->oProc->m_oTranslator->_GetColumns($GLOBALS['phpgw_setup']->oProc, $tablename, $sColumns, $sColumnName); |
---|
864 | |
---|
865 | while(list($key,$tbldata) = each($GLOBALS['phpgw_setup']->oProc->m_oTranslator->sCol)) |
---|
866 | { |
---|
867 | $arr .= $tbldata; |
---|
868 | } |
---|
869 | $pk = $GLOBALS['phpgw_setup']->oProc->m_oTranslator->pk; |
---|
870 | $fk = $GLOBALS['phpgw_setup']->oProc->m_oTranslator->fk; |
---|
871 | $ix = $GLOBALS['phpgw_setup']->oProc->m_oTranslator->ix; |
---|
872 | $uc = $GLOBALS['phpgw_setup']->oProc->m_oTranslator->uc; |
---|
873 | |
---|
874 | return array($arr,$pk,$fk,$ix,$uc); |
---|
875 | } |
---|
876 | } |
---|
877 | ?> |
---|