@@ -30,291 +30,291 @@ |
||
30 | 30 | */ |
31 | 31 | class SosaRecordsService |
32 | 32 | { |
33 | - private ?int $max_system_generations = null; |
|
33 | + private ?int $max_system_generations = null; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Maximum number of generation the system is able to hold. |
|
37 | - * This is based on the size of the bigint SQL type (2^63) and the maximum PHP integer type |
|
38 | - * |
|
39 | - * @return int |
|
40 | - */ |
|
41 | - public function maxSystemGenerations(): int |
|
42 | - { |
|
43 | - if ($this->max_system_generations === null) { |
|
44 | - $this->max_system_generations = min(63, $this->generation(PHP_INT_MAX)); |
|
45 | - } |
|
46 | - return $this->max_system_generations; |
|
47 | - } |
|
35 | + /** |
|
36 | + * Maximum number of generation the system is able to hold. |
|
37 | + * This is based on the size of the bigint SQL type (2^63) and the maximum PHP integer type |
|
38 | + * |
|
39 | + * @return int |
|
40 | + */ |
|
41 | + public function maxSystemGenerations(): int |
|
42 | + { |
|
43 | + if ($this->max_system_generations === null) { |
|
44 | + $this->max_system_generations = min(63, $this->generation(PHP_INT_MAX)); |
|
45 | + } |
|
46 | + return $this->max_system_generations; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Calculate the generation of a sosa |
|
51 | - * Sosa 1 is of generation 1. |
|
52 | - * |
|
53 | - * @param int $sosa |
|
54 | - * @return int |
|
55 | - */ |
|
56 | - public function generation(int $sosa): int |
|
57 | - { |
|
58 | - return BigInteger::of($sosa)->getBitLength(); |
|
59 | - } |
|
49 | + /** |
|
50 | + * Calculate the generation of a sosa |
|
51 | + * Sosa 1 is of generation 1. |
|
52 | + * |
|
53 | + * @param int $sosa |
|
54 | + * @return int |
|
55 | + */ |
|
56 | + public function generation(int $sosa): int |
|
57 | + { |
|
58 | + return BigInteger::of($sosa)->getBitLength(); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Calculate the descendant sosa of the given sosa, at the given generation. |
|
63 | - * For instance, the descendant of the Sosa 14 at generation 2 is Sosa 3 (mother). |
|
64 | - * |
|
65 | - * @param int $sosa |
|
66 | - * @param int $gen |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function sosaDescendantOf(int $sosa, int $gen): int |
|
70 | - { |
|
71 | - $gen_sosa = $this->generation($sosa); |
|
72 | - return $gen_sosa <= $gen ? $sosa : BigInteger::of($sosa) |
|
73 | - ->dividedBy(BigInteger::of(2)->power($this->generation($sosa) - $gen), RoundingMode::DOWN) |
|
74 | - ->toInt(); |
|
75 | - } |
|
61 | + /** |
|
62 | + * Calculate the descendant sosa of the given sosa, at the given generation. |
|
63 | + * For instance, the descendant of the Sosa 14 at generation 2 is Sosa 3 (mother). |
|
64 | + * |
|
65 | + * @param int $sosa |
|
66 | + * @param int $gen |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function sosaDescendantOf(int $sosa, int $gen): int |
|
70 | + { |
|
71 | + $gen_sosa = $this->generation($sosa); |
|
72 | + return $gen_sosa <= $gen ? $sosa : BigInteger::of($sosa) |
|
73 | + ->dividedBy(BigInteger::of(2)->power($this->generation($sosa) - $gen), RoundingMode::DOWN) |
|
74 | + ->toInt(); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Check whether an individual is a Sosa ancestor. |
|
79 | - * |
|
80 | - * @param Tree $tree |
|
81 | - * @param UserInterface $user |
|
82 | - * @param Individual $indi |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function isSosa(Tree $tree, UserInterface $user, Individual $indi): bool |
|
86 | - { |
|
87 | - return $this->sosaNumbers($tree, $user, $indi)->count() > 0; |
|
88 | - } |
|
77 | + /** |
|
78 | + * Check whether an individual is a Sosa ancestor. |
|
79 | + * |
|
80 | + * @param Tree $tree |
|
81 | + * @param UserInterface $user |
|
82 | + * @param Individual $indi |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function isSosa(Tree $tree, UserInterface $user, Individual $indi): bool |
|
86 | + { |
|
87 | + return $this->sosaNumbers($tree, $user, $indi)->count() > 0; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Returns all Sosa numbers associated to an Individual |
|
92 | - * |
|
93 | - * @param Tree $tree |
|
94 | - * @param UserInterface $user |
|
95 | - * @param Individual $indi |
|
96 | - * @return Collection<int, int> |
|
97 | - */ |
|
98 | - public function sosaNumbers(Tree $tree, UserInterface $user, Individual $indi): Collection |
|
99 | - { |
|
100 | - return Registry::cache()->array()->remember( |
|
101 | - 'sosanumbers-' . $indi->xref() . '@' . $tree->id() . '-' . $user->id(), |
|
102 | - function () use ($tree, $user, $indi): Collection { |
|
103 | - return DB::table('maj_sosa') |
|
104 | - ->select(['majs_sosa', 'majs_gen']) |
|
105 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
106 | - ->where('majs_user_id', '=', $user->id()) |
|
107 | - ->where('majs_i_id', '=', $indi->xref()) |
|
108 | - ->orderBy('majs_sosa') |
|
109 | - ->get()->pluck('majs_gen', 'majs_sosa'); |
|
110 | - } |
|
111 | - ); |
|
112 | - } |
|
90 | + /** |
|
91 | + * Returns all Sosa numbers associated to an Individual |
|
92 | + * |
|
93 | + * @param Tree $tree |
|
94 | + * @param UserInterface $user |
|
95 | + * @param Individual $indi |
|
96 | + * @return Collection<int, int> |
|
97 | + */ |
|
98 | + public function sosaNumbers(Tree $tree, UserInterface $user, Individual $indi): Collection |
|
99 | + { |
|
100 | + return Registry::cache()->array()->remember( |
|
101 | + 'sosanumbers-' . $indi->xref() . '@' . $tree->id() . '-' . $user->id(), |
|
102 | + function () use ($tree, $user, $indi): Collection { |
|
103 | + return DB::table('maj_sosa') |
|
104 | + ->select(['majs_sosa', 'majs_gen']) |
|
105 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
106 | + ->where('majs_user_id', '=', $user->id()) |
|
107 | + ->where('majs_i_id', '=', $indi->xref()) |
|
108 | + ->orderBy('majs_sosa') |
|
109 | + ->get()->pluck('majs_gen', 'majs_sosa'); |
|
110 | + } |
|
111 | + ); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Return a list of the Sosa ancestors across all generation |
|
116 | - * |
|
117 | - * @param Tree $tree |
|
118 | - * @param UserInterface $user |
|
119 | - * @return Collection<\stdClass> |
|
120 | - */ |
|
121 | - public function listAncestors(Tree $tree, UserInterface $user): Collection |
|
122 | - { |
|
123 | - return DB::table('maj_sosa') |
|
124 | - ->select(['majs_sosa', 'majs_i_id']) |
|
125 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
126 | - ->where('majs_user_id', '=', $user->id()) |
|
127 | - ->orderBy('majs_sosa') |
|
128 | - ->get(); |
|
129 | - } |
|
114 | + /** |
|
115 | + * Return a list of the Sosa ancestors across all generation |
|
116 | + * |
|
117 | + * @param Tree $tree |
|
118 | + * @param UserInterface $user |
|
119 | + * @return Collection<\stdClass> |
|
120 | + */ |
|
121 | + public function listAncestors(Tree $tree, UserInterface $user): Collection |
|
122 | + { |
|
123 | + return DB::table('maj_sosa') |
|
124 | + ->select(['majs_sosa', 'majs_i_id']) |
|
125 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
126 | + ->where('majs_user_id', '=', $user->id()) |
|
127 | + ->orderBy('majs_sosa') |
|
128 | + ->get(); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Return a list of the Sosa ancestors at a given generation |
|
133 | - * |
|
134 | - * @param Tree $tree |
|
135 | - * @param UserInterface $user |
|
136 | - * @param int $gen |
|
137 | - * @return Collection<\stdClass> |
|
138 | - */ |
|
139 | - public function listAncestorsAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
140 | - { |
|
141 | - return DB::table('maj_sosa') |
|
142 | - ->select(['majs_sosa', 'majs_i_id']) |
|
143 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
144 | - ->where('majs_user_id', '=', $user->id()) |
|
145 | - ->where('majs_gen', '=', $gen) |
|
146 | - ->orderBy('majs_sosa') |
|
147 | - ->get(); |
|
148 | - } |
|
131 | + /** |
|
132 | + * Return a list of the Sosa ancestors at a given generation |
|
133 | + * |
|
134 | + * @param Tree $tree |
|
135 | + * @param UserInterface $user |
|
136 | + * @param int $gen |
|
137 | + * @return Collection<\stdClass> |
|
138 | + */ |
|
139 | + public function listAncestorsAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
140 | + { |
|
141 | + return DB::table('maj_sosa') |
|
142 | + ->select(['majs_sosa', 'majs_i_id']) |
|
143 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
144 | + ->where('majs_user_id', '=', $user->id()) |
|
145 | + ->where('majs_gen', '=', $gen) |
|
146 | + ->orderBy('majs_sosa') |
|
147 | + ->get(); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Return a list of the Sosa families at a given generation |
|
152 | - * |
|
153 | - * @param Tree $tree |
|
154 | - * @param UserInterface $user |
|
155 | - * @param int $gen |
|
156 | - * @return Collection<\stdClass> |
|
157 | - */ |
|
158 | - public function listAncestorFamiliesAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
159 | - { |
|
160 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
161 | - return DB::table('families') |
|
162 | - ->join('maj_sosa AS sosa_husb', function (JoinClause $join) use ($tree, $user): void { |
|
163 | - // Link to family husband |
|
164 | - $join->on('families.f_file', '=', 'sosa_husb.majs_gedcom_id') |
|
165 | - ->on('families.f_husb', '=', 'sosa_husb.majs_i_id') |
|
166 | - ->where('sosa_husb.majs_gedcom_id', '=', $tree->id()) |
|
167 | - ->where('sosa_husb.majs_user_id', '=', $user->id()); |
|
168 | - }) |
|
169 | - ->join('maj_sosa AS sosa_wife', function (JoinClause $join) use ($tree, $user): void { |
|
170 | - // Link to family husband |
|
171 | - $join->on('families.f_file', '=', 'sosa_wife.majs_gedcom_id') |
|
172 | - ->on('families.f_wife', '=', 'sosa_wife.majs_i_id') |
|
173 | - ->where('sosa_wife.majs_gedcom_id', '=', $tree->id()) |
|
174 | - ->where('sosa_wife.majs_user_id', '=', $user->id()); |
|
175 | - }) |
|
176 | - ->select(['sosa_husb.majs_sosa', 'families.f_id']) |
|
177 | - ->where('sosa_husb.majs_gen', '=', $gen) |
|
178 | - ->whereRaw($table_prefix . 'sosa_husb.majs_sosa + 1 = ' . $table_prefix . 'sosa_wife.majs_sosa') |
|
179 | - ->orderBy('sosa_husb.majs_sosa') |
|
180 | - ->get(); |
|
181 | - } |
|
150 | + /** |
|
151 | + * Return a list of the Sosa families at a given generation |
|
152 | + * |
|
153 | + * @param Tree $tree |
|
154 | + * @param UserInterface $user |
|
155 | + * @param int $gen |
|
156 | + * @return Collection<\stdClass> |
|
157 | + */ |
|
158 | + public function listAncestorFamiliesAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
159 | + { |
|
160 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
161 | + return DB::table('families') |
|
162 | + ->join('maj_sosa AS sosa_husb', function (JoinClause $join) use ($tree, $user): void { |
|
163 | + // Link to family husband |
|
164 | + $join->on('families.f_file', '=', 'sosa_husb.majs_gedcom_id') |
|
165 | + ->on('families.f_husb', '=', 'sosa_husb.majs_i_id') |
|
166 | + ->where('sosa_husb.majs_gedcom_id', '=', $tree->id()) |
|
167 | + ->where('sosa_husb.majs_user_id', '=', $user->id()); |
|
168 | + }) |
|
169 | + ->join('maj_sosa AS sosa_wife', function (JoinClause $join) use ($tree, $user): void { |
|
170 | + // Link to family husband |
|
171 | + $join->on('families.f_file', '=', 'sosa_wife.majs_gedcom_id') |
|
172 | + ->on('families.f_wife', '=', 'sosa_wife.majs_i_id') |
|
173 | + ->where('sosa_wife.majs_gedcom_id', '=', $tree->id()) |
|
174 | + ->where('sosa_wife.majs_user_id', '=', $user->id()); |
|
175 | + }) |
|
176 | + ->select(['sosa_husb.majs_sosa', 'families.f_id']) |
|
177 | + ->where('sosa_husb.majs_gen', '=', $gen) |
|
178 | + ->whereRaw($table_prefix . 'sosa_husb.majs_sosa + 1 = ' . $table_prefix . 'sosa_wife.majs_sosa') |
|
179 | + ->orderBy('sosa_husb.majs_sosa') |
|
180 | + ->get(); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Return a list of Sosa ancestors missing at a given generation. |
|
185 | - * It includes the reference of either parent if it is known. |
|
186 | - * |
|
187 | - * @param Tree $tree |
|
188 | - * @param UserInterface $user |
|
189 | - * @param int $gen |
|
190 | - * @return Collection<\stdClass> |
|
191 | - */ |
|
192 | - public function listMissingAncestorsAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
193 | - { |
|
194 | - if ($gen === 1) { |
|
195 | - return collect(); |
|
196 | - } |
|
183 | + /** |
|
184 | + * Return a list of Sosa ancestors missing at a given generation. |
|
185 | + * It includes the reference of either parent if it is known. |
|
186 | + * |
|
187 | + * @param Tree $tree |
|
188 | + * @param UserInterface $user |
|
189 | + * @param int $gen |
|
190 | + * @return Collection<\stdClass> |
|
191 | + */ |
|
192 | + public function listMissingAncestorsAtGeneration(Tree $tree, UserInterface $user, int $gen): Collection |
|
193 | + { |
|
194 | + if ($gen === 1) { |
|
195 | + return collect(); |
|
196 | + } |
|
197 | 197 | |
198 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
199 | - return DB::table('maj_sosa AS sosa') |
|
200 | - ->select(['sosa.majs_i_id', 'sosa_fat.majs_i_id AS majs_fat_id', 'sosa_mot.majs_i_id AS majs_mot_id']) |
|
201 | - ->selectRaw('MIN(' . $table_prefix . 'sosa.majs_sosa) AS majs_sosa') |
|
202 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
203 | - // Link to sosa's father |
|
204 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
205 | - ->where('sosa_fat.majs_gedcom_id', '=', $tree->id()) |
|
206 | - ->where('sosa_fat.majs_user_id', '=', $user->id()); |
|
207 | - }) |
|
208 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
209 | - // Link to sosa's mother |
|
210 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
211 | - ->where('sosa_mot.majs_gedcom_id', '=', $tree->id()) |
|
212 | - ->where('sosa_mot.majs_user_id', '=', $user->id()); |
|
213 | - }) |
|
214 | - ->where('sosa.majs_gedcom_id', '=', $tree->id()) |
|
215 | - ->where('sosa.majs_user_id', '=', $user->id()) |
|
216 | - ->where('sosa.majs_gen', '=', $gen - 1) |
|
217 | - ->where(function (Builder $query): void { |
|
218 | - $query->whereNull('sosa_fat.majs_i_id') |
|
219 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
220 | - }) |
|
221 | - ->groupBy('sosa.majs_i_id', 'sosa_fat.majs_i_id', 'sosa_mot.majs_i_id') |
|
222 | - ->orderByRaw('MIN(' . $table_prefix . 'sosa.majs_sosa)') |
|
223 | - ->get(); |
|
224 | - } |
|
198 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
199 | + return DB::table('maj_sosa AS sosa') |
|
200 | + ->select(['sosa.majs_i_id', 'sosa_fat.majs_i_id AS majs_fat_id', 'sosa_mot.majs_i_id AS majs_mot_id']) |
|
201 | + ->selectRaw('MIN(' . $table_prefix . 'sosa.majs_sosa) AS majs_sosa') |
|
202 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
203 | + // Link to sosa's father |
|
204 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
205 | + ->where('sosa_fat.majs_gedcom_id', '=', $tree->id()) |
|
206 | + ->where('sosa_fat.majs_user_id', '=', $user->id()); |
|
207 | + }) |
|
208 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
209 | + // Link to sosa's mother |
|
210 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
211 | + ->where('sosa_mot.majs_gedcom_id', '=', $tree->id()) |
|
212 | + ->where('sosa_mot.majs_user_id', '=', $user->id()); |
|
213 | + }) |
|
214 | + ->where('sosa.majs_gedcom_id', '=', $tree->id()) |
|
215 | + ->where('sosa.majs_user_id', '=', $user->id()) |
|
216 | + ->where('sosa.majs_gen', '=', $gen - 1) |
|
217 | + ->where(function (Builder $query): void { |
|
218 | + $query->whereNull('sosa_fat.majs_i_id') |
|
219 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
220 | + }) |
|
221 | + ->groupBy('sosa.majs_i_id', 'sosa_fat.majs_i_id', 'sosa_mot.majs_i_id') |
|
222 | + ->orderByRaw('MIN(' . $table_prefix . 'sosa.majs_sosa)') |
|
223 | + ->get(); |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * Remove all Sosa entries related to the gedcom file and user |
|
228 | - * |
|
229 | - * @param Tree $tree |
|
230 | - * @param UserInterface $user |
|
231 | - */ |
|
232 | - public function deleteAll(Tree $tree, UserInterface $user): void |
|
233 | - { |
|
234 | - DB::table('maj_sosa') |
|
235 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
236 | - ->where('majs_user_id', '=', $user->id()) |
|
237 | - ->delete(); |
|
238 | - } |
|
226 | + /** |
|
227 | + * Remove all Sosa entries related to the gedcom file and user |
|
228 | + * |
|
229 | + * @param Tree $tree |
|
230 | + * @param UserInterface $user |
|
231 | + */ |
|
232 | + public function deleteAll(Tree $tree, UserInterface $user): void |
|
233 | + { |
|
234 | + DB::table('maj_sosa') |
|
235 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
236 | + ->where('majs_user_id', '=', $user->id()) |
|
237 | + ->delete(); |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * |
|
242 | - * @param Tree $tree |
|
243 | - * @param UserInterface $user |
|
244 | - * @param int $sosa |
|
245 | - */ |
|
246 | - public function deleteAncestorsFrom(Tree $tree, UserInterface $user, int $sosa): void |
|
247 | - { |
|
248 | - DB::table('maj_sosa') |
|
249 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
250 | - ->where('majs_user_id', '=', $user->id()) |
|
251 | - ->where('majs_sosa', '>=', $sosa) |
|
252 | - ->whereRaw( |
|
253 | - 'FLOOR(majs_sosa / (POW(2, (majs_gen - ?)))) = ?', |
|
254 | - [$this->generation($sosa), $sosa] |
|
255 | - ) |
|
256 | - ->delete(); |
|
257 | - } |
|
240 | + /** |
|
241 | + * |
|
242 | + * @param Tree $tree |
|
243 | + * @param UserInterface $user |
|
244 | + * @param int $sosa |
|
245 | + */ |
|
246 | + public function deleteAncestorsFrom(Tree $tree, UserInterface $user, int $sosa): void |
|
247 | + { |
|
248 | + DB::table('maj_sosa') |
|
249 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
250 | + ->where('majs_user_id', '=', $user->id()) |
|
251 | + ->where('majs_sosa', '>=', $sosa) |
|
252 | + ->whereRaw( |
|
253 | + 'FLOOR(majs_sosa / (POW(2, (majs_gen - ?)))) = ?', |
|
254 | + [$this->generation($sosa), $sosa] |
|
255 | + ) |
|
256 | + ->delete(); |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * Insert (or update if already existing) a list of Sosa individuals |
|
261 | - * |
|
262 | - * @param Tree $tree |
|
263 | - * @param UserInterface $user |
|
264 | - * @param array<array<string,mixed>> $sosa_records |
|
265 | - */ |
|
266 | - public function insertOrUpdate(Tree $tree, UserInterface $user, array $sosa_records): void |
|
267 | - { |
|
268 | - $mass_update = DB::connection()->getDriverName() === 'mysql'; |
|
259 | + /** |
|
260 | + * Insert (or update if already existing) a list of Sosa individuals |
|
261 | + * |
|
262 | + * @param Tree $tree |
|
263 | + * @param UserInterface $user |
|
264 | + * @param array<array<string,mixed>> $sosa_records |
|
265 | + */ |
|
266 | + public function insertOrUpdate(Tree $tree, UserInterface $user, array $sosa_records): void |
|
267 | + { |
|
268 | + $mass_update = DB::connection()->getDriverName() === 'mysql'; |
|
269 | 269 | |
270 | - $bindings_placeholders = $bindings_values = []; |
|
271 | - $has_records = false; |
|
272 | - foreach ($sosa_records as $i => $row) { |
|
273 | - $gen = $this->generation($row['sosa']); |
|
274 | - if ($gen <= $this->maxSystemGenerations()) { |
|
275 | - $has_records = true; |
|
276 | - if ($mass_update) { |
|
277 | - $bindings_placeholders[] = '(:tree_id' . $i . ', :user_id' . $i . ', :sosa' . $i . ',' . |
|
278 | - ' :indi_id' . $i . ', :gen' . $i . ',' . |
|
279 | - ' :byear' . $i . ', :byearest' . $i . ', :dyear' . $i . ', :dyearest' . $i . ')'; |
|
280 | - $bindings_values += [ |
|
281 | - 'tree_id' . $i => $tree->id(), |
|
282 | - 'user_id' . $i => $user->id(), |
|
283 | - 'sosa' . $i => $row['sosa'], |
|
284 | - 'indi_id' . $i => $row['indi'], |
|
285 | - 'gen' . $i => $gen, |
|
286 | - 'byear' . $i => $row['birth_year'], |
|
287 | - 'byearest' . $i => $row['birth_year_est'], |
|
288 | - 'dyear' . $i => $row['death_year'], |
|
289 | - 'dyearest' . $i => $row['death_year_est'] |
|
290 | - ]; |
|
291 | - } else { |
|
292 | - DB::table('maj_sosa')->updateOrInsert( |
|
293 | - [ 'majs_gedcom_id' => $tree->id(), 'majs_user_id' => $user->id(), 'majs_sosa' => $row['sosa']], |
|
294 | - [ |
|
295 | - 'majs_i_id' => $row['indi'], |
|
296 | - 'majs_gen' => $gen, |
|
297 | - 'majs_birth_year' => $row['birth_year'], |
|
298 | - 'majs_birth_year_est' => $row['birth_year_est'], |
|
299 | - 'majs_death_year' => $row['death_year'], |
|
300 | - 'majs_death_year_est' => $row['death_year_est'] |
|
301 | - ] |
|
302 | - ); |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
270 | + $bindings_placeholders = $bindings_values = []; |
|
271 | + $has_records = false; |
|
272 | + foreach ($sosa_records as $i => $row) { |
|
273 | + $gen = $this->generation($row['sosa']); |
|
274 | + if ($gen <= $this->maxSystemGenerations()) { |
|
275 | + $has_records = true; |
|
276 | + if ($mass_update) { |
|
277 | + $bindings_placeholders[] = '(:tree_id' . $i . ', :user_id' . $i . ', :sosa' . $i . ',' . |
|
278 | + ' :indi_id' . $i . ', :gen' . $i . ',' . |
|
279 | + ' :byear' . $i . ', :byearest' . $i . ', :dyear' . $i . ', :dyearest' . $i . ')'; |
|
280 | + $bindings_values += [ |
|
281 | + 'tree_id' . $i => $tree->id(), |
|
282 | + 'user_id' . $i => $user->id(), |
|
283 | + 'sosa' . $i => $row['sosa'], |
|
284 | + 'indi_id' . $i => $row['indi'], |
|
285 | + 'gen' . $i => $gen, |
|
286 | + 'byear' . $i => $row['birth_year'], |
|
287 | + 'byearest' . $i => $row['birth_year_est'], |
|
288 | + 'dyear' . $i => $row['death_year'], |
|
289 | + 'dyearest' . $i => $row['death_year_est'] |
|
290 | + ]; |
|
291 | + } else { |
|
292 | + DB::table('maj_sosa')->updateOrInsert( |
|
293 | + [ 'majs_gedcom_id' => $tree->id(), 'majs_user_id' => $user->id(), 'majs_sosa' => $row['sosa']], |
|
294 | + [ |
|
295 | + 'majs_i_id' => $row['indi'], |
|
296 | + 'majs_gen' => $gen, |
|
297 | + 'majs_birth_year' => $row['birth_year'], |
|
298 | + 'majs_birth_year_est' => $row['birth_year_est'], |
|
299 | + 'majs_death_year' => $row['death_year'], |
|
300 | + 'majs_death_year_est' => $row['death_year_est'] |
|
301 | + ] |
|
302 | + ); |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | 306 | |
307 | - if ($has_records && $mass_update) { |
|
308 | - DB::connection()->statement( |
|
309 | - 'INSERT INTO `' . DB::connection()->getTablePrefix() . 'maj_sosa`' . |
|
310 | - ' (majs_gedcom_id, majs_user_id, majs_sosa,' . |
|
311 | - ' majs_i_id, majs_gen, majs_birth_year, majs_birth_year_est, majs_death_year, majs_death_year_est)' . |
|
312 | - ' VALUES ' . implode(',', $bindings_placeholders) . |
|
313 | - ' ON DUPLICATE KEY UPDATE majs_i_id = VALUES(majs_i_id), majs_gen = VALUES(majs_gen),' . |
|
314 | - ' majs_birth_year = VALUES(majs_birth_year), majs_birth_year_est = VALUES(majs_birth_year_est),' . |
|
315 | - ' majs_death_year = VALUES(majs_death_year), majs_death_year_est = VALUES(majs_death_year_est)', |
|
316 | - $bindings_values |
|
317 | - ); |
|
318 | - } |
|
319 | - } |
|
307 | + if ($has_records && $mass_update) { |
|
308 | + DB::connection()->statement( |
|
309 | + 'INSERT INTO `' . DB::connection()->getTablePrefix() . 'maj_sosa`' . |
|
310 | + ' (majs_gedcom_id, majs_user_id, majs_sosa,' . |
|
311 | + ' majs_i_id, majs_gen, majs_birth_year, majs_birth_year_est, majs_death_year, majs_death_year_est)' . |
|
312 | + ' VALUES ' . implode(',', $bindings_placeholders) . |
|
313 | + ' ON DUPLICATE KEY UPDATE majs_i_id = VALUES(majs_i_id), majs_gen = VALUES(majs_gen),' . |
|
314 | + ' majs_birth_year = VALUES(majs_birth_year), majs_birth_year_est = VALUES(majs_birth_year_est),' . |
|
315 | + ' majs_death_year = VALUES(majs_death_year), majs_death_year_est = VALUES(majs_death_year_est)', |
|
316 | + $bindings_values |
|
317 | + ); |
|
318 | + } |
|
319 | + } |
|
320 | 320 | } |
@@ -29,538 +29,538 @@ |
||
29 | 29 | */ |
30 | 30 | class SosaStatisticsService |
31 | 31 | { |
32 | - private UserInterface $user; |
|
33 | - private Tree $tree; |
|
34 | - |
|
35 | - /** |
|
36 | - * Constructor for Sosa Statistics Service |
|
37 | - * |
|
38 | - * @param Tree $tree |
|
39 | - * @param UserInterface $user |
|
40 | - */ |
|
41 | - public function __construct(Tree $tree, UserInterface $user) |
|
42 | - { |
|
43 | - $this->tree = $tree; |
|
44 | - $this->user = $user; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Check if PDO support the queries used in Sosa statistics. |
|
49 | - * |
|
50 | - * SQLite does not support RIGHT and FULL JOIN, and POWER/SQRT require a specific compilation flag. |
|
51 | - * |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function isPdoSupported(): bool |
|
55 | - { |
|
56 | - return DB::connection()->getDriverName() !== 'sqlite'; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Return the root individual for the reference tree and user. |
|
61 | - * |
|
62 | - * @return Individual|NULL |
|
63 | - */ |
|
64 | - public function rootIndividual(): ?Individual |
|
65 | - { |
|
66 | - $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
67 | - return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get the highest generation for the reference tree and user. |
|
72 | - * |
|
73 | - * @return int |
|
74 | - */ |
|
75 | - public function maxGeneration(): int |
|
76 | - { |
|
77 | - return (int) DB::table('maj_sosa') |
|
78 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
79 | - ->where('majs_user_id', '=', $this->user->id()) |
|
80 | - ->max('majs_gen'); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get the total count of individuals in the tree. |
|
85 | - * |
|
86 | - * @return int |
|
87 | - */ |
|
88 | - public function totalIndividuals(): int |
|
89 | - { |
|
90 | - return DB::table('individuals') |
|
91 | - ->where('i_file', '=', $this->tree->id()) |
|
92 | - ->count(); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get the total count of Sosa ancestors for all generations |
|
97 | - * |
|
98 | - * @return int |
|
99 | - */ |
|
100 | - public function totalAncestors(): int |
|
101 | - { |
|
102 | - return DB::table('maj_sosa') |
|
103 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
104 | - ->where('majs_user_id', '=', $this->user->id()) |
|
105 | - ->count(); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get the total count of Sosa ancestors for a generation |
|
110 | - * |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function totalAncestorsAtGeneration(int $gen): int |
|
114 | - { |
|
115 | - return DB::table('maj_sosa') |
|
116 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
117 | - ->where('majs_user_id', '=', $this->user->id()) |
|
118 | - ->where('majs_gen', '=', $gen) |
|
119 | - ->count(); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get the total count of distinct Sosa ancestors for all generations |
|
124 | - * |
|
125 | - * @return int |
|
126 | - */ |
|
127 | - public function totalDistinctAncestors(): int |
|
128 | - { |
|
129 | - return DB::table('maj_sosa') |
|
130 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
131 | - ->where('majs_user_id', '=', $this->user->id()) |
|
132 | - ->distinct() |
|
133 | - ->count('majs_i_id'); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
138 | - * |
|
139 | - * @return float |
|
140 | - */ |
|
141 | - public function meanGenerationTime(): float |
|
142 | - { |
|
143 | - $row = DB::table('maj_sosa') |
|
144 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
145 | - ->where('majs_user_id', '=', $this->user->id()) |
|
146 | - ->whereNotNull('majs_birth_year') |
|
147 | - ->selectRaw('COUNT(majs_sosa) AS n') |
|
148 | - ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
149 | - ->selectRaw('SUM(majs_gen) AS sum_x') |
|
150 | - ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
151 | - ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
152 | - ->get()->first(); |
|
153 | - |
|
154 | - return $row->n === 0 ? 0 : |
|
155 | - -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Get the statistic array detailed by generation. |
|
160 | - * Statistics for each generation are: |
|
161 | - * - The number of Sosa in generation |
|
162 | - * - The number of Sosa up to generation |
|
163 | - * - The number of distinct Sosa up to generation |
|
164 | - * - The year of the first birth in generation |
|
165 | - * - The year of the first estimated birth in generation |
|
166 | - * - The year of the last birth in generation |
|
167 | - * - The year of the last estimated birth in generation |
|
168 | - * - The average year of birth in generation |
|
169 | - * |
|
170 | - * @return array<int, array<string, int|null>> Statistics array |
|
171 | - */ |
|
172 | - public function statisticsByGenerations(): array |
|
173 | - { |
|
174 | - $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
175 | - $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
176 | - |
|
177 | - $statistics_by_gen = []; |
|
178 | - foreach ($stats_by_gen as $gen => $stats_gen) { |
|
179 | - $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
180 | - 'sosaCount' => (int) $stats_gen->total_sosa, |
|
181 | - 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
182 | - 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
183 | - 'firstBirth' => $stats_gen->first_year, |
|
184 | - 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
185 | - 'lastBirth' => $stats_gen->last_year, |
|
186 | - 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - return $statistics_by_gen; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Returns the basic statistics data by generation. |
|
195 | - * |
|
196 | - * @return Collection<int, \stdClass> |
|
197 | - */ |
|
198 | - private function statisticsByGenerationBasicData(): Collection |
|
199 | - { |
|
200 | - return DB::table('maj_sosa') |
|
201 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
202 | - ->where('majs_user_id', '=', $this->user->id()) |
|
203 | - ->groupBy('majs_gen') |
|
204 | - ->orderBy('majs_gen', 'asc') |
|
205 | - ->select('majs_gen AS gen') |
|
206 | - ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
207 | - ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
208 | - ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
209 | - ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
210 | - ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
211 | - ->get()->keyBy('gen'); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns the cumulative statistics data by generation |
|
216 | - * |
|
217 | - * @return Collection<int, \stdClass> |
|
218 | - */ |
|
219 | - private function statisticsByGenerationCumulativeData(): Collection |
|
220 | - { |
|
221 | - $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
222 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
223 | - ->where('majs_user_id', '=', $this->user->id()); |
|
224 | - |
|
225 | - return DB::table('maj_sosa') |
|
226 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
227 | - $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
228 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
229 | - ->where('majs_user_id', '=', $this->user->id()); |
|
230 | - }) |
|
231 | - ->groupBy('list_gen.majs_gen') |
|
232 | - ->select('list_gen.majs_gen AS gen') |
|
233 | - ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
234 | - ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
235 | - ->selectRaw('1 - COUNT(DISTINCT majs_i_id) / COUNT(majs_i_id) AS pedi_collapse_simple') |
|
236 | - ->get()->keyBy('gen'); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Returns the pedigree collapse improved calculation by generation. |
|
241 | - * |
|
242 | - * Format: |
|
243 | - * - key : generation |
|
244 | - * - values: |
|
245 | - * - pedi_collapse_roots : pedigree collapse of ancestor roots for the generation |
|
246 | - * - pedi_collapse_xgen : pedigree cross-generation shrinkage for the generation |
|
247 | - * |
|
248 | - * @return array<int, array<string, float>> |
|
249 | - */ |
|
250 | - public function pedigreeCollapseByGenerationData(): array |
|
251 | - { |
|
252 | - if (!$this->isPdoSupported()) { |
|
253 | - return []; |
|
254 | - } |
|
255 | - |
|
256 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
257 | - |
|
258 | - $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
259 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
260 | - ->where('majs_user_id', '=', $this->user->id()); |
|
261 | - |
|
262 | - /* Compute the contributions of nodes of previous generations to the current generation */ |
|
263 | - $root_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
264 | - ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
265 | - ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
266 | - ->selectRaw( |
|
267 | - '(CASE ' . |
|
268 | - ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' . |
|
269 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
270 | - ' ELSE 0 ' . |
|
271 | - ' END)' . |
|
272 | - ' + (CASE ' . |
|
273 | - ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' . |
|
274 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
275 | - ' ELSE 0 ' . |
|
276 | - ' END) contrib' |
|
277 | - ) |
|
278 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
279 | - $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen') |
|
280 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
281 | - ->where('majs_user_id', '=', $this->user->id()); |
|
282 | - }) |
|
283 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
284 | - // Link to sosa's father |
|
285 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
286 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
287 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
288 | - }) |
|
289 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
290 | - // Link to sosa's mother |
|
291 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
292 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
293 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
294 | - }) |
|
295 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
296 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
297 | - ->where(function (Builder $query): void { |
|
298 | - $query->whereNull('sosa_fat.majs_i_id') |
|
299 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
300 | - }); |
|
301 | - |
|
302 | - /* Identify nodes in the generations with ancestors who are also in the same generation. |
|
32 | + private UserInterface $user; |
|
33 | + private Tree $tree; |
|
34 | + |
|
35 | + /** |
|
36 | + * Constructor for Sosa Statistics Service |
|
37 | + * |
|
38 | + * @param Tree $tree |
|
39 | + * @param UserInterface $user |
|
40 | + */ |
|
41 | + public function __construct(Tree $tree, UserInterface $user) |
|
42 | + { |
|
43 | + $this->tree = $tree; |
|
44 | + $this->user = $user; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Check if PDO support the queries used in Sosa statistics. |
|
49 | + * |
|
50 | + * SQLite does not support RIGHT and FULL JOIN, and POWER/SQRT require a specific compilation flag. |
|
51 | + * |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function isPdoSupported(): bool |
|
55 | + { |
|
56 | + return DB::connection()->getDriverName() !== 'sqlite'; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Return the root individual for the reference tree and user. |
|
61 | + * |
|
62 | + * @return Individual|NULL |
|
63 | + */ |
|
64 | + public function rootIndividual(): ?Individual |
|
65 | + { |
|
66 | + $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
67 | + return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get the highest generation for the reference tree and user. |
|
72 | + * |
|
73 | + * @return int |
|
74 | + */ |
|
75 | + public function maxGeneration(): int |
|
76 | + { |
|
77 | + return (int) DB::table('maj_sosa') |
|
78 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
79 | + ->where('majs_user_id', '=', $this->user->id()) |
|
80 | + ->max('majs_gen'); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get the total count of individuals in the tree. |
|
85 | + * |
|
86 | + * @return int |
|
87 | + */ |
|
88 | + public function totalIndividuals(): int |
|
89 | + { |
|
90 | + return DB::table('individuals') |
|
91 | + ->where('i_file', '=', $this->tree->id()) |
|
92 | + ->count(); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get the total count of Sosa ancestors for all generations |
|
97 | + * |
|
98 | + * @return int |
|
99 | + */ |
|
100 | + public function totalAncestors(): int |
|
101 | + { |
|
102 | + return DB::table('maj_sosa') |
|
103 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
104 | + ->where('majs_user_id', '=', $this->user->id()) |
|
105 | + ->count(); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get the total count of Sosa ancestors for a generation |
|
110 | + * |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function totalAncestorsAtGeneration(int $gen): int |
|
114 | + { |
|
115 | + return DB::table('maj_sosa') |
|
116 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
117 | + ->where('majs_user_id', '=', $this->user->id()) |
|
118 | + ->where('majs_gen', '=', $gen) |
|
119 | + ->count(); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get the total count of distinct Sosa ancestors for all generations |
|
124 | + * |
|
125 | + * @return int |
|
126 | + */ |
|
127 | + public function totalDistinctAncestors(): int |
|
128 | + { |
|
129 | + return DB::table('maj_sosa') |
|
130 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
131 | + ->where('majs_user_id', '=', $this->user->id()) |
|
132 | + ->distinct() |
|
133 | + ->count('majs_i_id'); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
138 | + * |
|
139 | + * @return float |
|
140 | + */ |
|
141 | + public function meanGenerationTime(): float |
|
142 | + { |
|
143 | + $row = DB::table('maj_sosa') |
|
144 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
145 | + ->where('majs_user_id', '=', $this->user->id()) |
|
146 | + ->whereNotNull('majs_birth_year') |
|
147 | + ->selectRaw('COUNT(majs_sosa) AS n') |
|
148 | + ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
149 | + ->selectRaw('SUM(majs_gen) AS sum_x') |
|
150 | + ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
151 | + ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
152 | + ->get()->first(); |
|
153 | + |
|
154 | + return $row->n === 0 ? 0 : |
|
155 | + -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Get the statistic array detailed by generation. |
|
160 | + * Statistics for each generation are: |
|
161 | + * - The number of Sosa in generation |
|
162 | + * - The number of Sosa up to generation |
|
163 | + * - The number of distinct Sosa up to generation |
|
164 | + * - The year of the first birth in generation |
|
165 | + * - The year of the first estimated birth in generation |
|
166 | + * - The year of the last birth in generation |
|
167 | + * - The year of the last estimated birth in generation |
|
168 | + * - The average year of birth in generation |
|
169 | + * |
|
170 | + * @return array<int, array<string, int|null>> Statistics array |
|
171 | + */ |
|
172 | + public function statisticsByGenerations(): array |
|
173 | + { |
|
174 | + $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
175 | + $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
176 | + |
|
177 | + $statistics_by_gen = []; |
|
178 | + foreach ($stats_by_gen as $gen => $stats_gen) { |
|
179 | + $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
180 | + 'sosaCount' => (int) $stats_gen->total_sosa, |
|
181 | + 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
182 | + 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
183 | + 'firstBirth' => $stats_gen->first_year, |
|
184 | + 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
185 | + 'lastBirth' => $stats_gen->last_year, |
|
186 | + 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + return $statistics_by_gen; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Returns the basic statistics data by generation. |
|
195 | + * |
|
196 | + * @return Collection<int, \stdClass> |
|
197 | + */ |
|
198 | + private function statisticsByGenerationBasicData(): Collection |
|
199 | + { |
|
200 | + return DB::table('maj_sosa') |
|
201 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
202 | + ->where('majs_user_id', '=', $this->user->id()) |
|
203 | + ->groupBy('majs_gen') |
|
204 | + ->orderBy('majs_gen', 'asc') |
|
205 | + ->select('majs_gen AS gen') |
|
206 | + ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
207 | + ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
208 | + ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
209 | + ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
210 | + ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
211 | + ->get()->keyBy('gen'); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns the cumulative statistics data by generation |
|
216 | + * |
|
217 | + * @return Collection<int, \stdClass> |
|
218 | + */ |
|
219 | + private function statisticsByGenerationCumulativeData(): Collection |
|
220 | + { |
|
221 | + $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
222 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
223 | + ->where('majs_user_id', '=', $this->user->id()); |
|
224 | + |
|
225 | + return DB::table('maj_sosa') |
|
226 | + ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
227 | + $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
228 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
229 | + ->where('majs_user_id', '=', $this->user->id()); |
|
230 | + }) |
|
231 | + ->groupBy('list_gen.majs_gen') |
|
232 | + ->select('list_gen.majs_gen AS gen') |
|
233 | + ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
234 | + ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
235 | + ->selectRaw('1 - COUNT(DISTINCT majs_i_id) / COUNT(majs_i_id) AS pedi_collapse_simple') |
|
236 | + ->get()->keyBy('gen'); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Returns the pedigree collapse improved calculation by generation. |
|
241 | + * |
|
242 | + * Format: |
|
243 | + * - key : generation |
|
244 | + * - values: |
|
245 | + * - pedi_collapse_roots : pedigree collapse of ancestor roots for the generation |
|
246 | + * - pedi_collapse_xgen : pedigree cross-generation shrinkage for the generation |
|
247 | + * |
|
248 | + * @return array<int, array<string, float>> |
|
249 | + */ |
|
250 | + public function pedigreeCollapseByGenerationData(): array |
|
251 | + { |
|
252 | + if (!$this->isPdoSupported()) { |
|
253 | + return []; |
|
254 | + } |
|
255 | + |
|
256 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
257 | + |
|
258 | + $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
259 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
260 | + ->where('majs_user_id', '=', $this->user->id()); |
|
261 | + |
|
262 | + /* Compute the contributions of nodes of previous generations to the current generation */ |
|
263 | + $root_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
264 | + ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
265 | + ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
266 | + ->selectRaw( |
|
267 | + '(CASE ' . |
|
268 | + ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' . |
|
269 | + ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
270 | + ' ELSE 0 ' . |
|
271 | + ' END)' . |
|
272 | + ' + (CASE ' . |
|
273 | + ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' . |
|
274 | + ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
275 | + ' ELSE 0 ' . |
|
276 | + ' END) contrib' |
|
277 | + ) |
|
278 | + ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
279 | + $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen') |
|
280 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
281 | + ->where('majs_user_id', '=', $this->user->id()); |
|
282 | + }) |
|
283 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
284 | + // Link to sosa's father |
|
285 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
286 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
287 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
288 | + }) |
|
289 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
290 | + // Link to sosa's mother |
|
291 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
292 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
293 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
294 | + }) |
|
295 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
296 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
297 | + ->where(function (Builder $query): void { |
|
298 | + $query->whereNull('sosa_fat.majs_i_id') |
|
299 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
300 | + }); |
|
301 | + |
|
302 | + /* Identify nodes in the generations with ancestors who are also in the same generation. |
|
303 | 303 | * This is the vertical/generational collapse that will reduce the number or roots. |
304 | 304 | */ |
305 | - $non_roots_ancestors = DB::table('maj_sosa AS sosa') |
|
306 | - ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa']) |
|
307 | - ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' . |
|
308 | - ' AS full_ancestors') |
|
309 | - ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void { |
|
310 | - $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen') |
|
311 | - ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' . |
|
312 | - $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' . |
|
313 | - $table_prefix . 'sosa.majs_sosa') |
|
314 | - ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id()) |
|
315 | - ->where('sosa_anc.majs_user_id', '=', $this->user->id()); |
|
316 | - }) |
|
317 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
318 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
319 | - ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void { |
|
320 | - $query->from('maj_sosa AS sosa_gen') |
|
321 | - ->select('sosa_gen.majs_i_id')->distinct() |
|
322 | - ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id()) |
|
323 | - ->where('sosa_gen.majs_user_id', '=', $this->user->id()) |
|
324 | - ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen'); |
|
325 | - }) |
|
326 | - ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', |
|
327 | - 'sosa.majs_sosa', 'sosa.majs_i_id']); |
|
328 | - |
|
329 | - /* Compute the contribution of the nodes in the generation, |
|
305 | + $non_roots_ancestors = DB::table('maj_sosa AS sosa') |
|
306 | + ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa']) |
|
307 | + ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' . |
|
308 | + ' AS full_ancestors') |
|
309 | + ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void { |
|
310 | + $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen') |
|
311 | + ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' . |
|
312 | + $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' . |
|
313 | + $table_prefix . 'sosa.majs_sosa') |
|
314 | + ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id()) |
|
315 | + ->where('sosa_anc.majs_user_id', '=', $this->user->id()); |
|
316 | + }) |
|
317 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
318 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
319 | + ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void { |
|
320 | + $query->from('maj_sosa AS sosa_gen') |
|
321 | + ->select('sosa_gen.majs_i_id')->distinct() |
|
322 | + ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id()) |
|
323 | + ->where('sosa_gen.majs_user_id', '=', $this->user->id()) |
|
324 | + ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen'); |
|
325 | + }) |
|
326 | + ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', |
|
327 | + 'sosa.majs_sosa', 'sosa.majs_i_id']); |
|
328 | + |
|
329 | + /* Compute the contribution of the nodes in the generation, |
|
330 | 330 | * excluding the nodes with ancestors in the same generation. |
331 | 331 | * Nodes with a parent missing are not excluded to cater for the missing one. |
332 | 332 | */ |
333 | - $known_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
334 | - ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
335 | - ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
336 | - ->selectRaw('1 AS contrib') |
|
337 | - ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void { |
|
338 | - $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen') |
|
339 | - ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa') |
|
340 | - ->where('nonroot.full_ancestors', '>', 0) |
|
341 | - ->where('nonroot.majs_gedcom_id', '=', $this->tree->id()) |
|
342 | - ->where('nonroot.majs_user_id', '=', $this->user->id()); |
|
343 | - }) |
|
344 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
345 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
346 | - ->whereNull('nonroot.majs_sosa'); |
|
347 | - |
|
348 | - /* Aggregate both queries, and calculate the sum of contributions by generation roots. |
|
333 | + $known_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
334 | + ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
335 | + ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
336 | + ->selectRaw('1 AS contrib') |
|
337 | + ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void { |
|
338 | + $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen') |
|
339 | + ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa') |
|
340 | + ->where('nonroot.full_ancestors', '>', 0) |
|
341 | + ->where('nonroot.majs_gedcom_id', '=', $this->tree->id()) |
|
342 | + ->where('nonroot.majs_user_id', '=', $this->user->id()); |
|
343 | + }) |
|
344 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
345 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
346 | + ->whereNull('nonroot.majs_sosa'); |
|
347 | + |
|
348 | + /* Aggregate both queries, and calculate the sum of contributions by generation roots. |
|
349 | 349 | * Exclude as well nodes that already appear in lower generations, as their branche has already been reduced. |
350 | 350 | */ |
351 | - $ancestors_contributions_sum = DB::connection()->query() |
|
352 | - ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs') |
|
353 | - ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id']) |
|
354 | - ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib']) |
|
355 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' . |
|
356 | - $table_prefix . 'sosa_contribs.contrib AS totalContrib') |
|
357 | - ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void { |
|
358 | - $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen') |
|
359 | - ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id') |
|
360 | - ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id()) |
|
361 | - ->where('sosa_low.majs_user_id', '=', $this->user->id()); |
|
362 | - }) |
|
363 | - ->whereNull('sosa_low.majs_sosa') |
|
364 | - ->groupBy(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id', |
|
365 | - 'sosa_contribs.majs_i_id', 'sosa_contribs.contrib']); |
|
366 | - |
|
367 | - // Aggregate all generation roots to compute root and generation pedigree collapse |
|
368 | - $pedi_collapse_coll = DB::connection()->query()->fromSub($ancestors_contributions_sum, 'sosa_contribs_sum') |
|
369 | - ->select(['gen'])->selectRaw('SUM(contrib), SUM(totalContrib)') |
|
370 | - ->selectRaw('1 - SUM(contrib) / SUM(totalContrib) AS pedi_collapse_roots') // Roots/horizontal collapse |
|
371 | - ->selectRaw('1 - SUM(totalContrib) / POWER ( 2, gen - 1) AS pedi_collapse_xgen') // Crossgeneration collapse |
|
372 | - ->groupBy(['gen', 'majs_gedcom_id', 'majs_user_id']) |
|
373 | - ->orderBy('gen') |
|
374 | - ->get(); |
|
375 | - |
|
376 | - $pedi_collapse_by_gen = []; |
|
377 | - foreach ($pedi_collapse_coll as $collapse_gen) { |
|
378 | - $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array( |
|
379 | - 'pedi_collapse_roots' => (float) $collapse_gen->pedi_collapse_roots, |
|
380 | - 'pedi_collapse_xgen' => (float) $collapse_gen->pedi_collapse_xgen |
|
381 | - ); |
|
382 | - } |
|
383 | - return $pedi_collapse_by_gen; |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
388 | - * Sosa 1 is of generation 1. |
|
389 | - * |
|
390 | - * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
391 | - * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
392 | - * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
393 | - * |
|
394 | - * Format: |
|
395 | - * - key : sosa number of the ancestor |
|
396 | - * - values: |
|
397 | - * - root_ancestor_id : ID of the ancestor |
|
398 | - * - mean_gen_depth : Mean generation depth |
|
399 | - * - stddev_gen_depth : Standard deviation of generation depth |
|
400 | - * |
|
401 | - * @param int $gen Sosa generation |
|
402 | - * @return Collection<int, \stdClass> |
|
403 | - */ |
|
404 | - public function generationDepthStatsAtGeneration(int $gen): Collection |
|
405 | - { |
|
406 | - if (!$this->isPdoSupported()) { |
|
407 | - return collect(); |
|
408 | - } |
|
409 | - |
|
410 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
411 | - $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
412 | - ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
413 | - ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
414 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
415 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
416 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
417 | - // Link to sosa's father |
|
418 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
419 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
420 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
421 | - }) |
|
422 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
423 | - // Link to sosa's mother |
|
424 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
425 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
426 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
427 | - }) |
|
428 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
429 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
430 | - ->where('sosa.majs_gen', '>=', $gen) |
|
431 | - ->where(function (Builder $query): void { |
|
432 | - $query->whereNull('sosa_fat.majs_i_id') |
|
433 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
434 | - }) |
|
435 | - ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
436 | - |
|
437 | - return DB::table('maj_sosa AS sosa_list') |
|
438 | - ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
439 | - ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
440 | - ->selectRaw(' SQRT(' . |
|
441 | - ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
442 | - ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
443 | - ' ) AS stddev_gen_depth') |
|
444 | - ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
445 | - $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
446 | - ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
447 | - ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
448 | - }) |
|
449 | - ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
450 | - ->orderBy('stats_by_gen.root_ancestor') |
|
451 | - ->get()->keyBy('root_ancestor_sosa'); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Return a collection of the most duplicated root Sosa ancestors. |
|
456 | - * The number of ancestors to return is limited by the parameter $limit. |
|
457 | - * If several individuals are tied when reaching the limit, none of them are returned, |
|
458 | - * which means that there can be less individuals returned than requested. |
|
459 | - * |
|
460 | - * Format: |
|
461 | - * - value: |
|
462 | - * - sosa_i_id : sosa individual |
|
463 | - * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
464 | - * |
|
465 | - * @param int $limit |
|
466 | - * @return Collection<\stdClass> |
|
467 | - */ |
|
468 | - public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
469 | - { |
|
470 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
471 | - $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
472 | - ->select('sosa.majs_i_id AS sosa_i_id') |
|
473 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
474 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
475 | - // Link to sosa's father |
|
476 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
477 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
478 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
479 | - }) |
|
480 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
481 | - // Link to sosa's mother |
|
482 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
483 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
484 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
485 | - }) |
|
486 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
487 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
488 | - ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
489 | - ->whereNull('sosa_mot.majs_sosa') |
|
490 | - ->groupBy('sosa.majs_i_id') |
|
491 | - ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
492 | - ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
493 | - ->limit($limit + 1) // We want to select one more than required, for ties |
|
494 | - ->get(); |
|
495 | - |
|
496 | - if ($multiple_ancestors->count() > $limit) { |
|
497 | - $last_count = $multiple_ancestors->last()->sosa_count; |
|
498 | - $multiple_ancestors = $multiple_ancestors->reject( |
|
499 | - fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
500 | - ); |
|
501 | - } |
|
502 | - return $multiple_ancestors; |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
507 | - * at a specified generation. |
|
508 | - * |
|
509 | - * Format: |
|
510 | - * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
511 | - * For instance 3 represent the maternal grand father |
|
512 | - * 0 is used for shared ancestors |
|
513 | - * - values: |
|
514 | - * - branches: same as key |
|
515 | - * - majs_i_id: xref of the ancestor at rank key in generation G, or null for shared ancestors |
|
516 | - * - count_indi: number of ancestors exclusively in the ancestors of the ancestor at rank key |
|
517 | - * |
|
518 | - * For instance a result at generation 3 could be : |
|
519 | - * [ |
|
520 | - * 0 => { branches: 0, majs_i_id: X1, count_indi: 12 } -> 12 ancestors are shared by the grand-parents |
|
521 | - * 1 => { branches: 1, majs_i_id: X2, count_indi: 32 } -> 32 ancestors are exclusive to the paternal grand-father |
|
522 | - * 2 => { branches: 2, majs_i_id: X3, count_indi: 25 } -> 25 ancestors are exclusive to the paternal grand-mother |
|
523 | - * 3 => { branches: 3, majs_i_id: X4, count_indi: 12 } -> 12 ancestors are exclusive to the maternal grand-father |
|
524 | - * 4 => { branches: 4, majs_i_id: X5, count_indi: 30 } -> 30 ancestors are exclusive to the maternal grand-mother |
|
525 | - * ] |
|
526 | - * |
|
527 | - * @param int $gen |
|
528 | - * @return Collection<int, \stdClass> |
|
529 | - */ |
|
530 | - public function ancestorsDispersionForGeneration(int $gen): Collection |
|
531 | - { |
|
532 | - if (!$this->isPdoSupported()) { |
|
533 | - return collect(); |
|
534 | - } |
|
535 | - |
|
536 | - $ancestors_branches = DB::table('maj_sosa') |
|
537 | - ->select('majs_i_id AS i_id') |
|
538 | - ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
539 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
540 | - ->where('majs_user_id', '=', $this->user->id()) |
|
541 | - ->where('majs_gen', '>=', $gen) |
|
542 | - ->groupBy('majs_i_id', 'branch'); |
|
543 | - |
|
544 | - $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
545 | - ->fromSub($ancestors_branches, 'indi_branch') |
|
546 | - ->select('i_id') |
|
547 | - ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
548 | - ->groupBy('i_id'); |
|
549 | - |
|
550 | - return DB::table('maj_sosa') |
|
551 | - ->rightJoinSub( |
|
552 | - $consolidated_ancestors_branches, |
|
553 | - 'indi_branch_consolidated', |
|
554 | - function (JoinClause $join) use ($gen): void { |
|
555 | - $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
556 | - ->where('maj_sosa.majs_user_id', '=', $this->user->id()) |
|
557 | - ->where('branches', '>', 0) |
|
558 | - ->whereRaw('majs_sosa = POW(2, ? - 1) + branches - 1', [$gen]); |
|
559 | - } |
|
560 | - ) |
|
561 | - ->select(['branches', 'majs_i_id']) |
|
562 | - ->selectRaw('COUNT(i_id) AS count_indi') |
|
563 | - ->groupBy(['branches', 'majs_i_id']) |
|
564 | - ->get()->keyBy('branches'); |
|
565 | - } |
|
351 | + $ancestors_contributions_sum = DB::connection()->query() |
|
352 | + ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs') |
|
353 | + ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id']) |
|
354 | + ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib']) |
|
355 | + ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' . |
|
356 | + $table_prefix . 'sosa_contribs.contrib AS totalContrib') |
|
357 | + ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void { |
|
358 | + $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen') |
|
359 | + ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id') |
|
360 | + ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id()) |
|
361 | + ->where('sosa_low.majs_user_id', '=', $this->user->id()); |
|
362 | + }) |
|
363 | + ->whereNull('sosa_low.majs_sosa') |
|
364 | + ->groupBy(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id', |
|
365 | + 'sosa_contribs.majs_i_id', 'sosa_contribs.contrib']); |
|
366 | + |
|
367 | + // Aggregate all generation roots to compute root and generation pedigree collapse |
|
368 | + $pedi_collapse_coll = DB::connection()->query()->fromSub($ancestors_contributions_sum, 'sosa_contribs_sum') |
|
369 | + ->select(['gen'])->selectRaw('SUM(contrib), SUM(totalContrib)') |
|
370 | + ->selectRaw('1 - SUM(contrib) / SUM(totalContrib) AS pedi_collapse_roots') // Roots/horizontal collapse |
|
371 | + ->selectRaw('1 - SUM(totalContrib) / POWER ( 2, gen - 1) AS pedi_collapse_xgen') // Crossgeneration collapse |
|
372 | + ->groupBy(['gen', 'majs_gedcom_id', 'majs_user_id']) |
|
373 | + ->orderBy('gen') |
|
374 | + ->get(); |
|
375 | + |
|
376 | + $pedi_collapse_by_gen = []; |
|
377 | + foreach ($pedi_collapse_coll as $collapse_gen) { |
|
378 | + $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array( |
|
379 | + 'pedi_collapse_roots' => (float) $collapse_gen->pedi_collapse_roots, |
|
380 | + 'pedi_collapse_xgen' => (float) $collapse_gen->pedi_collapse_xgen |
|
381 | + ); |
|
382 | + } |
|
383 | + return $pedi_collapse_by_gen; |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
388 | + * Sosa 1 is of generation 1. |
|
389 | + * |
|
390 | + * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
391 | + * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
392 | + * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
393 | + * |
|
394 | + * Format: |
|
395 | + * - key : sosa number of the ancestor |
|
396 | + * - values: |
|
397 | + * - root_ancestor_id : ID of the ancestor |
|
398 | + * - mean_gen_depth : Mean generation depth |
|
399 | + * - stddev_gen_depth : Standard deviation of generation depth |
|
400 | + * |
|
401 | + * @param int $gen Sosa generation |
|
402 | + * @return Collection<int, \stdClass> |
|
403 | + */ |
|
404 | + public function generationDepthStatsAtGeneration(int $gen): Collection |
|
405 | + { |
|
406 | + if (!$this->isPdoSupported()) { |
|
407 | + return collect(); |
|
408 | + } |
|
409 | + |
|
410 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
411 | + $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
412 | + ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
413 | + ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
414 | + ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
415 | + ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
416 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
417 | + // Link to sosa's father |
|
418 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
419 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
420 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
421 | + }) |
|
422 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
423 | + // Link to sosa's mother |
|
424 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
425 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
426 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
427 | + }) |
|
428 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
429 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
430 | + ->where('sosa.majs_gen', '>=', $gen) |
|
431 | + ->where(function (Builder $query): void { |
|
432 | + $query->whereNull('sosa_fat.majs_i_id') |
|
433 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
434 | + }) |
|
435 | + ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
436 | + |
|
437 | + return DB::table('maj_sosa AS sosa_list') |
|
438 | + ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
439 | + ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
440 | + ->selectRaw(' SQRT(' . |
|
441 | + ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
442 | + ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
443 | + ' ) AS stddev_gen_depth') |
|
444 | + ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
445 | + $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
446 | + ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
447 | + ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
448 | + }) |
|
449 | + ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
450 | + ->orderBy('stats_by_gen.root_ancestor') |
|
451 | + ->get()->keyBy('root_ancestor_sosa'); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Return a collection of the most duplicated root Sosa ancestors. |
|
456 | + * The number of ancestors to return is limited by the parameter $limit. |
|
457 | + * If several individuals are tied when reaching the limit, none of them are returned, |
|
458 | + * which means that there can be less individuals returned than requested. |
|
459 | + * |
|
460 | + * Format: |
|
461 | + * - value: |
|
462 | + * - sosa_i_id : sosa individual |
|
463 | + * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
464 | + * |
|
465 | + * @param int $limit |
|
466 | + * @return Collection<\stdClass> |
|
467 | + */ |
|
468 | + public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
469 | + { |
|
470 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
471 | + $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
472 | + ->select('sosa.majs_i_id AS sosa_i_id') |
|
473 | + ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
474 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
475 | + // Link to sosa's father |
|
476 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
477 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
478 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
479 | + }) |
|
480 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
481 | + // Link to sosa's mother |
|
482 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
483 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
484 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
485 | + }) |
|
486 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
487 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
488 | + ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
489 | + ->whereNull('sosa_mot.majs_sosa') |
|
490 | + ->groupBy('sosa.majs_i_id') |
|
491 | + ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
492 | + ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
493 | + ->limit($limit + 1) // We want to select one more than required, for ties |
|
494 | + ->get(); |
|
495 | + |
|
496 | + if ($multiple_ancestors->count() > $limit) { |
|
497 | + $last_count = $multiple_ancestors->last()->sosa_count; |
|
498 | + $multiple_ancestors = $multiple_ancestors->reject( |
|
499 | + fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
500 | + ); |
|
501 | + } |
|
502 | + return $multiple_ancestors; |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
507 | + * at a specified generation. |
|
508 | + * |
|
509 | + * Format: |
|
510 | + * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
511 | + * For instance 3 represent the maternal grand father |
|
512 | + * 0 is used for shared ancestors |
|
513 | + * - values: |
|
514 | + * - branches: same as key |
|
515 | + * - majs_i_id: xref of the ancestor at rank key in generation G, or null for shared ancestors |
|
516 | + * - count_indi: number of ancestors exclusively in the ancestors of the ancestor at rank key |
|
517 | + * |
|
518 | + * For instance a result at generation 3 could be : |
|
519 | + * [ |
|
520 | + * 0 => { branches: 0, majs_i_id: X1, count_indi: 12 } -> 12 ancestors are shared by the grand-parents |
|
521 | + * 1 => { branches: 1, majs_i_id: X2, count_indi: 32 } -> 32 ancestors are exclusive to the paternal grand-father |
|
522 | + * 2 => { branches: 2, majs_i_id: X3, count_indi: 25 } -> 25 ancestors are exclusive to the paternal grand-mother |
|
523 | + * 3 => { branches: 3, majs_i_id: X4, count_indi: 12 } -> 12 ancestors are exclusive to the maternal grand-father |
|
524 | + * 4 => { branches: 4, majs_i_id: X5, count_indi: 30 } -> 30 ancestors are exclusive to the maternal grand-mother |
|
525 | + * ] |
|
526 | + * |
|
527 | + * @param int $gen |
|
528 | + * @return Collection<int, \stdClass> |
|
529 | + */ |
|
530 | + public function ancestorsDispersionForGeneration(int $gen): Collection |
|
531 | + { |
|
532 | + if (!$this->isPdoSupported()) { |
|
533 | + return collect(); |
|
534 | + } |
|
535 | + |
|
536 | + $ancestors_branches = DB::table('maj_sosa') |
|
537 | + ->select('majs_i_id AS i_id') |
|
538 | + ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
539 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
540 | + ->where('majs_user_id', '=', $this->user->id()) |
|
541 | + ->where('majs_gen', '>=', $gen) |
|
542 | + ->groupBy('majs_i_id', 'branch'); |
|
543 | + |
|
544 | + $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
545 | + ->fromSub($ancestors_branches, 'indi_branch') |
|
546 | + ->select('i_id') |
|
547 | + ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
548 | + ->groupBy('i_id'); |
|
549 | + |
|
550 | + return DB::table('maj_sosa') |
|
551 | + ->rightJoinSub( |
|
552 | + $consolidated_ancestors_branches, |
|
553 | + 'indi_branch_consolidated', |
|
554 | + function (JoinClause $join) use ($gen): void { |
|
555 | + $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
556 | + ->where('maj_sosa.majs_user_id', '=', $this->user->id()) |
|
557 | + ->where('branches', '>', 0) |
|
558 | + ->whereRaw('majs_sosa = POW(2, ? - 1) + branches - 1', [$gen]); |
|
559 | + } |
|
560 | + ) |
|
561 | + ->select(['branches', 'majs_i_id']) |
|
562 | + ->selectRaw('COUNT(i_id) AS count_indi') |
|
563 | + ->groupBy(['branches', 'majs_i_id']) |
|
564 | + ->get()->keyBy('branches'); |
|
565 | + } |
|
566 | 566 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function maxGeneration(): int |
76 | 76 | { |
77 | - return (int) DB::table('maj_sosa') |
|
77 | + return (int)DB::table('maj_sosa') |
|
78 | 78 | ->where('majs_gedcom_id', '=', $this->tree->id()) |
79 | 79 | ->where('majs_user_id', '=', $this->user->id()) |
80 | 80 | ->max('majs_gen'); |
@@ -151,8 +151,7 @@ discard block |
||
151 | 151 | ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
152 | 152 | ->get()->first(); |
153 | 153 | |
154 | - return $row->n === 0 ? 0 : |
|
155 | - -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
154 | + return $row->n === 0 ? 0 : -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
156 | 155 | } |
157 | 156 | |
158 | 157 | /** |
@@ -176,10 +175,10 @@ discard block |
||
176 | 175 | |
177 | 176 | $statistics_by_gen = []; |
178 | 177 | foreach ($stats_by_gen as $gen => $stats_gen) { |
179 | - $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
180 | - 'sosaCount' => (int) $stats_gen->total_sosa, |
|
181 | - 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
182 | - 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
178 | + $statistics_by_gen[(int)$stats_gen->gen] = array( |
|
179 | + 'sosaCount' => (int)$stats_gen->total_sosa, |
|
180 | + 'sosaTotalCount' => (int)$cumul_stats_by_gen[$gen]->total_cumul, |
|
181 | + 'diffSosaTotalCount' => (int)$cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
183 | 182 | 'firstBirth' => $stats_gen->first_year, |
184 | 183 | 'firstEstimatedBirth' => $stats_gen->first_est_year, |
185 | 184 | 'lastBirth' => $stats_gen->last_year, |
@@ -223,7 +222,7 @@ discard block |
||
223 | 222 | ->where('majs_user_id', '=', $this->user->id()); |
224 | 223 | |
225 | 224 | return DB::table('maj_sosa') |
226 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
225 | + ->joinSub($list_gen, 'list_gen', function(JoinClause $join): void { |
|
227 | 226 | $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
228 | 227 | ->where('majs_gedcom_id', '=', $this->tree->id()) |
229 | 228 | ->where('majs_user_id', '=', $this->user->id()); |
@@ -264,37 +263,37 @@ discard block |
||
264 | 263 | ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
265 | 264 | ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
266 | 265 | ->selectRaw( |
267 | - '(CASE ' . |
|
268 | - ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' . |
|
269 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
270 | - ' ELSE 0 ' . |
|
271 | - ' END)' . |
|
272 | - ' + (CASE ' . |
|
273 | - ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' . |
|
274 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
275 | - ' ELSE 0 ' . |
|
266 | + '(CASE '. |
|
267 | + ' WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL'. |
|
268 | + ' THEN POWER(2, '.$table_prefix.'list_gen.majs_gen - '.$table_prefix.'sosa.majs_gen - 1)'. |
|
269 | + ' ELSE 0 '. |
|
270 | + ' END)'. |
|
271 | + ' + (CASE '. |
|
272 | + ' WHEN '.$table_prefix.'sosa_mot.majs_i_id IS NULL'. |
|
273 | + ' THEN POWER(2, '.$table_prefix.'list_gen.majs_gen - '.$table_prefix.'sosa.majs_gen - 1)'. |
|
274 | + ' ELSE 0 '. |
|
276 | 275 | ' END) contrib' |
277 | 276 | ) |
278 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
277 | + ->joinSub($list_gen, 'list_gen', function(JoinClause $join): void { |
|
279 | 278 | $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen') |
280 | 279 | ->where('majs_gedcom_id', '=', $this->tree->id()) |
281 | 280 | ->where('majs_user_id', '=', $this->user->id()); |
282 | 281 | }) |
283 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
282 | + ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void { |
|
284 | 283 | // Link to sosa's father |
285 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
284 | + $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa') |
|
286 | 285 | ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
287 | 286 | ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
288 | 287 | }) |
289 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
288 | + ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void { |
|
290 | 289 | // Link to sosa's mother |
291 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
290 | + $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1') |
|
292 | 291 | ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
293 | 292 | ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
294 | 293 | }) |
295 | 294 | ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
296 | 295 | ->where('sosa.majs_user_id', '=', $this->user->id()) |
297 | - ->where(function (Builder $query): void { |
|
296 | + ->where(function(Builder $query): void { |
|
298 | 297 | $query->whereNull('sosa_fat.majs_i_id') |
299 | 298 | ->orWhereNull('sosa_mot.majs_i_id'); |
300 | 299 | }); |
@@ -304,24 +303,24 @@ discard block |
||
304 | 303 | */ |
305 | 304 | $non_roots_ancestors = DB::table('maj_sosa AS sosa') |
306 | 305 | ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa']) |
307 | - ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' . |
|
306 | + ->selectRaw('MAX('.$table_prefix.'sosa_anc.majs_sosa) - MIN('.$table_prefix.'sosa_anc.majs_sosa)'. |
|
308 | 307 | ' AS full_ancestors') |
309 | - ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void { |
|
308 | + ->join('maj_sosa AS sosa_anc', function(JoinClause $join) use ($table_prefix): void { |
|
310 | 309 | $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen') |
311 | - ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' . |
|
312 | - $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' . |
|
313 | - $table_prefix . 'sosa.majs_sosa') |
|
310 | + ->whereRaw('FLOOR('.$table_prefix.'sosa_anc.majs_sosa / POWER(2, '. |
|
311 | + $table_prefix.'sosa_anc.majs_gen - '.$table_prefix.'sosa.majs_gen)) = '. |
|
312 | + $table_prefix.'sosa.majs_sosa') |
|
314 | 313 | ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id()) |
315 | 314 | ->where('sosa_anc.majs_user_id', '=', $this->user->id()); |
316 | 315 | }) |
317 | 316 | ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
318 | 317 | ->where('sosa.majs_user_id', '=', $this->user->id()) |
319 | - ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void { |
|
318 | + ->whereIn('sosa_anc.majs_i_id', function(Builder $query) use ($table_prefix): void { |
|
320 | 319 | $query->from('maj_sosa AS sosa_gen') |
321 | 320 | ->select('sosa_gen.majs_i_id')->distinct() |
322 | 321 | ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id()) |
323 | 322 | ->where('sosa_gen.majs_user_id', '=', $this->user->id()) |
324 | - ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen'); |
|
323 | + ->whereRaw($table_prefix.'sosa_gen.majs_gen = '.$table_prefix.'sosa.majs_gen'); |
|
325 | 324 | }) |
326 | 325 | ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', |
327 | 326 | 'sosa.majs_sosa', 'sosa.majs_i_id']); |
@@ -334,7 +333,7 @@ discard block |
||
334 | 333 | ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
335 | 334 | ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
336 | 335 | ->selectRaw('1 AS contrib') |
337 | - ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void { |
|
336 | + ->leftJoinSub($non_roots_ancestors, 'nonroot', function(JoinClause $join): void { |
|
338 | 337 | $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen') |
339 | 338 | ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa') |
340 | 339 | ->where('nonroot.full_ancestors', '>', 0) |
@@ -352,9 +351,9 @@ discard block |
||
352 | 351 | ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs') |
353 | 352 | ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id']) |
354 | 353 | ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib']) |
355 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' . |
|
356 | - $table_prefix . 'sosa_contribs.contrib AS totalContrib') |
|
357 | - ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void { |
|
354 | + ->selectRaw('COUNT('.$table_prefix.'sosa_contribs.majs_i_id) * '. |
|
355 | + $table_prefix.'sosa_contribs.contrib AS totalContrib') |
|
356 | + ->leftJoin('maj_sosa AS sosa_low', function(JoinClause $join): void { |
|
358 | 357 | $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen') |
359 | 358 | ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id') |
360 | 359 | ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id()) |
@@ -375,9 +374,9 @@ discard block |
||
375 | 374 | |
376 | 375 | $pedi_collapse_by_gen = []; |
377 | 376 | foreach ($pedi_collapse_coll as $collapse_gen) { |
378 | - $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array( |
|
379 | - 'pedi_collapse_roots' => (float) $collapse_gen->pedi_collapse_roots, |
|
380 | - 'pedi_collapse_xgen' => (float) $collapse_gen->pedi_collapse_xgen |
|
377 | + $pedi_collapse_by_gen[(int)$collapse_gen->gen] = array( |
|
378 | + 'pedi_collapse_roots' => (float)$collapse_gen->pedi_collapse_roots, |
|
379 | + 'pedi_collapse_xgen' => (float)$collapse_gen->pedi_collapse_xgen |
|
381 | 380 | ); |
382 | 381 | } |
383 | 382 | return $pedi_collapse_by_gen; |
@@ -409,26 +408,26 @@ discard block |
||
409 | 408 | |
410 | 409 | $table_prefix = DB::connection()->getTablePrefix(); |
411 | 410 | $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
412 | - ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
413 | - ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
414 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
415 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
416 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
411 | + ->selectRaw($table_prefix.'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
412 | + ->selectRaw('FLOOR((('.$table_prefix.'sosa.majs_sosa / POW(2, '.$table_prefix.'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
413 | + ->selectRaw('SUM(CASE WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL AND '.$table_prefix.'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
414 | + ->selectRaw('SUM(CASE WHEN '.$table_prefix.'sosa_fat.majs_i_id IS NULL AND '.$table_prefix.'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
415 | + ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void { |
|
417 | 416 | // Link to sosa's father |
418 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
417 | + $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa') |
|
419 | 418 | ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
420 | 419 | ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
421 | 420 | }) |
422 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
421 | + ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void { |
|
423 | 422 | // Link to sosa's mother |
424 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
423 | + $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1') |
|
425 | 424 | ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
426 | 425 | ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
427 | 426 | }) |
428 | 427 | ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
429 | 428 | ->where('sosa.majs_user_id', '=', $this->user->id()) |
430 | 429 | ->where('sosa.majs_gen', '>=', $gen) |
431 | - ->where(function (Builder $query): void { |
|
430 | + ->where(function(Builder $query): void { |
|
432 | 431 | $query->whereNull('sosa_fat.majs_i_id') |
433 | 432 | ->orWhereNull('sosa_mot.majs_i_id'); |
434 | 433 | }) |
@@ -437,11 +436,11 @@ discard block |
||
437 | 436 | return DB::table('maj_sosa AS sosa_list') |
438 | 437 | ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
439 | 438 | ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth') //@phpcs:ignore Generic.Files.LineLength.TooLong |
440 | - ->selectRaw(' SQRT(' . |
|
441 | - ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
442 | - ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
439 | + ->selectRaw(' SQRT('. |
|
440 | + ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))'.//@phpcs:ignore Generic.Files.LineLength.TooLong |
|
441 | + ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)'.//@phpcs:ignore Generic.Files.LineLength.TooLong |
|
443 | 442 | ' ) AS stddev_gen_depth') |
444 | - ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
443 | + ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function(JoinClause $join): void { |
|
445 | 444 | $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
446 | 445 | ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
447 | 446 | ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
@@ -470,16 +469,16 @@ discard block |
||
470 | 469 | $table_prefix = DB::connection()->getTablePrefix(); |
471 | 470 | $multiple_ancestors = DB::table('maj_sosa AS sosa') |
472 | 471 | ->select('sosa.majs_i_id AS sosa_i_id') |
473 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
474 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
472 | + ->selectRaw('COUNT('.$table_prefix.'sosa.majs_sosa) AS sosa_count') |
|
473 | + ->leftJoin('maj_sosa AS sosa_fat', function(JoinClause $join) use ($table_prefix): void { |
|
475 | 474 | // Link to sosa's father |
476 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
475 | + $join->whereRaw($table_prefix.'sosa_fat.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa') |
|
477 | 476 | ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
478 | 477 | ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
479 | 478 | }) |
480 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
479 | + ->leftJoin('maj_sosa AS sosa_mot', function(JoinClause $join) use ($table_prefix): void { |
|
481 | 480 | // Link to sosa's mother |
482 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
481 | + $join->whereRaw($table_prefix.'sosa_mot.majs_sosa = 2 * '.$table_prefix.'sosa.majs_sosa + 1') |
|
483 | 482 | ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
484 | 483 | ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
485 | 484 | }) |
@@ -488,15 +487,15 @@ discard block |
||
488 | 487 | ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
489 | 488 | ->whereNull('sosa_mot.majs_sosa') |
490 | 489 | ->groupBy('sosa.majs_i_id') |
491 | - ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
492 | - ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
490 | + ->havingRaw('COUNT('.$table_prefix.'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
491 | + ->orderByRaw('COUNT('.$table_prefix.'sosa.majs_sosa) DESC, MIN('.$table_prefix.'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
493 | 492 | ->limit($limit + 1) // We want to select one more than required, for ties |
494 | 493 | ->get(); |
495 | 494 | |
496 | 495 | if ($multiple_ancestors->count() > $limit) { |
497 | 496 | $last_count = $multiple_ancestors->last()->sosa_count; |
498 | 497 | $multiple_ancestors = $multiple_ancestors->reject( |
499 | - fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
498 | + fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
500 | 499 | ); |
501 | 500 | } |
502 | 501 | return $multiple_ancestors; |
@@ -551,7 +550,7 @@ discard block |
||
551 | 550 | ->rightJoinSub( |
552 | 551 | $consolidated_ancestors_branches, |
553 | 552 | 'indi_branch_consolidated', |
554 | - function (JoinClause $join) use ($gen): void { |
|
553 | + function(JoinClause $join) use ($gen): void { |
|
555 | 554 | $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id()) |
556 | 555 | ->where('maj_sosa.majs_user_id', '=', $this->user->id()) |
557 | 556 | ->where('branches', '>', 0) |
@@ -27,52 +27,52 @@ |
||
27 | 27 | */ |
28 | 28 | class SosaIconHook implements RecordNameTextExtenderInterface |
29 | 29 | { |
30 | - private ModuleInterface $module; |
|
31 | - private SosaRecordsService $sosa_records_service; |
|
30 | + private ModuleInterface $module; |
|
31 | + private SosaRecordsService $sosa_records_service; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Constructor for SosaIconHook |
|
35 | - * |
|
36 | - * @param ModuleInterface $module |
|
37 | - * @param SosaRecordsService $sosa_records_service |
|
38 | - */ |
|
39 | - public function __construct(ModuleInterface $module, SosaRecordsService $sosa_records_service) |
|
40 | - { |
|
41 | - $this->module = $module; |
|
42 | - $this->sosa_records_service = $sosa_records_service; |
|
43 | - } |
|
33 | + /** |
|
34 | + * Constructor for SosaIconHook |
|
35 | + * |
|
36 | + * @param ModuleInterface $module |
|
37 | + * @param SosaRecordsService $sosa_records_service |
|
38 | + */ |
|
39 | + public function __construct(ModuleInterface $module, SosaRecordsService $sosa_records_service) |
|
40 | + { |
|
41 | + $this->module = $module; |
|
42 | + $this->sosa_records_service = $sosa_records_service; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * {@inheritDoc} |
|
47 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
48 | - */ |
|
49 | - public function module(): ModuleInterface |
|
50 | - { |
|
51 | - return $this->module; |
|
52 | - } |
|
45 | + /** |
|
46 | + * {@inheritDoc} |
|
47 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
48 | + */ |
|
49 | + public function module(): ModuleInterface |
|
50 | + { |
|
51 | + return $this->module; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * {@inheritDoc} |
|
56 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\RecordNameTextExtenderInterface::recordNamePrepend() |
|
57 | - */ |
|
58 | - public function recordNamePrepend(GedcomRecord $record, bool $use_long = false, string $size = ''): string |
|
59 | - { |
|
60 | - return ''; |
|
61 | - } |
|
54 | + /** |
|
55 | + * {@inheritDoc} |
|
56 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\RecordNameTextExtenderInterface::recordNamePrepend() |
|
57 | + */ |
|
58 | + public function recordNamePrepend(GedcomRecord $record, bool $use_long = false, string $size = ''): string |
|
59 | + { |
|
60 | + return ''; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * {@inheritDoc} |
|
65 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\RecordNameTextExtenderInterface::recordNameAppend() |
|
66 | - */ |
|
67 | - public function recordNameAppend(GedcomRecord $record, bool $use_long = false, string $size = ''): string |
|
68 | - { |
|
69 | - $current_user = Auth::check() ? Auth::user() : new DefaultUser(); |
|
70 | - if ( |
|
71 | - $record instanceof Individual && |
|
72 | - $this->sosa_records_service->isSosa($record->tree(), $current_user, $record) |
|
73 | - ) { |
|
74 | - return view($this->module->name() . '::icons/sosa', [ 'size_style' => $size ]); |
|
75 | - } |
|
76 | - return ''; |
|
77 | - } |
|
63 | + /** |
|
64 | + * {@inheritDoc} |
|
65 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\RecordNameTextExtenderInterface::recordNameAppend() |
|
66 | + */ |
|
67 | + public function recordNameAppend(GedcomRecord $record, bool $use_long = false, string $size = ''): string |
|
68 | + { |
|
69 | + $current_user = Auth::check() ? Auth::user() : new DefaultUser(); |
|
70 | + if ( |
|
71 | + $record instanceof Individual && |
|
72 | + $this->sosa_records_service->isSosa($record->tree(), $current_user, $record) |
|
73 | + ) { |
|
74 | + return view($this->module->name() . '::icons/sosa', [ 'size_style' => $size ]); |
|
75 | + } |
|
76 | + return ''; |
|
77 | + } |
|
78 | 78 | } |
@@ -71,7 +71,7 @@ |
||
71 | 71 | $record instanceof Individual && |
72 | 72 | $this->sosa_records_service->isSosa($record->tree(), $current_user, $record) |
73 | 73 | ) { |
74 | - return view($this->module->name() . '::icons/sosa', [ 'size_style' => $size ]); |
|
74 | + return view($this->module->name().'::icons/sosa', ['size_style' => $size]); |
|
75 | 75 | } |
76 | 76 | return ''; |
77 | 77 | } |
@@ -41,165 +41,165 @@ |
||
41 | 41 | */ |
42 | 42 | class HealthCheckEmailTask implements TaskInterface, ConfigurableTaskInterface |
43 | 43 | { |
44 | - /** |
|
45 | - * Name of the Tree preference to check if the task is enabled for that tree |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
49 | - |
|
50 | - private ?AdminTasksModule $module; |
|
51 | - private HealthCheckService $healthcheck_service; |
|
52 | - private EmailService $email_service; |
|
53 | - private UserService $user_service; |
|
54 | - private TreeService $tree_service; |
|
55 | - private UpgradeService $upgrade_service; |
|
56 | - |
|
57 | - /** |
|
58 | - * Constructor for HealthCheckTask |
|
59 | - * |
|
60 | - * @param ModuleService $module_service |
|
61 | - * @param HealthCheckService $healthcheck_service |
|
62 | - * @param EmailService $email_service |
|
63 | - * @param UserService $user_service |
|
64 | - * @param TreeService $tree_service |
|
65 | - * @param UpgradeService $upgrade_service |
|
66 | - */ |
|
67 | - public function __construct( |
|
68 | - ModuleService $module_service, |
|
69 | - HealthCheckService $healthcheck_service, |
|
70 | - EmailService $email_service, |
|
71 | - UserService $user_service, |
|
72 | - TreeService $tree_service, |
|
73 | - UpgradeService $upgrade_service |
|
74 | - ) { |
|
75 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
76 | - $this->healthcheck_service = $healthcheck_service; |
|
77 | - $this->email_service = $email_service; |
|
78 | - $this->user_service = $user_service; |
|
79 | - $this->tree_service = $tree_service; |
|
80 | - $this->upgrade_service = $upgrade_service; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * {@inheritDoc} |
|
86 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
87 | - */ |
|
88 | - public function name(): string |
|
89 | - { |
|
90 | - return I18N::translate('Healthcheck Email'); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * {@inheritDoc} |
|
95 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
96 | - */ |
|
97 | - public function defaultFrequency(): int |
|
98 | - { |
|
99 | - return 10080; // = 1 week = 7 * 24 * 60 min |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritDoc} |
|
104 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
105 | - */ |
|
106 | - public function run(TaskSchedule $task_schedule): bool |
|
107 | - { |
|
108 | - if ($this->module === null) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - |
|
112 | - $res = true; |
|
113 | - |
|
114 | - // Compute the number of days to compute |
|
115 | - $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(CarbonImmutable::now()); |
|
116 | - $interval_frequency = CarbonInterval::minutes($task_schedule->frequency()); |
|
117 | - $interval = $interval_lastrun->greaterThan($interval_frequency) ? $interval_lastrun : $interval_frequency; |
|
118 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
119 | - |
|
120 | - $view_params_site = [ |
|
121 | - 'nb_days' => $nb_days, |
|
122 | - 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
123 | - 'latest_version' => $this->upgrade_service->latestVersion(), |
|
124 | - 'download_url' => $this->upgrade_service->downloadUrl(), |
|
125 | - 'all_users' => $this->user_service->all(), |
|
126 | - 'unapproved' => $this->user_service->unapproved(), |
|
127 | - 'unverified' => $this->user_service->unverified(), |
|
128 | - ]; |
|
129 | - |
|
130 | - foreach ($this->tree_service->all() as $tree) { |
|
131 | - /** @var Tree $tree */ |
|
132 | - |
|
133 | - if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
134 | - continue; |
|
135 | - } |
|
136 | - |
|
137 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
138 | - if ($webmaster === null) { |
|
139 | - continue; |
|
140 | - } |
|
141 | - I18N::init($webmaster->getPreference('language')); |
|
142 | - |
|
143 | - $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
144 | - $nb_errors = $error_logs->sum('nblogs'); |
|
145 | - |
|
146 | - $view_params = $view_params_site + [ |
|
147 | - 'tree' => $tree, |
|
148 | - 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
149 | - 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
150 | - 'error_logs' => $error_logs, |
|
151 | - 'nb_errors' => $nb_errors |
|
152 | - ]; |
|
153 | - |
|
154 | - $res = $res && $this->email_service->send( |
|
155 | - new TreeUser($tree), |
|
156 | - $webmaster, |
|
157 | - new NoReplyUser(), |
|
158 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
159 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
160 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - return $res; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * {@inheritDoc} |
|
169 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
170 | - */ |
|
171 | - public function configView(ServerRequestInterface $request): string |
|
172 | - { |
|
173 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
174 | - 'all_trees' => $this->tree_service->all() |
|
175 | - ]); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * {@inheritDoc} |
|
180 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
181 | - */ |
|
182 | - public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
183 | - { |
|
184 | - try { |
|
185 | - $validator = Validator::parsedBody($request); |
|
186 | - |
|
187 | - foreach ($this->tree_service->all() as $tree) { |
|
188 | - if (Auth::isManager($tree)) { |
|
189 | - $tree_enabled = $validator->boolean('HEALTHCHECK_ENABLED_' . $tree->id(), false); |
|
190 | - $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
191 | - } |
|
192 | - } |
|
193 | - return true; |
|
194 | - } catch (Throwable $ex) { |
|
195 | - Log::addErrorLog( |
|
196 | - sprintf( |
|
197 | - 'Error while updating the Task schedule "%s". Exception: %s', |
|
198 | - $task_schedule->id(), |
|
199 | - $ex->getMessage() |
|
200 | - ) |
|
201 | - ); |
|
202 | - } |
|
203 | - return false; |
|
204 | - } |
|
44 | + /** |
|
45 | + * Name of the Tree preference to check if the task is enabled for that tree |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
49 | + |
|
50 | + private ?AdminTasksModule $module; |
|
51 | + private HealthCheckService $healthcheck_service; |
|
52 | + private EmailService $email_service; |
|
53 | + private UserService $user_service; |
|
54 | + private TreeService $tree_service; |
|
55 | + private UpgradeService $upgrade_service; |
|
56 | + |
|
57 | + /** |
|
58 | + * Constructor for HealthCheckTask |
|
59 | + * |
|
60 | + * @param ModuleService $module_service |
|
61 | + * @param HealthCheckService $healthcheck_service |
|
62 | + * @param EmailService $email_service |
|
63 | + * @param UserService $user_service |
|
64 | + * @param TreeService $tree_service |
|
65 | + * @param UpgradeService $upgrade_service |
|
66 | + */ |
|
67 | + public function __construct( |
|
68 | + ModuleService $module_service, |
|
69 | + HealthCheckService $healthcheck_service, |
|
70 | + EmailService $email_service, |
|
71 | + UserService $user_service, |
|
72 | + TreeService $tree_service, |
|
73 | + UpgradeService $upgrade_service |
|
74 | + ) { |
|
75 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
76 | + $this->healthcheck_service = $healthcheck_service; |
|
77 | + $this->email_service = $email_service; |
|
78 | + $this->user_service = $user_service; |
|
79 | + $this->tree_service = $tree_service; |
|
80 | + $this->upgrade_service = $upgrade_service; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * {@inheritDoc} |
|
86 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
87 | + */ |
|
88 | + public function name(): string |
|
89 | + { |
|
90 | + return I18N::translate('Healthcheck Email'); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * {@inheritDoc} |
|
95 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
96 | + */ |
|
97 | + public function defaultFrequency(): int |
|
98 | + { |
|
99 | + return 10080; // = 1 week = 7 * 24 * 60 min |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritDoc} |
|
104 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
105 | + */ |
|
106 | + public function run(TaskSchedule $task_schedule): bool |
|
107 | + { |
|
108 | + if ($this->module === null) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + |
|
112 | + $res = true; |
|
113 | + |
|
114 | + // Compute the number of days to compute |
|
115 | + $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(CarbonImmutable::now()); |
|
116 | + $interval_frequency = CarbonInterval::minutes($task_schedule->frequency()); |
|
117 | + $interval = $interval_lastrun->greaterThan($interval_frequency) ? $interval_lastrun : $interval_frequency; |
|
118 | + $nb_days = (int) $interval->ceilDay()->totalDays; |
|
119 | + |
|
120 | + $view_params_site = [ |
|
121 | + 'nb_days' => $nb_days, |
|
122 | + 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
123 | + 'latest_version' => $this->upgrade_service->latestVersion(), |
|
124 | + 'download_url' => $this->upgrade_service->downloadUrl(), |
|
125 | + 'all_users' => $this->user_service->all(), |
|
126 | + 'unapproved' => $this->user_service->unapproved(), |
|
127 | + 'unverified' => $this->user_service->unverified(), |
|
128 | + ]; |
|
129 | + |
|
130 | + foreach ($this->tree_service->all() as $tree) { |
|
131 | + /** @var Tree $tree */ |
|
132 | + |
|
133 | + if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
134 | + continue; |
|
135 | + } |
|
136 | + |
|
137 | + $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
138 | + if ($webmaster === null) { |
|
139 | + continue; |
|
140 | + } |
|
141 | + I18N::init($webmaster->getPreference('language')); |
|
142 | + |
|
143 | + $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
144 | + $nb_errors = $error_logs->sum('nblogs'); |
|
145 | + |
|
146 | + $view_params = $view_params_site + [ |
|
147 | + 'tree' => $tree, |
|
148 | + 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
149 | + 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
150 | + 'error_logs' => $error_logs, |
|
151 | + 'nb_errors' => $nb_errors |
|
152 | + ]; |
|
153 | + |
|
154 | + $res = $res && $this->email_service->send( |
|
155 | + new TreeUser($tree), |
|
156 | + $webmaster, |
|
157 | + new NoReplyUser(), |
|
158 | + I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
159 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
160 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + return $res; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * {@inheritDoc} |
|
169 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
170 | + */ |
|
171 | + public function configView(ServerRequestInterface $request): string |
|
172 | + { |
|
173 | + return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
174 | + 'all_trees' => $this->tree_service->all() |
|
175 | + ]); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * {@inheritDoc} |
|
180 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
181 | + */ |
|
182 | + public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
183 | + { |
|
184 | + try { |
|
185 | + $validator = Validator::parsedBody($request); |
|
186 | + |
|
187 | + foreach ($this->tree_service->all() as $tree) { |
|
188 | + if (Auth::isManager($tree)) { |
|
189 | + $tree_enabled = $validator->boolean('HEALTHCHECK_ENABLED_' . $tree->id(), false); |
|
190 | + $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
191 | + } |
|
192 | + } |
|
193 | + return true; |
|
194 | + } catch (Throwable $ex) { |
|
195 | + Log::addErrorLog( |
|
196 | + sprintf( |
|
197 | + 'Error while updating the Task schedule "%s". Exception: %s', |
|
198 | + $task_schedule->id(), |
|
199 | + $ex->getMessage() |
|
200 | + ) |
|
201 | + ); |
|
202 | + } |
|
203 | + return false; |
|
204 | + } |
|
205 | 205 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(CarbonImmutable::now()); |
116 | 116 | $interval_frequency = CarbonInterval::minutes($task_schedule->frequency()); |
117 | 117 | $interval = $interval_lastrun->greaterThan($interval_frequency) ? $interval_lastrun : $interval_frequency; |
118 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
118 | + $nb_days = (int)$interval->ceilDay()->totalDays; |
|
119 | 119 | |
120 | 120 | $view_params_site = [ |
121 | 121 | 'nb_days' => $nb_days, |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | continue; |
135 | 135 | } |
136 | 136 | |
137 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
137 | + $webmaster = $this->user_service->find((int)$tree->getPreference('WEBMASTER_USER_ID')); |
|
138 | 138 | if ($webmaster === null) { |
139 | 139 | continue; |
140 | 140 | } |
@@ -155,9 +155,9 @@ discard block |
||
155 | 155 | new TreeUser($tree), |
156 | 156 | $webmaster, |
157 | 157 | new NoReplyUser(), |
158 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
159 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
160 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
158 | + I18N::translate('Health Check Report').' - '.I18N::translate('Tree %s', $tree->name()), |
|
159 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
160 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
161 | 161 | ); |
162 | 162 | } |
163 | 163 | |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | */ |
171 | 171 | public function configView(ServerRequestInterface $request): string |
172 | 172 | { |
173 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
173 | + return $this->module === null ? '' : view($this->module->name().'::tasks/healthcheck/config', [ |
|
174 | 174 | 'all_trees' => $this->tree_service->all() |
175 | 175 | ]); |
176 | 176 | } |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | |
187 | 187 | foreach ($this->tree_service->all() as $tree) { |
188 | 188 | if (Auth::isManager($tree)) { |
189 | - $tree_enabled = $validator->boolean('HEALTHCHECK_ENABLED_' . $tree->id(), false); |
|
189 | + $tree_enabled = $validator->boolean('HEALTHCHECK_ENABLED_'.$tree->id(), false); |
|
190 | 190 | $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
191 | 191 | } |
192 | 192 | } |
@@ -32,92 +32,92 @@ |
||
32 | 32 | */ |
33 | 33 | class TasksList implements RequestHandlerInterface |
34 | 34 | { |
35 | - private ?AdminTasksModule $module; |
|
36 | - private TaskScheduleService $taskschedules_service; |
|
35 | + private ?AdminTasksModule $module; |
|
36 | + private TaskScheduleService $taskschedules_service; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor for TasksList Request Handler |
|
40 | - * |
|
41 | - * @param ModuleService $module_service |
|
42 | - * @param TaskScheduleService $taskschedules_service |
|
43 | - */ |
|
44 | - public function __construct( |
|
45 | - ModuleService $module_service, |
|
46 | - TaskScheduleService $taskschedules_service |
|
47 | - ) { |
|
48 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
49 | - $this->taskschedules_service = $taskschedules_service; |
|
50 | - } |
|
38 | + /** |
|
39 | + * Constructor for TasksList Request Handler |
|
40 | + * |
|
41 | + * @param ModuleService $module_service |
|
42 | + * @param TaskScheduleService $taskschedules_service |
|
43 | + */ |
|
44 | + public function __construct( |
|
45 | + ModuleService $module_service, |
|
46 | + TaskScheduleService $taskschedules_service |
|
47 | + ) { |
|
48 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
49 | + $this->taskschedules_service = $taskschedules_service; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * {@inheritDoc} |
|
54 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
55 | - */ |
|
56 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
57 | - { |
|
58 | - if ($this->module === null) { |
|
59 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
60 | - } |
|
52 | + /** |
|
53 | + * {@inheritDoc} |
|
54 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
55 | + */ |
|
56 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
57 | + { |
|
58 | + if ($this->module === null) { |
|
59 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
60 | + } |
|
61 | 61 | |
62 | - $module = $this->module; |
|
63 | - $module_name = $this->module->name(); |
|
64 | - return Registry::responseFactory()->response(['data' => $this->taskschedules_service->all(true, true) |
|
65 | - ->map(function (TaskSchedule $schedule) use ($module, $module_name): array { |
|
66 | - $task = $this->taskschedules_service->findTask($schedule->taskId()); |
|
67 | - $task_name = $task !== null ? $task->name() : I18N::translate('Task not found'); |
|
68 | - $last_run_timestamp = Registry::timestampFactory()->make($schedule->lastRunTime()->getTimestamp()); |
|
62 | + $module = $this->module; |
|
63 | + $module_name = $this->module->name(); |
|
64 | + return Registry::responseFactory()->response(['data' => $this->taskschedules_service->all(true, true) |
|
65 | + ->map(function (TaskSchedule $schedule) use ($module, $module_name): array { |
|
66 | + $task = $this->taskschedules_service->findTask($schedule->taskId()); |
|
67 | + $task_name = $task !== null ? $task->name() : I18N::translate('Task not found'); |
|
68 | + $last_run_timestamp = Registry::timestampFactory()->make($schedule->lastRunTime()->getTimestamp()); |
|
69 | 69 | |
70 | - return [ |
|
71 | - 'edit' => view($module_name . '::admin/tasks-table-options', [ |
|
72 | - 'task_sched_id' => $schedule->id(), |
|
73 | - 'task_sched_enabled' => $schedule->isEnabled(), |
|
74 | - 'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]), |
|
75 | - 'task_status_route' => route(TaskStatusAction::class, [ |
|
76 | - 'task' => $schedule->id(), |
|
77 | - 'enable' => $schedule->isEnabled() ? 0 : 1 |
|
78 | - ]) |
|
79 | - ]), |
|
80 | - 'status' => [ |
|
81 | - 'display' => view($module_name . '::components/yes-no-icons', [ |
|
82 | - 'yes' => $schedule->isEnabled() |
|
83 | - ]), |
|
84 | - 'raw' => $schedule->isEnabled() ? 1 : 0 |
|
85 | - ], |
|
86 | - 'task_name' => [ |
|
87 | - 'display' => '<bdi>' . e($task_name) . '</bdi>', |
|
88 | - 'raw' => $task_name |
|
89 | - ], |
|
90 | - 'last_run' => [ |
|
91 | - 'display' => $last_run_timestamp->timestamp() === 0 ? |
|
92 | - view('components/datetime', ['timestamp' => $last_run_timestamp]) : |
|
93 | - view('components/datetime-diff', ['timestamp' => $last_run_timestamp]), |
|
94 | - 'raw' => $schedule->lastRunTime()->getTimestamp() |
|
95 | - ], |
|
96 | - 'last_result' => [ |
|
97 | - 'display' => view($module_name . '::components/yes-no-icons', [ |
|
98 | - 'yes' => $schedule->wasLastRunSuccess() |
|
99 | - ]), |
|
100 | - 'raw' => $schedule->wasLastRunSuccess() ? 1 : 0 |
|
101 | - ], |
|
102 | - 'frequency' => |
|
103 | - '<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>', |
|
104 | - 'nb_occurrences' => $schedule->remainingOccurrences() > 0 ? |
|
105 | - I18N::number($schedule->remainingOccurrences()) : |
|
106 | - I18N::translate('Unlimited'), |
|
107 | - 'running' => view($module_name . '::components/yes-no-icons', [ |
|
108 | - 'yes' => $schedule->isRunning(), |
|
109 | - 'text_yes' => I18N::translate('Running'), |
|
110 | - 'text_no' => I18N::translate('Not running') |
|
111 | - ]), |
|
112 | - 'run' => view($module_name . '::admin/tasks-table-run', [ |
|
113 | - 'task_sched_id' => $schedule->id(), |
|
114 | - 'run_route' => route(TaskTrigger::class, [ |
|
115 | - 'task' => $schedule->taskId(), |
|
116 | - 'force' => $module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN') |
|
117 | - ]) |
|
118 | - ]) |
|
119 | - ]; |
|
120 | - }) |
|
121 | - ]); |
|
122 | - } |
|
70 | + return [ |
|
71 | + 'edit' => view($module_name . '::admin/tasks-table-options', [ |
|
72 | + 'task_sched_id' => $schedule->id(), |
|
73 | + 'task_sched_enabled' => $schedule->isEnabled(), |
|
74 | + 'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]), |
|
75 | + 'task_status_route' => route(TaskStatusAction::class, [ |
|
76 | + 'task' => $schedule->id(), |
|
77 | + 'enable' => $schedule->isEnabled() ? 0 : 1 |
|
78 | + ]) |
|
79 | + ]), |
|
80 | + 'status' => [ |
|
81 | + 'display' => view($module_name . '::components/yes-no-icons', [ |
|
82 | + 'yes' => $schedule->isEnabled() |
|
83 | + ]), |
|
84 | + 'raw' => $schedule->isEnabled() ? 1 : 0 |
|
85 | + ], |
|
86 | + 'task_name' => [ |
|
87 | + 'display' => '<bdi>' . e($task_name) . '</bdi>', |
|
88 | + 'raw' => $task_name |
|
89 | + ], |
|
90 | + 'last_run' => [ |
|
91 | + 'display' => $last_run_timestamp->timestamp() === 0 ? |
|
92 | + view('components/datetime', ['timestamp' => $last_run_timestamp]) : |
|
93 | + view('components/datetime-diff', ['timestamp' => $last_run_timestamp]), |
|
94 | + 'raw' => $schedule->lastRunTime()->getTimestamp() |
|
95 | + ], |
|
96 | + 'last_result' => [ |
|
97 | + 'display' => view($module_name . '::components/yes-no-icons', [ |
|
98 | + 'yes' => $schedule->wasLastRunSuccess() |
|
99 | + ]), |
|
100 | + 'raw' => $schedule->wasLastRunSuccess() ? 1 : 0 |
|
101 | + ], |
|
102 | + 'frequency' => |
|
103 | + '<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>', |
|
104 | + 'nb_occurrences' => $schedule->remainingOccurrences() > 0 ? |
|
105 | + I18N::number($schedule->remainingOccurrences()) : |
|
106 | + I18N::translate('Unlimited'), |
|
107 | + 'running' => view($module_name . '::components/yes-no-icons', [ |
|
108 | + 'yes' => $schedule->isRunning(), |
|
109 | + 'text_yes' => I18N::translate('Running'), |
|
110 | + 'text_no' => I18N::translate('Not running') |
|
111 | + ]), |
|
112 | + 'run' => view($module_name . '::admin/tasks-table-run', [ |
|
113 | + 'task_sched_id' => $schedule->id(), |
|
114 | + 'run_route' => route(TaskTrigger::class, [ |
|
115 | + 'task' => $schedule->taskId(), |
|
116 | + 'force' => $module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN') |
|
117 | + ]) |
|
118 | + ]) |
|
119 | + ]; |
|
120 | + }) |
|
121 | + ]); |
|
122 | + } |
|
123 | 123 | } |
@@ -62,13 +62,13 @@ discard block |
||
62 | 62 | $module = $this->module; |
63 | 63 | $module_name = $this->module->name(); |
64 | 64 | return Registry::responseFactory()->response(['data' => $this->taskschedules_service->all(true, true) |
65 | - ->map(function (TaskSchedule $schedule) use ($module, $module_name): array { |
|
65 | + ->map(function(TaskSchedule $schedule) use ($module, $module_name): array { |
|
66 | 66 | $task = $this->taskschedules_service->findTask($schedule->taskId()); |
67 | 67 | $task_name = $task !== null ? $task->name() : I18N::translate('Task not found'); |
68 | 68 | $last_run_timestamp = Registry::timestampFactory()->make($schedule->lastRunTime()->getTimestamp()); |
69 | 69 | |
70 | 70 | return [ |
71 | - 'edit' => view($module_name . '::admin/tasks-table-options', [ |
|
71 | + 'edit' => view($module_name.'::admin/tasks-table-options', [ |
|
72 | 72 | 'task_sched_id' => $schedule->id(), |
73 | 73 | 'task_sched_enabled' => $schedule->isEnabled(), |
74 | 74 | 'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]), |
@@ -78,38 +78,36 @@ discard block |
||
78 | 78 | ]) |
79 | 79 | ]), |
80 | 80 | 'status' => [ |
81 | - 'display' => view($module_name . '::components/yes-no-icons', [ |
|
81 | + 'display' => view($module_name.'::components/yes-no-icons', [ |
|
82 | 82 | 'yes' => $schedule->isEnabled() |
83 | 83 | ]), |
84 | 84 | 'raw' => $schedule->isEnabled() ? 1 : 0 |
85 | 85 | ], |
86 | 86 | 'task_name' => [ |
87 | - 'display' => '<bdi>' . e($task_name) . '</bdi>', |
|
87 | + 'display' => '<bdi>'.e($task_name).'</bdi>', |
|
88 | 88 | 'raw' => $task_name |
89 | 89 | ], |
90 | 90 | 'last_run' => [ |
91 | 91 | 'display' => $last_run_timestamp->timestamp() === 0 ? |
92 | - view('components/datetime', ['timestamp' => $last_run_timestamp]) : |
|
93 | - view('components/datetime-diff', ['timestamp' => $last_run_timestamp]), |
|
92 | + view('components/datetime', ['timestamp' => $last_run_timestamp]) : view('components/datetime-diff', ['timestamp' => $last_run_timestamp]), |
|
94 | 93 | 'raw' => $schedule->lastRunTime()->getTimestamp() |
95 | 94 | ], |
96 | 95 | 'last_result' => [ |
97 | - 'display' => view($module_name . '::components/yes-no-icons', [ |
|
96 | + 'display' => view($module_name.'::components/yes-no-icons', [ |
|
98 | 97 | 'yes' => $schedule->wasLastRunSuccess() |
99 | 98 | ]), |
100 | 99 | 'raw' => $schedule->wasLastRunSuccess() ? 1 : 0 |
101 | 100 | ], |
102 | 101 | 'frequency' => |
103 | - '<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>', |
|
102 | + '<bdi>'.e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()).'</bdi>', |
|
104 | 103 | 'nb_occurrences' => $schedule->remainingOccurrences() > 0 ? |
105 | - I18N::number($schedule->remainingOccurrences()) : |
|
106 | - I18N::translate('Unlimited'), |
|
107 | - 'running' => view($module_name . '::components/yes-no-icons', [ |
|
104 | + I18N::number($schedule->remainingOccurrences()) : I18N::translate('Unlimited'), |
|
105 | + 'running' => view($module_name.'::components/yes-no-icons', [ |
|
108 | 106 | 'yes' => $schedule->isRunning(), |
109 | 107 | 'text_yes' => I18N::translate('Running'), |
110 | 108 | 'text_no' => I18N::translate('Not running') |
111 | 109 | ]), |
112 | - 'run' => view($module_name . '::admin/tasks-table-run', [ |
|
110 | + 'run' => view($module_name.'::admin/tasks-table-run', [ |
|
113 | 111 | 'task_sched_id' => $schedule->id(), |
114 | 112 | 'run_route' => route(TaskTrigger::class, [ |
115 | 113 | 'task' => $schedule->taskId(), |
@@ -30,41 +30,41 @@ |
||
30 | 30 | */ |
31 | 31 | class TaskTrigger implements RequestHandlerInterface |
32 | 32 | { |
33 | - private ?AdminTasksModule $module; |
|
34 | - private TaskScheduleService $taskschedules_service; |
|
33 | + private ?AdminTasksModule $module; |
|
34 | + private TaskScheduleService $taskschedules_service; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor for TaskTrigger request handler |
|
38 | - * @param ModuleService $module_service |
|
39 | - * @param TaskScheduleService $taskschedules_service |
|
40 | - */ |
|
41 | - public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
42 | - { |
|
43 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
44 | - $this->taskschedules_service = $taskschedules_service; |
|
45 | - } |
|
36 | + /** |
|
37 | + * Constructor for TaskTrigger request handler |
|
38 | + * @param ModuleService $module_service |
|
39 | + * @param TaskScheduleService $taskschedules_service |
|
40 | + */ |
|
41 | + public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
42 | + { |
|
43 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
44 | + $this->taskschedules_service = $taskschedules_service; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * {@inheritDoc} |
|
49 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | - */ |
|
51 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | - { |
|
53 | - if ($this->module === null) { |
|
54 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
55 | - } |
|
47 | + /** |
|
48 | + * {@inheritDoc} |
|
49 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | + */ |
|
51 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | + { |
|
53 | + if ($this->module === null) { |
|
54 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
55 | + } |
|
56 | 56 | |
57 | - $task_id = Validator::attributes($request)->string('task', ''); |
|
58 | - $token = $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN'); |
|
59 | - $force_token = Validator::queryParams($request)->string('force', ''); |
|
60 | - $force = $token !== '' && $token === $force_token; |
|
57 | + $task_id = Validator::attributes($request)->string('task', ''); |
|
58 | + $token = $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN'); |
|
59 | + $force_token = Validator::queryParams($request)->string('force', ''); |
|
60 | + $force = $token !== '' && $token === $force_token; |
|
61 | 61 | |
62 | - $task_schedules = $this->taskschedules_service->findTasksToRun($force, $task_id); |
|
62 | + $task_schedules = $this->taskschedules_service->findTasksToRun($force, $task_id); |
|
63 | 63 | |
64 | - foreach ($task_schedules as $task_schedule) { |
|
65 | - $this->taskschedules_service->run($task_schedule, $force); |
|
66 | - } |
|
64 | + foreach ($task_schedules as $task_schedule) { |
|
65 | + $this->taskschedules_service->run($task_schedule, $force); |
|
66 | + } |
|
67 | 67 | |
68 | - return Registry::responseFactory()->response(); |
|
69 | - } |
|
68 | + return Registry::responseFactory()->response(); |
|
69 | + } |
|
70 | 70 | } |
@@ -57,7 +57,7 @@ |
||
57 | 57 | $task_id = Validator::attributes($request)->string('task', ''); |
58 | 58 | $token = $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN'); |
59 | 59 | $force_token = Validator::queryParams($request)->string('force', ''); |
60 | - $force = $token !== '' && $token === $force_token; |
|
60 | + $force = $token !== '' && $token === $force_token; |
|
61 | 61 | |
62 | 62 | $task_schedules = $this->taskschedules_service->findTasksToRun($force, $task_id); |
63 | 63 |
@@ -34,146 +34,146 @@ |
||
34 | 34 | */ |
35 | 35 | class TaskEditAction implements RequestHandlerInterface |
36 | 36 | { |
37 | - private ?AdminTasksModule $module; |
|
38 | - private TaskScheduleService $taskschedules_service; |
|
39 | - |
|
40 | - /** |
|
41 | - * Constructor for TaskEditAction Request Handler |
|
42 | - * |
|
43 | - * @param ModuleService $module_service |
|
44 | - * @param TaskScheduleService $taskschedules_service |
|
45 | - */ |
|
46 | - public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
47 | - { |
|
48 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
49 | - $this->taskschedules_service = $taskschedules_service; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * {@inheritDoc} |
|
54 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
55 | - */ |
|
56 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
57 | - { |
|
58 | - if ($this->module === null) { |
|
59 | - FlashMessages::addMessage( |
|
60 | - I18N::translate('The attached module could not be found.'), |
|
61 | - 'danger' |
|
62 | - ); |
|
63 | - return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
64 | - } |
|
65 | - |
|
66 | - $task_sched_id = Validator::attributes($request)->integer('task', -1); |
|
67 | - $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
68 | - |
|
69 | - if ($task_schedule === null) { |
|
70 | - FlashMessages::addMessage( |
|
71 | - I18N::translate('The task shedule with ID “%s” does not exist.', I18N::number($task_sched_id)), |
|
72 | - 'danger' |
|
73 | - ); |
|
74 | - return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
75 | - } |
|
76 | - |
|
77 | - $success = $this->updateGeneralSettings($task_schedule, $request); |
|
78 | - $success = $success && $this->updateSpecificSettings($task_schedule, $request); |
|
79 | - |
|
80 | - if ($success) { |
|
81 | - FlashMessages::addMessage( |
|
82 | - I18N::translate('The scheduled task has been successfully updated.'), |
|
83 | - 'success' |
|
84 | - ); |
|
85 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
86 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
87 | - } |
|
88 | - |
|
89 | - return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Update general settings for the task, based on the request parameters |
|
94 | - * |
|
95 | - * @param TaskSchedule $task_schedule |
|
96 | - * @param ServerRequestInterface $request |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - private function updateGeneralSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool |
|
100 | - { |
|
101 | - if ($this->module === null) { |
|
102 | - return false; |
|
103 | - } |
|
104 | - |
|
105 | - $frequency = Validator::parsedBody($request)->integer('frequency', 0); |
|
106 | - if ($frequency > 0) { |
|
107 | - $task_schedule->setFrequency($frequency); |
|
108 | - } else { |
|
109 | - FlashMessages::addMessage(I18N::translate('The frequency is not in a valid format.'), 'danger'); |
|
110 | - } |
|
111 | - |
|
112 | - $is_limited = Validator::parsedBody($request)->boolean('is_limited', false); |
|
113 | - $nb_occur = Validator::parsedBody($request)->integer('nb_occur', 1); |
|
114 | - if ($is_limited) { |
|
115 | - if ($nb_occur > 0) { |
|
116 | - $task_schedule->setRemainingOccurrences($nb_occur); |
|
117 | - } else { |
|
118 | - FlashMessages::addMessage( |
|
119 | - I18N::translate('The number of remaining occurrences is not in a valid format.'), |
|
120 | - 'danger' |
|
121 | - ); |
|
122 | - } |
|
123 | - } else { |
|
124 | - $task_schedule->setRemainingOccurrences(0); |
|
125 | - } |
|
126 | - |
|
127 | - try { |
|
128 | - $this->taskschedules_service->update($task_schedule); |
|
129 | - return true; |
|
130 | - } catch (Throwable $ex) { |
|
131 | - Log::addErrorLog( |
|
132 | - sprintf( |
|
133 | - 'Error while updating the Task Schedule "%s". Exception: %s', |
|
134 | - $task_schedule->id(), |
|
135 | - $ex->getMessage() |
|
136 | - ) |
|
137 | - ); |
|
138 | - } |
|
139 | - |
|
140 | - FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger'); |
|
141 | - //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
142 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Update general settings for the task, based on the request parameters |
|
148 | - * |
|
149 | - * @param TaskSchedule $task_schedule |
|
150 | - * @param ServerRequestInterface $request |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - private function updateSpecificSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool |
|
154 | - { |
|
155 | - if ($this->module === null) { |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - $task = $this->taskschedules_service->findTask($task_schedule->taskId()); |
|
160 | - if ($task === null || !($task instanceof ConfigurableTaskInterface)) { |
|
161 | - return true; |
|
162 | - } |
|
163 | - |
|
164 | - /** @var \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface&\MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface $task */ |
|
165 | - if (!$task->updateConfig($request, $task_schedule)) { |
|
166 | - FlashMessages::addMessage( |
|
167 | - I18N::translate( |
|
168 | - 'An error occured while updating the specific settings of administrative task “%s”.', |
|
169 | - $task->name() |
|
170 | - ), |
|
171 | - 'danger' |
|
172 | - ); |
|
173 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
174 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.'); |
|
175 | - } |
|
176 | - |
|
177 | - return true; |
|
178 | - } |
|
37 | + private ?AdminTasksModule $module; |
|
38 | + private TaskScheduleService $taskschedules_service; |
|
39 | + |
|
40 | + /** |
|
41 | + * Constructor for TaskEditAction Request Handler |
|
42 | + * |
|
43 | + * @param ModuleService $module_service |
|
44 | + * @param TaskScheduleService $taskschedules_service |
|
45 | + */ |
|
46 | + public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
47 | + { |
|
48 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
49 | + $this->taskschedules_service = $taskschedules_service; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * {@inheritDoc} |
|
54 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
55 | + */ |
|
56 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
57 | + { |
|
58 | + if ($this->module === null) { |
|
59 | + FlashMessages::addMessage( |
|
60 | + I18N::translate('The attached module could not be found.'), |
|
61 | + 'danger' |
|
62 | + ); |
|
63 | + return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
64 | + } |
|
65 | + |
|
66 | + $task_sched_id = Validator::attributes($request)->integer('task', -1); |
|
67 | + $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
68 | + |
|
69 | + if ($task_schedule === null) { |
|
70 | + FlashMessages::addMessage( |
|
71 | + I18N::translate('The task shedule with ID “%s” does not exist.', I18N::number($task_sched_id)), |
|
72 | + 'danger' |
|
73 | + ); |
|
74 | + return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
75 | + } |
|
76 | + |
|
77 | + $success = $this->updateGeneralSettings($task_schedule, $request); |
|
78 | + $success = $success && $this->updateSpecificSettings($task_schedule, $request); |
|
79 | + |
|
80 | + if ($success) { |
|
81 | + FlashMessages::addMessage( |
|
82 | + I18N::translate('The scheduled task has been successfully updated.'), |
|
83 | + 'success' |
|
84 | + ); |
|
85 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
86 | + Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
87 | + } |
|
88 | + |
|
89 | + return Registry::responseFactory()->redirect(AdminConfigPage::class); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Update general settings for the task, based on the request parameters |
|
94 | + * |
|
95 | + * @param TaskSchedule $task_schedule |
|
96 | + * @param ServerRequestInterface $request |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + private function updateGeneralSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool |
|
100 | + { |
|
101 | + if ($this->module === null) { |
|
102 | + return false; |
|
103 | + } |
|
104 | + |
|
105 | + $frequency = Validator::parsedBody($request)->integer('frequency', 0); |
|
106 | + if ($frequency > 0) { |
|
107 | + $task_schedule->setFrequency($frequency); |
|
108 | + } else { |
|
109 | + FlashMessages::addMessage(I18N::translate('The frequency is not in a valid format.'), 'danger'); |
|
110 | + } |
|
111 | + |
|
112 | + $is_limited = Validator::parsedBody($request)->boolean('is_limited', false); |
|
113 | + $nb_occur = Validator::parsedBody($request)->integer('nb_occur', 1); |
|
114 | + if ($is_limited) { |
|
115 | + if ($nb_occur > 0) { |
|
116 | + $task_schedule->setRemainingOccurrences($nb_occur); |
|
117 | + } else { |
|
118 | + FlashMessages::addMessage( |
|
119 | + I18N::translate('The number of remaining occurrences is not in a valid format.'), |
|
120 | + 'danger' |
|
121 | + ); |
|
122 | + } |
|
123 | + } else { |
|
124 | + $task_schedule->setRemainingOccurrences(0); |
|
125 | + } |
|
126 | + |
|
127 | + try { |
|
128 | + $this->taskschedules_service->update($task_schedule); |
|
129 | + return true; |
|
130 | + } catch (Throwable $ex) { |
|
131 | + Log::addErrorLog( |
|
132 | + sprintf( |
|
133 | + 'Error while updating the Task Schedule "%s". Exception: %s', |
|
134 | + $task_schedule->id(), |
|
135 | + $ex->getMessage() |
|
136 | + ) |
|
137 | + ); |
|
138 | + } |
|
139 | + |
|
140 | + FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger'); |
|
141 | + //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
142 | + Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Update general settings for the task, based on the request parameters |
|
148 | + * |
|
149 | + * @param TaskSchedule $task_schedule |
|
150 | + * @param ServerRequestInterface $request |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + private function updateSpecificSettings(TaskSchedule $task_schedule, ServerRequestInterface $request): bool |
|
154 | + { |
|
155 | + if ($this->module === null) { |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + $task = $this->taskschedules_service->findTask($task_schedule->taskId()); |
|
160 | + if ($task === null || !($task instanceof ConfigurableTaskInterface)) { |
|
161 | + return true; |
|
162 | + } |
|
163 | + |
|
164 | + /** @var \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface&\MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface $task */ |
|
165 | + if (!$task->updateConfig($request, $task_schedule)) { |
|
166 | + FlashMessages::addMessage( |
|
167 | + I18N::translate( |
|
168 | + 'An error occured while updating the specific settings of administrative task “%s”.', |
|
169 | + $task->name() |
|
170 | + ), |
|
171 | + 'danger' |
|
172 | + ); |
|
173 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
174 | + Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.'); |
|
175 | + } |
|
176 | + |
|
177 | + return true; |
|
178 | + } |
|
179 | 179 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | 'success' |
84 | 84 | ); |
85 | 85 | //phpcs:ignore Generic.Files.LineLength.TooLong |
86 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
86 | + Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” has been updated.'); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | return Registry::responseFactory()->redirect(AdminConfigPage::class); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | FlashMessages::addMessage(I18N::translate('An error occured while updating the scheduled task.'), 'danger'); |
141 | 141 | //@phpcs:ignore Generic.Files.LineLength.TooLong |
142 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
142 | + Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” could not be updated. See error log.'); |
|
143 | 143 | return false; |
144 | 144 | } |
145 | 145 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | 'danger' |
172 | 172 | ); |
173 | 173 | //phpcs:ignore Generic.Files.LineLength.TooLong |
174 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : AdminTask “' . $task->name() . '” specific settings could not be updated. See error log.'); |
|
174 | + Log::addConfigurationLog('Module '.$this->module->title().' : AdminTask “'.$task->name().'” specific settings could not be updated. See error log.'); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | return true; |
@@ -30,34 +30,34 @@ |
||
30 | 30 | */ |
31 | 31 | class TokenGenerate implements RequestHandlerInterface |
32 | 32 | { |
33 | - private ?AdminTasksModule $module; |
|
34 | - private TokenService $token_service; |
|
35 | - |
|
36 | - /** |
|
37 | - * Constructor for TokenGenerate request handler |
|
38 | - * |
|
39 | - * @param ModuleService $module_service |
|
40 | - */ |
|
41 | - public function __construct(ModuleService $module_service, TokenService $token_service) |
|
42 | - { |
|
43 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
44 | - $this->token_service = $token_service; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * {@inheritDoc} |
|
49 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | - */ |
|
51 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | - { |
|
53 | - if ($this->module === null) { |
|
54 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
55 | - } |
|
56 | - |
|
57 | - $token = $this->token_service->generateRandomToken(); |
|
58 | - $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
|
59 | - Log::addConfigurationLog($this->module->title() . ' : New token generated.'); |
|
60 | - |
|
61 | - return Registry::responseFactory()->response(['token' => $token]); |
|
62 | - } |
|
33 | + private ?AdminTasksModule $module; |
|
34 | + private TokenService $token_service; |
|
35 | + |
|
36 | + /** |
|
37 | + * Constructor for TokenGenerate request handler |
|
38 | + * |
|
39 | + * @param ModuleService $module_service |
|
40 | + */ |
|
41 | + public function __construct(ModuleService $module_service, TokenService $token_service) |
|
42 | + { |
|
43 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
44 | + $this->token_service = $token_service; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * {@inheritDoc} |
|
49 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | + */ |
|
51 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | + { |
|
53 | + if ($this->module === null) { |
|
54 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
55 | + } |
|
56 | + |
|
57 | + $token = $this->token_service->generateRandomToken(); |
|
58 | + $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
|
59 | + Log::addConfigurationLog($this->module->title() . ' : New token generated.'); |
|
60 | + |
|
61 | + return Registry::responseFactory()->response(['token' => $token]); |
|
62 | + } |
|
63 | 63 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | |
57 | 57 | $token = $this->token_service->generateRandomToken(); |
58 | 58 | $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
59 | - Log::addConfigurationLog($this->module->title() . ' : New token generated.'); |
|
59 | + Log::addConfigurationLog($this->module->title().' : New token generated.'); |
|
60 | 60 | |
61 | 61 | return Registry::responseFactory()->response(['token' => $token]); |
62 | 62 | } |
@@ -29,47 +29,47 @@ |
||
29 | 29 | */ |
30 | 30 | class AdminConfigPage implements RequestHandlerInterface |
31 | 31 | { |
32 | - use ViewResponseTrait; |
|
32 | + use ViewResponseTrait; |
|
33 | 33 | |
34 | - private ?AdminTasksModule $module; |
|
35 | - private TokenService $token_service; |
|
34 | + private ?AdminTasksModule $module; |
|
35 | + private TokenService $token_service; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Constructor for Admin Config request handler |
|
39 | - * |
|
40 | - * @param ModuleService $module_service |
|
41 | - */ |
|
42 | - public function __construct(ModuleService $module_service, TokenService $token_service) |
|
43 | - { |
|
44 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
45 | - $this->token_service = $token_service; |
|
46 | - } |
|
37 | + /** |
|
38 | + * Constructor for Admin Config request handler |
|
39 | + * |
|
40 | + * @param ModuleService $module_service |
|
41 | + */ |
|
42 | + public function __construct(ModuleService $module_service, TokenService $token_service) |
|
43 | + { |
|
44 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
45 | + $this->token_service = $token_service; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * {@inheritDoc} |
|
50 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
51 | - */ |
|
52 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
53 | - { |
|
54 | - $this->layout = 'layouts/administration'; |
|
48 | + /** |
|
49 | + * {@inheritDoc} |
|
50 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
51 | + */ |
|
52 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
53 | + { |
|
54 | + $this->layout = 'layouts/administration'; |
|
55 | 55 | |
56 | - if ($this->module === null) { |
|
57 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
58 | - } |
|
56 | + if ($this->module === null) { |
|
57 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
58 | + } |
|
59 | 59 | |
60 | - $token = $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN'); |
|
61 | - if ($token === '') { |
|
62 | - $token = $this->token_service->generateRandomToken(); |
|
63 | - $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
|
64 | - } |
|
60 | + $token = $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN'); |
|
61 | + if ($token === '') { |
|
62 | + $token = $this->token_service->generateRandomToken(); |
|
63 | + $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
|
64 | + } |
|
65 | 65 | |
66 | - return $this->viewResponse($this->module->name() . '::admin/config', [ |
|
67 | - 'title' => $this->module->title(), |
|
68 | - 'trigger_token' => $token, |
|
69 | - 'trigger_route' => route(TaskTrigger::class, ['task' => '__TASKNAME__', 'force' => '__TOKEN__']), |
|
70 | - 'new_token_route' => route(TokenGenerate::class), |
|
71 | - 'tasks_data_route' => route(TasksList::class), |
|
72 | - 'js_script_url' => $this->module->assetUrl('js/admintasks.min.js') |
|
73 | - ]); |
|
74 | - } |
|
66 | + return $this->viewResponse($this->module->name() . '::admin/config', [ |
|
67 | + 'title' => $this->module->title(), |
|
68 | + 'trigger_token' => $token, |
|
69 | + 'trigger_route' => route(TaskTrigger::class, ['task' => '__TASKNAME__', 'force' => '__TOKEN__']), |
|
70 | + 'new_token_route' => route(TokenGenerate::class), |
|
71 | + 'tasks_data_route' => route(TasksList::class), |
|
72 | + 'js_script_url' => $this->module->assetUrl('js/admintasks.min.js') |
|
73 | + ]); |
|
74 | + } |
|
75 | 75 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | $this->module->setPreference('MAJ_AT_FORCE_EXEC_TOKEN', $token); |
64 | 64 | } |
65 | 65 | |
66 | - return $this->viewResponse($this->module->name() . '::admin/config', [ |
|
66 | + return $this->viewResponse($this->module->name().'::admin/config', [ |
|
67 | 67 | 'title' => $this->module->title(), |
68 | 68 | 'trigger_token' => $token, |
69 | 69 | 'trigger_route' => route(TaskTrigger::class, ['task' => '__TASKNAME__', 'force' => '__TOKEN__']), |