Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#28003] Multidb install #122

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
Move functionality to the base class
  • Loading branch information
elkuku committed Jan 12, 2012
commit 582e817167832be67a83cf0766e3fc964a633120
179 changes: 142 additions & 37 deletions libraries/cms/database/installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,29 @@ abstract class JDatabaseInstaller
*/
public static function getInstance(JObject $options)
{
$className = 'JDatabaseInstaller' . ucfirst($options->db_type);
$type = $options->db_type;

$type = ('mysqli' == $type) ? 'mysql' : $type;

$className = 'JDatabaseInstaller' . ucfirst($type);

if (!class_exists($className))
{
throw new JDatabaseInstallerException(sprintf('Required class %s not found', $className));
}

return new $className($options);
}

/**
* Pre check the database.
*
* @return JDatabaseInstaller
* Constructor.
*
* @throws JDatabaseInstallerException
* @param JObject $options Database install options.
*/
abstract public function preCheck();
protected function __construct(JObject $options)
{
$this->options = $options;
}

/**
* Check the database.
Expand All @@ -62,38 +72,41 @@ abstract public function check();
abstract public function createDatabase();

/**
* Update the database.
*
* @return JDatabaseInstallerMysql
* Method to set the database character set to UTF-8.
*
* @throws JDatabaseInstallerException
* @return JDatabaseInstaller
*/
abstract public function update();
abstract public function setDatabaseCharset();

/**
* Apply localization options.
* Update the database.
*
* @return JDatabaseInstaller
* @return JDatabaseInstallerMysql
*
* @throws JDatabaseInstallerException
*/
abstract public function localize();
abstract protected function updateDatabase();

/**
* Method to set the database character set to UTF-8.
* Get the type name.
*
* @return JDatabaseInstaller
* @return string
*/
abstract public function setDatabaseCharset();
protected function getType()
{
return $this->options->db_type;
}

/**
* Method to get a JDatabase object.
*
* @param string $select Should the database be selected.
* @param boolean $select Should the database be selected.
*
* @return JDatabase object on success, JException on error.
*
* @since
*/
protected function getDbo($select = false)
public function getDbo($select = false)
{
static $db = null;

Expand Down Expand Up @@ -126,7 +139,7 @@ protected function getDbo($select = false)
public function clean()
{
// Should any old database tables be removed or backed up?
if ($this->options->db_old == 'remove')
if ('remove' == $this->options->db_old)
{
// Attempt to delete the old database tables.
$this->deleteDatabase();
Expand Down Expand Up @@ -244,22 +257,56 @@ protected function deleteDatabase()
*/
public function populate()
{
// Set the appropriate schema script based on UTF-8 support.
$dbType = $this->options->db_type;

$dbType = ($dbType == 'mysqli') ? 'mysql' : $dbType;

// Check utf8 support.
$backward = ($this->getDbo()->hasUTF()) ? '' : '_backward';

$schema = 'sql/' . $dbType . '/joomla' . $backward . '.sql';
$schema = JPATH_INSTALLATION . '/sql/' . $this->getType() . '/joomla' . $backward . '.sql';

// Attempt to import the database schema.
$this->populateDatabase($schema);

return $this;
}

/**
* Update the database.
*
* @return JDatabaseInstaller
*
* @throws JDatabaseInstallerException
*/
public function update()
{
$db = $this->getDbo();

// Attempt to refresh manifest caches
$query = $db->getQuery(true);

$query->from('#__extensions');
$query->select('*');

$extensions = $db->setQuery($query)->loadObjectList();

JFactory::$database = $db;

$installer = JInstaller::getInstance();

foreach ($extensions as $extension)
{
if (!$installer->refreshManifestCache($extension->extension_id))
{
throw new JDatabaseInstallerException(
JText::sprintf('INSTL_DATABASE_COULD_NOT_REFRESH_MANIFEST_CACHE', $extension->name)
);
}
}

// Perform the update in extending classes
$this->updateDatabase();

return $this;
}

/**
* Install the sample data.
*
Expand All @@ -270,11 +317,7 @@ public function populate()
public function installSampleData()
{
// Build the path to the sample data file.
$type = $this->options->db_type;

$type = ($type == 'mysqli') ? 'mysql' : $type;

$data = JPATH_INSTALLATION . '/sql/' . $type . '/' . $this->options->sample_file;
$data = JPATH_INSTALLATION . '/sql/' . $this->getType() . '/' . $this->options->sample_file;

// Attempt to import the database schema.
if (!file_exists($data))
Expand All @@ -288,6 +331,59 @@ public function installSampleData()
return $this;
}

/**
* Apply localization options.
*
* @return JDatabaseInstaller
*/
public function localize()
{
$db = $this->getDbo();

// Load the localise.sql for translating the data in joomla.sql/joomla_backwards.sql
$path = 'sql/' . $this->getType() . '/localise.sql';

if (JFile::exists($path))
{
$this->populateDatabase($path);
}

// Handle default backend language setting. This feature is available for localized versions of Joomla 1.5.
$languages = JFactory::getApplication()->getLocaliseAdmin($db);

if (in_array($this->options->language, $languages['admin'])
|| in_array($this->options->language, $languages['site']))
{
// Build the language parameters for the language manager.
$params = array();

// Set default administrator/site language to sample data values:
$params['administrator'] = 'en-GB';
$params['site'] = 'en-GB';

if (in_array($this->options->language, $languages['admin']))
{
$params['administrator'] = $this->options->language;
}

if (in_array($this->options->language, $languages['site']))
{
$params['site'] = $this->options->language;
}

$params = json_encode($params);

// Update the language settings in the language manager.
$db->setQuery(
'UPDATE ' . $db->quoteName('#__extensions') .
' SET ' . $db->quoteName('params') . ' = ' . $db->Quote($params) .
' WHERE ' . $db->quoteName('element') . '=\'com_languages\''
)->query();
}

return $this;
}

/**
* Method to import a database schema from a file.
*
Expand All @@ -302,16 +398,21 @@ protected function populateDatabase($schema)
// Initialise variables.
$db = $this->getDbo(true);

if (!file_exists($schema))
{
// @todo filesystemexception
throw new JDatabaseInstallerException(JText::_('INSTL_DATABASE_SCHEMA_FILE_NOT_FOUND'));
}

// Get the contents of the schema file.
if (!$buffer = file_get_contents($schema))
{
// @todo filesystemexception
throw new JDatabaseInstallerException(JText::_('INSTL_DATABASE_SCHEMA_FILE_READ_ERROR'));
}

// Get an array of queries from the schema and process them.
$queries = $this->splitQueries($buffer);

foreach ($queries as $query)
foreach ($this->splitQueries($buffer) as $query)
{
// Trim any whitespace.
$query = trim($query);
Expand All @@ -320,8 +421,7 @@ protected function populateDatabase($schema)
if (!empty($query) && ($query{0} != '#'))
{
// Execute the query.
$db->setQuery($query);
$db->query();
$db->setQuery($query)->query();
}
}

Expand All @@ -336,6 +436,8 @@ protected function populateDatabase($schema)
* @return array Queries to perform.
*
* @since 1.0
*
* @todo Can JDatabase::splitQuery() do the same ¿
*/
protected function splitQueries($sql)
{
Expand Down Expand Up @@ -365,14 +467,17 @@ protected function splitQueries($sql)
$in_string = false;
}
elseif (!$in_string && ($sql[$i] == '"' || $sql[$i] == "'")
&& (!isset ($buffer[0]) || $buffer[0] != "\\"))
&& (!isset ($buffer[0]) || $buffer[0] != "\\")
)
{
$in_string = $sql[$i];
}

if (isset ($buffer[1]))
{
$buffer[0] = $buffer[1];
}

$buffer[1] = $sql[$i];
}

Expand Down
Loading