File: DatabaseInstaller.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (187 lines) | stat: -rw-r--r-- 5,181 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
 * FusionForge Database Installer Class
 *
 * Copyright (C) 2010-2011 Alain Peyrat
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

class DatabaseInstaller extends Error {

	/**
	 * __construct - DatabaseInstaller constructor
	 *
	 * @param string $name If set, name of plugin.
	 * @param string $path If set, path of upgrade scripts.
	 */
	function __construct($name = '', $path = '') {
		$this->name = $name;
		$this->path = $path;
	}

	/**
	 * install - Run database install scripts (for plugins)
	 *
	 * @return bool|string Output
	 */
	function install() {
		$name = $this->name;
		$path = $this->path;

		$init = "$path/$name-init.sql";
		if (is_file($init)) {
			$ret = $this->_runScript($init);
			if (!$ret) {
				return false;
			}
			return $this->upgrade($name, $path);
		}
		return $this->setError(_('No database installation scripts found.'));
	}

	/**
	 * upgrade - Upgrade database with upgrade scripts (usually in src/sql)
	 *
	 * @return bool|string Output
	 */
	function upgrade() {
		$name = $this->name;
		$path = $this->path;

		if ($name) {
			$prefix = $name.':';
			$date   = -1;
		} else {
			$prefix = '';
			$date   = $this->getDatabaseDate();
		}

		$scripts = $this->_getScripts($path);
		$output  = '';
		foreach ($scripts as $script) {
			if ((int)$script['date'] > $date) {
				$res = db_query_params('SELECT * FROM database_changes WHERE filename=$1',
					array($prefix.$script['filename']));
				if (!$res) {
					return $this->setError("ERROR-2: ".db_error());
				} elseif (db_numrows($res) == 0) {
					$output .= "Running script: {$script['filename']}\n";
					$result = $this->_runScript($path.'/'.$script['filename']);
					if ($result) {
						$res = db_query_params('INSERT INTO database_changes (filename) VALUES ($1)',
							array($prefix.$script['filename']));
						if (!$res) {
							return $this->setError("ERROR-3: ".db_error());
						}
					} else {
						return false;
					}
				}
			}
		}
		return $output;
	}

	/**
	 * getDatabaseDate - Get status (date format) of the database.
	 *
	 * @return bool|int date if success, false otherwise.
	 */
	private function getDatabaseDate() {
		// Check if table 'database_startpoint' has proper values
		$res = db_query_params('SELECT * FROM database_startpoint', array());
		if (!$res) { // db error
			return $this->setError("DB-ERROR-3: ".db_error()."\n");
		} elseif (db_numrows($res) == 0) { // table 'database_startpoint' is empty
			return $this->setError("Table 'database_startpoint' is empty, run startpoint.php first.");
		}
		return (int)db_result($res, 0, 'db_start_date');
	}

	/**
	 * _runScript - Execute migration script given as argument (file)
	 *
	 * @param string $file Filename
	 * @return bool True if success
	 */
	private function _runScript($file) {
		// If a condition statement if found, then run the script only if true.
		$content = file($file);
		if (preg_match('/^-- TRUE\? (.+)/', $content[0], $match)) {
			$res = db_query_params($match[1], array());
			if (db_result($res, 0, 0) == 'f') {
				return true;
			}
		}
		$res = db_query_from_file($file);
		if ($res) {
			while ($res) {
				db_free_result($res);
				$res = db_next_result();
			}
		} else {
			return $this->setError(_('Database initialisation error:').' '.db_error());
		}
		return true;
	}

	/**
	 * _getScripts - Return a sorted array of available database upgrade scripts.
	 *
	 * @static
	 * @param string $dir
	 * @return array
	 */
	private static function _getScripts($dir) {
		$data = array();
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					$pos = strrpos($file, '.');
					if ($pos !== false && $pos > 0) {
						$name = substr($file, 0, $pos);
						if (strlen($name) >= 8) {
							$date_aux = substr($name, 0, 8);
							$type_aux = substr($file, $pos + 1);
							if ((int)$date_aux > 20000000 && ($type_aux == 'sql' || $type_aux == 'php')
								&& strpos($file, 'debian') === false
							) {
								$data[] = array('date'=> $date_aux, 'filename'=> $file, 'ext'=> $type_aux);
							}
						}
					}
				}
				closedir($dh);
			}
			usort($data, array('DatabaseInstaller', '_compare_scripts'));
			reset($data);
		}
		return $data;
	}

	/**
	 * _compare_scripts - compare script for sorting (using filename)
	 *
	 * @static
	 * @param array $a
	 * @param array $b
	 * @return int
	 */
	private static function _compare_scripts($a, $b) {
		return strcmp($a['filename'], $b['filename']);
	}
}