-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconventionist.php
159 lines (144 loc) · 7.26 KB
/
conventionist.php
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
<?php
use MintyPHP\DB;
// Use default autoload implementation
require 'vendor/autoload.php';
// Load the config parameters
require 'config/config.php';
echo Conventionist::html();
class Conventionist
{
public static function html()
{$errors = static::check();
$str = "<h1>Conventionist</h1>";
$str .= "<table cellpadding=\"4\"><tr><th>#</th><th>type</th><th>table</th><th>field</th><th>message</th></tr>";
foreach ($errors as $i => $e) {
$str .= "<tr><td>" . ($i + 1) . ".</td><td>$e[type]</td><td>$e[table]</td><td>$e[field]</td><td>$e[message]</td></tr>";
}
$str .= "</table>";
return $str;
}
public static function text()
{$errors = static::check();
$str = "";
foreach ($errors as $i => $e) {
$str .= <<<END_OF_STR
#$i
type : $e[type]
table : $e[table]
field : $e[field]
message: $e[message]
END_OF_STR;
}
return $str;
}
private static function check()
{$tables = DB::select("SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_COLLATION FROM information_schema.TABLES WHERE table_schema=DATABASE() AND table_name NOT like '%_history' AND table_name NOT like 'history'");
$foreign_keys = DB::selectPairs("select concat(table_name, '.', column_name) as 'foreign_key', concat(referenced_table_name, '.', referenced_column_name) as 'references' from information_schema.key_column_usage where referenced_table_name is not null and table_schema=DATABASE()");
$errors = array();
$fieldsets = array();
$tableNames = array();
foreach ($tables as $i => $table) {$tables[$i] = $table['TABLES'];
$tableNames[] = $table['TABLES']['TABLE_NAME'];
}
for ($i = 0; $i < count($tableNames); $i++) {$table = $tables[$i]['TABLE_NAME'];
$fields = DB::select("SELECT COLUMN_NAME,COLUMN_KEY,EXTRA FROM information_schema.COLUMNS WHERE table_schema=DATABASE() and table_name = ?", $table);
foreach ($fields as $j => $field) {$fields[$j] = $field['COLUMNS'];
}
// table checks:
if (!preg_match('/^[a-z0-9_]+$/i', $table, $matches)) {$errors[] = array('type' => 'error', 'table' => $table, 'message' => 'invalid table name');
}
if (!preg_match('/^[a-z_]+$/', $table, $matches)) {$errors[] = array('type' => 'warning', 'table' => $table, 'message' => 'invalid table name');
}
if ($tables[$i]['TABLE_TYPE'] == "BASE TABLE" && $tables[$i]['ENGINE'] != "InnoDB") {$errors[] = array('type' => 'error', 'table' => $table, 'message' => 'type must be InnoDB');
}
if (!preg_match('/^utf8/i', $tables[$i]['TABLE_COLLATION'], $matches)) {$errors[] = array('type' => 'warning', 'table' => $table, 'message' => 'collation should be utf8');
}
// column checks:
$pk = false;
for ($j = 0; $j < count($fields); $j++) {$field = $fields[$j]['COLUMN_NAME'];
if (!preg_match('/^[a-z0-9_]+$/i', $field, $matches)) {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'invalid field name');
}
if (!preg_match('/^[a-z_]+$/', $field, $matches)) {$errors[] = array('type' => 'warning', 'table' => $table, 'field' => $field, 'message' => 'invalid field name');
}
if ($field == 'id') {$pk = true;
if ($fields[$j]['COLUMN_KEY'] != 'PRI') {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'must be primary key');
}
if ($fields[$j]['EXTRA'] != 'auto_increment') {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'must auto increment');
}
} else if (preg_match('/_id$/', $field, $matches)) {if ($fields[$j]['COLUMN_KEY'] == 'PRI') {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'may not be primary key');
}
if ($fields[$j]['COLUMN_KEY'] == '') {$errors[] = array('type' => 'warning', 'table' => $table, 'field' => $field, 'message' => 'should have index');
}
if ($fields[$j]['EXTRA'] == 'auto_increment') {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'may not auto increment');
}
$otherTable = static::pluralize(preg_replace('/_id$/', '', $field));
if (!in_array($otherTable, $tableNames)) {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => "table '$otherTable' should exist");
}
if (!isset($foreign_keys["$table.$field"])) {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => 'must be foreign key');
} else if ($foreign_keys["$table.$field"] != "$otherTable.id") {$errors[] = array('type' => 'error', 'table' => $table, 'field' => $field, 'message' => "must be foreign key to '$otherTable.id'");
}
}
}
if (!$pk) {
$errors[] = array('type' => 'error', 'table' => $table, 'field' => 'id', 'message' => 'must exist');
}
}
return $errors;
}
//code from Paul Osman: http://blog.eval.ca/2007/03/03/php-pluralize-method/
//who translated it from Ruby from the Rails Inflector class
private static function pluralize($string)
{$plural = array(
array('/(quiz)$/i', "$1zes"),
array('/^(ox)$/i', "$1en"),
array('/([m|l])ouse$/i', "$1ice"),
array('/(matr|vert|ind)ix|ex$/i', "$1ices"),
array('/(x|ch|ss|sh)$/i', "$1es"),
array('/([^aeiouy]|qu)y$/i', "$1ies"),
array('/([^aeiouy]|qu)ies$/i', "$1y"),
array('/(hive)$/i', "$1s"),
array('/(?:([^f])fe|([lr])f)$/i', "$1$2ves"),
array('/sis$/i', "ses"),
array('/([ti])um$/i', "$1a"),
array('/(buffal|tomat)o$/i', "$1oes"),
array('/(bu)s$/i', "$1ses"),
array('/(alias|status)$/i', "$1es"),
array('/(octop|vir)us$/i', "$1i"),
array('/(ax|test)is$/i', "$1es"),
array('/s$/i', "s"),
array('/$/', "s"),
);
$irregular = array(
array('move', 'moves'),
array('sex', 'sexes'),
array('child', 'children'),
array('man', 'men'),
array('person', 'people'),
);
$uncountable = array(
'sheep',
'fish',
'series',
'species',
'money',
'rice',
'information',
'equipment',
);
// save some time in the case that singular and plural are the same
if (in_array(strtolower($string), $uncountable)) {
return $string;
}
// check for irregular singular forms
foreach ($irregular as $noun) {if (strtolower($string) == $noun[0]) {
return $noun[1];
}
}
// check for matches using regular expressions
foreach ($plural as $pattern) {if (preg_match($pattern[0], $string)) {
return preg_replace($pattern[0], $pattern[1], $string);
}
}
return $string;
}
}