3 * FusionForge Database Installer Class
5 * Copyright (C) 2010-2011 Alain Peyrat
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 class DatabaseInstaller extends Error {
25 function DatabaseInstaller($name='', $path='') {
31 * TODO: Enter description here ...
32 * @return boolean|Ambigous <boolean, string>
38 $init = "$path/$name-init.sql";
40 $ret = $this->_runScript($init);
44 return $this->upgrade($name, $path);
46 return $this->setError(_('No database installation scripts found.'));
50 * TODO: Enter description here ...
51 * @return boolean|string
62 $date = $this->getDatabaseDate();
65 $scripts = $this->_getScripts($path);
67 foreach ($scripts as $script) {
68 if ((int) $script['date'] > $date) {
69 $res = db_query_params ('SELECT * FROM database_changes WHERE filename=$1',
70 array ($prefix.$script['filename'])) ;
72 return $this->setError("ERROR-2: ".db_error());
73 } else if (db_numrows($res) == 0) {
74 $output .= "Running script: {$script['filename']}\n";
75 $result = $this->_runScript($path.'/'.$script['filename']);
77 $res = db_query_params ('INSERT INTO database_changes (filename) VALUES ($1)',
78 array ($prefix.$script['filename'])) ;
80 return $this->setError("ERROR-3: ".db_error());
86 // $output .= "Skipping script: $prefix{$script['filename']}\n";
93 private static function getDatabaseDate() {
94 // Check if table 'database_startpoint' has proper values
95 $res = db_query_params ('SELECT * FROM database_startpoint', array()) ;
96 if (!$res) { // db error
97 return $this->setError("DB-ERROR-3: ".db_error()."\n");
98 } else if (db_numrows($res) == 0) { // table 'database_startpoint' is empty
99 return $this->setError("Table 'database_startpoint' is empty, run startpoint.php first.");
100 } else { // get the start date from the db
101 return (int) db_result($res, 0, 'db_start_date');
106 private function _runScript($file) {
107 // If a condition statement if found, then run the script only if true.
108 $content = file($file);
109 if (preg_match('/^-- TRUE\? (.+)/', $content[0], $match)) {
110 $res = db_query_params($match[1], array());
111 if (db_result($res,0,0) == 'f') {
115 $res = db_query_from_file($file);
118 db_free_result($res);
119 $res = db_next_result();
122 return $this->setError(_('Database initialisation error:').' '.db_error());
127 private static function _getScripts($dir) {
130 if ($dh = opendir($dir)) {
131 while (($file = readdir($dh)) !== false) {
132 $pos = strrpos($file, '.');
133 if ($pos !== false && $pos > 0) {
134 $name = substr($file, 0, $pos);
135 if (strlen($name) >= 8) {
136 $date_aux = substr($name, 0, 8);
137 $type_aux = substr($file, $pos + 1);
138 if ((int) $date_aux > 20000000 && ($type_aux=='sql' || $type_aux=='php')
139 && strpos($file, 'debian') === false) {
140 $data[] = array('date'=>$date_aux, 'filename'=>$file, 'ext'=>$type_aux);
147 usort($data, array('DatabaseInstaller', '_compare_scripts'));
153 private static function _compare_scripts($a, $b) {
154 return strcmp($a['filename'], $b['filename']);