PageRenderTime 111ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/cms/table/contenthistory.php

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