/
Setup.php
519 lines (443 loc) · 17.3 KB
/
Setup.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
namespace SV\AlertImprovements;
use SV\AlertImprovements\Enum\PopUpReadBehavior;
use SV\AlertImprovements\Job\AlertTotalRebuild;
use SV\AlertImprovements\Job\MigrateAlertPreferences;
use SV\StandardLib\InstallerHelper;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Db\Schema\Alter;
use XF\Db\Schema\Create;
use XF\Entity\AddOn as AddOnEntity;
use XF\Entity\Option as OptionEntity;
use XF\Entity\Template;
use XF\Entity\User as UserEntity;
use XF\Job\PermissionRebuild;
use XF\PreEscaped;
use XF\PrintableException;
use function array_keys;
use function max;
use function microtime;
use function min;
use function strpos;
use function version_compare;
/**
* Class Setup
*
* @package SV\AlertImprovements
*/
class Setup extends AbstractSetup
{
use InstallerHelper {
checkRequirements as protected checkRequirementsTrait;
}
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
public function installStep1(): void
{
$sm = $this->schemaManager();
foreach ($this->getTables() as $tableName => $callback)
{
$sm->createTable($tableName, $callback);
$sm->alterTable($tableName, $callback);
}
foreach ($this->getAlterTables() as $tableName => $callback)
{
if ($sm->tableExists($tableName))
{
$sm->alterTable($tableName, $callback);
}
}
}
public function installStep2(): void
{
$this->applyRegistrationDefaults([
'sv_alerts_popup_read_behavior' => PopUpReadBehavior::PerUser,
'sv_alerts_page_skips_summarize' => 1,
'sv_alerts_summarize_threshold' => 4,
]);
}
public function installStep3(): void
{
$this->applyDefaultPermissions(0);
}
public function upgrade2050001Step1(): void
{
$this->installStep1();
}
public function upgrade2050001Step2(): void
{
$this->installStep2();
}
public function upgrade2050001Step3(): void
{
$this->applyRegistrationDefaults([
'sv_alerts_popup_read_behavior' => PopUpReadBehavior::PerUser,
]);
}
public function upgrade2050201Step1(): void
{
$db = $this->db();
$db->query("
DELETE
FROM xf_user_alert
WHERE summerize_id IS NULL AND action IN ('like_summary', 'rate_summary', 'rating_summary')
");
$db->query('
UPDATE xf_user_alert
SET summerize_id = NULL
WHERE summerize_id IS NOT NULL
');
}
public function upgrade2050201Step2(): void
{
$this->db()->query("
UPDATE xf_user_alert
SET depends_on_addon_id = 'SV/AlertImprovements'
WHERE summerize_id IS NULL AND action IN ('like_summary', 'rate_summary', 'rating_summary')
");
}
public function upgrade2080002Step1(): void
{
$this->installStep1();
}
public function upgrade2080100Step1(): void
{
$this->renameOption('sv_alerts_summerize', 'svAlertsSummarize');
$this->renameOption('sv_alerts_summerize_flood', 'svAlertsSummarizeFlood');
$this->renameOption('sv_alerts_groupByDate', 'svAlertsGroupByDate');
}
public function upgrade2080606Step1(): void
{
$this->installStep1();
}
/**
* @param array $stepParams
* @return array|null
* @throws PrintableException
*/
public function upgrade2081101Step1(array $stepParams): ?array
{
$templateRenames = [
// admin - pre-2.7
'user_edits_alerts' => 'svAlertImprov_user_edits_alerts',
'option_template_registrationDefaults_alerts' => 'svAlertImprov_option_template_registrationDefaults',
// public - pre-2.7
'account_alerts_summary' => 'svAlertImprov_account_alerts_summary',
'account_preferences_alerts_extra' => 'svAlertImprov_account_preferences_extra',
// admin - 2.7 - 2.8
'svAlertsImprov_user_edits_alerts' => 'svAlertImprov_user_edits_alerts',
'svAlertsImprov_option_template_registrationDefaults' => 'svAlertImprov_option_template_registrationDefaults',
// public - 2.7 - 2.8
'svAlertsImprov_account_preferences_extra' => 'svAlertImprov_account_preferences_extra',
'svAlertsImprov_account_alerts_summary' => 'svAlertImprov_account_alerts_summary',
'svAlertsImprov_account_alerts_popup' => 'svAlertImprov_account_alerts_popup',
'svAlertsImprov_alerts' => 'svAlertImprov_alerts',
'svAlertsImprov_macros' => 'svAlertImprov_macros',
];
$finder = \XF::finder('XF:Template')
->where('title', '=', array_keys($templateRenames));
$stepData = $stepParams[2] ?? [];
if (!isset($stepData['max']))
{
$stepData['max'] = $finder->total();
}
$templates = $finder->fetch();
if ($templates->count() === 0)
{
return null;
}
$next = $stepParams[0] ?? 0;
$maxRunTime = max(min(\XF::app()->config('jobMaxRunTime'), 4), 1);
$startTime = microtime(true);
foreach($templates as $template)
{
/** @var Template $template*/
if (empty($templateRenames[$template->title]))
{
continue;
}
$next++;
$template->title = $templateRenames[$template->title];
$template->version_id = 2081101;
$template->version_string = '2.8.11';
$template->save(false, true);
if (microtime(true) - $startTime >= $maxRunTime)
{
break;
}
}
return [
$next,
"{$next} / {$stepData['max']}",
$stepData
];
}
public function upgrade2081101Step2()
{
$this->renamePhrases([
'svAlerts_select_all_for' => 'svAlertImprov_select_all_for',
'sv_alertimprovements.today' => 'svAlertImprov_date.today',
'sv_alertimprovements.yesterday' => 'svAlertImprov_date.yesterday',
'sv_alerts_page_and_summary' => 'svAlertImprov_alerts_page_and_summary',
'sv_alert_preferences' => 'svAlertImprov_alert_preferences',
'sv_on_viewing_alerts_page' => 'svAlertImprov_on_viewing_alerts_page',
'sv_resummarize_alerts' => 'svAlertImprov_resummarize_alerts',
'sv_unread_alert' => 'svAlertImprov_new_or_unread_alert',
]);
}
public function upgrade2081500Step2(): void
{
// purge broken jobs
$this->db()->query('DELETE FROM xf_job WHERE unique_key IN (?,?)', ['sViewedAlertCleanup', 'svUnviewedAlertCleanup']);
}
public function upgrade2090005Step1(): void
{
$this->schemaManager()->alterTable('xf_user_option', function(Alter $table) {
$table->dropColumns('sv_alerts_page_skips_mark_read');
});
}
public function upgrade1683812804Step1(): void
{
$this->installStep1();
}
public function upgrade1683812804Step2(): void
{
$this->installStep2();
}
public function upgrade1683812804Step3(): void
{
$this->query('
INSERT INTO xf_sv_user_alert_summary (alert_id, alerted_user_id, content_type, content_id, `action`)
SELECT alert_id, alerted_user_id, content_type, content_id, `action`
FROM xf_user_alert
WHERE summerize_id IS NULL AND `action` LIKE \'%_summary\';
');
}
public function upgrade1685991237Step1(): void
{
$this->installStep1();
}
public function upgrade1685991237Step2(): void
{
$sm = $this->schemaManager();
if ($sm->columnExists('xf_user_option', 'sv_alerts_popup_skips_mark_read'))
{
/** @noinspection SqlResolve */
$this->db()->query('
UPDATE xf_user_option
SET sv_alerts_popup_read_behavior = ?
WHERE sv_alerts_popup_skips_mark_read = 1
', [PopUpReadBehavior::NeverMarkRead]);
}
}
public function upgrade1685991237Step3(): void
{
$this->schemaManager()->alterTable('xf_user_option', function (Alter $table) {
$table->dropColumns('sv_alerts_popup_skips_mark_read');
});
}
public function upgrade1685991237Step4(): void
{
$option = \XF::app()->finder('XF:Option')
->where('option_id', '=', 'registrationDefaults')
->fetchOne();
assert($option instanceof OptionEntity);
$registrationDefaults = $option->option_value;
$intVal = (int)($registrationDefaults['sv_alerts_popup_skips_mark_read'] ?? 0);
if ($intVal === 1)
{
$registrationDefaults['sv_alerts_popup_read_behavior'] = PopUpReadBehavior::NeverMarkRead;
}
else
{
$registrationDefaults['sv_alerts_popup_read_behavior'] = PopUpReadBehavior::PerUser;
}
$option->option_value = $registrationDefaults;
$option->saveIfChanged();
}
public function upgrade1695694021Step1(): void
{
$this->renamePhrases([
'svAlertImprov_alerts_page_and_summary' => 'svAlertImprov_alert_preferences_header',
]);
}
public function upgrade1685991237Step5(): void
{
$db = $this->db();
$perUser = $db->quote(PopUpReadBehavior::PerUser);
$neverMarkRead = $db->quote(PopUpReadBehavior::NeverMarkRead);
$db->query("
UPDATE xf_change_log
SET field = ?,
old_value = (case when old_value = 0 then $perUser when old_value = 1 then $neverMarkRead else old_value end),
new_value = (case when new_value = 0 then $perUser when new_value = 1 then $neverMarkRead else new_value end)
WHERE field = ?
", ['sv_alerts_popup_read_behavior', 'sv_alerts_popup_skips_mark_read']);
}
public function uninstallStep1(): void
{
$sm = $this->schemaManager();
foreach ($this->getTables() as $tableName => $callback)
{
$sm->dropTable($tableName);
}
}
public function uninstallStep2(): void
{
$sm = $this->schemaManager();
foreach ($this->getRemoveAlterTables() as $tableName => $callback)
{
if ($sm->tableExists($tableName))
{
$sm->alterTable($tableName, $callback);
}
}
}
public function uninstallStep3(): void
{
$this->db()->query("
DELETE
FROM xf_user_alert
WHERE depends_on_addon_id = 'SV/AlertImprovements'
");
}
public function postInstall(array &$stateChanges): void
{
parent::postInstall($stateChanges);
\XF::app()->jobManager()->enqueueUnique('svMigrateAlertPreferences', MigrateAlertPreferences::class, [], false);
}
public function postUpgrade($previousVersion, array &$stateChanges): void
{
$previousVersion = (int)$previousVersion;
parent::postUpgrade($previousVersion, $stateChanges);
if ($this->applyDefaultPermissions($previousVersion))
{
\XF::app()->jobManager()->enqueueUnique('permissionRebuild', PermissionRebuild::class, [], true);
}
if ($previousVersion >= 2080000 && $previousVersion < 2080400)
{
\XF::app()->jobManager()->enqueueUnique('svAlertTotalRebuild', AlertTotalRebuild::class, [], true);
}
if ($previousVersion < 1683812804)
{
\XF::app()->jobManager()->enqueueUnique('svMigrateAlertPreferences', MigrateAlertPreferences::class, [], false);
}
}
protected function applyDefaultPermissions(int $previousVersion = 0): bool
{
$applied = false;
if ($previousVersion < 1695694020)
{
$this->applyGlobalPermissionByGroup('general', 'svCustomizeAdvAlertPrefs', [
UserEntity::GROUP_REG,
UserEntity::GROUP_MOD,
UserEntity::GROUP_ADMIN,
]);
$applied = true;
}
return $applied;
}
public function getTables(): array
{
$tables = [];
$tables['xf_sv_user_alert_rebuild'] = function ($table) {
/** @var Alter|Create $table */
$this->addOrChangeColumn($table, 'user_id', 'int')->primaryKey();
$this->addOrChangeColumn($table, 'rebuild_date', 'int');
$table->addKey('rebuild_date');
};
$tables['xf_sv_user_alert_summary'] = function ($table ) {
/** @var Alter|Create $table */
$this->addOrChangeColumn($table, 'alert_id', 'int')->primaryKey();
$this->addOrChangeColumn($table, 'alerted_user_id', 'int');
$this->addOrChangeColumn($table, 'content_type', 'varbinary', 25);
$this->addOrChangeColumn($table, 'content_id', 'int');
$this->addOrChangeColumn($table, 'action', 'varbinary', 30);
$table->addKey(['alerted_user_id', 'content_type', 'content_id', 'action'], 'alerted_user_id_content_type_content_id_action');
};
return $tables;
}
public function getAlterTables(): array
{
$tables = [];
$tables['xf_user_option'] = function (Alter $table) {
$this->addOrChangeColumn($table, 'sv_alerts_popup_read_behavior', 'enum')
->values(PopUpReadBehavior::get())
->setDefault(PopUpReadBehavior::PerUser);
$this->addOrChangeColumn($table, 'sv_alerts_page_skips_summarize', 'tinyint')->setDefault(1);
$this->addOrChangeColumn($table, 'sv_alerts_summarize_threshold', 'int')->setDefault(4);
$this->addOrChangeColumn($table, 'sv_alert_pref', 'blob')->nullable()->setDefault(null);
};
$tables['xf_user_alert'] = function (Alter $table) {
$hasReadDate = (bool)$table->getColumnDefinition('read_date');
$hasAutoRead = (bool)$table->getColumnDefinition('auto_read');
$this->addOrChangeColumn($table, 'read_date', 'int')->setDefault(0)->after('view_date');
$col2 = $this->addOrChangeColumn($table, 'auto_read', 'tinyint')->setDefault(1);
if ($hasReadDate || !$hasAutoRead)
{
// XF always does modifies before adds.
$col2->after('read_date');
}
// ensure summerize_id type matches the alert_id column
$rawType = $table->getColumnDefinition('alert_id')['Type'] ?? 'int(10) unsigned';
$idType = strpos($rawType, 'bigint') !== false ? 'bigint' : 'int';
$this->addOrChangeColumn($table, 'summerize_id', $idType)->nullable(true)->setDefault(null);
// index is superseded
$table->dropIndexes(['contentType_contentId', 'alertedUserId_viewDate']);
// for basic content type lookups
$table->addKey(['content_type', 'content_id', 'user_id']);
// primarily for looking up active alerts for a user from a set of content -- content_id will generally
// be a multiple element list, so further columns aren't particularly helpful
$table->addKey(['alerted_user_id', 'content_type', 'content_id']);
// for unviewed calculations
$table->addKey(['alerted_user_id', 'view_date']);
// for summarization
$table->addKey(['alerted_user_id', 'summerize_id'], 'alerted_user_id_summerize_id');
};
$tables['xf_user'] = function (Alter $table) {
$this->addOrChangeColumn($table,'alerts_unviewed', 'smallint', 5)->setDefault(0)->after('trophy_points');
};
return $tables;
}
protected function getRemoveAlterTables(): array
{
$tables = [];
$tables['xf_user_option'] = function (Alter $table) {
$table->dropColumns([
'sv_alert_pref',
'sv_alerts_popup_skips_mark_read',
'sv_alerts_popup_read_behavior',
'sv_alerts_page_skips_mark_read',
'sv_alerts_page_skips_summarize',
'sv_alerts_summarize_threshold',
]);
};
$tables['xf_user_alert'] = function (Alter $table) {
$table->dropIndexes('summerize_id');
};
return $tables;
}
/**
* @param array $errors
* @param array $warnings
* @noinspection PhpMissingParentCallCommonInspection
*/
public function checkRequirements(&$errors = [], &$warnings = []): void
{
$this->checkRequirementsTrait($errors, $warnings);
if ($this->isCliRecommendedCheck(0, 0, 500000, 50000))
{
/** @var ?AddOnEntity $addon */
$addon = $this->addOn->getInstalledAddOn();
if ($addon !== null && version_compare($addon->version_string, '2.9.5', '<='))
{
$html = 'For busy sites, it is recommended to close the site when upgrading from before v2.10.0 as to avoid alerts being generated with the wrong auto-read/mark-as-read configuration while user settings are migrated';
$warnings[] = new PreEscaped($html);
}
}
}
}