protected function generateSchema($app_id, $tables = array())
 {
     $plugin_id = false;
     if (strpos($app_id, '/') !== false) {
         list($app_id, $plugin_id) = explode('/', $app_id, 2);
         $path = wa()->getConfig()->getAppsPath($app_id, 'plugins/' . $plugin_id . '/lib/config/db.php');
     } else {
         $path = wa()->getConfig()->getAppsPath($app_id, 'lib/config/db.php');
     }
     if (waRequest::param('update') !== null) {
         $schema = (include $path);
         if (!$tables) {
             $tables = array_keys($schema);
         }
     } elseif ($tables) {
         if (!is_array($tables)) {
             $tables = array($tables);
         }
     } else {
         $prefix = $app_id == 'webasyst' ? 'wa' : $app_id;
         if ($plugin_id) {
             $prefix .= '_' . $plugin_id;
         }
         // @todo: use db adapter to get tables
         $sql = "SHOW TABLES LIKE '" . $prefix . "\\_%'";
         $tables = $this->model->query($sql)->fetchAll(null, true);
         $sql = "SHOW TABLES LIKE '" . $prefix . "'";
         $tables = array_merge($tables, $this->model->query($sql)->fetchAll(null, true));
     }
     $schema = array();
     foreach ($tables as $t) {
         echo $t . "\n";
         try {
             $schema[$t] = $this->model->describe($t, 1);
         } catch (waDbException $ex) {
             print "\tError: " . $ex->getMessage() . "\n";
         }
     }
     if ($schema) {
         // save schema to lib/config/db.php of the app
         waUtils::varExportToFile($this->schemaToString($schema), $path, false);
     }
 }
 protected function generateSchema($app_id, $tables = array())
 {
     $plugin_id = false;
     if (strpos($app_id, '/') !== false) {
         list($app_id, $plugin_id) = explode('/', $app_id, 2);
         $path = wa()->getConfig()->getAppsPath($app_id, 'plugins/' . $plugin_id . '/lib/config/db.php');
     } else {
         $path = wa()->getConfig()->getAppsPath($app_id, 'lib/config/db.php');
     }
     $exists_schema = array();
     if (file_exists($path)) {
         $schema = (include $path);
         $exists_schema = $schema;
         $exists_tables = array_keys($schema);
     } else {
         $exists_tables = array();
     }
     $exclude_patterns = array();
     if (!in_array($this->getParam('update'), array('all', 'none'))) {
         if (!$tables) {
             $tables = $exists_tables;
         }
     } elseif ($tables) {
         if (!is_array($tables)) {
             $tables = array($tables);
         }
         //TODO add pattern support
     } else {
         $exclude = array();
         if ($app_id == 'webasyst') {
             $prefix = 'wa';
         } else {
             $prefix = $app_id;
             if ($plugin_id) {
                 $prefix .= '_' . $plugin_id;
             } else {
                 if (SystemConfig::isDebug()) {
                     $plugins = waFiles::listdir(wa()->getConfig()->getAppsPath($app_id, 'plugins/'));
                     foreach ($plugins as $_id => $plugin_id) {
                         if (!preg_match('@^[a-z][a-z0-9_]+$@', $plugin_id)) {
                             unset($plugins[$_id]);
                         }
                     }
                     $plugins = array_values($plugins);
                 } else {
                     $plugins = wa($app_id)->getConfig()->getPlugins();
                     $plugins = array_keys($plugins);
                 }
                 foreach ($plugins as $plugin_id) {
                     if ($this->getParam('ignore') == 'pattern') {
                         $plugin_prefix = $app_id . '_' . $plugin_id;
                         if (in_array($plugin_prefix, $exists_tables)) {
                             print sprintf("Warning: Plugin %s has conflicted table namespace\n", $plugin_id);
                         }
                         $exclude = array_merge($exclude, $this->getTables($plugin_prefix));
                     } elseif ($this->getParam('ignore') == 'config') {
                         $plugin_path = wa()->getConfig()->getAppsPath($app_id, 'plugins/' . $plugin_id . '/lib/config/db.php');
                         if (file_exists($plugin_path)) {
                             $exclude_tables = (include $plugin_path);
                             if (is_array($exclude_tables)) {
                                 $exclude = array_merge($exclude, array_keys($exclude_tables));
                             }
                         }
                     }
                     $exclude_patterns[] = sprintf('@(^%1$s$|^%1$s_|_%1$s$)@', $plugin_id);
                 }
             }
         }
         $tables = $this->getTables($prefix);
         $tables = array_diff($tables, $exclude);
     }
     print str_repeat('-', 120) . "\n";
     echo sprintf("%s\n", $app_id);
     $schema = array();
     if ($exists_tables || $tables) {
         $max = max(array_map('strlen', array_merge($exists_tables, array_keys($tables), array('12345678'))));
         $format = "%s|%-{$max}s|%8s|%30s|%30s\n";
         print str_repeat('-', 120) . "\n";
         echo sprintf($format, 'S', 'TABLE', 'STATUS', 'FIELDS', 'KEYS');
         print str_repeat('-', 120) . "\n";
         foreach ($tables as $t) {
             try {
                 $schema[$t] = $this->model->describe($t, 1);
                 if (in_array($t, $exists_tables)) {
                     $compare = $this->compareTable($schema[$t], $exists_schema[$t], $exclude_patterns);
                 } else {
                     $compare = array('s' => '+', 'c' => 'ADDED');
                 }
             } catch (waDbException $ex) {
                 $compare = array('s' => '!', 'c' => 'ERROR: ' . $ex->getMessage());
             }
             if ($compare['s'] !== '=' || $this->getParam('view') == 'all') {
                 echo sprintf($format, $compare['s'], $t, $compare['c'], ifset($compare['r']['FIELDS']), ifset($compare['r']['KEYS']));
             }
         }
         foreach ($exists_tables as $t) {
             if (!isset($schema[$t])) {
                 $s = '-';
                 $c = 'DELETED';
                 echo sprintf($format, $s, $t, $c, '', '');
             }
         }
     } else {
         echo sprintf("There no tables for %s\n", $app_id);
     }
     print str_repeat('-', 120) . "\n";
     if ($schema && $this->getParam('update') != 'none') {
         echo sprintf("Schema saved for %s\n", $app_id);
         if ($exclude_patterns) {
             foreach ($schema as &$table_schema) {
                 $table_schema = $this->cleanupSchema($table_schema, $exclude_patterns);
             }
             unset($table_schema);
         }
         // save schema to lib/config/db.php of the app
         waUtils::varExportToFile($this->schemaToString($schema), $path, false);
     }
 }