/
database.php
222 lines (204 loc) · 5.46 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* @package Joomla.Administrator
* @subpackage com_installer
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JLoader::register('InstallerModel', __DIR__ . '/extension.php');
JLoader::register('joomlaInstallerScript', JPATH_ADMINISTRATOR . '/components/com_admin/script.php');
/**
* Installer Manage Model
*
* @package Joomla.Administrator
* @subpackage com_installer
* @since 1.6
*/
class InstallerModelDatabase extends InstallerModel
{
protected $_context = 'com_installer.discover';
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @since 1.6
*/
protected function populateState($ordering = null, $direction = null)
{
$app = JFactory::getApplication();
$this->setState('message', $app->getUserState('com_installer.message'));
$this->setState('extension_message', $app->getUserState('com_installer.extension_message'));
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
parent::populateState('name', 'asc');
}
/**
*
* Fixes database problems
*/
public function fix()
{
$changeSet = $this->getItems();
$changeSet->fix();
$this->fixSchemaVersion($changeSet);
$this->fixUpdateVersion();
$installer = new joomlaInstallerScript;
$installer->deleteUnexistingFiles();
$this->fixDefaultTextFilters();
}
/**
*
* Gets the changeset object
*
* @return JSchemaChangeset
*/
public function getItems()
{
$folder = JPATH_ADMINISTRATOR . '/components/com_admin/sql/updates/';
$changeSet = JSchemaChangeset::getInstance(JFactory::getDbo(), $folder);
return $changeSet;
}
public function getPagination()
{
return true;
}
/**
* Get version from #__schemas table
*
* @return mixed the return value from the query, or null if the query fails
* @throws Exception
*/
public function getSchemaVersion() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('version_id')->from($db->qn('#__schemas'))
->where('extension_id = 700');
$db->setQuery($query);
$result = $db->loadResult();
if ($db->getErrorNum()) {
throw new Exception('Database error - getSchemaVersion');
}
return $result;
}
/**
* Fix schema version if wrong
*
* @param JSchemaChangeSet
*
* @return mixed string schema version if success, false if fail
*/
public function fixSchemaVersion($changeSet)
{
// Get correct schema version -- last file in array
$schema = $changeSet->getSchema();
$db = JFactory::getDbo();
$result = false;
// Check value. If ok, don't do update
$version = $this->getSchemaVersion();
if ($version == $schema)
{
$result = $version;
}
else
{
// Delete old row
$query = $db->getQuery(true);
$query->delete($db->qn('#__schemas'));
$query->where($db->qn('extension_id') . ' = 700');
$db->setQuery($query);
$db->execute();
// Add new row
$query = $db->getQuery(true);
$query->insert($db->qn('#__schemas'));
$query->set($db->qn('extension_id') . '= 700');
$query->set($db->qn('version_id') . '= ' . $db->q($schema));
$db->setQuery($query);
if ($db->execute()) {
$result = $schema;
}
}
return $result;
}
/**
* Get current version from #__extensions table
*
* @return mixed version if successful, false if fail
*/
public function getUpdateVersion()
{
$table = JTable::getInstance('Extension');
$table->load('700');
$cache = new JRegistry($table->manifest_cache);
return $cache->get('version');
}
/**
* Fix Joomla version in #__extensions table if wrong (doesn't equal JVersion short version)
*
* @return mixed string update version if success, false if fail
*/
public function fixUpdateVersion()
{
$table = JTable::getInstance('Extension');
$table->load('700');
$cache = new JRegistry($table->manifest_cache);
$updateVersion = $cache->get('version');
$cmsVersion = new JVersion;
if ($updateVersion == $cmsVersion->getShortVersion())
{
return $updateVersion;
}
else
{
$cache->set('version', $cmsVersion->getShortVersion());
$table->manifest_cache = $cache->toString();
if ($table->store())
{
return $cmsVersion->getShortVersion();
}
else
{
return false;
}
}
}
/**
* For version 2.5.x only
* Check if com_config parameters are blank.
*
* @return string default text filters (if any)
*/
public function getDefaultTextFilters()
{
$table = JTable::getInstance('Extension');
$table->load($table->find(array('name' => 'com_config')));
return $table->params;
}
/**
* For version 2.5.x only
* Check if com_config parameters are blank. If so, populate with com_content text filters.
*
* @return mixed boolean true if params are updated, null otherwise
*/
public function fixDefaultTextFilters()
{
$table = JTable::getInstance('Extension');
$table->load($table->find(array('name' => 'com_config')));
// Check for empty $config and non-empty content filters
if (!$table->params)
{
// Get filters from com_content and store if you find them
$contentParams = JComponentHelper::getParams('com_content');
if ($contentParams->get('filters'))
{
$newParams = new JRegistry;
$newParams->set('filters', $contentParams->get('filters'));
$table->params = (string) $newParams;
$table->store();
return true;
}
}
}
}