summaryrefslogtreecommitdiff
blob: ddef0b189810461ad7c8cd54007fc1cb59149f03 (plain)
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
#!/usr/bin/php
<?php
// TODO Get rid of interactive merging
require_once(dirname(__FILE__).'/shared/include/includes.php'); // USE __DIR__ in 5.3.0
require_once(SHARED.'/config.php');
require_once(SHARED.'/include/dbinit.php');
function prompt_bool($prompt, $default=true) {
	$yes=$default?'Y':'y';
	$no=$default?'n':'N';
	while (true) {
		echo trim($prompt)." [$yes/$no] ";
		$answer=trim(fgets(STDIN));
		if (strlen($answer) == 0) {
			return $default;
		}
		if (strtolower(substr($answer, 0, 1)) == 'y') {
			return true;
		} elseif (strtolower(substr($answer, 0, 1)) == 'n') {
			return false;
		} else {
			echo "Please respond yes or no.\n";
		}
	}
}
function prompt_string($prompt, $default=null) {
	echo $prompt.' '.($default?"[$default] ":'');
	$answer=trim(fgets(STDIN));
	if (strlen($answer) == 0) {
		if ($default) {
			return $default;
		}
	}
	return $answer;
}
function write_table_class_to_file($table, $class, $file) {
	$stream=fopen($file, 'w');
	fputs($stream, "<?php\n");
	$tmpclass='genclass_'.randstring(30);
	eval ("class $tmpclass extends ".(class_exists($class)?get_parent_class($class):'sql_row_obj')." {\nprotected \$table='$table';\t}");
	$obj=new $tmpclass();
	fputs($stream, str_replace($tmpclass, $class, $obj->to_php()));
	fputs($stream, "?>\n");
	fclose($stream);
}
$pdo=&$S['pdo'];
$r=$pdo->query('SHOW TABLES');
$tables=$r->fetchAll(PDO::FETCH_COLUMN);
$check_existing=prompt_bool('Check existing classes for changes from the database?');
foreach ($tables as $i => $table) {
	if ($class=sql_row_obj::table_to_class($table)) {
		unset($tables[$i]);
		if (!$check_existing) {
			continue;
		}
		if (substr($class, 0, 4) == 'sql_') {
			$origfile=substr($class, 4);
		} else {
			$origfile=$class;
		}
		$origfile=SHARED.'/classes/'.$origfile.'.php';
		if (is_file($origfile)) {
			write_table_class_to_file($table, $class, '.temp_class');
			$lines=count(explode("\n", rtrim(file_get_contents('.temp_class'))))-2;
			$diff=`diff "$origfile" .temp_class`;
			$difftmp=fopen('.temp_diff', 'w');
			$diffname='.temp_diff';
			$domerge=false;
			foreach (explode("\n", $diff) as $line) {
				$line.="\n";
				if (strlen($line) && is_numeric(substr($line, 0, 1))) {
					if (preg_match('/^[0-9]+(?:,[0-9]+)?[a-zA-Z](?:[0-9]+,)?([0-9]+)?\s/', $line, $match)) {
						$endl=$match[1];
						if ($endl > $lines-3) {
							break;
						}
					}
				}
				if (strlen(trim($line))) {
					$domerge=true;
				}
				fputs($difftmp, $line);
			}
			fclose($difftmp);
			if ($domerge) {
				copy($origfile, '.temp_class');
				shell_exec('patch .temp_class < .temp_diff');
				passthru('colordiff -u "'.$origfile.'" .temp_class');
				if (prompt_bool('Keep changes?')) {
					rename('.temp_class', $origfile);
				}
			}
			unlink('.temp_diff');
			if (is_file('.temp_class'))
				unlink('.temp_class'); // TODO unlink others
		}
	}
}
if ($tables) {
	echo 'Found table(s) without corresponding class: '.implode(', ', $tables)."\n";
	if (!prompt_bool('Generate classes for these tables?')) {
		unset($tables);
	}
} else {
	echo "No tables found without corresponding classes.\n";
}
if (!$tables) {
	die("Exiting.\n");
	/*
	echo 'Which tables should we generate classes for? (space-separated) ';
	$tables=explode(' ', trim(fgets(STDIN)));
	if ($tables[0] == '') {
		echo "Exiting.\n";
		exit;
	}
	 */
}
echo 'Generating classes for table(s): '.implode(', ', $tables)."\n";
foreach ($tables as $table) {
	$default='sql_';
	if (substr($table, -1) == 's') {
		$default.=substr($table, 0, strlen($table)-1);
	} else {
		$default.=$table;
	}
	$class=prompt_string('Class name for table '.$table.':', $default);
	if (substr($class, 0, 4) == 'sql_') {
		$default=substr($class, 4);
	} else {
		$default=$class;
	}
	$default.='.php';
	$file=SHARED.'/classes/'.prompt_string('Filename for class '.$class.': '.SHARED.'/classes/', $default);
	if (is_file($file)) {
		if (!prompt_bool($file.' exists. Overwrite?', false)) {
			echo 'Skipping table '.$table."\n";
			continue;
		}
	}
	echo 'Writing '.$file.'...';
	write_table_class_to_file($table, $class, $file);
	echo "done\n";
}
?>