CoreFiles by mySites.guru
DatabaseModel.php697 lines, 18,007 bytes, from Joomla_4.0.0-alpha7-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha6
md5
69ca0d8ac8fb468ec9fa0d6a19bc4c5b
sha1
097c72cb7db25d81478f415a5a2bc4f006b981c9
sha256
fa773c0c192a26f8a7f6590822385739e039093c71d41e7750ff744fd0e7dd80
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_installer
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Component\Installer\Administrator\Model;

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Schema\ChangeSet;
use Joomla\CMS\Table\Extension;
use Joomla\CMS\Version;
use Joomla\Component\Installer\Administrator\Helper\InstallerHelper;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Database\UTF8MB4SupportInterface;
use Joomla\Registry\Registry;

\JLoader::register('JoomlaInstallerScript', JPATH_ADMINISTRATOR . '/components/com_admin/script.php');

/**
 * Installer Database Model
 *
 * @since  1.6
 */
class DatabaseModel extends InstallerModel
{
	/**
	 * Set the model context
	 *
	 * @var    string
	 *
	 * @since  4.0.0
	 */
	protected $_context = 'com_installer.discover';

	/**
	 * ChangeSet of all extensions
	 *
	 * @var    array
	 *
	 * @since  4.0.0
	 */
	private $changeSetList = array();

	/**
	 * Total of errors
	 *
	 * @var    integer
	 *
	 * @since  4.0.0
	 */
	private $errorCount = 0;

	/**
	 * Constructor.
	 *
	 * @param   array                $config   An optional associative array of configuration settings.
	 * @param   MVCFactoryInterface  $factory  The factory.
	 *
	 * @see     ListModel
	 * @since   4.0.0
	 */
	public function __construct($config = array(), MVCFactoryInterface $factory = null)
	{
		if (empty($config['filter_fields']))
		{
			$config['filter_fields'] = array(
				'update_site_name',
				'name',
				'client_id',
				'client', 'client_translated',
				'status',
				'type', 'type_translated',
				'folder', 'folder_translated',
				'extension_id'
			);
		}

		parent::__construct($config, $factory);
	}

	/**
	 * Method to return the total number of errors in all the extensions, saved in cache.
	 *
	 * @return  integer
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	public function getErrorCount()
	{
		return $this->errorCount;
	}

	/**
	 * Method to populate the schema cache.
	 *
	 * @param   integer  $cid  The extension ID to get the schema for
	 *
	 * @return  void
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	private function fetchSchemaCache($cid = 0)
	{
		// We already have it
		if (array_key_exists($cid, $this->changeSetList))
		{
			return;
		}

		// Add the ID to the state so it can be used for filtering
		if ($cid)
		{
			$this->setState('filter.extension_id', $cid);
		}

		// With the parent::save it can get the limit and we need to make sure it gets all extensions
		$results = $this->_getList($this->getListQuery());

		foreach ($results as $result)
		{
			$errorMessages = array();
			$errorCount    = 0;

			if (strcmp($result->element, 'joomla') === 0)
			{
				$result->element = 'com_admin';

				if (!$this->getDefaultTextFilters())
				{
					$errorMessages[] = Text::_('COM_INSTALLER_MSG_DATABASE_FILTER_ERROR');
					$errorCount++;
				}
			}

			$db        = $this->getDbo();
			$folderTmp = JPATH_ADMINISTRATOR . '/components/' . $result->element . '/sql/updates/';

			// If the extension doesn't follow the standard location for the
			// update sql files we don't support it
			if (!file_exists($folderTmp))
			{
				$installationXML = InstallerHelper::getInstallationXML($result->element, $result->type);
				$folderTmp       = (string) $installationXML->update->schemas->schemapath[0];

				$a = explode('/', $folderTmp);
				array_pop($a);
				$folderTmp = JPATH_ADMINISTRATOR . '/components/' . $result->element . '/' . implode('/', $a);
			}

			$changeSet = new ChangeSet($db, $folderTmp);

			// If the version in the #__schemas is different
			// than the update files, add to problems message
			$schema = $changeSet->getSchema();

			if ($result->version_id !== $schema)
			{
				$errorMessages[] = Text::sprintf('COM_INSTALLER_MSG_DATABASE_SCHEMA_ERROR', $result->version_id, $schema);
				$errorCount++;
			}

			// If the version in the manifest_cache is different than the
			// version in the installation xml, add to problems message
			$compareUpdateMessage = $this->compareUpdateVersion($result);

			if ($compareUpdateMessage)
			{
				$errorMessages[] = $compareUpdateMessage;
				$errorCount++;
			}

			// If there are errors in the database, add to the problems message
			$errors = $changeSet->check();

			$errorsMessage = $this->getErrorsMessage($errors);

			if ($errorsMessage)
			{
				$errorMessages = array_merge($errorMessages, $errorsMessage);
				$errorCount++;
			}

			// Number of database tables Checked and Skipped
			$errorMessages = array_merge($errorMessages, $this->getOtherInformationMessage($changeSet->getStatus()));

			// Set the total number of errors
			$this->errorCount += $errorCount;

			// Collect the extension details
			$this->changeSetList[$result->extension_id] = array(
				'folderTmp'     => $folderTmp,
				'errorsMessage' => $errorMessages,
				'errorsCount'   => $errorCount,
				'results'       => $changeSet->getStatus(),
				'schema'        => $schema,
				'extension'     => $result
			);
		}
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @param   string  $ordering   An optional ordering field.
	 * @param   string  $direction  An optional direction (asc|desc).
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function populateState($ordering = 'name', $direction = 'asc')
	{
		$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
		$this->setState('filter.client_id', $this->getUserStateFromRequest($this->context . '.filter.client_id', 'filter_client_id', null, 'int'));
		$this->setState('filter.type', $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string'));
		$this->setState('filter.folder', $this->getUserStateFromRequest($this->context . '.filter.folder', 'filter_folder', '', 'string'));

		// Prepare the utf8mb4 conversion check table
		$this->prepareUtf8mb4StatusTable();

		parent::populateState($ordering, $direction);
	}

	/**
	 * Fixes database problems.
	 *
	 * @param   array  $cids  List of the selected extensions to fix
	 *
	 * @return  void|boolean
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	public function fix($cids = array())
	{
		$db = $this->getDbo();

		foreach ($cids as $i => $cid)
		{
			// Load the database issues
			$this->fetchSchemaCache($cid);

			$changeSet = $this->changeSetList[$cid];
			$changeSet['changeset'] = new ChangeSet($db, $changeSet['folderTmp']);
			$changeSet['changeset']->fix();

			$this->fixSchemaVersion($changeSet['changeset'], $changeSet['extension']->extension_id);
			$this->fixUpdateVersion($changeSet['extension']->extension_id);

			if ($i === 'com_admin')
			{
				$installer = new \JoomlaInstallerScript;
				$installer->deleteUnexistingFiles();
				$this->fixDefaultTextFilters();

				/*
				 * Finally, if the schema updates succeeded, make sure the database table is
				 * converted to utf8mb4 or, if not suported by the server, compatible to it.
				 */
				$statusArray = $changeSet['changeset']->getStatus();

				if (count($statusArray['error']) == 0)
				{
					$installer->convertTablesToUtf8mb4(false);
				}
			}
		}
	}

	/**
	 * Gets the changeset array.
	 *
	 * @return  array  Array with the information of the versions problems, errors and the extensions itself
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	public function getItems()
	{
		$this->fetchSchemaCache();

		$results = parent::getItems();
		$results = $this->mergeSchemaCache($results);

		return $results;
	}

	/**
	 * Method to get the database query
	 *
	 * @return  \JDatabaseQuery  The database query
	 *
	 * @since   4.0.0
	 */
	protected function getListQuery()
	{
		$db    = $this->getDbo();
		$query = $db->getQuery(true)
			->select(
				$db->quoteName(
					array(
						'extensions.client_id',
						'extensions.element',
						'extensions.extension_id',
						'extensions.folder',
						'extensions.manifest_cache',
						'extensions.name',
						'extensions.type',
						'schemas.version_id'
					)
				)
			)
			->from(
				$db->quoteName(
					'#__schemas',
					'schemas'
				)
			)->join(
				'INNER',
				$db->quoteName(
					'#__extensions', 'extensions'
				) . ' ON (' . $db->quoteName(
					'schemas.extension_id'
				) . ' = ' . $db->quoteName(
					'extensions.extension_id'
				) . ')'
			);

		$type        = $this->getState('filter.type');
		$clientId    = $this->getState('filter.client_id');
		$extensionId = $this->getState('filter.extension_id');
		$folder      = $this->getState('filter.folder');

		if ($type)
		{
			$query->where($db->quoteName('extensions.type') . ' = ' . $db->quote($type));
		}

		if ($clientId != '')
		{
			$query->where($db->quoteName('extensions.client_id') . ' = ' . (int) $clientId);
		}

		if ($extensionId != '')
		{
			$query->where($db->quoteName('extensions.extension_id') . ' = ' . (int) $extensionId);
		}

		if ($folder != '' && in_array($type, array('plugin', 'library', '')))
		{
			$query->where($db->quoteName('extensions.folder') . ' = ' . $db->quote($folder == '*' ? '' : $folder));
		}

		// Process search filter (update site id).
		$search = $this->getState('filter.search');

		if (!empty($search) && stripos($search, 'id:') === 0)
		{
			$query->where($db->quoteName('schemas.extension_id') . ' = ' . (int) substr($search, 3));
		}

		return $query;
	}

	/**
	 * Merge the items that will be visible with the changeSet information in cache
	 *
	 * @param   array  $results  extensions returned from parent::getItems().
	 *
	 * @return  array  the changeSetList of the merged items
	 *
	 * @since   4.0.0
	 */
	protected function mergeSchemaCache($results)
	{
		$changeSetList = $this->changeSetList;
		$finalResults  = array();

		foreach ($results as $result)
		{
			if (array_key_exists($result->extension_id, $changeSetList) && $changeSetList[$result->extension_id])
			{
				$finalResults[] = $changeSetList[$result->extension_id];
			}
		}

		return $finalResults;
	}

	/**
	 * Get version from #__schemas table.
	 *
	 * @param   integer  $extensionId  id of the extensions.
	 *
	 * @return  mixed  the return value from the query, or null if the query fails.
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	public function getSchemaVersion($extensionId)
	{
		$db    = $this->getDbo();
		$query = $db->getQuery(true)
			->select($db->quoteName('version_id'))
			->from($db->quoteName('#__schemas'))
			->where($db->quoteName('extension_id') . ' = ' . (int) $extensionId);
		$db->setQuery($query);

		return $db->loadResult();
	}

	/**
	 * Fix schema version if wrong.
	 *
	 * @param   ChangeSet  $changeSet    Schema change set.
	 * @param   integer    $extensionId  ID of the extensions.
	 *
	 * @return  mixed  string schema version if success, false if fail.
	 *
	 * @throws  \Exception
	 *
	 * @since   4.0.0
	 */
	public function fixSchemaVersion($changeSet, $extensionId)
	{
		// Get correct schema version -- last file in array.
		$schema = $changeSet->getSchema();

		// Check value. If ok, don't do update.
		if ($schema == $this->getSchemaVersion($extensionId))
		{
			return $schema;
		}

		// Delete old row.
		$db = $this->getDbo();
		$query = $db->getQuery(true)
			->delete($db->quoteName('#__schemas'))
			->where($db->quoteName('extension_id') . ' = ' . (int) $extensionId);
		$db->setQuery($query)->execute();

		// Add new row.
		$query->clear()
			->insert($db->quoteName('#__schemas'))
			->columns($db->quoteName('extension_id') . ',' . $db->quoteName('version_id'))
			->values((int) $extensionId . ', ' . $db->quote($schema));
		$db->setQuery($query);

		try
		{
			$db->execute();
		}
		catch (ExecutionFailureException $e)
		{
			return false;
		}

		return $schema;
	}

	/**
	 * Get current version from #__extensions table.
	 *
	 * @param   object  $extension  data from #__extensions of a single extension.
	 *
	 * @return  mixed  string message with the errors with the update version or null if none
	 *
	 * @since   4.0.0
	 */
	public function compareUpdateVersion($extension)
	{
		$updateVersion = json_decode($extension->manifest_cache)->version;

		if ($extension->element === 'com_admin')
		{
			$extensionVersion = JVERSION;
		}
		else
		{
			$installationXML  = InstallerHelper::getInstallationXML($extension->element, $extension->type);
			$extensionVersion = (string) $installationXML->version;
		}

		if (version_compare($extensionVersion, $updateVersion) != 0)
		{
			return Text::sprintf('COM_INSTALLER_MSG_DATABASE_UPDATEVERSION_ERROR', $updateVersion, $extension->name, $extensionVersion);
		}

		return null;
	}

	/**
	 * Get a message of the tables skipped and checked
	 *
	 * @param   array  $status  status of of the update files
	 *
	 * @return  array  Messages with the errors with the update version
	 *
	 * @since   4.0.0
	 */
	private function getOtherInformationMessage($status)
	{
		$problemsMessage = array();
		$problemsMessage[] = Text::sprintf('COM_INSTALLER_MSG_DATABASE_CHECKED_OK', count($status['ok']));
		$problemsMessage[] = Text::sprintf('COM_INSTALLER_MSG_DATABASE_SKIPPED', count($status['skipped']));

		return $problemsMessage;
	}

	/**
	 * Get a message with all errors found in a given extension
	 *
	 * @param   array  $errors  data from #__extensions of a single extension.
	 *
	 * @return  array  List of messages with the errors in the database
	 *
	 * @since   4.0.0
	 */
	private function getErrorsMessage($errors)
	{
		$errorMessages = array();

		foreach ($errors as $line => $error)
		{
			$key             = 'COM_INSTALLER_MSG_DATABASE_' . $error->queryType;
			$messages        = $error->msgElements;
			$file            = basename($error->file);
			$message0        = isset($messages[0]) ? $messages[0] : ' ';
			$message1        = isset($messages[1]) ? $messages[1] : ' ';
			$message2        = isset($messages[2]) ? $messages[2] : ' ';
			$errorMessages[] = Text::sprintf($key, $file, $message0, $message1, $message2);
		}

		return $errorMessages;
	}

	/**
	 * Fix Joomla version in #__extensions table if wrong (doesn't equal \JVersion short version).
	 *
	 * @param   integer  $extensionId  id of the extension
	 *
	 * @return  mixed  string update version if success, false if fail.
	 *
	 * @since   4.0.0
	 */
	public function fixUpdateVersion($extensionId)
	{
		$table = new Extension($this->getDbo());
		$table->load($extensionId);
		$cache = new Registry($table->manifest_cache);
		$updateVersion = $cache->get('version');

		if ($table->get('type') === 'file' && $table->get('element') === 'joomla')
		{
			$extensionVersion = new Version;
			$extensionVersion = $extensionVersion->getShortVersion();
		}
		else
		{
			$installationXML  = InstallerHelper::getInstallationXML($table->get('element'), $table->get('type'));
			$extensionVersion = (string) $installationXML->version;
		}

		if ($updateVersion === $extensionVersion)
		{
			return $updateVersion;
		}

		$cache->set('version', $extensionVersion);
		$table->set('manifest_cache', $cache->toString());

		if ($table->store())
		{
			return $extensionVersion;
		}

		return false;
	}

	/**
	 * For version 2.5.x only
	 * Check if com_config parameters are blank.
	 *
	 * @return  string  default text filters (if any).
	 *
	 * @since   4.0.0
	 */
	public function getDefaultTextFilters()
	{
		$table = new Extension($this->getDbo());
		$table->load($table->find(array('name' => 'com_config')));

		return $table->params;
	}

	/**
	 * For version 2.5.x only
	 * Check if com_config parameters are blank. If so, populate with com_content text filters.
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	private function fixDefaultTextFilters()
	{
		$table = new Extension($this->getDbo());
		$table->load($table->find(array('name' => 'com_config')));

		// Check for empty $config and non-empty content filters.
		if (!$table->params)
		{
			// Get filters from com_content and store if you find them.
			$contentParams = ComponentHelper::getComponent('com_content')->getParams();

			if ($contentParams->get('filters'))
			{
				$newParams = new Registry;
				$newParams->set('filters', $contentParams->get('filters'));
				$table->params = (string) $newParams;
				$table->store();
			}
		}
	}

	/**
	 * Prepare the table to save the status of utf8mb4 conversion
	 * Make sure it contains 1 initialized record if there is not
	 * already exactly 1 record.
	 *
	 * @return  void
	 *
	 * @since   3.5
	 */
	private function prepareUtf8mb4StatusTable()
	{
		$db = Factory::getDbo();

		if (!$db instanceof UTF8MB4SupportInterface)
		{
			return;
		}

		$creaTabSql = 'CREATE TABLE IF NOT EXISTS ' . $db->quoteName('#__utf8_conversion')
			. ' (' . $db->quoteName('converted') . ' tinyint(4) NOT NULL DEFAULT 0'
			. ') ENGINE=InnoDB';

		if ($db->hasUTF8mb4Support())
		{
			$creaTabSql = $creaTabSql
				. ' DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci;';
		}
		else
		{
			$creaTabSql = $creaTabSql
				. ' DEFAULT CHARSET=utf8 DEFAULT COLLATE=utf8_unicode_ci;';
		}

		$db->setQuery($creaTabSql)->execute();

		$db->setQuery('SELECT COUNT(*) FROM ' . $db->quoteName('#__utf8_conversion') . ';');

		$count = $db->loadResult();

		if ($count > 1)
		{
			// Table messed up somehow, clear it
			$db->setQuery('DELETE FROM ' . $db->quoteName('#__utf8_conversion')
				. ';')->execute();
			$db->setQuery('INSERT INTO ' . $db->quoteName('#__utf8_conversion')
				. ' (' . $db->quoteName('converted') . ') VALUES (0);')->execute();
		}
		elseif ($count == 0)
		{
			// Record missing somehow, fix this
			$db->setQuery('INSERT INTO ' . $db->quoteName('#__utf8_conversion')
				. ' (' . $db->quoteName('converted') . ') VALUES (0);')->execute();
		}
	}
}

The same content is in 4 releases, Joomla 4.0.0-alpha7 to 4.0.0-alpha10. See them all, or every version of this file.