You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseSchemaChecker.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  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. namespace OC\App\CodeChecker;
  24. class DatabaseSchemaChecker {
  25. /**
  26. * @param string $appId
  27. * @return array
  28. */
  29. public function analyse($appId) {
  30. $appPath = \OC_App::getAppPath($appId);
  31. if ($appPath === false) {
  32. throw new \RuntimeException("No app with given id <$appId> known.");
  33. }
  34. if (!file_exists($appPath . '/appinfo/database.xml')) {
  35. return ['errors' => [], 'warnings' => []];
  36. }
  37. libxml_use_internal_errors(true);
  38. $loadEntities = libxml_disable_entity_loader(false);
  39. $xml = simplexml_load_file($appPath . '/appinfo/database.xml');
  40. libxml_disable_entity_loader($loadEntities);
  41. $errors = $warnings = [];
  42. foreach ($xml->table as $table) {
  43. // Table names
  44. if (strpos((string)$table->name, '*dbprefix*') !== 0) {
  45. $errors[] = 'Database schema error: name of table ' . (string)$table->name . ' does not start with *dbprefix*';
  46. }
  47. $tableName = substr((string)$table->name, strlen('*dbprefix*'));
  48. if (strpos($tableName, '*dbprefix*') !== false) {
  49. $warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . (string)$table->name;
  50. }
  51. if (strlen($tableName) > 27) {
  52. $errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
  53. }
  54. $hasAutoIncrement = false;
  55. // Column names
  56. foreach ($table->declaration->field as $column) {
  57. if (strpos((string)$column->name, '*dbprefix*') !== false) {
  58. $warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . (string)$column->name . ' on table ' . (string)$table->name;
  59. }
  60. if (strlen((string)$column->name) > 30) {
  61. $errors[] = 'Database schema error: Name of column ' . (string)$column->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed';
  62. }
  63. if ($column->autoincrement) {
  64. if ($hasAutoIncrement) {
  65. $errors[] = 'Database schema error: Table ' . (string)$table->name . ' has multiple autoincrement columns';
  66. }
  67. if (strlen($tableName) > 21) {
  68. $errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
  69. }
  70. $hasAutoIncrement = true;
  71. }
  72. }
  73. // Index names
  74. foreach ($table->declaration->index as $index) {
  75. $hasPrefix = strpos((string)$index->name, '*dbprefix*');
  76. if ($hasPrefix !== false && $hasPrefix !== 0) {
  77. $warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . (string)$index->name . ' on table ' . (string)$table->name;
  78. }
  79. $indexName = $hasPrefix === 0 ? substr((string)$index->name, strlen('*dbprefix*')) : (string)$index->name;
  80. if (strlen($indexName) > 27) {
  81. $errors[] = 'Database schema error: Name of index ' . (string)$index->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed';
  82. }
  83. }
  84. }
  85. return ['errors' => $errors, 'warnings' => $warnings];
  86. }
  87. }