InstallerScript.php
8.84 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Installer;
defined('_JEXEC') or die;
\JLoader::import('joomla.filesystem.file');
\JLoader::import('joomla.filesystem.folder');
/**
* Base install script for use by extensions providing helper methods for common behaviours.
*
* @since 3.6
*/
class InstallerScript
{
/**
* The version number of the extension.
*
* @var string
* @since 3.6
*/
protected $release;
/**
* The table the parameters are stored in.
*
* @var string
* @since 3.6
*/
protected $paramTable;
/**
* The extension name. This should be set in the installer script.
*
* @var string
* @since 3.6
*/
protected $extension;
/**
* A list of files to be deleted
*
* @var array
* @since 3.6
*/
protected $deleteFiles = array();
/**
* A list of folders to be deleted
*
* @var array
* @since 3.6
*/
protected $deleteFolders = array();
/**
* A list of CLI script files to be copied to the cli directory
*
* @var array
* @since 3.6
*/
protected $cliScriptFiles = array();
/**
* Minimum PHP version required to install the extension
*
* @var string
* @since 3.6
*/
protected $minimumPhp;
/**
* Minimum Joomla! version required to install the extension
*
* @var string
* @since 3.6
*/
protected $minimumJoomla;
/**
* Allow downgrades of your extension
*
* Use at your own risk as if there is a change in functionality people may wish to downgrade.
*
* @var boolean
* @since 3.6
*/
protected $allowDowngrades = false;
/**
* Function called before extension installation/update/removal procedure commences
*
* @param string $type The type of change (install, update or discover_install, not uninstall)
* @param InstallerAdapter $parent The class calling this method
*
* @return boolean True on success
*
* @since 3.6
*/
public function preflight($type, $parent)
{
// Check for the minimum PHP version before continuing
if (!empty($this->minimumPhp) && version_compare(PHP_VERSION, $this->minimumPhp, '<'))
{
\JLog::add(\JText::sprintf('JLIB_INSTALLER_MINIMUM_PHP', $this->minimumPhp), \JLog::WARNING, 'jerror');
return false;
}
// Check for the minimum Joomla version before continuing
if (!empty($this->minimumJoomla) && version_compare(JVERSION, $this->minimumJoomla, '<'))
{
\JLog::add(\JText::sprintf('JLIB_INSTALLER_MINIMUM_JOOMLA', $this->minimumJoomla), \JLog::WARNING, 'jerror');
return false;
}
// Extension manifest file version
$this->extension = $parent->getName();
$this->release = $parent->get('manifest')->version;
$extensionType = substr($this->extension, 0, 3);
// Modules parameters are located in the module table - else in the extension table
if ($extensionType === 'mod')
{
$this->paramTable = '#__modules';
}
else
{
$this->paramTable = '#__extensions';
}
// Abort if the extension being installed is not newer than the currently installed version
if (!$this->allowDowngrades && strtolower($type) === 'update')
{
$manifest = $this->getItemArray('manifest_cache', '#__extensions', 'element', \JFactory::getDbo()->quote($this->extension));
$oldRelease = $manifest['version'];
if (version_compare($this->release, $oldRelease, '<'))
{
\JFactory::getApplication()->enqueueMessage(\JText::sprintf('JLIB_INSTALLER_INCORRECT_SEQUENCE', $oldRelease, $this->release), 'error');
return false;
}
}
return true;
}
/**
* Gets each instance of a module in the #__modules table
*
* @param boolean $isModule True if the extension is a module as this can have multiple instances
*
* @return array An array of ID's of the extension
*
* @since 3.6
*/
public function getInstances($isModule)
{
$db = \JFactory::getDbo();
$query = $db->getQuery(true);
// Select the item(s) and retrieve the id
$query->select($db->quoteName('id'));
if ($isModule)
{
$query->from($db->quoteName('#__modules'))
->where($db->quoteName('module') . ' = ' . $db->quote($this->extension));
}
else
{
$query->from($db->quoteName('#__extensions'))
->where($db->quoteName('element') . ' = ' . $db->quote($this->extension));
}
// Set the query and obtain an array of id's
return $db->setQuery($query)->loadColumn();
}
/**
* Gets parameter value in the extensions row of the extension table
*
* @param string $name The name of the parameter to be retrieved
* @param integer $id The id of the item in the Param Table
*
* @return string The parameter desired
*
* @since 3.6
*/
public function getParam($name, $id = 0)
{
if (!is_int($id) || $id == 0)
{
// Return false if there is no item given
return false;
}
$params = $this->getItemArray('params', $this->paramTable, 'id', $id);
return $params[$name];
}
/**
* Sets parameter values in the extensions row of the extension table. Note that the
* this must be called separately for deleting and editing. Note if edit is called as a
* type then if the param doesn't exist it will be created
*
* @param array $param_array The array of parameters to be added/edited/removed
* @param string $type The type of change to be made to the param (edit/remove)
* @param integer $id The id of the item in the relevant table
*
* @return boolean True on success
*
* @since 3.6
*/
public function setParams($param_array = null, $type = 'edit', $id = 0)
{
if (!is_int($id) || $id == 0)
{
// Return false if there is no valid item given
return false;
}
$params = $this->getItemArray('params', $this->paramTable, 'id', $id);
if ($param_array)
{
foreach ($param_array as $name => $value)
{
if ($type === 'edit')
{
// Add or edit the new variable(s) to the existing params
if (is_array($value))
{
// Convert an array into a json encoded string
$params[(string) $name] = array_values($value);
}
else
{
$params[(string) $name] = (string) $value;
}
}
elseif ($type === 'remove')
{
// Unset the parameter from the array
unset($params[(string) $name]);
}
}
}
// Store the combined new and existing values back as a JSON string
$paramsString = json_encode($params);
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->update($db->quoteName($this->paramTable))
->set('params = ' . $db->quote($paramsString))
->where('id = ' . $id);
// Update table
$db->setQuery($query)->execute();
return true;
}
/**
* Builds a standard select query to produce better DRY code in this script.
* This should produce a single unique cell which is json encoded - it will then
* return an associated array with this data in.
*
* @param string $element The element to get from the query
* @param string $table The table to search for the data in
* @param string $column The column of the database to search from
* @param mixed $identifier The integer id or the already quoted string
*
* @return array Associated array containing data from the cell
*
* @since 3.6
*/
public function getItemArray($element, $table, $column, $identifier)
{
// Get the DB and query objects
$db = \JFactory::getDbo();
// Build the query
$query = $db->getQuery(true)
->select($db->quoteName($element))
->from($db->quoteName($table))
->where($db->quoteName($column) . ' = ' . $identifier);
$db->setQuery($query);
// Load the single cell and json_decode data
return json_decode($db->loadResult(), true);
}
/**
* Remove the files and folders in the given array from
*
* @return void
*
* @since 3.6
*/
public function removeFiles()
{
if (!empty($this->deleteFiles))
{
foreach ($this->deleteFiles as $file)
{
if (file_exists(JPATH_ROOT . $file) && !\JFile::delete(JPATH_ROOT . $file))
{
echo \JText::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $file) . '<br />';
}
}
}
if (!empty($this->deleteFolders))
{
foreach ($this->deleteFolders as $folder)
{
if (\JFolder::exists(JPATH_ROOT . $folder) && !\JFolder::delete(JPATH_ROOT . $folder))
{
echo \JText::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $folder) . '<br />';
}
}
}
}
/**
* Moves the CLI scripts into the CLI folder in the CMS
*
* @return void
*
* @since 3.6
*/
public function moveCliFiles()
{
if (!empty($this->cliScriptFiles))
{
foreach ($this->cliScriptFiles as $file)
{
$name = basename($file);
if (file_exists(JPATH_ROOT . $file) && !\JFile::move(JPATH_ROOT . $file, JPATH_ROOT . '/cli/' . $name))
{
echo \JText::sprintf('JLIB_INSTALLER_FILE_ERROR_MOVE', $name);
}
}
}
}
}