|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017, Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\App\CodeChecker; |
|
25
|
|
|
|
|
26
|
|
|
class DatabaseSchemaChecker { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $appId |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public function analyse($appId) { |
|
33
|
|
|
$appPath = \OC_App::getAppPath($appId); |
|
34
|
|
|
if ($appPath === false) { |
|
35
|
|
|
throw new \RuntimeException("No app with given id <$appId> known."); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!file_exists($appPath . '/appinfo/database.xml')) { |
|
39
|
|
|
return ['errors' => [], 'warnings' => []]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
libxml_use_internal_errors(true); |
|
43
|
|
|
$loadEntities = libxml_disable_entity_loader(false); |
|
44
|
|
|
$xml = simplexml_load_file($appPath . '/appinfo/database.xml'); |
|
45
|
|
|
libxml_disable_entity_loader($loadEntities); |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$errors = $warnings = []; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($xml->table as $table) { |
|
51
|
|
|
// Table names |
|
52
|
|
|
if (strpos($table->name, '*dbprefix*') !== 0) { |
|
53
|
|
|
$errors[] = 'Database schema error: name of table ' . $table->name . ' does not start with *dbprefix*'; |
|
54
|
|
|
} |
|
55
|
|
|
$tableName = substr($table->name, strlen('*dbprefix*')); |
|
56
|
|
|
if (strpos($tableName, '*dbprefix*') !== false) { |
|
57
|
|
|
$warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . $table->name; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (strlen($tableName) > 27) { |
|
61
|
|
|
$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$hasAutoIncrement = false; |
|
65
|
|
|
|
|
66
|
|
|
// Column names |
|
67
|
|
|
foreach ($table->declaration->field as $column) { |
|
68
|
|
|
if (strpos($column->name, '*dbprefix*') !== false) { |
|
69
|
|
|
$warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . $column->name . ' on table ' . $table->name; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
if (strlen($column->name) > 30) { |
|
73
|
|
|
$errors[] = 'Database schema error: Name of column ' . $column->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($column->autoincrement) { |
|
77
|
|
|
if ($hasAutoIncrement) { |
|
78
|
|
|
$errors[] = 'Database schema error: Table ' . $table->name . ' has multiple autoincrement columns'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (strlen($tableName) > 21) { |
|
82
|
|
|
$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$hasAutoIncrement = true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Index names |
|
90
|
|
|
foreach ($table->declaration->index as $index) { |
|
91
|
|
|
$hasPrefix = strpos($index->name, '*dbprefix*'); |
|
92
|
|
|
if ($hasPrefix !== false && $hasPrefix !== 0) { |
|
93
|
|
|
$warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . $index->name . ' on table ' . $table->name; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$indexName = $hasPrefix === 0 ? substr($index->name, strlen('*dbprefix*')) : $index->name; |
|
97
|
|
View Code Duplication |
if (strlen($indexName) > 27) { |
|
98
|
|
|
$errors[] = 'Database schema error: Name of index ' . $index->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed'; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return ['errors' => $errors, 'warnings' => $warnings]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|