- md5
- c3691b4cc592aadf38fa1f3de07adefd
- sha1
- a5977390cf5619412b54194a914af7d24c6f2d95
- sha256
- 5d187a570ab9b2f1d56e935923a0fa458ebf95cf61a8d5abc22241f473777332
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
<?php
/**
* @package fof
* @copyright 2014 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU GPL version 3 or later
*/
class FOFDatabaseInstaller
{
/** @var JDatabase The database connector object */
private $db = null;
/**
* @var FOFInput Input variables
*/
protected $input = array();
/** @var string The directory where the XML schema files are stored */
private $xmlDirectory = null;
/** @var array A list of the base names of the XML schema files */
public $xmlFiles = array('mysql', 'mysqli', 'pdomysql', 'postgresql', 'sqlsrv', 'mssql');
/**
* Public constructor
*
* @param array $config The configuration array
*/
public function __construct($config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
// Get the input
if (array_key_exists('input', $config))
{
if ($config['input'] instanceof FOFInput)
{
$this->input = $config['input'];
}
else
{
$this->input = new FOFInput($config['input']);
}
}
else
{
$this->input = new FOFInput;
}
// Set the database object
if (array_key_exists('dbo', $config))
{
$this->db = $config['dbo'];
}
else
{
$this->db = FOFPlatform::getInstance()->getDbo();
}
// Set the $name/$_name variable
$component = $this->input->getCmd('option', 'com_foobar');
if (array_key_exists('option', $config))
{
$component = $config['option'];
}
// Figure out where the XML schema files are stored
if (array_key_exists('dbinstaller_directory', $config))
{
$this->xmlDirectory = $config['dbinstaller_directory'];
}
else
{
// Nothing is defined, assume the files are stored in the sql/xml directory inside the component's administrator section
$directories = FOFPlatform::getInstance()->getComponentBaseDirs($component);
$this->setXmlDirectory($directories['admin'] . '/sql/xml');
}
// Do we have a set of XML files to look for?
if (array_key_exists('dbinstaller_files', $config))
{
$files = $config['dbinstaller_files'];
if (!is_array($files))
{
$files = explode(',', $files);
}
$this->xmlFiles = $files;
}
}
/**
* Sets the directory where XML schema files are stored
*
* @param string $xmlDirectory
*/
public function setXmlDirectory($xmlDirectory)
{
$this->xmlDirectory = $xmlDirectory;
}
/**
* Returns the directory where XML schema files are stored
*
* @return string
*/
public function getXmlDirectory()
{
return $this->xmlDirectory;
}
/**
* Creates or updates the database schema
*
* @return void
*
* @throws Exception When a database query fails and it doesn't have the canfail flag
*/
public function updateSchema()
{
// Get the schema XML file
$xml = $this->findSchemaXml();
if (empty($xml))
{
return;
}
// Make sure there are SQL commands in this file
if (!$xml->sql)
{
return;
}
// Walk the sql > action tags to find all tables
$tables = array();
/** @var SimpleXMLElement $actions */
$actions = $xml->sql->children();
/** @var SimpleXMLElement $action */
foreach ($actions as $action)
{
// Get the attributes
$attributes = $action->attributes();
// Get the table / view name
$table = $attributes->table ? $attributes->table : '';
if (empty($table))
{
continue;
}
// Am I allowed to let this action fail?
$canFailAction = $attributes->canfail ? $attributes->canfail : 0;
// Evaluate conditions
$shouldExecute = true;
/** @var SimpleXMLElement $node */
foreach ($action->children() as $node)
{
if ($node->getName() == 'condition')
{
// Get the operator
$operator = $node->attributes()->operator ? (string)$node->attributes()->operator : 'and';
$operator = empty($operator) ? 'and' : $operator;
$condition = $this->conditionMet($table, $node);
switch ($operator)
{
case 'not':
$shouldExecute = $shouldExecute && !$condition;
break;
case 'or':
$shouldExecute = $shouldExecute || $condition;
break;
case 'nor':
$shouldExecute = $shouldExecute || !$condition;
break;
case 'xor':
$shouldExecute = ($shouldExecute xor $condition);
break;
case 'maybe':
$shouldExecute = $condition ? true : $shouldExecute;
break;
default:
$shouldExecute = $shouldExecute && $condition;
break;
}
}
if (!$shouldExecute)
{
break;
}
}
// Make sure all conditions are met
if (!$shouldExecute)
{
continue;
}
// Execute queries
foreach ($action->children() as $node)
{
if ($node->getName() == 'query')
{
$canFail = $node->attributes->canfail ? $node->attributes->canfail : $canFailAction;
$this->db->setQuery((string) $node);
try
{
if (version_compare(JVERSION, '3.1', 'lt'))
{
$handlers = array(
E_NOTICE => JError::getErrorHandling(E_NOTICE),
E_WARNING => JError::getErrorHandling(E_WARNING),
E_ERROR => JError::getErrorHandling(E_ERROR),
);
$handlers[E_NOTICE]['options'] = isset($handlers[E_NOTICE]['options']) ? $handlers[E_NOTICE]['options'] : null;
$handlers[E_WARNING]['options'] = isset($handlers[E_WARNING]['options']) ? $handlers[E_WARNING]['options'] : null;
$handlers[E_ERROR]['options'] = isset($handlers[E_ERROR]['options']) ? $handlers[E_ERROR]['options'] : null;
JError::setErrorHandling(E_NOTICE, 'ignore');
JError::setErrorHandling(E_WARNING, 'ignore');
JError::setErrorHandling(E_ERROR, 'ignore');
}
$this->db->execute();
if (version_compare(JVERSION, '3.1', 'lt'))
{
JError::setErrorHandling(E_NOTICE, $handlers[E_NOTICE]['mode'], $handlers[E_NOTICE]['options']);
JError::setErrorHandling(E_WARNING, $handlers[E_WARNING]['mode'], $handlers[E_WARNING]['options']);
JError::setErrorHandling(E_ERROR, $handlers[E_ERROR]['mode'], $handlers[E_ERROR]['options']);
}
if (version_compare(JVERSION, '3.1', 'lt') && $this->db->getErrorNum())
{
if (!$canFail)
{
JError::raise(E_WARNING, $this->db->getErrorNum(), $this->db->getErrorMsg());
}
}
/**/
}
catch (Exception $e)
{
// If we are not allowed to fail, throw back the exception we caught
if (!$canFail)
{
throw $e;
}
}
}
}
}
}
/**
* Uninstalls the database schema
*
* @return void
*/
public function removeSchema()
{
// Get the schema XML file
$xml = $this->findSchemaXml();
if (empty($xml))
{
return;
}
// Make sure there are SQL commands in this file
if (!$xml->sql)
{
return;
}
// Walk the sql > action tags to find all tables
$tables = array();
/** @var SimpleXMLElement $actions */
$actions = $xml->sql->children();
/** @var SimpleXMLElement $action */
foreach ($actions as $action)
{
$attributes = $action->attributes();
$tables[] = (string)$attributes->table;
}
// Simplify the tables list
$tables = array_unique($tables);
// Start dropping tables
foreach ($tables as $table)
{
try
{
$this->db->dropTable($table);
}
catch (Exception $e)
{
// Do not fail if I can't drop the table
}
}
}
/**
* Find an suitable schema XML file for this database type and return the SimpleXMLElement holding its information
*
* @return null|SimpleXMLElement Null if no suitable schema XML file is found
*/
protected function findSchemaXml()
{
$driverType = $this->db->name;
$xml = null;
// And now look for the file
foreach ($this->xmlFiles as $baseName)
{
// Remove any accidental whitespace
$baseName = trim($baseName);
// Get the full path to the file
$fileName = $this->xmlDirectory . '/' . $baseName . '.xml';
// Make sure the file exists
if (!@file_exists($fileName))
{
continue;
}
// Make sure the file is a valid XML document
try
{
$xml = new SimpleXMLElement($fileName, LIBXML_NONET, true);
}
catch (Exception $e)
{
$xml = null;
continue;
}
// Make sure the file is an XML schema file
if ($xml->getName() != 'schema')
{
$xml = null;
continue;
}
if (!$xml->meta)
{
$xml = null;
continue;
}
if (!$xml->meta->drivers)
{
$xml = null;
continue;
}
/** @var SimpleXMLElement $drivers */
$drivers = $xml->meta->drivers;
// Strict driver name match
foreach ($drivers->children() as $driverTypeTag)
{
$thisDriverType = (string)$driverTypeTag;
if ($thisDriverType == $driverType)
{
return $xml;
}
}
// Some custom database drivers use a non-standard $name variable. Let try a relaxed match.
foreach ($drivers->children() as $driverTypeTag)
{
$thisDriverType = (string)$driverTypeTag;
if (
// e.g. $driverType = 'mysqlistupid', $thisDriverType = 'mysqli' => driver matched
strpos($driverType, $thisDriverType) === 0
// e.g. $driverType = 'stupidmysqli', $thisDriverType = 'mysqli' => driver matched
|| (substr($driverType, -strlen($thisDriverType)) == $thisDriverType)
)
{
return $xml;
}
}
$xml = null;
}
return $xml;
}
/**
* Checks if a condition is met
*
* @param string $table The table we're operating on
* @param SimpleXMLElement $node The condition definition node
*
* @return bool
*/
protected function conditionMet($table, SimpleXMLElement $node)
{
static $allTables = null;
if (empty($allTables))
{
$allTables = $this->db->getTableList();
}
// Does the table exist?
$tableNormal = $this->db->replacePrefix($table);
$tableExists = in_array($tableNormal, $allTables);
// Initialise
$condition = false;
// Get the condition's attributes
$attributes = $node->attributes();
$type = $attributes->type ? $attributes->type : null;
$value = $attributes->value ? (string) $attributes->value : null;
switch ($type)
{
// Check if a table or column is missing
case 'missing':
$tableName = (string)$table;
$fieldName = (string)$value;
if (empty($fieldName))
{
$condition = !$tableExists;
}
else
{
$tableColumns = $this->db->getTableColumns($tableNormal, true);
$condition = !array_key_exists($fieldName, $tableColumns);
}
break;
// Check if a column type matches the "coltype" attribute
case 'type':
$tableColumns = $this->db->getTableColumns($table, false);
$condition = false;
if (array_key_exists($value, $tableColumns))
{
$coltype = $attributes->coltype ? $attributes->coltype : null;
if (!empty($coltype))
{
$coltype = strtolower($coltype);
$currentType = strtolower($tableColumns[$value]->Type);
$condition = ($coltype == $currentType);
}
}
break;
// Check if the result of a query matches our expectation
case 'equals':
$query = (string)$node;
$this->db->setQuery($query);
try
{
$result = $this->db->loadResult();
$condition = ($result == $value);
}
catch (Exception $e)
{
return false;
}
break;
// Always returns true
case 'true':
return true;
break;
default:
return false;
break;
}
return $condition;
}
}
The same content is in 34 releases, Joomla 3.4.2-rc to 3.6.2. See them all, or every version of this file.