-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmergeSMF.php
1469 lines (1255 loc) · 48.7 KB
/
mergeSMF.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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
v2.1.3
Modified version for SMF 2.1.3 to fold in new SMF 2.1 record types, pm_labels, pm_labeled_messages, user_alerts_prefs and user_likes. April, 2023
Modified version for SMF 2.0.17 to better duplicate member handling & logging of any member renames. Thanks to GL700Wing, Dec, 2020
Modified version for SMF 2.0.17 to add several new record types, use mysqli, enhance error reporting, address column conflicts. shawnb61, Sept, 2020
Modified version for SMF 2.0.6 by bfeist (fall, 2013). Let's call this new version 2.0.
Modified version for SMF 1.1RC3 by Resourcez at resourcez dot biz (way back in 2006)
Many thanks for throwing it together and giving us something to work with.
Original version for SMF 1.0.x by Oldiesmann at SimpleMachines dot org
Description:
This script will merge two SMF 2.1 forums. It will takes the boards, topics, members, messages, etc from a SECONDARY forum and merge them into a PRIMARY forum.
You need to be comfortable with PHP, and wrapping your head around databases etc to use this script. It works, but it will take some trial and error to get everything right.
Instructions:
1. Restore your two forums and get them working side-by-side on your site, both installations within the same database (just use a different database prefix like smf2_ for the SECONDARY one prior to the DB restore).
2. Edit this file in a text editor and change the prefix to match the database prefix of the PRIMARY smf board (define('PRIMARY_DB_PREFIX', 'smf_') - change smf_ to whatever you used...).
3. Also set $secondary_suffix to be the value appended to the end of member names & group names & other records in order to make them unique, in case of conflicts with the primary forum.
4. Save this modified file, put it in the directory for the SECONDARY smf installation and run it (just like you would run install.php or the converter).
5. Follow the instructions, clicking the "Continue" link as needed.
6. Pay careful attention to any errors reported.
7. When completed, copy all files in your SECONDARY attachments/to_move_to_primary directory to the primary attachments directory.
8. Copy all files in your SECONDARY custom_avatar/to_move_to_primary directory to the primary custom_avatar directory. Be careful to confirm the name of the directory, it is different for different forums.
9. Copy all files in your SECONDARY avatars directory to the primary avatars directory.
10. Once you're done, if everything worked, login to your PRIMARY SMF board, go to Admin -> Forum Maintenance and click on "Recount all totals and statistics" - this will update everything for you.
11. Go to Admin -> Forum Maintenance and click on "Find and repair any errors". Usually just some subject cache cleanup.
12. Go to Admin -> Forum Maintenance and click on "Empty SMF's Cache".
13. Check for any members in the new (merged) PRIMARY SMF installation that end with your $secondary_suffix. If desired, these members can be merged with the members of the same name using a tool like SMF Admin Toolbox (http://www.simplemachines.org/community/index.php?topic=470463.0)
14. Carefully audit all board permissions, membergroups, and subscriptions.
*/
// *** User configs - important! ***
define('PRIMARY_DB_PREFIX', 'smf_');
$secondary_suffix = '-smf2';
// *** End of user configs ***
include_once('Settings.php');
if (empty($db_port))
$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd, $db_name);
else
$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd, $db_name, $db_port);
if (!empty($db_character_set))
mysqli_query($db_connection, "SET NAMES {$db_character_set}");
// Might as well try...
@set_time_limit(6000);
@ini_set('memory_limit', '512M');
function head()
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><title>SMF Board Merger</title>
<style type="text/css">
body {font-family: Verdana, sans-serif; background-color: #D4D4D4; margin: 0; }
div#header {background-color: white; padding: 12px 4% 12px 4%; font-family: Georgia, serif; font-size: xx-large; border-bottom: 1px solid black; height: 60px; text-align:center; }
div#content {margin: 20px 30px; }
</style>
<body>
<div id="header" title="Resourcez">
<div>SMF Board Merger</div>
<div style="font-size:14px;font-weight:bold;">(You should be running this script on your SECONDARY site - the one you wish to copy content from)</div>
</div>
<div id="content">';
}
function foot()
{
echo '</div></body></html>';
}
/*****************************************************************
*
* This table drives all the action...
*
* <Tables> acted upon, the "driver" tables
* incby = rec key value which all related tables/fields must be incremented by (from primary)
* rels = list of related records to update
* <tables> => array of fields to be updated by the value of incby
* nzrels = list of related records to update - ***but only if the field is non-zero***
* <tables> => array of fields to be updated by the value of incby
* userfunc = user-defined callback for any special logic
*
*****************************************************************/
$tables = array(
'boards' => array (
'incby' => 'id_board',
'rels' => array(
'boards' => array('id_board', 'board_order'),
'messages' => array('id_board'),
'moderators' => array('id_board'),
'topics' => array('id_board'),
'log_reported' => array('id_board'),
),
'nzrels' => array(
'boards' => array('id_parent'),
'log_actions' => array('id_board'),
'log_notify' => array('id_board'),
),
),
'categories' => array (
'incby' => 'id_cat',
'rels' => array(
'categories' => array('id_cat', 'cat_order'),
'boards' => array('id_cat'),
),
),
'messages' => array (
'incby' => 'id_msg',
'rels' => array(
'messages' => array('id_msg'),
'topics' => array('id_first_msg', 'id_last_msg'),
'log_reported' => array('id_msg'),
'user_likes' => array('content_id'),
),
'nzrels' => array(
'boards' => array('id_last_msg'),
'log_actions' => array('id_msg'),
'attachments' => array('id_msg'),
),
),
'attachments' => array (
'incby' => 'id_attach',
'rels' => array(
'attachments' => array('id_attach'),
),
'nzrels' => array(
'attachments' => array('id_thumb'),
),
'userfunc' => 'renameattachments',
),
'members' => array (
'incby' => 'id_member',
'rels' => array(
'members' => array('id_member'),
'pm_recipients' => array('id_member'),
'log_notify' => array('id_member'),
'group_moderators' => array('id_member'),
'moderators' => array('id_member'),
'log_subscribed' => array('id_member'),
'log_reported' => array('id_member'),
'log_reported_comments' => array('id_member'),
'pm_labels' => array('id_member'),
),
'nzrels' => array(
'messages' => array('id_member'),
'polls' => array('id_member'),
'personal_messages' => array('id_member_from'),
'log_actions' => array('id_member'),
'log_polls' => array('id_member'),
'topics' => array('id_member_started', 'id_member_updated'),
'attachments' => array('id_member'),
'ban_items' => array('id_member'),
'user_alerts_prefs' => array('id_member'),
'user_likes' => array('id_member'),
),
'userfunc' => 'renamemembers',
),
'personal_messages' => array (
'incby' => 'id_pm',
'rels' => array(
'personal_messages' => array('id_pm', 'id_pm_head'),
'pm_recipients' => array('id_pm'),
'pm_labeled_messages' => array('id_pm'),
),
),
'polls' => array (
'incby' => 'id_poll',
'rels' => array(
'polls' => array('id_poll'),
'log_polls' => array('id_poll'),
'poll_choices' => array('id_poll'),
),
'nzrels' => array(
'topics' => array('id_poll'),
),
),
'poll_choices' => array (
'incby' => 'id_choice',
'rels' => array(
'poll_choices' => array('id_choice'),
'log_polls' => array('id_choice'),
),
),
'topics' => array (
'incby' => 'id_topic',
'rels' => array(
'topics' => array('id_topic'),
'messages' => array('id_topic'),
'log_reported' => array('id_topic'),
),
'nzrels' => array(
'log_actions' => array('id_topic'),
'log_notify' => array('id_topic'),
),
),
'log_actions' => array (
'incby' => 'id_action',
'rels' => array(
'log_actions' => array('id_action'),
),
'userfunc' => 'packed_strings',
),
'membergroups' => array (
'incby' => 'id_group',
'rels' => array(
'membergroups' => array('id_group'),
),
'nzrels' => array(
'members' => array('id_group'),
'group_moderators' => array('id_group'),
'board_permissions' => array('id_group'),
'permissions' => array('id_group'),
'membergroups' => array('id_parent'),
'log_subscribed' => array('old_id_group'),
),
'userfunc' => 'cleanrenamegroups',
),
'permission_profiles' => array (
'userfunc' => 'processprofiles',
),
'subscriptions' => array (
'incby' => 'id_subscribe',
'rels' => array(
'subscriptions' => array('id_subscribe'),
'log_subscribed' => array('id_subscribe'),
),
),
'log_subscribed' => array (
'incby' => 'id_sublog',
'rels' => array(
'log_subscribed' => array('id_sublog'),
),
),
'log_reported' => array (
'incby' => 'id_report',
'rels' => array(
'log_reported' => array('id_report'),
'log_reported_comments' => array('id_report'),
),
),
'log_reported_comments' => array (
'incby' => 'id_comment',
'rels' => array(
'log_reported_comments' => array('id_comment'),
),
),
'ban_groups' => array (
'incby' => 'id_ban_group',
'rels' => array(
'ban_groups' => array('id_ban_group'),
'ban_items' => array('id_ban_group'),
),
),
'ban_items' => array (
'incby' => 'id_ban',
'rels' => array(
'ban_items' => array('id_ban'),
),
),
'pm_labels' => array (
'incby' => 'id_label',
'rels' => array(
'pm_labels' => array('id_label'),
'pm_labeled_messages' => array('id_label'),
),
),
'user_alerts_prefs' => array(
'userfunc' => 'updboardtopicwatches',
),
// These records don't drive updates to other related records.
// They only get copied over once updated themselves.
'board_permissions' => array(),
'group_moderators' => array(),
'moderators' => array(),
'permissions' => array(),
'log_notify' => array(),
'log_polls' => array(),
'pm_recipients' => array(),
'pm_labeled_messages' => array(),
'user_likes' => array(),
);
/****************************
* This is the main program
****************************/
// Get the branch...
$sql = "SELECT value FROM {$db_prefix}settings WHERE variable = 'smfVersion' LIMIT 1";
$result = call_mysqli_query($sql, false);
list ($version) = mysqli_fetch_row($result);
mysqli_free_result($result);
// Strip unnecessary stuff for us from the branch...
$pattern = '/^(\d\.\d{1,3})/';
$matches = array();
preg_match($pattern, $version, $matches);
$version = $matches[1];
if(!isset($_REQUEST['step']))
{
// Check this Secondary Prefix is different...
if ($db_prefix == PRIMARY_DB_PREFIX)
{
head();
echo 'You are on the wrong installation,<br />Or the prefix you set in this file is wrong.<br /><br /><a href="'.$_SERVER['PHP_SELF'].'">Please remedy and start again.</a>';
foot();
}
elseif (($version != '2.0') && ($version != '2.1'))
{
head();
echo 'This script does not work on this version of SMF.<br /><br /><a href="'.$_SERVER['PHP_SELF'].'">Please remedy and start again.</a>';
foot();
}
else
{
call_user_func('dostep0');
}
}
else
{
call_user_func('dostep' . $_REQUEST['step']);
}
function dostep0()
{
head();
echo 'You are on the right installation. Let\'s start.<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=1">Start</a></center>';
foot();
}
// Update board and category IDs
function dostep1()
{
head();
echo 'Updating category IDs...<br>';
dotable('categories');
echo '<br>Updating board IDs...<br>';
dotable('boards');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=2">Continue Step 2</a></center>';
foot();
}
// Update messages
function dostep2()
{
head();
echo 'Updating message IDs for boards, topics, attachments and messages...<br>';
dotable('messages');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=3">Continue Step 3</a></center>';
foot();
}
// Update attachments filenames based on new attachment IDs
// Also, move all SECONDARY attachments (renamed using new attachment IDs) to a to_move_to_primary subfolder in the SECONDARY attachments directory
function dostep3()
{
head();
dotable('attachments');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=4">Continue Step 4</a></center>';
foot();
}
// Rename attachments
function renameattachments()
{
global $db_prefix;
echo 'Renaming & copying attachments and thumbs...<br>';
// Get the secondary attachments directory...
echo 'Getting attachment upload dir';
$sql = "SELECT value FROM {$db_prefix}settings WHERE variable = 'attachmentUploadDir' LIMIT 1";
$result = call_mysqli_query($sql, false);
list ($attachmentUploadDir) = mysqli_fetch_row($result);
mysqli_free_result($result);
echo "...done ({$attachmentUploadDir})<br>";
// Get the secondary attachments current directory #...
echo 'Getting secondary attachment upload dir #';
$sql = "SELECT value FROM {$db_prefix}settings WHERE variable = 'currentAttachmentUploadDir' LIMIT 1";
$result = call_mysqli_query($sql, false);
list ($currAttachmentUploadDir) = mysqli_fetch_row($result);
mysqli_free_result($result);
echo "...done ({$currAttachmentUploadDir})<br>";
// Unpacking of attachment folders needed?
$attachDirs = array();
if (empty($currAttachmentUploadDir))
{
$currAttachmentUploadDir = 1;
if (is_dir($attachmentUploadDir))
$attachDirs[1] = $attachmentUploadDir;
else
$attachDirs = unpack_string($attachmentUploadDir);
}
else
$attachDirs = unpack_string($attachmentUploadDir);
// Get the primary attachments current directory #...
echo 'Getting primary attachment upload dir #';
$sql = "SELECT value FROM " . PRIMARY_DB_PREFIX . "settings WHERE variable = 'currentAttachmentUploadDir' LIMIT 1";
$result = call_mysqli_query($sql, false);
list ($currPrimaryUploadDir) = mysqli_fetch_row($result);
mysqli_free_result($result);
// Empty means 1...
if (empty($currPrimaryUploadDir))
$currPrimaryUploadDir = 1;
echo "...done ({$currPrimaryUploadDir})<br>";
// If there is only one source attachment dir, move its contents to the current dir on the target.
if (count($attachDirs) == 1)
{
if ($currPrimaryUploadDir != 1)
{
echo 'Changing folder # on attachments to match primary current folder';
$sql = "UPDATE {$db_prefix}attachments SET id_folder = {$currPrimaryUploadDir}";
$query = call_mysqli_query($sql, false);
echo '...done<br>';
}
$maxTargetFolder = $currPrimaryUploadDir - 1;
}
else
{
// If there are multiple source attachment dirs, new corresponding dirs should be added to the end of the target attachment dir list.
// Find the highest folder # actually used in the PRIMARY, target DB. We want the new attachment dirs to go beyond that.
echo 'Determining number of attachment folders used in the primary system';
$sql = "SELECT MAX(id_folder) FROM " . PRIMARY_DB_PREFIX . "attachments";
$query = call_mysqli_query($sql, false);
list ($maxTargetFolder) = mysqli_fetch_row($query);
if (empty($maxTargetFolder))
$maxTargetFolder = 0;
mysqli_free_result($query);
echo "...done ({$maxTargetFolder})<br>";
echo 'Incrementing folder #s on attachment records';
$sql = "UPDATE {$db_prefix}attachments SET id_folder = id_folder + {$maxTargetFolder}";
$query = call_mysqli_query($sql, false);
echo '...done<br>';
// Leave it to the admin to create & name the new folders needed...
echo '<span style = "color:red"><strong>*** You must manually add new attachment folders in the target system corresponding to the folders in the source system.***</strong></span><br>';
}
// Get the max attach ID
echo 'Getting max attachment ID from primary';
$sql = "SELECT MAX(ID_ATTACH) FROM " . PRIMARY_DB_PREFIX . "attachments";
$query = call_mysqli_query($sql, false);
$maxattach = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
echo '...done<br>';
foreach($attachDirs AS $ix => $attachDir)
{
// Make temporary attachments directory...
$attachmentTempDir = $attachDir . '/to_move_to_primary/';
echo 'Creating to_move_to_primary attachments directory ' . $attachmentTempDir;
@mkdir($attachmentTempDir, 0755);
echo '...done<br>';
echo 'Copying attachments intended for folder ' . ($ix + $maxTargetFolder) . ' ';
$sql = "SELECT ID_ATTACH FROM {$db_prefix}attachments ORDER BY ID_ATTACH DESC";
$query = call_mysqli_query($sql, false);
while($attach = mysqli_fetch_row($query))
{
$prefix_old = '/^' . preg_quote($attachDir .'/' . $attach[0] . '_', '/') . '/';
$prefix_new = $attachmentTempDir . ($attach[0] + $maxattach) . '_';
$filename = glob($attachDir .'/'. $attach[0] . '_*', GLOB_NOESCAPE);
if (!empty($filename[0]) && is_file($filename[0]))
{
$filenew = preg_replace($prefix_old, $prefix_new, $filename[0]);
copy($filename[0], $filenew);
}
}
mysqli_free_result($query);
echo '...done<br>';
}
return;
}
// Custom Avatars...
function dostep4()
{
head();
// Now do the same with custom avatars...
custom_avs();
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=5">Continue Step 5</a></center>';
foot();
}
// Custom Avatars...
function custom_avs()
{
global $db_prefix;
// Get the custom_avatars directory...
$sql = "SELECT value FROM {$db_prefix}settings WHERE variable = 'custom_avatar_dir' LIMIT 1";
$result = call_mysqli_query($sql, false);
// If it wasn't setup, no need to go any further.
if (mysqli_num_rows($result) == 0)
{
echo 'No custom avatars.<br>';
return;
}
echo 'Getting custom avatar dir';
list ($customAvatarDir) = mysqli_fetch_row($result);
mysqli_free_result($result);
echo '...done<br>';
// Make sure it's a real directory...
if (empty($customAvatarDir) || !is_dir($customAvatarDir))
{
echo '<br>No custom avatars.<br>';
return;
}
// Make temporary directory...
$customAvTempDir = $customAvatarDir . '/to_move_to_primary/';
echo 'Creating to_move_to_primary custom avatar directory' . $customAvTempDir;
@mkdir($customAvTempDir, 0755);
echo '...done<br>';
// Get the max member ID
echo 'Getting max member ID from primary';
$sql = "SELECT MAX(id_member) FROM " . PRIMARY_DB_PREFIX . "members";
$query = call_mysqli_query($sql, false);
$maxmember = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
echo '...done<br>';
// Simplest here to just rename & move 'em over based on the directory contents.
echo 'Copying custom avatars';
$files = glob($customAvatarDir . '/avatar_*', GLOB_NOESCAPE);
$pattern = '/^' . preg_quote($customAvatarDir . '/', '/') . 'avatar_(\d+)_(.*)$/';
$matches = array();
foreach($files AS $filename)
{
if ((preg_match($pattern, $filename, $matches) == 1) && is_file($filename))
{
$filenew = $customAvTempDir . 'avatar_' . ($matches[1] + $maxmember) . '_' . $matches[2];
copy($filename, $filenew);
}
}
echo '...done<br><br>';
// Finally, update the filename in the attachments table for the custom avatars...
echo 'Renaming avatar filenames in members table';
$sql = "SELECT id_attach, filename FROM {$db_prefix}attachments WHERE attachment_type = 1 and id_member > 0 ORDER BY id_member DESC";
$query = call_mysqli_query($sql, false);
// Increase 'em all...
$pattern = '/^avatar_(\d+)_(.*)$/';
$matches = array();
while($avinfo = mysqli_fetch_assoc($query))
{
if (preg_match($pattern, $avinfo['filename'], $matches) == 1)
{
$filenew = 'avatar_' . ($matches[1] + $maxmember) . '_' . $matches[2];
$sql = "UPDATE {$db_prefix}attachments SET filename = '{$filenew}' WHERE id_attach = {$avinfo['id_attach']}";
$query0 = call_mysqli_query($sql, false);
}
}
mysqli_free_result($query);
echo '...done';
}
// Log actions...
function dostep5()
{
head();
dotable('log_actions');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=6">Continue Step 6</a></center>';
foot();
}
// Update member and applicator values
function packed_strings()
{
global $db_prefix, $db_connection;
echo 'Updating log_actions - fixing extras...<br>';
// Get the max member ID
$sql = 'SELECT MAX(id_member) FROM ' . PRIMARY_DB_PREFIX . 'members';
$query = call_mysqli_query($sql, false);
$maxmember = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
// Collect all the extras that need updating...
echo 'Getting member-related extras to update';
$sql = "SELECT id_action, extra FROM `{$db_prefix}log_actions` WHERE extra like '%member%' OR extra like '%applicator%'";
$query = call_mysqli_query($sql, false);
echo '...done<br>';
// Increase 'em all...
echo 'Updating member-related extras';
while($action = mysqli_fetch_assoc($query))
{
$checkvals = unpack_string($action['extra']);
if ($checkvals === false)
echo_error_text('Cannot parse action: ' . $action['id_action'] . ' extra: ' . $action['extra']);
else
{
if (array_key_exists('member', $checkvals))
$checkvals['member'] = (string) ($checkvals['member'] + $maxmember);
elseif (array_key_exists('applicator', $checkvals))
$checkvals['applicator'] = (string) ($checkvals['applicator'] + $maxmember);
else
// Just in case the value just happened to be in a string...
continue;
$newstring = mysqli_real_escape_string($db_connection, pack_string($checkvals));
$sql = "UPDATE {$db_prefix}log_actions SET extra = '{$newstring}' WHERE id_action = {$action['id_action']}";
$query0 = call_mysqli_query($sql, false);
}
}
mysqli_free_result($query);
echo '...done<br>';
// Board #s need to be fixed on moves...
// Get the max board ID
$sql = 'SELECT MAX(id_board) FROM ' . PRIMARY_DB_PREFIX . 'boards';
$query = call_mysqli_query($sql, false);
$maxboard = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
// Collect all the extras that need updating...
echo 'Getting board-related extras to update';
$sql = "SELECT id_action, extra FROM `{$db_prefix}log_actions` WHERE action = 'move'";
$query = call_mysqli_query($sql, false);
echo '...done<br>';
// Increase 'em all...
echo 'Updating board-related extras';
while($action = mysqli_fetch_assoc($query))
{
$checkvals = unpack_string($action['extra']);
if ($checkvals === false)
echo_error_text('Cannot parse action: ' . $action['id_action'] . ' extra: ' . $action['extra']);
else
{
if (array_key_exists('board_from', $checkvals))
$checkvals['board_from'] = (string) ($checkvals['board_from'] + $maxboard);
else
// Just in case the value just happened to be in a string...
continue;
$newstring = mysqli_real_escape_string($db_connection, pack_string($checkvals));
$sql = "UPDATE {$db_prefix}log_actions SET extra = '{$newstring}' WHERE id_action = {$action['id_action']}";
$query0 = call_mysqli_query($sql, false);
}
}
mysqli_free_result($query);
echo '...done<br>';
}
// Update Members
function dostep6()
{
head();
echo 'Updating member IDs...<br>';
dotable('members');
echo '<br /><br /><center><a href="' . $_SERVER['PHP_SELF'] . '?step=7">Continue Step 7</a></center>';
foot();
}
// Update several items related to members:
// - Rename if needed to make names unique
// - Update buddy/ignore lists
// - Theme & smiley defaults
//
function renamemembers()
{
global $db_prefix, $secondary_suffix;
// Clear log file for renames
$renameMembersLog = getcwd() . '/renameMembersLog';
@unlink($renameMembersLog);
// Get member names
echo 'Getting member names';
$sql = "SELECT ID_MEMBER, member_name, real_name, email_address FROM {$db_prefix}members ORDER BY ID_MEMBER DESC";
$query = call_mysqli_query($sql, false);
echo '...done<br>';
while($mem = mysqli_fetch_row($query))
{
// Make sure member_name is unique...
$sql = "SELECT ID_MEMBER FROM " . PRIMARY_DB_PREFIX . "members where member_name = '$mem[1]'";
$queryPrimaryMemberID = call_mysqli_query($sql, false);
$member_name_hits = mysqli_num_rows($queryPrimaryMemberID);
// Also check real_name...
$sql = "SELECT ID_MEMBER FROM " . PRIMARY_DB_PREFIX . "members where real_name = '$mem[2]'";
$queryPrimaryMemberID = call_mysqli_query($sql, false);
$real_name_hits = mysqli_num_rows($queryPrimaryMemberID);
// Also check email...
$sql = "SELECT ID_MEMBER FROM " . PRIMARY_DB_PREFIX . "members where email_address = '$mem[3]'";
$queryPrimaryMemberID = call_mysqli_query($sql, false);
$email_hits = mysqli_num_rows($queryPrimaryMemberID);
//IF MEMBERNAME EXISTS IN PRIMARY DATABASE, rename the member to {membername]-suffix.
//You can use a tool like SMF Admin Toolbox to merge these members with the primary members of the same name after finished with this script.
if(($member_name_hits > 0) || ($real_name_hits > 0) || ($email_hits > 0))
{
$mn_suffix = ($member_name_hits > 0 ? $secondary_suffix : '');
$rn_suffix = ($real_name_hits > 0 ? $secondary_suffix : '');
$ea_suffix = ($email_hits > 0 ? $secondary_suffix : '');
$matches = ($member_name_hits > 0 ? ' \'member_name\' ' : '');
$matches .= ($real_name_hits > 0 ? ' \'real_name\' ' : '');
$matches .= ($email_hits > 0 ? ' \'email_address\' ' : '');
$exists = "The same <strong>$matches</strong> for the username <strong> '$mem[1]' </strong> (member ID '$mem[0]') already exists in primary.";
$renaming = "Renaming <strong>$matches</strong> in secondary ...";
echo "$exists $renaming <br />";
$now = '[' . date("d-M-Y H:i:s e") . '] ';
$exists = str_replace(array(' ', '<strong>', '</strong>'), array(' ', '', ''), $exists);
$renaming = str_replace(array(' ', '<strong>', '</strong>'), array(' ', '', ''), $renaming);
error_log(print_r($now . $exists . " " . $renaming . "\n\n", true), 3, $renameMembersLog);
$sql = "UPDATE {$db_prefix}members SET member_name = CONCAT(member_name, '{$mn_suffix}'), real_name = CONCAT(real_name, '{$rn_suffix}'), email_address = CONCAT(email_address, '{$ea_suffix}') WHERE ID_MEMBER = '$mem[0]'";
$query0 = call_mysqli_query($sql, false);
}
else
{
echo "Member $mem[1] does not exist in primary.<BR>";
}
mysqli_free_result($queryPrimaryMemberID);
}
mysqli_free_result($query);
// Get the max id_member
echo 'Getting max member ID in primary';
$sql = "SELECT MAX(id_member) FROM " . PRIMARY_DB_PREFIX . "members";
$query = call_mysqli_query($sql, false);
$maxmember = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
echo '...done<br>';
// Look at buddy list on members...
echo '<br>Updating buddy lists on members';
$sql = "SELECT id_member, buddy_list FROM {$db_prefix}members WHERE buddy_list <> ''";
$query = call_mysqli_query($sql, false);
while($mem = mysqli_fetch_assoc($query))
{
$members = explode(',', $mem['buddy_list']);
foreach ($members AS $ix => $member)
{
if ($member > 0)
$members[$ix] = $members[$ix] + $maxmember;
}
$members_new = implode(',', $members);
$sql = "UPDATE {$db_prefix}members SET buddy_list = '{$members_new}' WHERE id_member = {$mem['id_member']}";
$query0 = call_mysqli_query($sql, false);
}
mysqli_free_result($query);
echo '...done<br>';
// Look at ignore list on members...
echo 'Updating ignore lists on members';
$sql = "SELECT id_member, pm_ignore_list FROM {$db_prefix}members WHERE pm_ignore_list <> ''";
$query = call_mysqli_query($sql, false);
while($mem = mysqli_fetch_assoc($query))
{
$members = explode(',', $mem['pm_ignore_list']);
foreach ($members AS $ix => $member)
{
if ($member > 0)
$members[$ix] = $members[$ix] + $maxmember;
}
$members_new = implode(',', $members);
$sql = "UPDATE {$db_prefix}members SET pm_ignore_list = '{$members_new}' WHERE id_member = {$mem['id_member']}";
$query0 = call_mysqli_query($sql, false);
}
mysqli_free_result($query);
echo '...done.<br><br>';
//Themes & smiley sets may not line up, so set users who are about to move over to defaults
echo "Setting themes, smileys to defaults";
$sql = "UPDATE {$db_prefix}members SET id_theme = 0, smiley_set = ''";
$query9a = call_mysqli_query($sql, false);
echo "...done.<br>";
//SMF doesn't like Member profile IPs of 000.000.000.000 (I had some of these from very old members, probably from my original migration from Snitz).
echo "Fixing old broken IPs";
$sql = "UPDATE {$db_prefix}members SET member_ip = '1.1.1.1', member_ip2 = '1.1.1.1' WHERE member_ip = '000.000.000.000'";
$query9b = call_mysqli_query($sql, false);
echo "...done.<br><br>";
return;
}
// Update PM IDs...
function dostep7()
{
head();
echo 'Updating PM IDs...<br>';
dotable('personal_messages');
echo '<br /><br /><center><a href="' . $_SERVER['PHP_SELF'] . '?step=8">Continue Step 8</a></center>';
foot();
}
// Update polls...
function dostep8()
{
head();
echo 'Updating poll IDs in polls, poll choices, polls log and topics...<br>';
dotable('polls');
echo '<br>Updating choice IDs in polls log and poll choices...<br>';
dotable('poll_choices');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=9">Continue Step 9</a></center>';
foot();
}
// Update topic IDs...
function dostep9()
{
head();
echo 'Updating topic IDs in notify log, messages and topics...<br>';
dotable('topics');
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=10">Continue Step 10</a></center>';
foot();
}
// Combine the daily statistics...
function dostep10()
{
head();
global $db_prefix;
echo 'Merging daily statistics...<br>';
// Get stats from primary
echo 'Getting stats from primary';
$primary_activity = array();
$sql = 'SELECT date, hits, topics, posts, registers, most_on FROM ' . PRIMARY_DB_PREFIX . 'log_activity';
$query = call_mysqli_query($sql, false);
while($row = mysqli_fetch_assoc($query))
$primary_activity[$row['date']] = array('hits' => $row['hits'], 'topics' => $row['topics'], 'posts' => $row['posts'], 'registers' => $row['registers'], 'most_on' => $row['most_on']);
mysqli_free_result($query);
echo "...done.<br>";
// Get stats from secondary
echo 'Getting stats from secondary';
$secondary_activity = array();
$sql = 'SELECT date, hits, topics, posts, registers, most_on FROM ' . $db_prefix . 'log_activity';
$query = call_mysqli_query($sql, false);
while($row = mysqli_fetch_assoc($query))
$secondary_activity[$row['date']] = array('hits' => $row['hits'], 'topics' => $row['topics'], 'posts' => $row['posts'], 'registers' => $row['registers'], 'most_on' => $row['most_on']);
mysqli_free_result($query);
echo "...done.<br>";
// Add secondary stats into primary
echo 'Adding them together';
foreach($secondary_activity AS $secdate => $stats)
{
if (array_key_exists($secdate, $primary_activity))
{
$primary_activity[$secdate]['hits'] = $primary_activity[$secdate]['hits'] + $secondary_activity[$secdate]['hits'];
$primary_activity[$secdate]['topics'] = $primary_activity[$secdate]['topics'] + $secondary_activity[$secdate]['topics'];
$primary_activity[$secdate]['posts'] = $primary_activity[$secdate]['posts'] + $secondary_activity[$secdate]['posts'];
$primary_activity[$secdate]['registers'] = $primary_activity[$secdate]['registers'] + $secondary_activity[$secdate]['registers'];
$primary_activity[$secdate]['most_on'] = $primary_activity[$secdate]['most_on'] + $secondary_activity[$secdate]['most_on'];
}
else
$primary_activity[$secdate] = $stats;
}
// Convert to text for easy inserts
$inserts = array();
foreach($primary_activity AS $primdate => $stats)
$inserts[] = '(\'' . $primdate . '\',' . implode(',', $stats) . ')';
echo "...done.<br>";
// Wipe out target stats & replace
echo 'Updating stats on primary';
$sql = 'TRUNCATE ' . PRIMARY_DB_PREFIX . 'log_activity';
$query = call_mysqli_query($sql, false);
$sql = 'INSERT INTO ' . PRIMARY_DB_PREFIX . 'log_activity (date, hits, topics, posts, registers, most_on) VALUES ' . implode(', ', $inserts);
$query = call_mysqli_query($sql, false);
echo "...done.<br>";
echo '<br /><br /><center><a href="'.$_SERVER['PHP_SELF'].'?step=11">Continue Step 11</a></center>';
foot();
}
// Update Membergroups...
function dostep11()
{
head();
echo 'Updating membergroups...<br>';
dotable('membergroups');
echo '<br /><br /><center><a href="' . $_SERVER['PHP_SELF'] . '?step=12">Continue Step 12</a></center>';
foot();
}
// Update Membergroups...
// Remember at the end, all records in the tables get copied over...
// So you gotta get rid of anything that will collide with settings/records already in primary.
// Everything to do with -1 (guests) and 0 (default) must be left alone and not migrated over.
// Also, post-count based groups will not be brought over - rules from the primary should be used.
//
function cleanrenamegroups()
{
global $db_prefix, $secondary_suffix;
// Get the max id_group
$sql = "SELECT MAX(id_group) FROM " . PRIMARY_DB_PREFIX . "membergroups";
$query = call_mysqli_query($sql, false);
$maxgroup = mysqli_fetch_row($query)[0];
mysqli_free_result($query);
// membergroups - remove recs we won't be moving over; use primary board's post-count based groups instead.
echo '<br>Updating membergroups - do not migrate post-count based groups...';
$sql = "DELETE FROM {$db_prefix}membergroups WHERE min_posts > -1";
$query = call_mysqli_query($sql, false);
echo 'done.<br>';
// permissions - remove -1 & 0; guest & default behavior defined by primary
echo 'Updating permissions - default values should be coming from primary...';
$sql = "DELETE FROM {$db_prefix}permissions WHERE id_group < 1";
$query = call_mysqli_query($sql, false);
echo 'done.<br>';
// board_permissions - remove groups -1 and profiles < 5
echo 'Updating board_permissions - default values should be coming from primary...';
$sql = "DELETE FROM {$db_prefix}board_permissions WHERE id_group < 0 AND id_profile < 5";
$query = call_mysqli_query($sql, false);
echo 'done.<br>';
// This is a weird one... Admins (id_group = 1) don't have entries in board_permissions...
// Thus, they lose EVERYTHING post migration. Worse than guests!!! They should at least get what
// regular members get in primary. So... Change the regular member entries to admin entries...
// This effectively (1) gives admins access AND (2) gets rid of the last of the 'default' values
// to avoid conflicts with primary...
echo 'Updating board_permissions - old admins need perms...';
$sql = "UPDATE {$db_prefix}board_permissions SET id_group = 1 WHERE id_group = 0";
$query = call_mysqli_query($sql, false);
echo 'done.<br>';
// Identify set of remaining groups - needed for cleaning group lists...
echo 'Identifying remaining groups...';
$grouplist = array();
$sql = "SELECT id_group FROM {$db_prefix}membergroups";
$query = call_mysqli_query($sql, false);
while($grp = mysqli_fetch_assoc($query))
$grouplist[] = $grp['id_group'];
mysqli_free_result($query);
echo 'done.<br>';
// Look at member_groups on boards...
echo 'Updating member_groups on boards...';
$sql = "SELECT id_board, member_groups FROM {$db_prefix}boards WHERE member_groups <> ''";
$query = call_mysqli_query($sql, false);
while($mem = mysqli_fetch_assoc($query))
{