[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Versioning/ -> Versioning.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Versioning;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Event\AbstractEvent;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Plugin\PluginHelper;
  16  use Joomla\CMS\Table\Table;
  17  use Joomla\CMS\Workflow\WorkflowServiceInterface;
  18  use Joomla\Database\ParameterType;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Handle the versioning of content items
  26   *
  27   * @since  4.0.0
  28   */
  29  class Versioning
  30  {
  31      /**
  32       * Method to get a list of available versions of this item.
  33       *
  34       * @param   string   $typeAlias  Typealias of the component
  35       * @param   integer  $id         ID of the content item to get
  36       *
  37       * @return  object[]   A list of history items
  38       *
  39       * @since   4.0.0
  40       */
  41      public static function get($typeAlias, $id)
  42      {
  43          $db = Factory::getDbo();
  44          $itemid = $typeAlias . '.' . $id;
  45          $query = $db->getQuery(true);
  46          $query->select($db->quoteName('h.version_note') . ',' . $db->quoteName('h.save_date') . ',' . $db->quoteName('u.name'))
  47              ->from($db->quoteName('#__history', 'h'))
  48              ->leftJoin($db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('h.editor_user_id'))
  49              ->where($db->quoteName('item_id') . ' = :item_id')
  50              ->bind(':item_id', $itemid, ParameterType::STRING)
  51              ->order($db->quoteName('save_date') . ' DESC ');
  52          $db->setQuery($query);
  53  
  54          return $db->loadObjectList();
  55      }
  56  
  57      /**
  58       * Method to delete the history for an item.
  59       *
  60       * @param   string   $typeAlias  Typealias of the component
  61       * @param   integer  $id         ID of the content item to delete
  62       *
  63       * @return  boolean  true on success, otherwise false.
  64       *
  65       * @since   4.0.0
  66       */
  67      public static function delete($typeAlias, $id)
  68      {
  69          $db = Factory::getDbo();
  70          $itemid = $typeAlias . '.' . $id;
  71          $query = $db->getQuery(true);
  72          $query->delete($db->quoteName('#__history'))
  73              ->where($db->quoteName('item_id') . ' = :item_id')
  74              ->bind(':item_id', $itemid, ParameterType::STRING);
  75          $db->setQuery($query);
  76  
  77          return $db->execute();
  78      }
  79  
  80      /**
  81       * Method to save a version snapshot to the content history table.
  82       *
  83       * @param   string   $typeAlias  Typealias of the content type
  84       * @param   integer  $id         ID of the content item
  85       * @param   mixed    $data       Array or object of data that can be
  86       *                               en- and decoded into JSON
  87       * @param   string   $note       Note for the version to store
  88       *
  89       * @return  boolean  True on success, otherwise false.
  90       *
  91       * @since   4.0.0
  92       */
  93      public static function store($typeAlias, $id, $data, $note = '')
  94      {
  95          $typeTable = Table::getInstance('Contenttype', 'JTable');
  96          $typeTable->load(array('type_alias' => $typeAlias));
  97  
  98          $historyTable = Table::getInstance('Contenthistory', 'JTable');
  99          $historyTable->item_id = $typeAlias . '.' . $id;
 100  
 101          $aliasParts = explode('.', $typeAlias);
 102  
 103          // Don't store unless we have a non-zero item id
 104          if (!$historyTable->item_id) {
 105              return true;
 106          }
 107  
 108          // We should allow workflow items interact with the versioning
 109          $component = Factory::getApplication()->bootComponent($aliasParts[0]);
 110  
 111          if ($component instanceof WorkflowServiceInterface && $component->isWorkflowActive($typeAlias)) {
 112              PluginHelper::importPlugin('workflow');
 113  
 114              // Pre-processing by observers
 115              $event = AbstractEvent::create(
 116                  'onContentVersioningPrepareTable',
 117                  [
 118                      'subject'   => $historyTable,
 119                      'extension' => $typeAlias
 120                  ]
 121              );
 122  
 123              Factory::getApplication()->getDispatcher()->dispatch('onContentVersioningPrepareTable', $event);
 124          }
 125  
 126          $historyTable->version_data = json_encode($data);
 127          $historyTable->version_note = $note;
 128  
 129          // Don't save if hash already exists and same version note
 130          $historyTable->sha1_hash = $historyTable->getSha1($data, $typeTable);
 131  
 132          if ($historyRow = $historyTable->getHashMatch()) {
 133              if (!$note || ($historyRow->version_note === $note)) {
 134                  return true;
 135              } else {
 136                  // Update existing row to set version note
 137                  $historyTable->version_id = $historyRow->version_id;
 138              }
 139          }
 140  
 141          $result = $historyTable->store();
 142  
 143          // Load history_limit config from extension.
 144          $context = $aliasParts[1] ?? '';
 145  
 146          $maxVersionsContext = ComponentHelper::getParams($aliasParts[0])->get('history_limit_' . $context, 0);
 147  
 148          if ($maxVersionsContext) {
 149              $historyTable->deleteOldVersions($maxVersionsContext);
 150          } elseif ($maxVersions = ComponentHelper::getParams($aliasParts[0])->get('history_limit', 0)) {
 151              $historyTable->deleteOldVersions($maxVersions);
 152          }
 153  
 154          return $result;
 155      }
 156  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer