-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.php
More file actions
251 lines (215 loc) · 7.79 KB
/
script.php
File metadata and controls
251 lines (215 loc) · 7.79 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
<?php
/**
* @package Joomla.Site
* @subpackage mod_page_qr_code
*
* @copyright www.nik-o-mat.de
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
/**
* Script file of Joomla CMS
*
* @since 2.0.0
*/
class Mod_Page_Qr_CodeInstallerScript
{
/**
* The version we are updating from
*
* @var string
* @since 2.0.0
*/
public static $fromVersion = null;
/**
* Function to act prior to installation process begins
*
* @param string $action Which action is happening (install|uninstall|discover_install|update)
* @param Installer $installer The class calling this method
*
* @return boolean True on success
*
* @since 2.0.0
*/
public function preflight($action, $installer)
{
if ($action === 'update') {
// Get the version we are updating from
if (empty($installer->extension->manifest_cache)) {
$manifestValues = json_decode($this->getExtensionValue('manifest_cache'), true);
} else {
$manifestValues = json_decode($installer->extension->manifest_cache, true);
}
if (array_key_exists('version', $manifestValues)) {
self::$fromVersion = $manifestValues['version'];
}
}
return true;
}
/**
* Called after any type of action
*
* @param string $action Which action is happening (install|uninstall|discover_install|update)
* @param Installer $installer The class calling this method
*
* @return boolean True on success
*
* @since 2.0.0
*/
public function postflight($action, $installer)
{
if ($action !== 'update') {
return true;
}
$this->deleteUnexistingFiles();
if (!empty(self::$fromVersion) && version_compare(self::$fromVersion, '2.0.0', 'lt')) {
$this->fixDefaultParams();
$this->fixModulesParams();
}
return true;
}
/**
* Delete files that should not exist
*
* @param bool $dryRun If set to true, will not actually delete files, but just report their status for use in CLI
* @param bool $suppressOutput Set to true to suppress echoing any errors, and just return the $status array
*
* @return array
*
* @since 2.0.0
*/
private function deleteUnexistingFiles($dryRun = false, $suppressOutput = false)
{
$status = [
'files_exist' => [],
'files_deleted' => [],
'files_errors' => [],
'files_checked' => [],
];
$files = [
// From prior 2.0.0
'/modules/mod_page_qr_code/mod_page_qr_code.php',
'/language/de-DE/de-DE.mod_page_qr_code.ini',
'/language/en-GB/en-GB.mod_page_qr_code.ini',
// From prior 2.0.4
'/language/de-DE/mod_page_qr_code.ini',
'/language/en-GB/mod_page_qr_code.ini',
'/language/de-DE/mod_page_qr_code.sys.ini',
'/language/en-GB/mod_page_qr_code.sys.ini',
];
$status['files_checked'] = $files;
foreach ($files as $file) {
if ($fileExists = File::exists(JPATH_ROOT . $file)) {
$status['files_exist'][] = $file;
if ($dryRun === false) {
if (File::delete(JPATH_ROOT . $file)) {
$status['files_deleted'][] = $file;
} else {
$status['files_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file);
}
}
}
}
if ($suppressOutput === false && count($status['files_errors'])) {
echo implode('<br>', $status['files_errors']);
}
return $status;
}
/**
* Fix the params for the extension itself.
*
* @return void
*
* @since 2.0.0
*/
private function fixDefaultParams(): void
{
/** @var $db \Joomla\Database\DatabaseDriver */
$db = Factory::getContainer()->get('DatabaseDriver');
$oldParams = json_decode($this->getExtensionValue('params'), true);
$params = new Registry($oldParams);
$groesse = $params->get('groesse');
list($width) = explode('x', $groesse);
$params->set('groesse', (int) $width);
$params->remove('moduleclass_sfx');
$query = $db->getQuery(true)
->update($db->quoteName('#__extensions'))
->set($db->quoteName('params') . '=' . $db->quote($params->toString()))
->where($db->quoteName('type') . '=' . $db->quote('module'))
->andWhere($db->quoteName('element') . '=' . $db->quote('mod_page_qr_code'));
try {
$db->setQuery($query)->execute();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
}
}
/**
* Fix the params for all site modules in use.
*
* @return void
*
* @since 2.0.0
*/
private function fixModulesParams(): void
{
/** @var $db \Joomla\Database\DatabaseDriver */
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select($db->quoteName(['id', 'params']))
->from('#__modules')
->where($db->quoteName('module') . '=' . $db->quote('mod_page_qr_code'));
try {
$modules = $db->setQuery($query)->loadRowList();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
return;
}
foreach ($modules as $module) {
$moduleId = $module[0];
$moduleParams = json_decode($module[1], true);
$moduleParams = new Registry($moduleParams);
$groesse = $moduleParams->get('groesse');
list($width) = explode('x', $groesse);
$moduleParams->set('groesse', (int) $width);
$moduleParams->remove('moduleclass_sfx');
$query = $db->getQuery(true)
->update($db->quoteName('#__modules'))
->set($db->quoteName('params') . '=' . $db->quote($moduleParams->toString()))
->where($db->quoteName('id') . '=' . $db->quote($moduleId));
try {
$db->setQuery($query)->execute();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
}
}
}
/**
* Get the column value from database for the extension
*
* @param string $column The column name.
*
* @return string|integer|null
*/
private function getExtensionValue($column) {
/** @var $db \Joomla\Database\DatabaseDriver */
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select($db->quoteName($column))
->from('#__extensions')
->where($db->quoteName('type') . '=' . $db->quote('module'))
->andWhere($db->quoteName('element') . '=' . $db->quote('mod_page_qr_code'));
try {
return $db->setQuery($query)->loadResult();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
return null;
}
}
}