Passed
Pull Request — develop (#912)
by Shandak
05:00
created

ContentHistoryHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 55
c 1
b 0
f 0
dl 0
loc 144
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B store() 0 63 9
A deleteHistory() 0 14 1
A getHistory() 0 13 1
1
<?php
2
/**
3
 * Joomla! Content Management System
4
 *
5
 * @copyright  Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
7
 */
8
9
namespace Joomla\CMS\Helper;
10
11
defined('JPATH_PLATFORM') or die;
12
13
use Joomla\CMS\Component\ComponentHelper;
0 ignored issues
show
Bug introduced by
The type Joomla\CMS\Component\ComponentHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Joomla\CMS\Table\Table;
0 ignored issues
show
Bug introduced by
The type Joomla\CMS\Table\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Versions helper class, provides methods to perform various tasks relevant
18
 * versioning of content.
19
 *
20
 * @since  3.2
21
 */
22
class ContentHistoryHelper extends CMSHelper
0 ignored issues
show
Bug introduced by
The type Joomla\CMS\Helper\CMSHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
{
24
	/**
25
	 * Alias for storing type in versions table
26
	 *
27
	 * @var    string
28
	 * @since  3.2
29
	 */
30
	public $typeAlias = null;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param   string  $typeAlias  The type of content to be versioned (for example, 'com_content.article').
36
	 *
37
	 * @since   3.2
38
	 */
39
	public function __construct($typeAlias = null)
40
	{
41
		$this->typeAlias = $typeAlias;
42
	}
43
44
	/**
45
	 * Method to delete the history for an item.
46
	 *
47
	 * @param   Table  $table  Table object being versioned
48
	 *
49
	 * @return  boolean  true on success, otherwise false.
50
	 *
51
	 * @since   3.2
52
	 */
53
	public function deleteHistory($table)
54
	{
55
		$key = $table->getKeyName();
56
		$id = $table->$key;
57
		$typeTable = Table::getInstance('Contenttype', 'JTable');
58
		$typeId = $typeTable->getTypeId($this->typeAlias);
59
		$db = \JFactory::getDbo();
60
		$query = $db->getQuery(true);
61
		$query->delete($db->quoteName('#__ucm_history'))
62
			->where($db->quoteName('ucm_item_id') . ' = ' . (int) $id)
63
			->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId);
64
		$db->setQuery($query);
65
66
		return $db->execute();
67
	}
68
69
	/**
70
	 * Method to get a list of available versions of this item.
71
	 *
72
	 * @param   integer  $typeId  Type id for this component item.
73
	 * @param   mixed    $id      Primary key of row to get history for.
74
	 *
75
	 * @return  mixed   The return value or null if the query failed.
76
	 *
77
	 * @since   3.2
78
	 */
79
	public function getHistory($typeId, $id)
80
	{
81
		$db = \JFactory::getDbo();
82
		$query = $db->getQuery(true);
83
		$query->select($db->quoteName('h.version_note') . ',' . $db->quoteName('h.save_date') . ',' . $db->quoteName('u.name'))
84
			->from($db->quoteName('#__ucm_history') . ' AS h ')
85
			->leftJoin($db->quoteName('#__users') . ' AS u ON ' . $db->quoteName('u.id') . ' = ' . $db->quoteName('h.editor_user_id'))
86
			->where($db->quoteName('ucm_item_id') . ' = ' . $db->quote($id))
0 ignored issues
show
Bug introduced by
Are you sure $db->quote($id) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
			->where($db->quoteName('ucm_item_id') . ' = ' . /** @scrutinizer ignore-type */ $db->quote($id))
Loading history...
87
			->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId)
88
			->order($db->quoteName('save_date') . ' DESC ');
89
		$db->setQuery($query);
90
91
		return $db->loadObjectList();
92
	}
93
94
	/**
95
	 * Method to save a version snapshot to the content history table.
96
	 *
97
	 * @param   Table  $table  Table object being versioned
98
	 *
99
	 * @return  boolean  True on success, otherwise false.
100
	 *
101
	 * @since   3.2
102
	 */
103
	public function store($table)
104
	{
105
		$dataObject = $this->getDataObject($table);
106
		$historyTable = Table::getInstance('Contenthistory', 'JTable');
107
		$typeTable = Table::getInstance('Contenttype', 'JTable');
108
		$typeTable->load(array('type_alias' => $this->typeAlias));
109
		$historyTable->set('ucm_type_id', $typeTable->type_id);
110
111
		$key = $table->getKeyName();
112
		$historyTable->set('ucm_item_id', $table->$key);
113
114
		// Don't store unless we have a non-zero item id
115
		if (!$historyTable->ucm_item_id)
116
		{
117
			return true;
118
		}
119
120
		$historyTable->set('version_data', json_encode($dataObject));
121
		$input = \JFactory::getApplication()->input;
122
		$data = $input->get('jform', array(), 'array');
123
		$versionName = false;
124
125
		if (isset($data['version_note']))
126
		{
127
			$versionName = \JFilterInput::getInstance()->clean($data['version_note'], 'string');
128
			$historyTable->set('version_note', $versionName);
129
		}
130
131
		// Don't save if hash already exists and same version note
132
		$historyTable->set('sha1_hash', $historyTable->getSha1($dataObject, $typeTable));
133
134
		if ($historyRow = $historyTable->getHashMatch())
135
		{
136
			if (!$versionName || ($historyRow->version_note === $versionName))
137
			{
138
				return true;
139
			}
140
			else
141
			{
142
				// Update existing row to set version note
143
				$historyTable->set('version_id', $historyRow->version_id);
144
			}
145
		}
146
147
		$result = $historyTable->store();
148
149
		// Load history_limit config from extension.
150
		$aliasParts = explode('.', $this->typeAlias);
151
152
		$context = isset($aliasParts[1]) ? $aliasParts[1] : '';
153
154
		$maxVersionsContext = ComponentHelper::getParams($aliasParts[0])->get('history_limit' . '_' . $context, 0);
155
156
		if ($maxVersionsContext)
157
		{
158
			$historyTable->deleteOldVersions($maxVersionsContext);
159
		}
160
		elseif ($maxVersions = ComponentHelper::getParams($aliasParts[0])->get('history_limit', 0))
161
		{
162
			$historyTable->deleteOldVersions($maxVersions);
163
		}
164
165
		return $result;
166
	}
167
}
168