Source for file contenthistory.php

Documentation is available at contenthistory.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Table
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Content History table.
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Table
  17.  * @since       3.2
  18.  */
  19. class JTableContenthistory extends JTable
  20. {
  21.  
  22.     /**
  23.      * Array of object fields to unset from the data object before calculating SHA1 hash. This allows us to detect a meaningful change
  24.      * in the database row using the hash. This can be read from the #__content_types content_history_options column.
  25.      *
  26.      * @var    array 
  27.      * @since  3.2
  28.      */
  29.     public $ignoreChanges = array();
  30.  
  31.     /**
  32.      * Array of object fields to convert to integers before calculating SHA1 hash. Some values are stored differently
  33.      * when an item is created than when the item is changed and saved. This works around that issue.
  34.      * This can be read from the #__content_types content_history_options column.
  35.      *
  36.      * @var    array 
  37.      * @since  3.2
  38.      */
  39.     public $convertToInt = array();
  40.  
  41.     /**
  42.      * Constructor
  43.      *
  44.      * @param   JDatabaseDriver  $db  A database connector object
  45.      *
  46.      * @since   3.1
  47.      */
  48.     public function __construct($db)
  49.     {
  50.         parent::__construct('#__ucm_history''version_id'$db);
  51.         $this->ignoreChanges = array('modified_by''modified_user_id''modified''modified_time''checked_out''checked_out_time''version''hits''path');
  52.         $this->convertToInt = array('publish_up''publish_down''ordering''featured');
  53.     }
  54.  
  55.     /**
  56.      * Overrides JTable::store to set modified hash, user id, and save date.
  57.      *
  58.      * @param   boolean  $updateNulls  True to update fields even if they are null.
  59.      *
  60.      * @return  boolean  True on success.
  61.      *
  62.      * @since   3.2
  63.      */
  64.     public function store($updateNulls false)
  65.     {
  66.         $this->set('character_count'strlen($this->get('version_data')));
  67.         $typeTable JTable::getInstance('Contenttype');
  68.         $typeTable->load($this->ucm_type_id);
  69.  
  70.         if (!isset($this->sha1_hash))
  71.         {
  72.             $this->set('sha1_hash'$this->getSha1($this->get('version_data')$typeTable));
  73.         }
  74.  
  75.         $this->set('editor_user_id'JFactory::getUser()->id);
  76.         $this->set('save_date'JFactory::getDate()->toSql());
  77.  
  78.         return parent::store($updateNulls);
  79.     }
  80.  
  81.     /**
  82.      * Utility method to get the hash after removing selected values. This lets us detect changes other than
  83.      * modified date (which will change on every save).
  84.      *
  85.      * @param   mixed              $jsonData   Either an object or a string with json-encoded data
  86.      * @param   JTableContenttype  $typeTable  Table object with data for this content type
  87.      *
  88.      * @return  string  SHA1 hash on sucess. Empty string on failure.
  89.      *
  90.      * @since   3.2
  91.      */
  92.     public function getSha1($jsonDataJTableContenttype $typeTable)
  93.     {
  94.         $object (is_object($jsonData)) $jsonData json_decode($jsonData);
  95.  
  96.         if (isset($typeTable->content_history_options&& (is_object(json_decode($typeTable->content_history_options))))
  97.         {
  98.              $options json_decode($typeTable->content_history_options);
  99.              $this->ignoreChanges = isset($options->ignoreChanges$options->ignoreChanges $this->ignoreChanges;
  100.              $this->convertToInt = isset($options->convertToInt$options->convertToInt $this->convertToInt;
  101.         }
  102.  
  103.         foreach ($this->ignoreChanges as $remove)
  104.         {
  105.             if (property_exists($object$remove))
  106.             {
  107.                 unset($object->$remove);
  108.             }
  109.         }
  110.  
  111.         // Convert integers, booleans, and nulls to strings to get a consistent hash value
  112.         foreach ($object as $name => $value)
  113.         {
  114.             if (is_object($value))
  115.             {
  116.                 // Go one level down for JSON column values
  117.                 foreach ($value as $subName => $subValue)
  118.                 {
  119.                     $object->$subName (is_int($subValue|| is_bool($subValue|| is_null($subValue)) ? (string) $subValue $subValue;
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 $object->$name (is_int($value|| is_bool($value|| is_null($value)) ? (string) $value $value;
  125.             }
  126.         }
  127.  
  128.         // Work around empty values
  129.         foreach ($this->convertToInt as $convert)
  130.         {
  131.             if (isset($object->$convert))
  132.             {
  133.                 $object->$convert = (int) $object->$convert;
  134.             }
  135.         }
  136.  
  137.         if (isset($object->review_time))
  138.         {
  139.             $object->review_time = (int) $object->review_time;
  140.         }
  141.  
  142.         return sha1(json_encode($object));
  143.     }
  144.  
  145.     /**
  146.      * Utility method to get a matching row based on the hash value and id columns.
  147.      * This lets us check to make sure we don't save duplicate versions.
  148.      *
  149.      * @return  string  SHA1 hash on sucess. Empty string on failure.
  150.      *
  151.      * @since   3.2
  152.      */
  153.     public function getHashMatch()
  154.     {
  155.         $db $this->_db;
  156.         $query $db->getQuery(true);
  157.         $query->select('*')
  158.             ->from($db->quoteName('#__ucm_history'))
  159.             ->where($db->quoteName('ucm_item_id'' = ' $this->get('ucm_item_id'))
  160.             ->where($db->quoteName('ucm_type_id'' = ' $this->get('ucm_type_id'))
  161.             ->where($db->quoteName('sha1_hash'' = ' $db->quote($this->get('sha1_hash')));
  162.         $db->setQuery($query01);
  163.  
  164.         return $db->loadObject();
  165.     }
  166.  
  167.     /**
  168.      * Utility method to remove the oldest versions of an item, saving only the most recent versions.
  169.      *
  170.      * @param   integer  $maxVersions  The maximum number of versions to save. All others will be deleted.
  171.      *
  172.      * @return  boolean   true on sucess, false on failure.
  173.      *
  174.      * @since   3.2
  175.      */
  176.     public function deleteOldVersions($maxVersions)
  177.     {
  178.         $result true;
  179.  
  180.         // Get the list of version_id values we want to save
  181.         $db $this->_db;
  182.         $query $db->getQuery(true);
  183.         $query->select($db->quoteName('version_id'))
  184.             ->from($db->quoteName('#__ucm_history'))
  185.             ->where($db->quoteName('ucm_item_id'' = ' . (int) $this->ucm_item_id)
  186.             ->where($db->quoteName('ucm_type_id'' = ' . (int) $this->ucm_type_id)
  187.             ->where($db->quoteName('keep_forever'' != 1')
  188.             ->order($db->quoteName('save_date'' DESC ');
  189.         $db->setQuery($query0(int) $maxVersions);
  190.         $idsToSave $db->loadColumn(0);
  191.  
  192.         // Don't process delete query unless we have at least the maximum allowed versions
  193.         if (count($idsToSave== (int) $maxVersions)
  194.         {
  195.             // Delete any rows not in our list and and not flagged to keep forever.
  196.             $query $db->getQuery(true);
  197.             $query->delete($db->quoteName('#__ucm_history'))
  198.                 ->where($db->quoteName('ucm_item_id'' = ' . (int) $this->ucm_item_id)
  199.                 ->where($db->quoteName('ucm_type_id'' = ' . (int) $this->ucm_type_id)
  200.                 ->where($db->quoteName('version_id'' NOT IN (' implode(','$idsToSave')')
  201.                 ->where($db->quoteName('keep_forever'' != 1');
  202.             $db->setQuery($query);
  203.             $result = (boolean) $db->execute();
  204.         }
  205.  
  206.         return $result;
  207.     }
  208. }

Documentation generated on Tue, 19 Nov 2013 14:56:58 +0100 by phpDocumentor 1.4.3