| Current Path : /home/digilove/www/41423/ |
| Current File : /home/digilove/www/41423/ContentHistory.php.tar |
home/digilove/public_html/libraries/src/Table/ContentHistory.php 0000644 00000015122 15234500767 0021111 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Table;
defined('JPATH_PLATFORM') or die;
/**
* Content History table.
*
* @since 3.2
*/
class ContentHistory extends Table
{
/**
* Array of object fields to unset from the data object before calculating SHA1 hash. This allows us to detect a meaningful change
* in the database row using the hash. This can be read from the #__content_types content_history_options column.
*
* @var array
* @since 3.2
*/
public $ignoreChanges = array();
/**
* Array of object fields to convert to integers before calculating SHA1 hash. Some values are stored differently
* when an item is created than when the item is changed and saved. This works around that issue.
* This can be read from the #__content_types content_history_options column.
*
* @var array
* @since 3.2
*/
public $convertToInt = array();
/**
* Constructor
*
* @param \JDatabaseDriver $db A database connector object
*
* @since 3.1
*/
public function __construct($db)
{
parent::__construct('#__ucm_history', 'version_id', $db);
$this->ignoreChanges = array(
'modified_by',
'modified_user_id',
'modified',
'modified_time',
'checked_out',
'checked_out_time',
'version',
'hits',
'path',
);
$this->convertToInt = array('publish_up', 'publish_down', 'ordering', 'featured');
}
/**
* Overrides Table::store to set modified hash, user id, and save date.
*
* @param boolean $updateNulls True to update fields even if they are null.
*
* @return boolean True on success.
*
* @since 3.2
*/
public function store($updateNulls = false)
{
$this->set('character_count', strlen($this->get('version_data')));
$typeTable = Table::getInstance('ContentType', 'JTable', array('dbo' => $this->getDbo()));
$typeTable->load($this->ucm_type_id);
if (!isset($this->sha1_hash))
{
$this->set('sha1_hash', $this->getSha1($this->get('version_data'), $typeTable));
}
// Modify author and date only when not toggling Keep Forever
if ($this->get('keep_forever') === null)
{
$this->set('editor_user_id', \JFactory::getUser()->id);
$this->set('save_date', \JFactory::getDate()->toSql());
}
return parent::store($updateNulls);
}
/**
* Utility method to get the hash after removing selected values. This lets us detect changes other than
* modified date (which will change on every save).
*
* @param mixed $jsonData Either an object or a string with json-encoded data
* @param ContentType $typeTable Table object with data for this content type
*
* @return string SHA1 hash on success. Empty string on failure.
*
* @since 3.2
*/
public function getSha1($jsonData, ContentType $typeTable)
{
$object = is_object($jsonData) ? $jsonData : json_decode($jsonData);
if (isset($typeTable->content_history_options) && is_object(json_decode($typeTable->content_history_options)))
{
$options = json_decode($typeTable->content_history_options);
$this->ignoreChanges = isset($options->ignoreChanges) ? $options->ignoreChanges : $this->ignoreChanges;
$this->convertToInt = isset($options->convertToInt) ? $options->convertToInt : $this->convertToInt;
}
foreach ($this->ignoreChanges as $remove)
{
if (property_exists($object, $remove))
{
unset($object->$remove);
}
}
// Convert integers, booleans, and nulls to strings to get a consistent hash value
foreach ($object as $name => $value)
{
if (is_object($value))
{
// Go one level down for JSON column values
foreach ($value as $subName => $subValue)
{
$object->$subName = is_int($subValue) || is_bool($subValue) || $subValue === null ? (string) $subValue : $subValue;
}
}
else
{
$object->$name = is_int($value) || is_bool($value) || $value === null ? (string) $value : $value;
}
}
// Work around empty values
foreach ($this->convertToInt as $convert)
{
if (isset($object->$convert))
{
$object->$convert = (int) $object->$convert;
}
}
if (isset($object->review_time))
{
$object->review_time = (int) $object->review_time;
}
return sha1(json_encode($object));
}
/**
* Utility method to get a matching row based on the hash value and id columns.
* This lets us check to make sure we don't save duplicate versions.
*
* @return string SHA1 hash on success. Empty string on failure.
*
* @since 3.2
*/
public function getHashMatch()
{
$db = $this->_db;
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('sha1_hash') . ' = ' . $db->quote($this->get('sha1_hash')));
$db->setQuery($query, 0, 1);
return $db->loadObject();
}
/**
* Utility method to remove the oldest versions of an item, saving only the most recent versions.
*
* @param integer $maxVersions The maximum number of versions to save. All others will be deleted.
*
* @return boolean true on success, false on failure.
*
* @since 3.2
*/
public function deleteOldVersions($maxVersions)
{
$result = true;
// Get the list of version_id values we want to save
$db = $this->_db;
$query = $db->getQuery(true);
$query->select($db->quoteName('version_id'))
->from($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('keep_forever') . ' != 1')
->order($db->quoteName('save_date') . ' DESC ');
$db->setQuery($query, 0, (int) $maxVersions);
$idsToSave = $db->loadColumn(0);
// Don't process delete query unless we have at least the maximum allowed versions
if (count($idsToSave) === (int) $maxVersions)
{
// Delete any rows not in our list and and not flagged to keep forever.
$query = $db->getQuery(true);
$query->delete($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('version_id') . ' NOT IN (' . implode(',', $idsToSave) . ')')
->where($db->quoteName('keep_forever') . ' != 1');
$db->setQuery($query);
$result = (boolean) $db->execute();
}
return $result;
}
}
home/digilove/public_html/110/libraries/src/Table/ContentHistory.php 0000644 00000015122 15235200112 0021370 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Table;
defined('JPATH_PLATFORM') or die;
/**
* Content History table.
*
* @since 3.2
*/
class ContentHistory extends Table
{
/**
* Array of object fields to unset from the data object before calculating SHA1 hash. This allows us to detect a meaningful change
* in the database row using the hash. This can be read from the #__content_types content_history_options column.
*
* @var array
* @since 3.2
*/
public $ignoreChanges = array();
/**
* Array of object fields to convert to integers before calculating SHA1 hash. Some values are stored differently
* when an item is created than when the item is changed and saved. This works around that issue.
* This can be read from the #__content_types content_history_options column.
*
* @var array
* @since 3.2
*/
public $convertToInt = array();
/**
* Constructor
*
* @param \JDatabaseDriver $db A database connector object
*
* @since 3.1
*/
public function __construct($db)
{
parent::__construct('#__ucm_history', 'version_id', $db);
$this->ignoreChanges = array(
'modified_by',
'modified_user_id',
'modified',
'modified_time',
'checked_out',
'checked_out_time',
'version',
'hits',
'path',
);
$this->convertToInt = array('publish_up', 'publish_down', 'ordering', 'featured');
}
/**
* Overrides Table::store to set modified hash, user id, and save date.
*
* @param boolean $updateNulls True to update fields even if they are null.
*
* @return boolean True on success.
*
* @since 3.2
*/
public function store($updateNulls = false)
{
$this->set('character_count', strlen($this->get('version_data')));
$typeTable = Table::getInstance('ContentType', 'JTable', array('dbo' => $this->getDbo()));
$typeTable->load($this->ucm_type_id);
if (!isset($this->sha1_hash))
{
$this->set('sha1_hash', $this->getSha1($this->get('version_data'), $typeTable));
}
// Modify author and date only when not toggling Keep Forever
if ($this->get('keep_forever') === null)
{
$this->set('editor_user_id', \JFactory::getUser()->id);
$this->set('save_date', \JFactory::getDate()->toSql());
}
return parent::store($updateNulls);
}
/**
* Utility method to get the hash after removing selected values. This lets us detect changes other than
* modified date (which will change on every save).
*
* @param mixed $jsonData Either an object or a string with json-encoded data
* @param ContentType $typeTable Table object with data for this content type
*
* @return string SHA1 hash on success. Empty string on failure.
*
* @since 3.2
*/
public function getSha1($jsonData, ContentType $typeTable)
{
$object = is_object($jsonData) ? $jsonData : json_decode($jsonData);
if (isset($typeTable->content_history_options) && is_object(json_decode($typeTable->content_history_options)))
{
$options = json_decode($typeTable->content_history_options);
$this->ignoreChanges = isset($options->ignoreChanges) ? $options->ignoreChanges : $this->ignoreChanges;
$this->convertToInt = isset($options->convertToInt) ? $options->convertToInt : $this->convertToInt;
}
foreach ($this->ignoreChanges as $remove)
{
if (property_exists($object, $remove))
{
unset($object->$remove);
}
}
// Convert integers, booleans, and nulls to strings to get a consistent hash value
foreach ($object as $name => $value)
{
if (is_object($value))
{
// Go one level down for JSON column values
foreach ($value as $subName => $subValue)
{
$object->$subName = is_int($subValue) || is_bool($subValue) || $subValue === null ? (string) $subValue : $subValue;
}
}
else
{
$object->$name = is_int($value) || is_bool($value) || $value === null ? (string) $value : $value;
}
}
// Work around empty values
foreach ($this->convertToInt as $convert)
{
if (isset($object->$convert))
{
$object->$convert = (int) $object->$convert;
}
}
if (isset($object->review_time))
{
$object->review_time = (int) $object->review_time;
}
return sha1(json_encode($object));
}
/**
* Utility method to get a matching row based on the hash value and id columns.
* This lets us check to make sure we don't save duplicate versions.
*
* @return string SHA1 hash on success. Empty string on failure.
*
* @since 3.2
*/
public function getHashMatch()
{
$db = $this->_db;
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('sha1_hash') . ' = ' . $db->quote($this->get('sha1_hash')));
$db->setQuery($query, 0, 1);
return $db->loadObject();
}
/**
* Utility method to remove the oldest versions of an item, saving only the most recent versions.
*
* @param integer $maxVersions The maximum number of versions to save. All others will be deleted.
*
* @return boolean true on success, false on failure.
*
* @since 3.2
*/
public function deleteOldVersions($maxVersions)
{
$result = true;
// Get the list of version_id values we want to save
$db = $this->_db;
$query = $db->getQuery(true);
$query->select($db->quoteName('version_id'))
->from($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('keep_forever') . ' != 1')
->order($db->quoteName('save_date') . ' DESC ');
$db->setQuery($query, 0, (int) $maxVersions);
$idsToSave = $db->loadColumn(0);
// Don't process delete query unless we have at least the maximum allowed versions
if (count($idsToSave) === (int) $maxVersions)
{
// Delete any rows not in our list and and not flagged to keep forever.
$query = $db->getQuery(true);
$query->delete($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
->where($db->quoteName('version_id') . ' NOT IN (' . implode(',', $idsToSave) . ')')
->where($db->quoteName('keep_forever') . ' != 1');
$db->setQuery($query);
$result = (boolean) $db->execute();
}
return $result;
}
}
home/digilove/public_html/110/libraries/src/Table/Observer/ContentHistory.php 0000644 00000007077 15235427776 0023224 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Table\Observer;
defined('JPATH_PLATFORM') or die;
/**
* Table class supporting modified pre-order tree traversal behavior.
*
* @since 3.2
*/
class ContentHistory extends AbstractObserver
{
/**
* Helper object for storing and deleting version history information associated with this table observer
*
* @var \JHelperContenthistory
* @since 3.2
*/
protected $contenthistoryHelper;
/**
* The pattern for this table's TypeAlias
*
* @var string
* @since 3.2
*/
protected $typeAliasPattern = null;
/**
* Not public, so marking private and deprecated, but needed internally in parseTypeAlias for
* PHP < 5.4.0 as it's not passing context $this to closure function.
*
* @var ContentHistory
* @since 3.2
* @deprecated Never use this
* @private
*/
public static $_myTableForPregreplaceOnly;
/**
* Creates the associated observer instance and attaches it to the $observableObject
* Creates the associated content history helper class instance
* $typeAlias can be of the form "{variableName}.type", automatically replacing {variableName} with table-instance variables variableName
*
* @param \JObservableInterface $observableObject The subject object to be observed
* @param array $params ( 'typeAlias' => $typeAlias )
*
* @return ContentHistory
*
* @since 3.2
*/
public static function createObserver(\JObservableInterface $observableObject, $params = array())
{
$typeAlias = $params['typeAlias'];
$observer = new self($observableObject);
$observer->contenthistoryHelper = new \JHelperContenthistory($typeAlias);
$observer->typeAliasPattern = $typeAlias;
return $observer;
}
/**
* Post-processor for $table->store($updateNulls)
*
* @param boolean &$result The result of the load
*
* @return void
*
* @since 3.2
*/
public function onAfterStore(&$result)
{
if ($result)
{
$this->parseTypeAlias();
$aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
if (\JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
{
$this->contenthistoryHelper->store($this->table);
}
}
}
/**
* Pre-processor for $table->delete($pk)
*
* @param mixed $pk An optional primary key value to delete. If not set the instance property value is used.
*
* @return void
*
* @since 3.2
* @throws \UnexpectedValueException
*/
public function onBeforeDelete($pk)
{
$this->parseTypeAlias();
$aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
if (\JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
{
$this->parseTypeAlias();
$this->contenthistoryHelper->deleteHistory($this->table);
}
}
/**
* Internal method
* Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
* Storing result into $this->contenthistoryHelper->typeAlias
*
* @return void
*
* @since 3.2
*/
protected function parseTypeAlias()
{
// Needed for PHP < 5.4.0 as it's not passing context $this to closure function
static::$_myTableForPregreplaceOnly = $this->table;
$this->contenthistoryHelper->typeAlias = preg_replace_callback('/{([^}]+)}/',
function($matches)
{
return ContentHistory::$_myTableForPregreplaceOnly->{$matches[1]};
},
$this->typeAliasPattern
);
}
}