26 error_reporting(E_ALL);
27 if ((php_sapi_name() !=
'cli')) {
28 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
31 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
32 if (empty($SYSTEM_ROOT)) {
33 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
37 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
38 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
42 $DELETING_ASSET_TYPE = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
43 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
45 $root_user =& $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
48 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
49 echo
"ERROR: Failed loggin in as root user\n";
53 define(
'SQ_FOREIGN_KEY_INTEGRITY_CHECK_OUTPUT_WIDTH', 72);
74 'status_changed_userid',
83 'table' =>
'sq_ast_lnk_tree',
90 'table' =>
'sq_ast_attr',
98 'table' =>
'sq_ast_lnk',
106 'table' =>
'sq_ast_url',
113 'type_code' => Array(
114 'table' =>
'sq_ast_typ',
129 var $tables = Array();
140 if (!$this->
load()) {
155 return $this->tables;
171 if (!in_array($table, $this->
tables())) {
175 $db =& $GLOBALS[
'SQ_SYSTEM']->db;
176 $dbtype = $this->_getDbType();
179 $sql =
"SELECT column_name FROM user_tab_cols WHERE table_name = '".strtoupper($table).
"'";
182 $sql =
"select column_name from information_schema.columns WHERE table_name='".$table.
"'";
187 foreach ($results as $column) {
188 $column_name = array_get_index($column,
'column_name',
'');
189 if (!empty($column_name)) {
190 $columns[] = strtolower($column_name);
210 $columns = $this->
columns($table);
212 foreach ($this->
fks as $fk => $fk_info) {
214 $fk_columns = $fk_info[
'match'];
217 $match_fks = array_intersect($fk_columns, $columns);
218 if (empty($match_fks))
continue;
221 foreach ($match_fks as $fk) {
222 $fks[$fk] = $fk_info;
238 $db =& $GLOBALS[
'SQ_SYSTEM']->db;
239 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db');
240 $dbtype = $this->_getDbType();
245 $sql =
"SELECT object_name AS table_name FROM user_objects WHERE object_type = 'TABLE'";
248 $sql =
"SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'";
253 foreach ($results as $table) {
254 $table_name = array_get_index($table,
'table_name',
'');
255 if (!empty($table_name)) {
256 $tables[] = strtolower($table_name);
261 if (empty($this->
tables)) {
278 $tables = $this->
tables();
280 foreach ($tables as $table) {
281 if (substr($table, 0, 6) !=
'sq_ast') {
300 $db =& $GLOBALS[
'SQ_SYSTEM']->db;
301 $dbtype = $this->_getDbType();
304 $fks = $this->
fks($table);
310 foreach ($fks as $fk_field => $fk_info) {
315 case 'inhd_type_code':
318 $where_cond = $fk_field.
" > ''";
321 $where_cond = $fk_field.
"::text > ''";
327 $where_cond =
'CAST ('.$fk_field.
" as int) > 0";
330 $where_cond = $fk_field.
"::int > 0";
333 $sub_sql =
'SELECT '.$fk_field.
' FROM '.$fk_info[
'table'];
334 $sql =
'SELECT '.$fk_field.
' FROM '.$table.
' WHERE '.$where_cond.
' AND '.$fk_field.
' NOT IN ('.$sub_sql.
')';
337 $broken_count = count($assetids);
338 if (!is_array($assetids)) {
342 $this->
printColumn($fk_info[
'table'], $fk_field, $broken_count);
344 if (is_array($assetids) && !empty($assetids)) {
363 echo
"> checking table $table...\n\n";
380 $column_text =
"$table.$column";
381 $count_text =
"$count found";
384 echo str_repeat(
' ', (SQ_FOREIGN_KEY_INTEGRITY_CHECK_OUTPUT_WIDTH - strlen($column_text) - strlen($count_text)));
385 echo $count_text.
"\n";
400 echo
"\t".
'\''.implode(
'\', \
'', $assetids).
'\''.
"\n";
411 private function _getDbType()
413 $dbtype = MatrixDAL::GetDbType();
415 if ($dbtype instanceof PDO) {
416 $dbtype = $dbtype->getAttribute(PDO::ATTR_DRIVER_NAME);
418 return strtolower($dbtype);
427 $foreign_key_checker->checkDatabase();