-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetup.php
758 lines (677 loc) · 22.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
<?php
namespace TickTackk\ChangeContentOwner;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\App as BaseApp;
use XF\Entity\Option as OptionEntity;
use XF\Job\Manager as JobManager;
/**
* Class Setup
*
* @package TickTackk\ChangeContentOwner
*/
class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
public function installStep1() : void
{
// thread
$this->applyGlobalPermission(
'forum', 'changeThreadOwner',
'forum', 'manageAnyThread'
);
$this->applyGlobalPermission(
'forum', 'changeThreadDate',
'forum', 'manageAnyThread'
);
// post
$this->applyGlobalPermission(
'forum', 'changePostOwner',
'forum', 'manageAnyThread'
);
$this->applyGlobalPermission(
'forum', 'changePostDate',
'forum', 'manageAnyThread'
);
// profile post
$this->applyGlobalPermission(
'profilePost', 'changeProfilePostOwner',
'profilePost', 'editAny'
);
$this->applyGlobalPermission(
'profilePost', 'changeProfilePostDate',
'profilePost', 'editAny'
);
// profile post comment
$this->applyGlobalPermission(
'profilePost', 'changeCommentOwner',
'profilePost', 'editAny'
);
$this->applyGlobalPermission(
'profilePost', 'changeCommentDate',
'profilePost', 'editAny'
);
}
public function installStep2() : void
{
$addOns = $this->app()->container('addon.cache');
$xfmgSupport = $addOns['XFMG'] ?? 0 >= 1000070;
if ($xfmgSupport)
{
// media
$this->applyGlobalPermission(
'xfmg', 'changeMediaOwner',
'forum', 'manageAnyThread'
);
$this->applyGlobalPermission(
'xfmg', 'changeMediaDate',
'forum', 'manageAnyThread'
);
// album
$this->applyGlobalPermission(
'xfmg', 'changeAlbumOwner',
'forum', 'manageAnyThread'
);
$this->applyGlobalPermission(
'xfmg', 'changeAlbumDate',
'forum', 'manageAnyThread'
);
// comments
$this->applyGlobalPermission(
'xfmg', 'changeCommentOwner',
'forum', 'editAny'
);
$this->applyGlobalPermission(
'xfmg', 'changeCommentDate',
'forum', 'editAny'
);
}
}
public function upgrade2000011Step1() : void
{
// thread
$this->applyGlobalPermission(
'forum', 'changeThreadOwner',
'forum', 'changeThreadAuthor'
);
$this->applyContentPermission(
'forum', 'changeThreadOwner',
'forum', 'changeThreadAuthor'
);
$this->applyGlobalPermission(
'forum', 'changeThreadDate',
'forum', 'changeThreadOwner'
);
$this->applyContentPermission(
'forum', 'changeThreadDate',
'forum', 'changeThreadOwner'
);
// post
$this->applyGlobalPermission(
'forum', 'changePostOwner',
'forum', 'changePostAuthor'
);
$this->applyContentPermission(
'forum', 'changePostOwner',
'forum', 'changePostAuthor'
);
$this->applyGlobalPermission(
'forum', 'changePostDate',
'forum', 'changePostOwner'
);
$this->applyContentPermission(
'forum', 'changePostDate',
'forum', 'changePostOwner'
);
// profile post
$this->applyGlobalPermission(
'profilePost', 'changeProfilePostOwner',
'profilePost', 'changeProfilePostAuthor'
);
$this->applyGlobalPermission(
'profilePost', 'changeProfilePostDate',
'profilePost', 'changeProfilePostOwner'
);
// profile post comment
$this->applyGlobalPermission(
'profilePost', 'changeCommentOwner',
'profilePost', 'changeProfilePostOwner'
);
$this->applyGlobalPermission(
'profilePost', 'changeCommentDate',
'profilePost', 'changeProfilePostDate'
);
$addOns = $this->app()->container('addon.cache');
$xfmgSupport = $addOns['XFMG'] ?? 0 >= 1000070;
if ($xfmgSupport)
{
// media item
$this->applyGlobalPermission(
'xfmg', 'changeMediaDate',
'xfmg', 'changeMediaOwner'
);
$this->applyContentPermission(
'xfmg', 'changeMediaDate',
'xfmg', 'changeMediaOwner'
);
// album
$this->applyGlobalPermission(
'xfmg', 'changeAlbumDate',
'xfmg', 'changeAlbumOwner'
);
$this->applyContentPermission(
'xfmg', 'changeAlbumDate',
'xfmg', 'changeAlbumOwner'
);
// comment
$this->applyGlobalPermission(
'xfmg', 'changeCommentOwner',
'xfmg', 'changeCommentAuthor'
);
$this->applyContentPermission(
'xfmg', 'changeCommentOwner',
'xfmg', 'changeCommentAuthor'
);
$this->applyGlobalPermission(
'xfmg', 'changeCommentDate',
'xfmg', 'changeCommentOwner'
);
$this->applyContentPermission(
'xfmg', 'changeCommentDate',
'xfmg', 'changeCommentOwner'
);
}
}
public function upgrade2000013Step1() : void
{
$this->upgrade2000011Step1();
}
/**
* @throws \XF\PrintableException
*/
public function upgrade2000013Step2() : void
{
/** @var OptionEntity $option */
$option = $this->app()->find('XF:Option', 'tckChangeContentOwner_defaultNewDateTimeInterval');
if ($option)
{
$optionValue = $option->option_value;
$seconds = $this->db()->fetchOne('SELECT option_value FROM xf_option WHERE option_id = ?', 'tckChangeContentOwner_timeInterval');
$optionValue['seconds'] = (int) $seconds;
$option->option_value = $optionValue;
$option->save();
}
}
public function upgrade2000270Step1() : void
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__,
'TickTackk\ChangeContentOwner:Upgrade\RebuildModeratorLogAction',
[],
false
);
}
public function upgrade2000470Step1() : void
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__,
'TickTackk\ChangeContentOwner:Upgrade\RebuildThreadUserPostCount',
[],
false
);
}
public function upgrade2000670Step1() : void
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__,
'TickTackk\ChangeContentOwner:Upgrade\RebuildNewsFeedEntryEventDate',
[],
false
);
}
public function upgrade2000770Step1() : void
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__,
'TickTackk\ChangeContentOwner:Upgrade\RebuildModeratorLogAction',
[],
false
);
}
public function upgrade2001170Step1() : void
{
foreach (['thread', 'post', 'xfmg_media', 'xfmg_album', 'xfmg_comment', 'graph'] AS $contentType)
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__ . '-' . $contentType,
'TickTackk\ChangeContentOwner:Upgrade\RebuildAttachmentOwner',
['content_type' => $contentType],
false
);
}
}
/**
* @since 2.0.14
*
* @param array $stepParams
*
* @return array|bool
*/
public function upgrade2001470Step1(array $stepParams)
{
$position = !empty($stepParams[0]) ? $stepParams[0] : 0;
$perPage = 1000;
$db = $this->db();
$db->beginTransaction();
if (!isset($stepData['max']))
{
$stepData['max'] = $db->fetchOne("SELECT MAX(thread_id) FROM xf_thread");
}
$threadIds = $db->fetchAllColumn($db->limit(
"
SELECT DISTINCT post.thread_id
FROM xf_post AS post
INNER JOIN xf_thread AS thread
ON (post.thread_id = thread.thread_id)
WHERE post.message_state = 'visible'
AND thread.last_post_date < post.post_date
AND thread.thread_id > ?
ORDER BY post.thread_id ASC
", $perPage
), $position);
if (!$threadIds)
{
$db->commit();
return true;
}
$startTime = microtime(true);
$maxRunTime = $this->app()->config('jobMaxRunTime');
foreach ($threadIds AS $threadId)
{
$position = $threadId;
$lastPost = $db->fetchRow("
SELECT post_id, post_date, user_id, username
FROM xf_post USE INDEX (thread_id_post_date)
WHERE thread_id = ?
AND message_state = 'visible'
ORDER BY post_date DESC
LIMIT 1
", $threadId);
if (!$lastPost) // This can be first post as well :)
{
continue;
}
$db->update('xf_thread', [
'last_post_id' => (int) $lastPost['post_id'],
'last_post_date' => (int) $lastPost['post_date'],
'last_post_user_id' => (int) $lastPost['user_id'],
'last_post_username' => $lastPost['username'] ?: '-'
], 'thread_id = ?', $threadId);
if ($maxRunTime && microtime(true) - $startTime > $maxRunTime)
{
break;
}
}
$db->commit();
return [
$position,
"{$position} / {$stepData['max']}",
$stepData
];
}
/**
* @since 2.0.15
*
* @param array $stepParams
*
* @return array|bool
*/
public function upgrade2001470Step2(array $stepParams)
{
$position = !empty($stepParams[0]) ? $stepParams[0] : 0;
$perPage = 1000;
$db = $this->db();
$db->beginTransaction();
if (!isset($stepData['max']))
{
$stepData['max'] = $db->fetchOne("SELECT MAX(node_id) FROM xf_forum");
}
$profilePostIds = $db->fetchAllColumn($db->limit(
"
SELECT DISTINCT comment.profile_post_id
FROM xf_profile_post_comment AS comment
INNER JOIN xf_profile_post AS profile_post
ON (comment.profile_post_id = profile_post.profile_post_id)
WHERE comment.message_state = 'visible'
AND ((profile_post.first_comment_date < comment.comment_date) OR (profile_post.last_comment_date > comment.comment_date))
AND comment.profile_post_id > ?
ORDER BY comment.profile_post_id ASC
", $perPage
), $position);
if (!$profilePostIds)
{
$db->commit();
return true;
}
$startTime = microtime(true);
$maxRunTime = $this->app()->config('jobMaxRunTime');
foreach ($profilePostIds AS $profilePostId)
{
$position = $profilePostId;
$firstComment = $db->fetchRow("
SELECT profile_post_comment_id, comment_date, user_id, username
FROM xf_profile_post_comment
WHERE profile_post_id = ?
AND message_state = 'visible'
ORDER BY comment_date
LIMIT 1
", $profilePostId);
if ($firstComment)
{
$lastComment = $db->fetchRow("
SELECT profile_post_comment_id, comment_date, user_id, username
FROM xf_profile_post_comment
WHERE profile_post_id = ?
AND message_state = 'visible'
ORDER BY comment_date DESC
LIMIT 1
", $profilePostId); // this can be the first comment as well ;)
}
else
{
$firstComment = [
'profile_post_comment_id' => 0,
'comment_date' => 0,
'user_id' => 0,
'username' => ''
];
$lastComment = $firstComment;
}
$latestCommentIds = [];
if ($firstComment['profile_post_comment_id'])
{
$comments = $db->fetchAllKeyed($db->limit(
"
SELECT profile_post_id, profile_post_comment_id, message_state, user_id
FROM xf_profile_post_comment
WHERE profile_post_id = ?
ORDER BY comment_date DESC
", 20
), 'profile_post_comment_id', $profilePostId);
$visCount = 0;
$latestComments = [];
foreach ($comments AS $commentId => $comment)
{
if ($comment['message_state'] === 'visible')
{
$visCount++;
}
$latestComments[$commentId] = [$comment['message_state'], $comment['user_id']];
if ($visCount === 3)
{
break;
}
}
$latestCommentIds = array_reverse($latestComments, true);
}
$db->update('xf_profile_post', [
'first_comment_date' => (int) $firstComment['profile_post_comment_id'],
'last_comment_date' => (int) $lastComment['profile_post_comment_id'],
'latest_comment_ids' => json_encode($latestCommentIds, JSON_PARTIAL_OUTPUT_ON_ERROR)
], 'profile_post_id = ?', $profilePostId);
if ($maxRunTime && microtime(true) - $startTime > $maxRunTime)
{
break;
}
}
$db->commit();
return [
$position,
"{$position} / {$stepData['max']}",
$stepData
];
}
/**
* @since 2.0.15
*
* @param array $stepParams
*
* @return array|bool
*/
public function upgrade2001470Step3(array $stepParams)
{
return $this->rebuildXFMGContentLastComment(
$stepParams,
'xf_mg_media_item',
'media_id',
'xfmg_media'
);
}
/**
* @since 2.0.15
*
* @param array $stepParams
*
* @return array|bool
*/
public function upgrade2001470Step4(array $stepParams)
{
return $this->rebuildXFMGContentLastComment(
$stepParams,
'xf_mg_album',
'album_id',
'xfmg_album'
);
}
/**
* @since 2.0.14
*
* @param array $stepParams
* @param string $tableName
* @param string $primaryKey
* @param string $contentType
* @param int $perPage
*
* @return array|bool
*/
protected function rebuildXFMGContentLastComment(
array $stepParams,
string $tableName,
string $primaryKey,
string $contentType,
int $perPage = 1000
)
{
if (!$this->tableExists($tableName))
{
return true;
}
$position = !empty($stepParams[0]) ? $stepParams[0] : 0;
$db = $this->db();
$db->beginTransaction();
if (!isset($stepData['max']))
{
$stepData['max'] = $db->fetchOne("SELECT MAX({$primaryKey}) FROM {$tableName}");
}
$quotedContentType = $db->quote($contentType);
$contentIds = $db->fetchAllColumn($db->limit(
"
SELECT DISTINCT comment.content_id
FROM xf_mg_comment AS comment
INNER JOIN {$tableName} AS content
ON (content.{$primaryKey} = comment.content_id AND comment.content_type = {$quotedContentType})
WHERE comment.content_type = {$quotedContentType}
AND comment.content_id > ?
AND comment.comment_state = 'visible'
AND content.last_comment_date < comment.comment_date
ORDER BY comment.content_id ASC
", $perPage
), $position);
if (!$contentIds)
{
$db->commit();
return true;
}
$startTime = microtime(true);
$maxRunTime = $this->app()->config('jobMaxRunTime');
foreach ($contentIds AS $contentId)
{
$position = $contentId;
$lastComment = $db->fetchRow("
SELECT comment_id AS last_comment_id,
comment_date AS last_comment_date,
user_id AS last_comment_user_id,
username AS last_comment_username
FROM xf_mg_comment
WHERE content_id = ?
AND content_type = ?
AND comment_state = 'visible'
ORDER BY comment_date DESC
LIMIT 1
", [$contentId, $contentType]);
if (!$lastComment)
{
$lastComment = [
'comment_id' => 0,
'comment_date' => 0,
'user_id' => 0,
'username' => ''
];
}
$db->update($tableName, $lastComment, "{$primaryKey} = ?", $contentId);
if ($maxRunTime && microtime(true) - $startTime > $maxRunTime)
{
break;
}
}
$db->commit();
return [
$position,
"{$position} / {$stepData['max']}",
$stepData
];
}
/**
* @since 2.0.15
*
* @param array $stepParams
*
* @return array|bool
*/
public function upgrade2001570Step1(array $stepParams)
{
$position = !empty($stepParams[0]) ? $stepParams[0] : 0;
$perPage = 1000;
$db = $this->db();
$db->beginTransaction();
if (!isset($stepData['max']))
{
$stepData['max'] = $db->fetchOne("SELECT MAX(node_id) FROM xf_forum");
}
$nodeIds = $db->fetchAllColumn($db->limit(
"
SELECT DISTINCT forum.node_id
FROM xf_forum AS forum
INNER JOIN xf_thread AS thread
ON (thread.node_id = forum.node_id)
WHERE thread.discussion_state = 'visible'
AND thread.discussion_type <> 'redirect'
AND thread.last_post_date > forum.last_post_date
AND forum.node_id > ?
ORDER BY forum.node_id ASC
", $perPage
), $position);
if (!$nodeIds)
{
$db->commit();
return true;
}
$startTime = microtime(true);
$maxRunTime = $this->app()->config('jobMaxRunTime');
foreach ($nodeIds AS $nodeId)
{
$position = $nodeId;
$lastThread = $db->fetchRow("
SELECT post.*, thread.*
FROM xf_thread AS thread
INNER JOIN xf_post AS post
ON (post.thread_id = thread.thread_id)
WHERE thread.node_id = ?
AND thread.discussion_state = 'visible'
AND thread.discussion_type <> 'redirect'
AND post.post_id = thread.last_post_id
ORDER BY thread.last_post_date DESC
LIMIT 1
", $nodeId);
if (!$lastThread)
{
$lastThread = [
'post_id' => 0,
'post_date' => 0,
'user_id' => 0,
'username' => '', // this should be empty string or '-' ?
'thread_id' => 0,
'title' => '',
'prefix_id' => 0
];
}
$db->update('xf_forum', [
'last_post_id' => (int) $lastThread['post_id'],
'last_post_date' => (int) $lastThread['post_date'],
'last_post_user_id' => (int) $lastThread['user_id'],
'last_post_username' => $lastThread['username'] ?: '-',
'last_thread_id' => (int) $lastThread['thread_id'],
'last_thread_title' => (string) $lastThread['title'],
'last_thread_prefix_id' => (int) $lastThread['prefix_id']
], 'node_id = ?', $nodeId);
if ($maxRunTime && microtime(true) - $startTime > $maxRunTime)
{
break;
}
}
$db->commit();
return [
$position,
"{$position} / {$stepData['max']}",
$stepData
];
}
/**
* @param array $errors
* @param array $warnings
*/
public function checkRequirements(&$errors = [], &$warnings = []) : void
{
$xfaCSVGrapher = $this->app()->addOnManager()->getById('XFA/CSVGrapher');
if ($xfaCSVGrapher)
{
$installed = $xfaCSVGrapher->getInstalledAddOn();
if ($installed && $installed->version_id < 904000019)
{
$warnings[] = 'You must upgrade to [XFA] Datalogger 4.0.0 Alpha 9 or later.';
}
}
}
/**
* @return JobManager
*/
protected function jobManager() : JobManager
{
return $this->app()->jobManager();
}
/**
* @return BaseApp
*/
protected function app() : BaseApp
{
if (method_exists(get_parent_class($this), 'app'))
{
return parent::app();
}
return parent::app();
}
}