Source for file database.php
Documentation is available at database.php
* @package Joomla.Installation
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Database configuration model for the Joomla Core Installer.
* @package Joomla.Installation
protected static $userId =
0;
* @return integer The user ID
$randUserId =
$session->get('randUserId');
// Create the ID for the root user only once and store in session
$session->set('randUserId', $randUserId);
$session =
JFactory::getSession();
$session->set('randUserId', self::$userId);
* Retrieves the default user ID and sets it if necessary
* @return integer The user ID
self::$userId =
self::generateRandUserId();
* Method to initialise the database
* @param array $options The options to use for configuration
* @return boolean True on success
/* @var InstallationApplicationWeb $app */
$app =
JFactory::getApplication();
// Get the options as a object for easier handling.
// Load the back-end language files so that the DB error messages work
$currentLang =
$lang->getTag();
// Load the selected language
// Pre-load en-GB in case the chosen language files do not exist
// Ensure a database type was selected.
if (empty($options->db_type))
$app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_TYPE'), 'notice');
// Ensure that a hostname and user name were input.
if (empty($options->db_host) ||
empty($options->db_user))
$app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_DB_DETAILS'), 'notice');
// Ensure that a database name was input.
if (empty($options->db_name))
$app->enqueueMessage(JText::_('INSTL_DATABASE_EMPTY_NAME'), 'notice');
// Validate database table prefix.
if (!preg_match('#^[a-zA-Z]+[a-zA-Z0-9_]*$#', $options->db_prefix))
$app->enqueueMessage(JText::_('INSTL_DATABASE_PREFIX_INVALID_CHARS'), 'notice');
// Validate length of database table prefix.
if (strlen($options->db_prefix) >
15)
$app->enqueueMessage(JText::_('INSTL_DATABASE_FIX_TOO_LONG'), 'notice');
// Validate length of database name.
if (strlen($options->db_name) >
64)
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_TOO_LONG'), 'notice');
// Get a database object.
$options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix, $options->db_select
catch
(RuntimeException $e)
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice');
* Method to create a new database.
* @param array $options The configuration options
* @return boolean True on success.
/* @var InstallationApplicationWeb $app */
// Disable autoselect database before it's created
if (isset
($options['db_select']))
$tmpSelect =
$options['db_select'];
$options['db_select'] =
false;
// Get the options as a object for easier handling.
// Check database version.
$type =
$options->db_type;
$db_version =
$db->getVersion();
catch
(RuntimeException $e)
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice');
if (!$db->isMinimumVersion())
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_INVALID_' .
strtoupper($type) .
'_VERSION', $db_version), 'notice');
if (($type ==
'mysql') ||
($type ==
'mysqli'))
// @internal MySQL versions pre 5.1.6 forbid . / or \ or NULL
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_INVALID_NAME', $db_version), 'notice');
// @internal Check for spaces in beginning or end of name
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_INVALID_SPACES'), 'notice');
// @internal Check for asc(00) Null in name
if (strpos($options->db_name, chr(00)) !==
false)
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_INVALID_CHAR'), 'notice');
// PostgreSQL database older than version 9.0.0 needs to run 'CREATE LANGUAGE' to create function.
if (($options->db_type ==
'postgresql') &&
(version_compare($db_version, '9.0.0', '<')))
$db->setQuery("CREATE LANGUAGE plpgsql");
// Get database's UTF support
$utfSupport =
$db->hasUTFSupport();
// Try to select the database
$db->select($options->db_name);
catch
(RuntimeException $e)
// If the database could not be selected, attempt to create it and then select it.
if ($this->createDB($db, $options, $utfSupport))
$db->select($options->db_name);
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_ERROR_CREATE', $options->db_name), 'notice');
$options = (array)
$options;
foreach ($options as $i =>
$option)
if (isset
($i['1']) &&
$i['1'] ==
'*')
$options =
array_merge(array('db_created' =>
1), $options);
// Restore autoselect value after database creation
$options['db_select'] =
$tmpSelect;
$session->set('setup.options', $options);
* Method to process the old database
* @param array $options The options array
* @return boolean True on success
if (!isset
($options['db_created']) ||
!$options['db_created'])
// Get the options as a object for easier handling.
// Set the character set to UTF-8 for pre-existing databases.
// Should any old database tables be removed or backed up?
if ($options->db_old ==
'remove')
// Attempt to delete the old database tables.
// Message queued by method, simply return
// If the database isn't being deleted, back it up.
* Method to create the database tables
* @param array $options The options array
* @return boolean True on success
/* @var InstallationApplicationWeb $app */
if (!isset
($options['db_created']) ||
!$options['db_created'])
// Get the options as a object for easier handling.
$type =
$options->db_type;
// Set the character set to UTF-8 for pre-existing databases.
// Set the appropriate schema script based on UTF-8 support.
if ($type ==
'mysqli' ||
$type ==
'mysql')
$schema =
'sql/mysql/joomla.sql';
elseif ($type ==
'sqlsrv' ||
$type ==
'sqlazure')
$schema =
'sql/sqlazure/joomla.sql';
$schema =
'sql/' .
$type .
'/joomla.sql';
// Check if the schema is a valid file
$app->enqueueMessage(JText::sprintf('INSTL_ERROR_DB', JText::_('INSTL_DATABASE_NO_SCHEMA')), 'notice');
// Attempt to import the database schema.
// Attempt to update the table #__schema.
if ($type ==
'mysqli' ||
$type ==
'mysql')
elseif ($type ==
'sqlsrv' ||
$type ==
'sqlazure')
$pathPart .=
'sqlazure/';
$pathPart .=
$type .
'/';
$app->enqueueMessage(JText::_('INSTL_ERROR_INITIALISE_SCHEMA'), 'notice');
foreach ($files as $file)
$query =
$db->getQuery(true)
->insert($db->quoteName('#__schemas'))
$db->quoteName('extension_id'),
$db->quoteName('version_id')
->values('700, ' .
$db->quote($version));
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'notice');
// Attempt to refresh manifest caches
$extensions =
$db->loadObjectList();
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'notice');
$installer =
JInstaller::getInstance();
foreach ($extensions as $extension)
if (!$installer->refreshManifestCache($extension->extension_id))
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_REFRESH_MANIFEST_CACHE', $extension->name), 'notice');
// Load the localise.sql for translating the data in joomla.sql
if ($type ==
'mysqli' ||
$type ==
'mysql')
$dblocalise =
'sql/mysql/localise.sql';
elseif ($type ==
'sqlsrv' ||
$type ==
'sqlazure')
$dblocalise =
'sql/sqlazure/localise.sql';
$dblocalise =
'sql/' .
$type .
'/localise.sql';
// Handle default backend language setting. This feature is available for localized versions of Joomla.
$languages =
$app->getLocaliseAdmin($db);
if (in_array($options->language, $languages['admin']) ||
in_array($options->language, $languages['site']))
// Build the language parameters for the language manager.
// Set default administrator/site language to sample data values:
$params['administrator'] =
'en-GB';
$params['site'] =
'en-GB';
if (in_array($options->language, $languages['admin']))
$params['administrator'] =
$options->language;
if (in_array($options->language, $languages['site']))
$params['site'] =
$options->language;
// Update the language settings in the language manager.
->update($db->quoteName('#__extensions'))
->set($db->quoteName('params') .
' = ' .
$db->quote($params))
->where($db->quoteName('element') .
' = ' .
$db->quote('com_languages'));
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'notice');
* Method to install the sample data
* @param array $options The options array
* @return boolean True on success
/* @var InstallationApplicationWeb $app */
if (!isset
($options['db_created']) ||
!$options['db_created'])
// Get the options as a object for easier handling.
// Build the path to the sample data file.
$type =
$options->db_type;
elseif ($type ==
'sqlsrv')
// Attempt to import the database schema if one is chosen.
if ($options->sample_file !=
'')
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_FILE_DOES_NOT_EXIST', $data), 'notice');
* Method to update the user id of the sample data content to the new rand user id
* @param JDatabaseDriver $db Database connector object $db*
// Create the ID for the root user
$userId =
self::getUserId();
// Update all created_by field of the tables with the random user id
// categories (created_user_id), contact_details, content, newsfeeds, weblinks
'categories' =>
'created_user_id',
'contact_details' =>
'created_by',
'content' =>
'created_by',
'newsfeeds' =>
'created_by',
'weblinks' =>
'created_by',
foreach ($updates_array as $table =>
$field)
'UPDATE ' .
$db->quoteName('#__' .
$table) .
' SET ' .
$db->quoteName($field) .
' = ' .
$db->quote($userId)
* Method to backup all tables in a database with a given prefix.
* @param JDatabaseDriver $db JDatabaseDriver object.
* @param string $prefix Database table prefix.
* @return boolean True on success.
$backup =
'bak_' .
$prefix;
// Get the tables in the database.
$tables =
$db->getTableList();
foreach ($tables as $table)
// If the table uses the given prefix, back it up.
if (strpos($table, $prefix) ===
0)
// Drop the backup table.
$db->dropTable($backupTable, true);
catch
(RuntimeException $e)
// Rename the current table to the backup table.
$db->renameTable($table, $backupTable, $backup, $prefix);
catch
(RuntimeException $e)
* Method to create a new database.
* @param JDatabaseDriver $db JDatabase object.
* @param JObject $options JObject coming from "initialise" function to pass user
* and database name to database driver.
* @param boolean $utf True if the database supports the UTF-8 character set.
* @return boolean True on success.
public function createDB($db, $options, $utf)
// Build the create database query.
// Run the create database query.
$db->createDatabase($options, $utf);
catch
(RuntimeException $e)
// If an error occurred return false.
* Method to delete all tables in a database with a given prefix.
* @param JDatabaseDriver $db JDatabaseDriver object.
* @param string $prefix Database table prefix.
* @return boolean True on success.
// Get the tables in the database.
$tables =
$db->getTableList();
foreach ($tables as $table)
// If the table uses the given prefix, drop it.
if (strpos($table, $prefix) ===
0)
catch
(RuntimeException $e)
* Method to import a database schema from a file.
* @param JDatabaseDriver $db JDatabase object.
* @param string $schema Path to the schema file.
* @return boolean True on success.
/* @var InstallationApplicationWeb $app */
// Get the contents of the schema file.
$app->enqueueMessage($db->getErrorMsg(), 'notice');
// Get an array of queries from the schema and process them.
foreach ($queries as $query)
// If the query isn't empty and is not a MySQL or PostgreSQL comment, execute it.
if (!empty($query) &&
($query{0} !=
'#') &&
($query{0} !=
'-'))
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'notice');
* Method to set the database character set to UTF-8.
* @param JDatabaseDriver $db JDatabaseDriver object.
* @param string $name Name of the database to process.
* @return boolean True on success.
// Run the create database query.
$db->setQuery($db->getAlterDbCharacterSet($name));
catch
(RuntimeException $e)
* Method to split up queries from a schema file into an array.
* @param string $query SQL schema.
* @return array Queries to perform.
// Remove PostgreSQL comment lines.
$funct =
explode('CREATE OR REPLACE FUNCTION', $query);
// Save sql before function and parse it
// Parse the schema file to break up queries.
for ($i =
0; $i <
strlen($query) -
1; $i++
)
if ($query[$i] ==
";" &&
!$in_string)
$queries[] =
substr($query, 0, $i);
$query =
substr($query, $i +
1);
if ($in_string &&
($query[$i] ==
$in_string) &&
$buffer[1] !=
"\\")
elseif (!$in_string &&
($query[$i] ==
'"' ||
$query[$i] ==
"'") &&
(!isset
($buffer[0]) ||
$buffer[0] !=
"\\"))
// If the is anything left over, add it to the queries.
// Add function part as is
for ($f =
1; $f <
count($funct); $f++
)
$queries[] =
'CREATE OR REPLACE FUNCTION ' .
$funct[$f];
Documentation generated on Tue, 19 Nov 2013 14:57:50 +0100 by phpDocumentor 1.4.3