This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DizkusModuleInstaller.php
452 lines (407 loc) · 16.2 KB
/
DizkusModuleInstaller.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
declare(strict_types=1);
/**
* Dizkus.
*
* @copyright (c) 2001-now, Dizkus Development Team
*
* @see https://github.com/zikula-modules/Dizkus
*
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
*/
namespace Zikula\DizkusModule;
use Zikula\Core\AbstractExtensionInstaller;
use Zikula\DizkusModule\Entity\ForumEntity;
use Zikula\DizkusModule\Entity\RankEntity;
class DizkusModuleInstaller extends AbstractExtensionInstaller
{
/**
* Module name
* (needed for static methods).
*
* @var string
*/
const MODULENAME = 'ZikulaDizkusModule';
private $entities = [
'Zikula\DizkusModule\Entity\ForumEntity',
'Zikula\DizkusModule\Entity\PostEntity',
'Zikula\DizkusModule\Entity\TopicEntity',
'Zikula\DizkusModule\Entity\ForumUserFavoriteEntity',
'Zikula\DizkusModule\Entity\ForumUserEntity',
'Zikula\DizkusModule\Entity\ForumSubscriptionEntity',
'Zikula\DizkusModule\Entity\TopicSubscriptionEntity',
'Zikula\DizkusModule\Entity\RankEntity',
'Zikula\DizkusModule\Entity\ModeratorUserEntity',
'Zikula\DizkusModule\Entity\ModeratorGroupEntity',
];
//import
private $importTables = [
'dizkus_categories',
'dizkus_forum_mods',
'dizkus_forums',
'dizkus_posts',
'dizkus_subscription',
'dizkus_ranks',
'dizkus_topics',
'dizkus_topic_subscription',
'dizkus_forum_favorites',
'dizkus_users',
];
/**
* Initialize a new install of the Dizkus module.
*
* This function will initialize a new installation of Dizkus.
* It is accessed via the Zikula Admin interface and should
* not be called directly.
*/
public function install()
{
try {
$this->schemaTool->create($this->entities);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
return false;
}
// set the module vars
$this->setVars(self::getDefaultVars());
// set up forum root (required)
$forumRoot = new ForumEntity();
$forumRoot->setName(ForumEntity::ROOTNAME);
$forumRoot->lock();
$this->entityManager->persist($forumRoot);
// set up EXAMPLE forums
$this->setUpExampleForums($forumRoot);
// set up sample ranks
$this->setUpSampleRanks();
return true;
}
/**
* Set up example forums on install.
*/
private function setUpExampleForums($forumRoot)
{
$food = new ForumEntity();
$food->setName('Food');
$food->setParent($forumRoot);
$food->lock();
$this->entityManager->persist($food);
$fruits = new ForumEntity();
$fruits->setName('Fruits');
$fruits->setParent($food);
$this->entityManager->persist($fruits);
$vegetables = new ForumEntity();
$vegetables->setName('Vegetables');
$vegetables->setParent($food);
$this->entityManager->persist($vegetables);
$carrots = new ForumEntity();
$carrots->setName('Carrots');
$carrots->setParent($vegetables);
$this->entityManager->persist($carrots);
$this->entityManager->flush();
}
private function setUpSampleRanks()
{
//title, description, minimumCount, maximumCount, type, image
$ranks = [
[
'title' => 'Level 1',
'description' => 'New forum user',
'minimumCount' => 1,
'maximumCount' => 9,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'zerostar.gif', ],
[
'title' => 'Level 2',
'description' => 'Basic forum user',
'minimumCount' => 10,
'maximumCount' => 49,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'onestar.gif', ],
[
'title' => 'Level 3',
'description' => 'Moderate forum user',
'minimumCount' => 50,
'maximumCount' => 99,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'twostars.gif', ],
[
'title' => 'Level 4',
'description' => 'Advanced forum user',
'minimumCount' => 100,
'maximumCount' => 199,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'threestars.gif', ],
[
'title' => 'Level 5',
'description' => 'Expert forum user',
'minimumCount' => 200,
'maximumCount' => 499,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'fourstars.gif', ],
[
'title' => 'Level 6',
'description' => 'Superior forum user',
'minimumCount' => 500,
'maximumCount' => 999,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'fivestars.gif', ],
[
'title' => 'Level 7',
'description' => 'Senior forum user',
'minimumCount' => 1000,
'maximumCount' => 4999,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'spezstars.gif', ],
[
'title' => 'Legend',
'description' => 'Legend forum user',
'minimumCount' => 5000,
'maximumCount' => 1000000,
'type' => RankEntity::TYPE_POSTCOUNT,
'image' => 'adminstars.gif', ], ];
foreach ($ranks as $rank) {
$r = new RankEntity();
$r->merge($rank);
$this->entityManager->persist($r);
}
$this->entityManager->flush();
}
/**
* Deletes an install of the Dizkus module.
*
* This function removes Dizkus from your
* Zikula install and should be accessed via
* the Zikula Admin interface
*/
public function uninstall()
{
try {
$this->schemaTool->drop($this->entities);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
return false;
}
// remove module vars
$this->delVars();
return true;
}
public function upgrade($oldversion)
{
// Only support upgrade from version 3.1 and up. Notify users if they have a version below that one.
if (version_compare($oldversion, '3.1', '<')) {
// Inform user about error, and how he can upgrade to $modversion
$upgradeToVersion = $this->bundle->getMetaData()->getVersion();
$this->addFlash('error', $this->__f('Notice: This version does not support upgrades from versions of Dizkus less than 3.1. Please upgrade to 3.1 before upgrading again to version %s.', $upgradeToVersion));
return false;
}
switch ($oldversion) {
case '3.1':
case '3.1.0':
case '3.2.0':
if (!$this->upgrade_settings()) {
return false;
}
$connection = $this->entityManager->getConnection();
$dbName = $this->container->getParameter('database_name');
$connection->executeQuery("DELETE FROM ${dbName}.`hook_area` WHERE `owner` = 'Dizkus'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_binding` WHERE `sowner` = 'Dizkus'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_runtime` WHERE `sowner` = 'Dizkus'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_subscriber` WHERE `owner` = 'Dizkus'");
$prefix = $this->container->hasParameter('prefix') ? $this->container->getParameter('prefix') : '';
$schemaManager = $connection->getSchemaManager();
$schema = $schemaManager->createSchema();
if (!$schema->hasTable($prefix . 'dizkus_categories')) {
$this->addFlash('error', $this->__f('There was a problem recognizing the existing Dizkus tables. Please confirm that your settings for prefix in $ZConfig[\'System\'][\'prefix\'] match the actual Dizkus tables in the database. (Current prefix loaded as `%s`)', ['%s' => $prefix]));
return false;
}
$name = $prefix . 'dizkus_users';
if (!$schema->hasTable($name)) {
// 3.2.0 users dummy table
$table = $schema->createTable($name);
$table->addColumn('user_id', 'integer');
$table->addColumn('user_posts', 'integer');
$table->addColumn('user_rank', 'integer');
$table->addColumn('user_level', 'integer');
$table->addColumn('user_lastvisit', 'datetime');
$table->addColumn('user_favorites', 'integer');
$table->addColumn('user_post_order', 'integer');
$sql = $schemaManager->getDatabasePlatform()->getCreateTableSQL($table);
$statement = $connection->prepare($sql[0]);
$statement->execute();
}
if ('' !== $prefix) {
$this->removeTablePrefixes($prefix);
}
// mark tables for import
$upgrade_mark = str_replace('.', '_', $oldversion) . '_';
$this->markTablesForImport($upgrade_mark);
// add upgrading info for later
$this->setVar('upgrading', str_replace('.', '_', $oldversion));
//install module now
try {
$this->schemaTool->create($this->entities);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
return false;
}
// set up forum root (required)
$forumRoot = new ForumEntity();
$forumRoot->setName(ForumEntity::ROOTNAME);
$forumRoot->lock();
$this->entityManager->persist($forumRoot);
$this->entityManager->flush();
$this->addFlash('status', $this->__('Please go to Dizkus admin import to perform full data import.'));
break;
case '4.0.0':
if (!$this->upgrade_settings()) {
return false;
}
// reinstall hooks
$connection = $this->entityManager->getConnection();
$dbName = $this->container->getParameter('database_name');
$connection->executeQuery("DELETE FROM ${dbName}.`hook_area` WHERE `owner` = 'ZikulaDizkusModule'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_binding` WHERE `sowner` = 'ZikulaDizkusModule'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_runtime` WHERE `sowner` = 'ZikulaDizkusModule'");
$connection->executeQuery("DELETE FROM ${dbName}.`hook_subscriber` WHERE `owner` = 'ZikulaDizkusModule'");
break;
case '4.1.0':
// nothing to do now
break;
}
return true;
}
/**
* remove all table prefixes.
*/
public function removeTablePrefixes($prefix)
{
$connection = $this->entityManager->getConnection();
$schemaManager = $connection->getSchemaManager();
$schema = $schemaManager->createSchema();
// remove table prefixes
foreach ($this->importTables as $value) {
if (!$schema->hasTable($prefix . $value)) {
continue;
}
$sql = 'RENAME TABLE ' . $prefix . $value . ' TO ' . $value;
$stmt = $connection->prepare($sql);
try {
$stmt->execute();
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage() . $this->__f('There was a problem recognizing the existing Dizkus tables. Please confirm that your prefix match the actual Dizkus tables in the database. (Current prefix loaded as `%s`)', ['%s' => $prefix]));
return false;
}
}
}
/**
* Mark tables for import with import_ prefix
*/
public function markTablesForImport($prefix)
{
$connection = $this->entityManager->getConnection();
// remove table prefixes
foreach ($this->importTables as $value) {
$sql = 'RENAME TABLE ' . $value . ' TO ' . $prefix . $value;
$stmt = $connection->prepare($sql);
try {
$stmt->execute();
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage() . $this->__f('There was a problem recognizing the existing Dizkus tables. Please confirm that your prefix match the actual Dizkus tables in the database. (Current prefix loaded as `%s`)', ['%s' => $prefix]));
return false;
}
}
}
/**
* get the default module var values.
*
* @return array
*/
public static function getDefaultVars()
{
return [
'posts_per_page' => 15,
'topics_per_page' => 15,
'hot_threshold' => 20,
'email_from' => '', // use system default email
'url_ranks_images' => "ranks",
'post_sort_order' => 'ASC',
'log_ip' => false,
'extendedsearch' => false,
'm2f_enabled' => false,
'favorites_enabled' => false,
'removesignature' => false,
'striptags' => false,
'hooks' => ['providers' => [], 'subscribers' => []],
'rss2f_enabled' => false,
'timespanforchanges' => 24,
'forum_enabled' => true,
'forum_disabled_info' => 'Sorry! The forums are currently off-line for maintenance. Please try later.',
'signaturemanagement' => false,
'signature_start' => '--',
'signature_end' => '--',
'showtextinsearchresults' => false,
'minsearchlength' => 3,
'maxsearchlength' => 30,
'fulltextindex' => false,
'solved_enabled' => false,
'ajax' => false,
'striptagsfromemail' => false,
'indexTo' => '',
'notifyAdminAsMod' => 2,
'defaultPoster' => 2,
'onlineusers_moderatorcheck' => false,
'forum_subscriptions_enabled' => false,
'topic_subscriptions_enabled' => false,
];
}
/**
* Upgrade settings to current version (to_5_0_0)
*/
private function upgrade_settings()
{
$currentModVars = $this->getVars();
$defVars = $this->getDefaultVars();
foreach ($defVars as $key => $defVar) {
if (array_key_exists($key, $currentModVars)) {
$type = gettype($defVar);
switch ($type) {
case 'boolean':
if (in_array($currentModVars[$key], ['yes', 'no'])) {
$var = 'yes' === $currentModVars[$key] ? true : false;
} else {
$var = ((bool) ($currentModVars[$key]));
}
break;
default:
$var = $defVar;
break;
}
if ('defaultPoster' === $key) {
$var = 2; // not bolean anymore assume admin id but maybe guest?
}
}
$this->setVar($key, $var);
}
return true;
}
/**
* find the relative path of this script from current full path.
*
* @param string $path default __DIR__
* @param string $from default 'modules'
*
* @return string
*/
public static function generateRelativePath($path = __DIR__, $from = 'modules')
{
$path = realpath($path);
$parts = explode(DIRECTORY_SEPARATOR, $path);
foreach ($parts as $part) {
if ($part === $from) {
return $path;
}
$path = mb_substr($path, mb_strlen($part . DIRECTORY_SEPARATOR));
}
return $path;
}
}