1 | <?php |
||||
2 | |||||
3 | namespace LeKoala\DevToolkit\Helpers; |
||||
4 | |||||
5 | use Exception; |
||||
6 | use SilverStripe\ORM\DB; |
||||
7 | use SilverStripe\Core\ClassInfo; |
||||
8 | use SilverStripe\ORM\DataObject; |
||||
9 | use SilverStripe\Security\Member; |
||||
10 | use SilverStripe\Core\Config\Config; |
||||
11 | use SilverStripe\Subsites\Model\Subsite; |
||||
0 ignored issues
–
show
|
|||||
12 | |||||
13 | /** |
||||
14 | * Merge members |
||||
15 | * |
||||
16 | * @author Koala |
||||
17 | */ |
||||
18 | class DuplicateMembersMerger |
||||
19 | { |
||||
20 | public static function merge($records) |
||||
21 | { |
||||
22 | $all = array(); |
||||
23 | $all_but_oldest = array(); |
||||
24 | $all_but_latest = array(); |
||||
25 | |||||
26 | $latest = null; |
||||
27 | $oldest = null; |
||||
28 | foreach ($records as $r) { |
||||
29 | if (!is_object($r)) { |
||||
30 | $r = (object) $r; |
||||
31 | } |
||||
32 | if (!$r instanceof Member) { |
||||
33 | $r = Member::get()->byID($r->ID); |
||||
34 | } |
||||
35 | if (!$latest) { |
||||
36 | $latest = $r; |
||||
37 | } else { |
||||
38 | if (strtotime($r->LastEdited) > strtotime($latest->LastEdited)) { |
||||
39 | $latest = $r; |
||||
40 | } |
||||
41 | } |
||||
42 | if (!$oldest) { |
||||
43 | $oldest = $r; |
||||
44 | } else { |
||||
45 | if ($r->ID < $oldest->ID) { |
||||
46 | $oldest = $r->ID; |
||||
47 | } |
||||
48 | } |
||||
49 | |||||
50 | $all[] = $r; |
||||
51 | } |
||||
52 | |||||
53 | foreach ($all as $a) { |
||||
54 | if ($a->ID == $oldest->ID) { |
||||
55 | continue; |
||||
56 | } |
||||
57 | $all_but_oldest[] = $a; |
||||
58 | } |
||||
59 | |||||
60 | foreach ($all as $a) { |
||||
61 | if ($a->ID == $latest->ID) { |
||||
62 | continue; |
||||
63 | } |
||||
64 | $all_but_latest[] = $a; |
||||
65 | } |
||||
66 | |||||
67 | if (class_exists('Subsite')) { |
||||
68 | Subsite::$disable_subsite_filter = true; |
||||
69 | } |
||||
70 | |||||
71 | Config::modify()->set(DataObject::class, 'validation_enabled', false); |
||||
72 | |||||
73 | // Rewrite all relations so everything is pointing to oldest |
||||
74 | // For some reason, the code in merge fails to do this properly |
||||
75 | $tables = DataObject::getSchema()->getTableNames(); |
||||
76 | $objects = ClassInfo::subclassesFor('DataObject'); |
||||
77 | foreach ($objects as $o) { |
||||
78 | $config = $o::config(); |
||||
79 | if ($config->has_one) { |
||||
80 | foreach ($config->has_one as $name => $class) { |
||||
81 | if ($class == 'Member') { |
||||
82 | $table = ClassInfo::table_for_object_field( |
||||
0 ignored issues
–
show
The function
SilverStripe\Core\ClassI...able_for_object_field() has been deprecated: 4.0.0:5.0.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.
Loading history...
|
|||||
83 | $o, |
||||
84 | $name . 'ID' |
||||
85 | ); |
||||
86 | if ($table && in_array(strtolower($table), $tables)) { |
||||
87 | foreach ($all_but_oldest as $a) { |
||||
88 | $sql = "UPDATE $table SET " . $name . 'ID = ' . $oldest->ID . ' WHERE ' . $name . 'ID = ' . $a->ID; |
||||
89 | DB::alteration_message($sql); |
||||
90 | DB::query($sql); |
||||
91 | } |
||||
92 | } |
||||
93 | } |
||||
94 | } |
||||
95 | } |
||||
96 | if ($config->has_many) { |
||||
97 | foreach ($config->has_many as $name => $class) { |
||||
98 | if ($class == 'Member') { |
||||
99 | $table = ClassInfo::table_for_object_field( |
||||
0 ignored issues
–
show
The function
SilverStripe\Core\ClassI...able_for_object_field() has been deprecated: 4.0.0:5.0.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.
Loading history...
|
|||||
100 | $o, |
||||
101 | $name . 'ID' |
||||
102 | ); |
||||
103 | if ($table && in_array(strtolower($table), $tables)) { |
||||
104 | foreach ($all_but_oldest as $a) { |
||||
105 | $sql = "UPDATE $table SET " . $name . 'ID = ' . $oldest->ID . ' WHERE ' . $name . 'ID = ' . $a->ID; |
||||
106 | DB::alteration_message($sql); |
||||
107 | DB::query($sql); |
||||
108 | } |
||||
109 | } |
||||
110 | } |
||||
111 | } |
||||
112 | } |
||||
113 | if ($config->many_many) { |
||||
114 | foreach ($config->many_many as $name => $class) { |
||||
115 | if ($class == 'Member') { |
||||
116 | $table = ClassInfo::table_for_object_field( |
||||
0 ignored issues
–
show
The function
SilverStripe\Core\ClassI...able_for_object_field() has been deprecated: 4.0.0:5.0.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.
Loading history...
|
|||||
117 | $o, |
||||
118 | $name . 'ID' |
||||
119 | ); |
||||
120 | if ($table && in_array(strtolower($table), $tables)) { |
||||
121 | foreach ($all_but_oldest as $a) { |
||||
122 | $sql = "UPDATE $table SET " . $name . 'ID = ' . $oldest->ID . ' WHERE ' . $name . 'ID = ' . $a->ID; |
||||
123 | DB::alteration_message($sql); |
||||
124 | DB::query($sql); |
||||
125 | } |
||||
126 | } |
||||
127 | } |
||||
128 | } |
||||
129 | } |
||||
130 | } |
||||
131 | |||||
132 | // Now, we update to oldest record with the latest info |
||||
133 | |||||
134 | $orgOldest = $oldest; |
||||
135 | $oldest->merge($latest, 'right', false); |
||||
136 | foreach ($all_but_oldest as $a) { |
||||
137 | $a->delete(); |
||||
138 | } |
||||
139 | |||||
140 | try { |
||||
141 | $oldest->write(); |
||||
142 | } catch (Exception $ex) { |
||||
143 | $orgOldest->write(); |
||||
144 | } |
||||
145 | } |
||||
146 | } |
||||
147 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths