Exemplo n.º 1
0
 /**
  * Create the extract from the DCA or the database.sql files
  * 
  * @throws \Exception If the table information could not be loaded
  */
 protected function createExtract()
 {
     // Load the DataContainer
     if (!isset($GLOBALS['loadDataContainer'][$this->strTable])) {
         $this->loadDataContainer($this->strTable);
     }
     $blnFromFile = false;
     $arrRelations = array();
     foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $field => $config) {
         // Check whether all fields have an SQL definition
         if (!isset($config['sql']) && isset($config['inputType'])) {
             $blnFromFile = true;
         }
         // Check whether there is a relation
         if (isset($config['foreignKey']) && isset($config['relation'])) {
             $table = substr($config['foreignKey'], 0, strrpos($config['foreignKey'], '.'));
             $arrRelations[$field] = array_merge(array('table' => $table, 'field' => 'id'), $config['relation']);
         }
     }
     $sql = $GLOBALS['TL_DCA'][$this->strTable]['config']['sql'];
     $fields = $GLOBALS['TL_DCA'][$this->strTable]['fields'];
     // Add the default engine and charset if none is given
     if (!isset($sql['engine'])) {
         $sql['engine'] = 'MyISAM';
     }
     if (!isset($sql['charset'])) {
         $sql['charset'] = 'utf8';
     }
     // Get the SQL information from the database.sql files (backwards compatibility)
     if ($blnFromFile) {
         if (!isset(static::$arrSql[$this->strTable])) {
             static::$arrSql = $this->getFromFile();
         }
         $arrTable = static::$arrSql[$this->strTable];
         // Meta
         list($engine, , $charset) = explode(' ', trim($arrTable['TABLE_OPTIONS']));
         $sql['engine'] = str_replace('ENGINE=', '', $engine);
         $sql['charset'] = str_replace('CHARSET=', '', $charset);
         // Fields
         if (isset($arrTable['TABLE_FIELDS'])) {
             foreach ($arrTable['TABLE_FIELDS'] as $k => $v) {
                 $fields[$k]['sql'] = str_replace('`' . $k . '` ', '', $v);
             }
         }
         // Keys
         if (isset($arrTable['TABLE_CREATE_DEFINITIONS'])) {
             foreach ($arrTable['TABLE_CREATE_DEFINITIONS'] as $strKey) {
                 $strKey = preg_replace('/^([A-Z]+ )?KEY .+\\(`([^`]+)`\\)$/', '$2 $1', $strKey);
                 list($field, $type) = explode(' ', $strKey);
                 $sql['keys'][$field] = $type != '' ? strtolower($type) : 'index';
             }
         }
     }
     // Not all information could be loaded
     if (!is_array($sql) || !is_array($fields)) {
         throw new \Exception('Could not load the table information of ' . $this->strTable);
     }
     // Create the file
     $objFile = new \File('system/cache/sql/' . $this->strTable . '.php');
     $objFile->write("<?php\n\n");
     // Meta
     $objFile->append("\$this->arrMeta = array\n(");
     $objFile->append("\t'engine' => '{$sql['engine']}',");
     $objFile->append("\t'charset' => '{$sql['charset']}',");
     $objFile->append(');', "\n\n");
     // Fields
     $objFile->append("\$this->arrFields = array\n(");
     foreach ($fields as $field => $config) {
         if (!isset($config['sql']) && !isset($config['inputType'])) {
             continue;
         }
         $objFile->append("\t'{$field}' => \"{$config['sql']}\",");
     }
     $objFile->append(');', "\n\n");
     // Keys
     if (is_array($sql['keys']) && !empty($sql['keys'])) {
         $objFile->append("\$this->arrKeys = array\n(");
         foreach ($sql['keys'] as $field => $type) {
             $objFile->append("\t'{$field}' => '{$type}',");
         }
         $objFile->append(');', "\n\n");
     }
     // Relations
     if (!empty($arrRelations)) {
         $objFile->append("\$this->arrRelations = array\n(");
         foreach ($arrRelations as $field => $config) {
             $objFile->append("\t'{$field}' => array\n\t(");
             foreach ($config as $k => $v) {
                 $objFile->append("\t\t'{$k}'=>'{$v}',");
             }
             $objFile->append("\t),");
         }
         $objFile->append(');', "\n");
     }
     $objFile->close();
     // Include the file so the class properties are filled
     include TL_ROOT . '/system/cache/sql/' . $this->strTable . '.php';
 }
 /**
  * Create the extract from the DCA or the database.sql files
  */
 protected function createExtract()
 {
     // Load the default language file (see #7202)
     if (empty($GLOBALS['TL_LANG']['MSC'])) {
         System::loadLanguageFile('default');
     }
     // Load the data container
     if (!isset($GLOBALS['loadDataContainer'][$this->strTable])) {
         $this->loadDataContainer($this->strTable);
     }
     // Return if the DC type is "File"
     if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dataContainer'] == 'File') {
         return;
     }
     $blnFromFile = false;
     $arrRelations = array();
     // Check whether there are fields (see #4826)
     if (isset($GLOBALS['TL_DCA'][$this->strTable]['fields'])) {
         foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $field => $config) {
             // Check whether all fields have an SQL definition
             if (!isset($config['sql']) && isset($config['inputType'])) {
                 $blnFromFile = true;
             }
             // Check whether there is a relation (see #6524)
             if (isset($config['relation'])) {
                 $table = substr($config['foreignKey'], 0, strrpos($config['foreignKey'], '.'));
                 $arrRelations[$field] = array_merge(array('table' => $table, 'field' => 'id'), $config['relation']);
                 // Table name and field name are mandatory
                 if (empty($arrRelations[$field]['table']) || empty($arrRelations[$field]['field'])) {
                     throw new \Exception('Incomplete relation defined for ' . $this->strTable . '.' . $field);
                 }
             }
         }
     }
     $sql = $GLOBALS['TL_DCA'][$this->strTable]['config']['sql'] ?: array();
     $fields = $GLOBALS['TL_DCA'][$this->strTable]['fields'] ?: array();
     // Get the SQL information from the database.sql files (backwards compatibility)
     if ($blnFromFile) {
         if (!isset(static::$arrSql[$this->strTable])) {
             $objInstaller = new \Database\Installer();
             static::$arrSql = $objInstaller->getFromFile();
         }
         $arrTable = static::$arrSql[$this->strTable];
         list($engine, , $charset) = explode(' ', trim($arrTable['TABLE_OPTIONS']));
         if ($engine != '') {
             $sql['engine'] = str_replace('ENGINE=', '', $engine);
         }
         if ($charset != '') {
             $sql['charset'] = str_replace('CHARSET=', '', $charset);
         }
         // Fields
         if (isset($arrTable['TABLE_FIELDS'])) {
             foreach ($arrTable['TABLE_FIELDS'] as $k => $v) {
                 $fields[$k]['sql'] = str_replace('`' . $k . '` ', '', $v);
             }
         }
         // Keys
         if (isset($arrTable['TABLE_CREATE_DEFINITIONS'])) {
             foreach ($arrTable['TABLE_CREATE_DEFINITIONS'] as $strKey) {
                 if (preg_match('/^([A-Z]+ )?KEY .+\\(([^)]+)\\)$/', $strKey, $arrMatches) && preg_match_all('/`([^`]+)`/', $arrMatches[2], $arrFields)) {
                     $type = trim($arrMatches[1]);
                     $field = implode(',', $arrFields[1]);
                     $sql['keys'][$field] = $type != '' ? strtolower($type) : 'index';
                 }
             }
         }
     }
     // Not a database table or no field information
     if (empty($sql) || empty($fields)) {
         return;
     }
     // Add the default engine and charset if none is given
     if (empty($sql['engine'])) {
         $sql['engine'] = 'MyISAM';
     }
     if (empty($sql['charset'])) {
         $sql['charset'] = \Config::get('dbCharset');
     }
     // Meta
     $this->arrMeta = array('engine' => $sql['engine'], 'charset' => $sql['charset']);
     // Fields
     if (!empty($fields)) {
         $this->arrFields = array();
         $this->arrOrderFields = array();
         foreach ($fields as $field => $config) {
             if (isset($config['sql'])) {
                 $this->arrFields[$field] = $config['sql'];
             }
             // Only add order fields of binary fields (see #7785)
             if ($config['inputType'] == 'fileTree' && isset($config['eval']['orderField'])) {
                 $this->arrOrderFields[] = $config['eval']['orderField'];
             }
             if (isset($config['eval']['unique']) && $config['eval']['unique']) {
                 $this->arrUniqueFields[] = $field;
             }
         }
     }
     // Keys
     if (!empty($sql['keys']) && is_array($sql['keys'])) {
         $this->arrKeys = array();
         foreach ($sql['keys'] as $field => $type) {
             $this->arrKeys[$field] = $type;
             if ($type == 'unique') {
                 $this->arrUniqueFields[] = $field;
             }
         }
     }
     // Relations
     if (!empty($arrRelations)) {
         $this->arrRelations = array();
         foreach ($arrRelations as $field => $config) {
             $this->arrRelations[$field] = array();
             foreach ($config as $k => $v) {
                 $this->arrRelations[$field][$k] = $v;
             }
         }
     }
     $this->arrUniqueFields = array_unique($this->arrUniqueFields);
     $this->blnIsDbTable = true;
 }
Exemplo n.º 3
0
 /**
  * Create the extract from the DCA or the database.sql files
  */
 protected function createExtract()
 {
     // Load the default language file (see #7202)
     if (empty($GLOBALS['TL_LANG']['MSC'])) {
         System::loadLanguageFile('default');
     }
     // Load the data container
     if (!isset($GLOBALS['loadDataContainer'][$this->strTable])) {
         $this->loadDataContainer($this->strTable);
     }
     // Return if the DC type is "File"
     if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dataContainer'] == 'File') {
         return;
     }
     $blnFromFile = false;
     $arrRelations = array();
     // Check whether there are fields (see #4826)
     if (isset($GLOBALS['TL_DCA'][$this->strTable]['fields'])) {
         foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $field => $config) {
             // Check whether all fields have an SQL definition
             if (!isset($config['sql']) && isset($config['inputType'])) {
                 $blnFromFile = true;
             }
             // Check whether there is a relation (see #6524)
             if (isset($config['relation'])) {
                 $table = substr($config['foreignKey'], 0, strrpos($config['foreignKey'], '.'));
                 $arrRelations[$field] = array_merge(array('table' => $table, 'field' => 'id'), $config['relation']);
                 // Table name and field name are mandatory
                 if (empty($arrRelations[$field]['table']) || empty($arrRelations[$field]['field'])) {
                     throw new \Exception('Incomplete relation defined for ' . $this->strTable . '.' . $field);
                 }
             }
         }
     }
     $sql = $GLOBALS['TL_DCA'][$this->strTable]['config']['sql'] ?: array();
     $fields = $GLOBALS['TL_DCA'][$this->strTable]['fields'] ?: array();
     // Deprecated since Contao 4.0, to be removed in Contao 5.0
     if ($blnFromFile) {
         trigger_error('Using database.sql files has been deprecated and will no longer work in Contao 5.0. Use a DCA file instead.', E_USER_DEPRECATED);
         if (!isset(static::$arrSql[$this->strTable])) {
             try {
                 /** @var SplFileInfo[] $files */
                 $files = \System::getContainer()->get('contao.resource_locator')->locate('config/database.sql', null, false);
             } catch (\InvalidArgumentException $e) {
                 return array();
             }
             $arrSql = array();
             foreach ($files as $file) {
                 $arrSql = array_merge_recursive($arrSql, \SqlFileParser::parse($file));
             }
             static::$arrSql = $arrSql;
         }
         $arrTable = static::$arrSql[$this->strTable];
         list($engine, , $charset) = explode(' ', trim($arrTable['TABLE_OPTIONS']));
         if ($engine != '') {
             $sql['engine'] = str_replace('ENGINE=', '', $engine);
         }
         if ($charset != '') {
             $sql['charset'] = str_replace('CHARSET=', '', $charset);
         }
         // Fields
         if (isset($arrTable['TABLE_FIELDS'])) {
             foreach ($arrTable['TABLE_FIELDS'] as $k => $v) {
                 $fields[$k]['sql'] = str_replace('`' . $k . '` ', '', $v);
             }
         }
         // Keys
         if (isset($arrTable['TABLE_CREATE_DEFINITIONS'])) {
             foreach ($arrTable['TABLE_CREATE_DEFINITIONS'] as $strKey) {
                 if (preg_match('/^([A-Z]+ )?KEY .+\\(([^)]+)\\)$/', $strKey, $arrMatches) && preg_match_all('/`([^`]+)`/', $arrMatches[2], $arrFields)) {
                     $type = trim($arrMatches[1]);
                     $field = implode(',', $arrFields[1]);
                     $sql['keys'][$field] = $type != '' ? strtolower($type) : 'index';
                 }
             }
         }
     }
     // Not a database table or no field information
     if (empty($sql) || empty($fields)) {
         return;
     }
     // Add the default engine and charset if none is given
     if (empty($sql['engine'])) {
         $sql['engine'] = 'MyISAM';
     }
     if (empty($sql['charset'])) {
         $sql['charset'] = \Config::get('dbCharset');
     }
     // Meta
     $this->arrMeta = array('engine' => $sql['engine'], 'charset' => $sql['charset']);
     // Fields
     if (!empty($fields)) {
         $this->arrFields = array();
         $this->arrOrderFields = array();
         foreach ($fields as $field => $config) {
             if (isset($config['sql'])) {
                 $this->arrFields[$field] = $config['sql'];
             }
             // Only add order fields of binary fields (see #7785)
             if (isset($config['inputType']) && $config['inputType'] == 'fileTree' && isset($config['eval']['orderField'])) {
                 $this->arrOrderFields[] = $config['eval']['orderField'];
             }
             if (isset($config['eval']['unique']) && $config['eval']['unique']) {
                 $this->arrUniqueFields[] = $field;
             }
         }
     }
     // Keys
     if (!empty($sql['keys']) && is_array($sql['keys'])) {
         $this->arrKeys = array();
         foreach ($sql['keys'] as $field => $type) {
             $this->arrKeys[$field] = $type;
             if ($type == 'unique') {
                 $this->arrUniqueFields[] = $field;
             }
         }
     }
     // Relations
     if (!empty($arrRelations)) {
         $this->arrRelations = array();
         foreach ($arrRelations as $field => $config) {
             $this->arrRelations[$field] = array();
             foreach ($config as $k => $v) {
                 $this->arrRelations[$field][$k] = $v;
             }
         }
     }
     $this->arrUniqueFields = array_unique($this->arrUniqueFields);
     $this->blnIsDbTable = true;
 }