Repository navigation
Expand file tree
/
Copy pathscript.php
More file actions
311 lines (266 loc) · 9.62 KB
/
Copy pathscript.php
File metadata and controls
311 lines (266 loc) · 9.62 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
<?php
/**
* @package TLWebdesign.Module
* @subpackage mod_prettymasthead
*
* @copyright Copyright (C) 2022 TLWebdesign. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
\defined('_JEXEC') or die;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Installer\InstallerScriptInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Joomla\Filesystem\File;
/**
* Installer script for mod_prettymasthead
*
* @since 1.2.0
*/
return new class () implements InstallerScriptInterface {
/**
* Minimum Joomla version required to install the module.
*
* @since 1.2.0
*/
private const MINIMUM_JOOMLA = '5.4.0';
/**
* Minimum PHP version required to install the module.
*
* @since 1.2.0
*/
private const MINIMUM_PHP = '8.1.0';
/**
* Function called after the extension is installed.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 1.2.0
*/
public function install(InstallerAdapter $adapter): bool
{
echo Text::_('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_INSTALL');
return true;
}
/**
* Function called after the extension is updated.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 1.2.0
*/
public function update(InstallerAdapter $adapter): bool
{
echo Text::_('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_UPDATE');
$this->removeLegacyEntryFile($adapter);
$this->migrateParams();
$this->cleanCache();
return true;
}
/**
* Function called after the extension is uninstalled.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 1.2.0
*/
public function uninstall(InstallerAdapter $adapter): bool
{
echo Text::_('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_UNINSTALL');
return true;
}
/**
* Function called before the extension is installed, updated or uninstalled.
*
* @param string $type The type of change (install, update, discover_install or uninstall)
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True to continue, false to abort
*
* @since 1.2.0
*/
public function preflight(string $type, InstallerAdapter $adapter): bool
{
if ($type === 'uninstall') {
return true;
}
if (version_compare(PHP_VERSION, self::MINIMUM_PHP, '<')) {
Log::add(Text::sprintf('JLIB_INSTALLER_MINIMUM_PHP', self::MINIMUM_PHP), Log::WARNING, 'jerror');
return false;
}
if (version_compare(JVERSION, self::MINIMUM_JOOMLA, '<')) {
Log::add(Text::sprintf('JLIB_INSTALLER_MINIMUM_JOOMLA', self::MINIMUM_JOOMLA), Log::WARNING, 'jerror');
return false;
}
return true;
}
/**
* Function called after the extension is installed, updated or uninstalled.
*
* @param string $type The type of change (install, update, discover_install or uninstall)
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 1.2.0
*/
public function postflight(string $type, InstallerAdapter $adapter): bool
{
if ($type === 'update') {
echo Text::_('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_RESAVE_MODULE');
}
return true;
}
/**
* Remove the entry file used before the module had a service provider.
*
* Joomla 6.1 removes files that are no longer in the manifest, Joomla 5 does not.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return void
*
* @since 1.2.0
*/
private function removeLegacyEntryFile(InstallerAdapter $adapter): void
{
$file = $adapter->getParent()->getPath('extension_root') . '/mod_prettymasthead.php';
if (!is_file($file)) {
return;
}
try {
File::delete($file);
} catch (\Throwable $e) {
// The file is no longer loaded, so leaving it behind is harmless
}
}
/**
* Clear cached module output and the cached module list.
*
* The module list cache holds the module params, including the old cache mode,
* so without this the old mode and its cached output stay in use until they expire.
*
* @return void
*
* @since 1.2.0
*/
private function cleanCache(): void
{
$app = Factory::getApplication();
foreach (['com_modules', 'mod_prettymasthead'] as $group) {
try {
Factory::getContainer()->get(CacheControllerFactoryInterface::class)
->createCacheController('callback', ['defaultgroup' => $group, 'cachebase' => $app->get('cache_path', JPATH_CACHE)])
->clean();
} catch (\Throwable $e) {
Log::add(Text::sprintf('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_CACHEMODE_FAILED', $e->getMessage()), Log::WARNING, 'jerror');
}
}
}
/**
* Bring the params of existing module instances up to date.
*
* - The cache mode becomes the page-aware "safeuri": the hidden cachemode field posts its stored value back,
* so re-saving a module would keep the old "static" mode, which served one masthead on every page.
* - The single button of the default masthead and of each menu masthead becomes the first row of its buttons list.
*
* @return void
*
* @since 1.2.0
*/
private function migrateParams(): void
{
try {
$db = Factory::getContainer()->get(DatabaseInterface::class);
$module = 'mod_prettymasthead';
$query = $db->createQuery()
->select($db->quoteName(['id', 'params']))
->from($db->quoteName('#__modules'))
->where($db->quoteName('module') . ' = :module')
->bind(':module', $module);
$rows = $db->setQuery($query)->loadObjectList();
foreach ($rows as $row) {
$params = json_decode((string) $row->params);
if (!$params instanceof \stdClass) {
continue;
}
$changed = $this->migrateButton($params, 'default');
foreach ((array) ($params->mastheads ?? []) as $masthead) {
if ($masthead instanceof \stdClass && $this->migrateButton($masthead, 'masthead')) {
$changed = true;
}
}
if (($params->cachemode ?? '') !== 'safeuri') {
$params->cachemode = 'safeuri';
$changed = true;
}
if (!$changed) {
continue;
}
$json = json_encode($params);
$id = (int) $row->id;
if ($json === false) {
continue;
}
$update = $db->createQuery()
->update($db->quoteName('#__modules'))
->set($db->quoteName('params') . ' = :params')
->where($db->quoteName('id') . ' = :id')
->bind(':params', $json)
->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($update)->execute();
}
} catch (\Throwable $e) {
// Never block the update over this; affected modules keep their old params until they are fixed by hand
Log::add(Text::sprintf('MOD_PRETTYMASTHEAD_INSTALLERSCRIPT_CACHEMODE_FAILED', $e->getMessage()), Log::WARNING, 'jerror');
}
}
/**
* Move a single button (<prefix>buttontext, <prefix>buttonurl, <prefix>buttonclass) into the <prefix>buttons list.
*
* @param \stdClass $holder The module params, or one menu masthead row.
* @param string $prefix "default" or "masthead".
*
* @return boolean True when $holder was changed.
*
* @since 1.2.0
*/
private function migrateButton(\stdClass $holder, string $prefix): bool
{
$list = $prefix . 'buttons';
$changed = false;
$button = [];
foreach (['buttontext', 'buttonurl', 'buttonclass'] as $key) {
if (property_exists($holder, $prefix . $key)) {
$button[$key] = (string) $holder->{$prefix . $key};
$changed = true;
unset($holder->{$prefix . $key});
}
}
$existing = $holder->{$list} ?? null;
$hasList = (\is_object($existing) || \is_array($existing)) && (array) $existing !== [];
// A button without text was never shown
if (trim($button['buttontext'] ?? '') === '' || $hasList) {
return $changed;
}
// Subform rows are stored as <field name><index>
$holder->{$list} = (object) [
$list . '0' => (object) [
'buttontext' => $button['buttontext'],
'buttonurl' => $button['buttonurl'] ?? '',
'buttonclass' => $button['buttonclass'] ?? 'btn-primary',
],
];
return true;
}
};