function testRelationsdefined() { require_code('relations'); if (in_safe_mode()) { $this->assertTrue(false, 'Cannot work in safe mode'); return; } /* Actually only done for complex ones $all_tables=$GLOBALS['SITE_DB']->query('SELECT DISTINCT m_table FROM '.get_table_prefix().'db_meta WHERE m_type LIKE \''.db_encode_like('%AUTO_LINK%').'\' ORDER BY m_table'); $table_descriptions=get_table_descriptions(); foreach ($all_tables as $t) { $this->assertFalse(!array_key_exists($t['m_table'],$table_descriptions),'Table not described: '.$t['m_table']); }*/ $all_links = $GLOBALS['SITE_DB']->query('SELECT m_table,m_name FROM ' . get_table_prefix() . 'db_meta WHERE m_type LIKE \'' . db_encode_like('%AUTO_LINK%') . '\' ORDER BY m_table'); $links = get_relation_map(); foreach ($all_links as $l) { $_l = $l['m_table'] . '.' . $l['m_name']; $this->assertFalse(!array_key_exists($_l, $links), 'Link not described: ' . $_l); } }
/* ocPortal Copyright (c) ocProducts, 2004-2012 See text/EN/licence.txt for full licencing information. NOTE TO PROGRAMMERS: Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes **** */ /* Used to generate a database schema in the form of SQL code that can be imported into MySQL Workbench First run this, then import it all into a new database (existing is problematic as it needs to be InnoDB), then run SQLEditor on that database -- or if you like try your luck importing, but that was crashing for me. */ $filename = 'ocportal-erd.sql'; if (!isset($_GET['testing'])) { header('Content-Type: application/octet-stream' . '; authoritative=true;'); if (strstr(ocp_srv('HTTP_USER_AGENT'), 'MSIE') !== false) { header('Content-Disposition: filename="' . str_replace(chr(13), '', str_replace(chr(10), '', addslashes($filename))) . '"'); } else { header('Content-Disposition: attachment; filename="' . str_replace(chr(13), '', str_replace(chr(10), '', addslashes($filename))) . '"'); } } else { header('Content-type: text/plain'); } require_code('relations'); $relation_map = get_relation_map(); $tables = get_all_tables(); echo get_innodb_table_sql($tables, $tables); $GLOBALS['SCREEN_TEMPLATE_CALLED'] = ''; exit;
function get_innodb_table_sql($tables, $all_tables) { $out = ''; $relations = array(); $relation_map = get_relation_map(); $i = 0; $table_prefix = get_table_prefix(); for ($loop_it = 0; $loop_it < count($tables); $loop_it++) { $tables_keys = array_keys($tables); $tables_values = array_values($tables); $table = $tables_keys[$loop_it]; $fields = $tables_values[$loop_it]; $_i = strval($i); $out .= "\t\tCREATE TABLE {$table_prefix}{$table}\n\t\t(\n"; $keys = array(); $type_remap = get_innodb_data_types(); foreach ($fields as $field => $type) { $_type = $type_remap[str_replace(array('*', '?'), array('', ''), $type)]; $nullness = strpos($type, '*') !== false ? 'NULL' : 'NOT NULL'; $out .= "\t\t\t{$field} {$_type} {$nullness},\n"; if (strpos($type, '*') !== false) { $keys[] = $field; } if (strpos($type, 'AUTO_LINK') !== false || array_key_exists($table . '.' . $field, $relation_map)) { $relations[$table . '.' . $field] = $relation_map[$table . '.' . $field]; } if (strpos($type, 'USER') !== false) { $relations[$table . '.' . $field] = 'f_members.id'; } if (strpos($type, 'GROUP') !== false) { $relations[$table . '.' . $field] = 'f_groups.id'; } if (strpos($type, 'TRANS') !== false) { $relations[$table . '.' . $field] = 'translate.id'; } if (strpos($field, 'author') !== false && $type == 'ID_TEXT' && $table != 'authors' && $field != 'block_author' && $field != 'module_author') { $relations[$table . '.' . $field] = 'authors.author'; } if (isset($relations[$table . '.' . $field])) { $mapped_table = preg_replace('#\\..*$#', '', $relations[$table . '.' . $field]); if (!isset($tables[$mapped_table])) { $tables[$mapped_table] = $all_tables[$mapped_table]; } } } $out .= "\t\t\tPRIMARY KEY ("; foreach ($keys as $it => $key) { if ($it != 0) { $out .= ','; } $out .= $key; } $out .= ")\n\t\t) TYPE=InnoDB;\n\n"; $i++; } foreach ($relations as $from => $to) { $from_table = preg_replace('#\\..*$#', '', $from); $to_table = preg_replace('#\\..*$#', '', $to); $from_field = preg_replace('#^.*\\.#', '', $from); $to_field = preg_replace('#^.*\\.#', '', $to); $source_id = strval(array_search($from_table, array_keys($tables))); $target_id = strval(array_search($to_table, array_keys($tables))); $out .= "\n\t\tCREATE INDEX `{$from}` ON {$table_prefix}{$from_table}({$from_field});\n\t\tALTER TABLE {$table_prefix}{$from_table} ADD FOREIGN KEY `{$from}` ({$from_field}) REFERENCES {$table_prefix}{$to_table} ({$to_field});\n"; } return $out; }