5 include_once($path_to_root . "/includes/session.inc");
7 page(_("Software Upgrade"));
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/admin/db/company_db.inc");
11 include_once($path_to_root . "/admin/db/maintenance_db.inc");
12 include_once($path_to_root . "/includes/ui.inc");
15 // Checks $field existence in $table with given field $properties
16 // $table - table name without prefix
17 // $field - optional field name
18 // $properties - optional properties of field defined by MySQL:
19 // 'Type', 'Null', 'Key', 'Default', 'Extra'
21 function check_table($pref, $table, $field=null, $properties=null)
23 $fields = db_query("SHOW COLUMNS FROM ".$pref.$table);
25 return 1; // no such table or error
28 return 0; // table exists
30 while( $row = db_fetch_assoc($fields))
32 if ($row['Field'] == $field)
34 if (!isset($properties))
36 foreach($properties as $property => $value)
38 if ($row[$property] != $value)
39 return 3; // failed type/length check
41 return 0; // property check ok.
44 return 2; // field not found
47 // Creates table of installer objects sorted by version.
49 function get_installers()
53 $patchdir = $path_to_root."/sql/";
55 $datadir = @opendir($patchdir);
59 while(false !== ($fname = readdir($datadir)))
60 { // check all php files but index.php
61 if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
62 && stristr($fname, '.php') != false)
65 include_once($patchdir . $fname);
66 if (isset($install)) // add installer if found
67 $upgrades[$install->version] = $install;
70 ksort($upgrades); // sort by file name
71 $upgrades = array_values($upgrades);
76 // Apply one differential data set.
78 function upgrade_step($index, $conn)
80 global $path_to_root, $installers;
82 $inst = $installers[$index];
84 $pref = $conn['tbpref'];
87 $force = get_post('force_'.$index);
88 if ($force || get_post('install_'.$index))
90 if (!$inst->installed($pref) || $force)
92 // if(!$inst->pre_check($pref)) return false;
95 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
97 $ret &= $inst->install($pref, $force);
103 function db_open($conn)
105 $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]);
108 if (!mysql_select_db($conn["dbname"], $db))
113 $installers = get_installers();
115 if (get_post('Upgrade'))
119 foreach ($db_connections as $conn)
121 // connect to database
122 if (!($db = db_open($conn)))
124 display_error(_("Cannot connect to database for company")
125 ." '".$conn['name']."'");
128 // create security backup
129 if ($conn['tbpref'] != "")
130 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
132 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
134 db_export($conn, $filename, 'no', 'Security backup before upgrade', $conn['tbpref']);
135 // apply all upgrade data
136 foreach ($installers as $i => $inst)
138 $ret = upgrade_step($i, $conn);
141 sprintf(_("Database upgrade to version %s failed for company '%s'."),
142 $inst->version, $conn['name'])
144 ._('You should restore company database from latest backup file'));
146 // db_close($conn); ?
150 display_notification(_('All companies data has been successfully updated'));
151 $Ajax->activate('_page_body');
155 start_table($table_style);
156 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
160 $k = 0; //row colour counter
161 foreach($installers as $i => $inst)
163 alt_table_row_color($k);
165 label_cell($inst->version);
166 label_cell($inst->description);
167 label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
168 // this is checked only for first (site admin) company,
169 // but in fact we should always upgrade all data sets after
171 if ($inst->installed(TB_PREF))
172 label_cell(_("Installed"));
174 check_cells(null,'install_'.$i, 0);
175 check_cells(null,'force_'.$i, 0);
179 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');