@@ -22,12 +22,12 @@ |
||
22 | 22 | class Migration0 implements MigrationInterface |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * {@inheritDoc} |
|
27 | - * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
28 | - */ |
|
29 | - public function upgrade(): void |
|
30 | - { |
|
31 | - // These migrations have been merged into migration 2. |
|
32 | - } |
|
25 | + /** |
|
26 | + * {@inheritDoc} |
|
27 | + * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
28 | + */ |
|
29 | + public function upgrade(): void |
|
30 | + { |
|
31 | + // These migrations have been merged into migration 2. |
|
32 | + } |
|
33 | 33 | } |
@@ -24,36 +24,36 @@ |
||
24 | 24 | class Migration2 implements MigrationInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * {@inheritDoc} |
|
29 | - * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
30 | - */ |
|
31 | - public function upgrade(): void |
|
32 | - { |
|
33 | - |
|
34 | - // Clean up previous sosa table if it exists |
|
35 | - DB::schema()->dropIfExists('maj_sosa'); |
|
36 | - |
|
37 | - DB::schema()->create('maj_sosa', static function (Blueprint $table): void { |
|
38 | - |
|
39 | - $table->integer('majs_gedcom_id'); |
|
40 | - $table->integer('majs_user_id')->default(-1); |
|
41 | - $table->bigInteger('majs_sosa')->unsigned(); // Allow to calculate sosa on 64 generations |
|
42 | - $table->string('majs_i_id', 20); |
|
43 | - $table->tinyInteger('majs_gen')->nullable(); |
|
44 | - $table->smallInteger('majs_birth_year')->nullable(); |
|
45 | - $table->smallInteger('majs_birth_year_est')->nullable(); |
|
46 | - $table->smallInteger('majs_death_year')->nullable(); |
|
47 | - $table->smallInteger('majs_death_year_est')->nullable(); |
|
48 | - |
|
49 | - $table->primary(['majs_gedcom_id', 'majs_user_id', 'majs_sosa']); |
|
50 | - |
|
51 | - $table->index(['majs_gedcom_id', 'majs_user_id']); |
|
52 | - $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_i_id']); |
|
53 | - $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_gen']); |
|
54 | - |
|
55 | - $table->foreign('majs_gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade'); |
|
56 | - $table->foreign('majs_user_id')->references('user_id')->on('user')->onDelete('cascade'); |
|
57 | - }); |
|
58 | - } |
|
27 | + /** |
|
28 | + * {@inheritDoc} |
|
29 | + * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
30 | + */ |
|
31 | + public function upgrade(): void |
|
32 | + { |
|
33 | + |
|
34 | + // Clean up previous sosa table if it exists |
|
35 | + DB::schema()->dropIfExists('maj_sosa'); |
|
36 | + |
|
37 | + DB::schema()->create('maj_sosa', static function (Blueprint $table): void { |
|
38 | + |
|
39 | + $table->integer('majs_gedcom_id'); |
|
40 | + $table->integer('majs_user_id')->default(-1); |
|
41 | + $table->bigInteger('majs_sosa')->unsigned(); // Allow to calculate sosa on 64 generations |
|
42 | + $table->string('majs_i_id', 20); |
|
43 | + $table->tinyInteger('majs_gen')->nullable(); |
|
44 | + $table->smallInteger('majs_birth_year')->nullable(); |
|
45 | + $table->smallInteger('majs_birth_year_est')->nullable(); |
|
46 | + $table->smallInteger('majs_death_year')->nullable(); |
|
47 | + $table->smallInteger('majs_death_year_est')->nullable(); |
|
48 | + |
|
49 | + $table->primary(['majs_gedcom_id', 'majs_user_id', 'majs_sosa']); |
|
50 | + |
|
51 | + $table->index(['majs_gedcom_id', 'majs_user_id']); |
|
52 | + $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_i_id']); |
|
53 | + $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_gen']); |
|
54 | + |
|
55 | + $table->foreign('majs_gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade'); |
|
56 | + $table->foreign('majs_user_id')->references('user_id')->on('user')->onDelete('cascade'); |
|
57 | + }); |
|
58 | + } |
|
59 | 59 | } |
@@ -29,367 +29,367 @@ |
||
29 | 29 | class SosaStatisticsService |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * Reference user |
|
34 | - * @var User $user |
|
35 | - */ |
|
36 | - private $user; |
|
32 | + /** |
|
33 | + * Reference user |
|
34 | + * @var User $user |
|
35 | + */ |
|
36 | + private $user; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Reference tree |
|
40 | - * @var Tree $tree |
|
41 | - */ |
|
42 | - private $tree; |
|
38 | + /** |
|
39 | + * Reference tree |
|
40 | + * @var Tree $tree |
|
41 | + */ |
|
42 | + private $tree; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Constructor for Sosa Statistics Service |
|
46 | - * |
|
47 | - * @param Tree $tree |
|
48 | - * @param User $user |
|
49 | - */ |
|
50 | - public function __construct(Tree $tree, User $user) |
|
51 | - { |
|
52 | - $this->tree = $tree; |
|
53 | - $this->user = $user; |
|
54 | - } |
|
44 | + /** |
|
45 | + * Constructor for Sosa Statistics Service |
|
46 | + * |
|
47 | + * @param Tree $tree |
|
48 | + * @param User $user |
|
49 | + */ |
|
50 | + public function __construct(Tree $tree, User $user) |
|
51 | + { |
|
52 | + $this->tree = $tree; |
|
53 | + $this->user = $user; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Return the root individual for the reference tree and user. |
|
58 | - * |
|
59 | - * @return Individual|NULL |
|
60 | - */ |
|
61 | - public function rootIndividual(): ?Individual |
|
62 | - { |
|
63 | - $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
64 | - return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
65 | - } |
|
56 | + /** |
|
57 | + * Return the root individual for the reference tree and user. |
|
58 | + * |
|
59 | + * @return Individual|NULL |
|
60 | + */ |
|
61 | + public function rootIndividual(): ?Individual |
|
62 | + { |
|
63 | + $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
64 | + return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Get the highest generation for the reference tree and user. |
|
69 | - * |
|
70 | - * @return int |
|
71 | - */ |
|
72 | - public function maxGeneration(): int |
|
73 | - { |
|
74 | - return (int) DB::table('maj_sosa') |
|
75 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
76 | - ->where('majs_user_id', '=', $this->user->id()) |
|
77 | - ->max('majs_gen'); |
|
78 | - } |
|
67 | + /** |
|
68 | + * Get the highest generation for the reference tree and user. |
|
69 | + * |
|
70 | + * @return int |
|
71 | + */ |
|
72 | + public function maxGeneration(): int |
|
73 | + { |
|
74 | + return (int) DB::table('maj_sosa') |
|
75 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
76 | + ->where('majs_user_id', '=', $this->user->id()) |
|
77 | + ->max('majs_gen'); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Get the total count of individuals in the tree. |
|
82 | - * |
|
83 | - * @return int |
|
84 | - */ |
|
85 | - public function totalIndividuals(): int |
|
86 | - { |
|
87 | - return DB::table('individuals') |
|
88 | - ->where('i_file', '=', $this->tree->id()) |
|
89 | - ->count(); |
|
90 | - } |
|
80 | + /** |
|
81 | + * Get the total count of individuals in the tree. |
|
82 | + * |
|
83 | + * @return int |
|
84 | + */ |
|
85 | + public function totalIndividuals(): int |
|
86 | + { |
|
87 | + return DB::table('individuals') |
|
88 | + ->where('i_file', '=', $this->tree->id()) |
|
89 | + ->count(); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Get the total count of Sosa ancestors for all generations |
|
94 | - * |
|
95 | - * @return int |
|
96 | - */ |
|
97 | - public function totalAncestors(): int |
|
98 | - { |
|
99 | - return DB::table('maj_sosa') |
|
100 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
101 | - ->where('majs_user_id', '=', $this->user->id()) |
|
102 | - ->count(); |
|
103 | - } |
|
92 | + /** |
|
93 | + * Get the total count of Sosa ancestors for all generations |
|
94 | + * |
|
95 | + * @return int |
|
96 | + */ |
|
97 | + public function totalAncestors(): int |
|
98 | + { |
|
99 | + return DB::table('maj_sosa') |
|
100 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
101 | + ->where('majs_user_id', '=', $this->user->id()) |
|
102 | + ->count(); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Get the total count of Sosa ancestors for a generation |
|
107 | - * |
|
108 | - * @return int |
|
109 | - */ |
|
110 | - public function totalAncestorsAtGeneration(int $gen): int |
|
111 | - { |
|
112 | - return DB::table('maj_sosa') |
|
113 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
114 | - ->where('majs_user_id', '=', $this->user->id()) |
|
115 | - ->where('majs_gen', '=', $gen) |
|
116 | - ->count(); |
|
117 | - } |
|
105 | + /** |
|
106 | + * Get the total count of Sosa ancestors for a generation |
|
107 | + * |
|
108 | + * @return int |
|
109 | + */ |
|
110 | + public function totalAncestorsAtGeneration(int $gen): int |
|
111 | + { |
|
112 | + return DB::table('maj_sosa') |
|
113 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
114 | + ->where('majs_user_id', '=', $this->user->id()) |
|
115 | + ->where('majs_gen', '=', $gen) |
|
116 | + ->count(); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Get the total count of distinct Sosa ancestors for all generations |
|
121 | - * |
|
122 | - * @return int |
|
123 | - */ |
|
124 | - public function totalDistinctAncestors(): int |
|
125 | - { |
|
126 | - return DB::table('maj_sosa') |
|
127 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
128 | - ->where('majs_user_id', '=', $this->user->id()) |
|
129 | - ->distinct() |
|
130 | - ->count('majs_i_id'); |
|
131 | - } |
|
119 | + /** |
|
120 | + * Get the total count of distinct Sosa ancestors for all generations |
|
121 | + * |
|
122 | + * @return int |
|
123 | + */ |
|
124 | + public function totalDistinctAncestors(): int |
|
125 | + { |
|
126 | + return DB::table('maj_sosa') |
|
127 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
128 | + ->where('majs_user_id', '=', $this->user->id()) |
|
129 | + ->distinct() |
|
130 | + ->count('majs_i_id'); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
135 | - * |
|
136 | - * @return float |
|
137 | - */ |
|
138 | - public function meanGenerationTime(): float |
|
139 | - { |
|
140 | - $row = DB::table('maj_sosa') |
|
141 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
142 | - ->where('majs_user_id', '=', $this->user->id()) |
|
143 | - ->whereNotNull('majs_birth_year') |
|
144 | - ->selectRaw('COUNT(majs_sosa) AS n') |
|
145 | - ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
146 | - ->selectRaw('SUM(majs_gen) AS sum_x') |
|
147 | - ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
148 | - ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
149 | - ->get()->first(); |
|
133 | + /** |
|
134 | + * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
135 | + * |
|
136 | + * @return float |
|
137 | + */ |
|
138 | + public function meanGenerationTime(): float |
|
139 | + { |
|
140 | + $row = DB::table('maj_sosa') |
|
141 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
142 | + ->where('majs_user_id', '=', $this->user->id()) |
|
143 | + ->whereNotNull('majs_birth_year') |
|
144 | + ->selectRaw('COUNT(majs_sosa) AS n') |
|
145 | + ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
146 | + ->selectRaw('SUM(majs_gen) AS sum_x') |
|
147 | + ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
148 | + ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
149 | + ->get()->first(); |
|
150 | 150 | |
151 | - return $row->n == 0 ? 0 : |
|
152 | - -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
153 | - } |
|
151 | + return $row->n == 0 ? 0 : |
|
152 | + -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Get the statistic array detailed by generation. |
|
157 | - * Statistics for each generation are: |
|
158 | - * - The number of Sosa in generation |
|
159 | - * - The number of Sosa up to generation |
|
160 | - * - The number of distinct Sosa up to generation |
|
161 | - * - The year of the first birth in generation |
|
162 | - * - The year of the first estimated birth in generation |
|
163 | - * - The year of the last birth in generation |
|
164 | - * - The year of the last estimated birth in generation |
|
165 | - * - The average year of birth in generation |
|
166 | - * |
|
167 | - * @return array<int, array<string, int|null>> Statistics array |
|
168 | - */ |
|
169 | - public function statisticsByGenerations(): array |
|
170 | - { |
|
171 | - $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
172 | - $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
155 | + /** |
|
156 | + * Get the statistic array detailed by generation. |
|
157 | + * Statistics for each generation are: |
|
158 | + * - The number of Sosa in generation |
|
159 | + * - The number of Sosa up to generation |
|
160 | + * - The number of distinct Sosa up to generation |
|
161 | + * - The year of the first birth in generation |
|
162 | + * - The year of the first estimated birth in generation |
|
163 | + * - The year of the last birth in generation |
|
164 | + * - The year of the last estimated birth in generation |
|
165 | + * - The average year of birth in generation |
|
166 | + * |
|
167 | + * @return array<int, array<string, int|null>> Statistics array |
|
168 | + */ |
|
169 | + public function statisticsByGenerations(): array |
|
170 | + { |
|
171 | + $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
172 | + $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
173 | 173 | |
174 | - $statistics_by_gen = []; |
|
175 | - foreach ($stats_by_gen as $gen => $stats_gen) { |
|
176 | - $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
177 | - 'sosaCount' => (int) $stats_gen->total_sosa, |
|
178 | - 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
179 | - 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
180 | - 'firstBirth' => $stats_gen->first_year, |
|
181 | - 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
182 | - 'lastBirth' => $stats_gen->last_year, |
|
183 | - 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
184 | - ); |
|
185 | - } |
|
174 | + $statistics_by_gen = []; |
|
175 | + foreach ($stats_by_gen as $gen => $stats_gen) { |
|
176 | + $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
177 | + 'sosaCount' => (int) $stats_gen->total_sosa, |
|
178 | + 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
179 | + 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
180 | + 'firstBirth' => $stats_gen->first_year, |
|
181 | + 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
182 | + 'lastBirth' => $stats_gen->last_year, |
|
183 | + 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
184 | + ); |
|
185 | + } |
|
186 | 186 | |
187 | - return $statistics_by_gen; |
|
188 | - } |
|
187 | + return $statistics_by_gen; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Returns the basic statistics data by generation. |
|
192 | - * |
|
193 | - * @return Collection |
|
194 | - */ |
|
195 | - private function statisticsByGenerationBasicData(): Collection |
|
196 | - { |
|
197 | - return DB::table('maj_sosa') |
|
198 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
199 | - ->where('majs_user_id', '=', $this->user->id()) |
|
200 | - ->groupBy('majs_gen') |
|
201 | - ->orderBy('majs_gen', 'asc') |
|
202 | - ->select('majs_gen AS gen') |
|
203 | - ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
204 | - ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
205 | - ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
206 | - ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
207 | - ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
208 | - ->get()->keyBy('gen'); |
|
209 | - } |
|
190 | + /** |
|
191 | + * Returns the basic statistics data by generation. |
|
192 | + * |
|
193 | + * @return Collection |
|
194 | + */ |
|
195 | + private function statisticsByGenerationBasicData(): Collection |
|
196 | + { |
|
197 | + return DB::table('maj_sosa') |
|
198 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
199 | + ->where('majs_user_id', '=', $this->user->id()) |
|
200 | + ->groupBy('majs_gen') |
|
201 | + ->orderBy('majs_gen', 'asc') |
|
202 | + ->select('majs_gen AS gen') |
|
203 | + ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
204 | + ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
205 | + ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
206 | + ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
207 | + ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
208 | + ->get()->keyBy('gen'); |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Returns the cumulative statistics data by generation |
|
213 | - * |
|
214 | - * @return Collection |
|
215 | - */ |
|
216 | - private function statisticsByGenerationCumulativeData(): Collection |
|
217 | - { |
|
218 | - $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
219 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
220 | - ->where('majs_user_id', '=', $this->user->id()); |
|
211 | + /** |
|
212 | + * Returns the cumulative statistics data by generation |
|
213 | + * |
|
214 | + * @return Collection |
|
215 | + */ |
|
216 | + private function statisticsByGenerationCumulativeData(): Collection |
|
217 | + { |
|
218 | + $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
219 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
220 | + ->where('majs_user_id', '=', $this->user->id()); |
|
221 | 221 | |
222 | - return DB::table('maj_sosa') |
|
223 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
224 | - $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
225 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
226 | - ->where('majs_user_id', '=', $this->user->id()); |
|
227 | - }) |
|
228 | - ->groupBy('list_gen.majs_gen') |
|
229 | - ->select('list_gen.majs_gen AS gen') |
|
230 | - ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
231 | - ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
232 | - ->get()->keyBy('gen'); |
|
233 | - } |
|
222 | + return DB::table('maj_sosa') |
|
223 | + ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
224 | + $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
225 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
226 | + ->where('majs_user_id', '=', $this->user->id()); |
|
227 | + }) |
|
228 | + ->groupBy('list_gen.majs_gen') |
|
229 | + ->select('list_gen.majs_gen AS gen') |
|
230 | + ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
231 | + ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
232 | + ->get()->keyBy('gen'); |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
237 | - * Sosa 1 is of generation 1. |
|
238 | - * |
|
239 | - * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
240 | - * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
241 | - * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
242 | - * |
|
243 | - * Format: |
|
244 | - * - key : sosa number of the ancestor |
|
245 | - * - values: |
|
246 | - * - root_ancestor_id : ID of the ancestor |
|
247 | - * - mean_gen_depth : Mean generation depth |
|
248 | - * - stddev_gen_depth : Standard deviation of generation depth |
|
249 | - * |
|
250 | - * @param int $gen Sosa generation |
|
251 | - * @return Collection |
|
252 | - */ |
|
253 | - public function generationDepthStatsAtGeneration(int $gen): Collection |
|
254 | - { |
|
255 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
256 | - $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
257 | - ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
258 | - ->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 |
|
259 | - ->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 |
|
260 | - ->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 |
|
261 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
262 | - // Link to sosa's father |
|
263 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
264 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
265 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
266 | - }) |
|
267 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
268 | - // Link to sosa's mother |
|
269 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
270 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
271 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
272 | - }) |
|
273 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
274 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
275 | - ->where('sosa.majs_gen', '>=', $gen) |
|
276 | - ->where(function (Builder $query): void { |
|
277 | - $query->whereNull('sosa_fat.majs_i_id') |
|
278 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
279 | - }) |
|
280 | - ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
235 | + /** |
|
236 | + * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
237 | + * Sosa 1 is of generation 1. |
|
238 | + * |
|
239 | + * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
240 | + * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
241 | + * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
242 | + * |
|
243 | + * Format: |
|
244 | + * - key : sosa number of the ancestor |
|
245 | + * - values: |
|
246 | + * - root_ancestor_id : ID of the ancestor |
|
247 | + * - mean_gen_depth : Mean generation depth |
|
248 | + * - stddev_gen_depth : Standard deviation of generation depth |
|
249 | + * |
|
250 | + * @param int $gen Sosa generation |
|
251 | + * @return Collection |
|
252 | + */ |
|
253 | + public function generationDepthStatsAtGeneration(int $gen): Collection |
|
254 | + { |
|
255 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
256 | + $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
257 | + ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
258 | + ->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 |
|
259 | + ->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 |
|
260 | + ->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 |
|
261 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
262 | + // Link to sosa's father |
|
263 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
264 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
265 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
266 | + }) |
|
267 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
268 | + // Link to sosa's mother |
|
269 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
270 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
271 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
272 | + }) |
|
273 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
274 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
275 | + ->where('sosa.majs_gen', '>=', $gen) |
|
276 | + ->where(function (Builder $query): void { |
|
277 | + $query->whereNull('sosa_fat.majs_i_id') |
|
278 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
279 | + }) |
|
280 | + ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
281 | 281 | |
282 | - return DB::table('maj_sosa AS sosa_list') |
|
283 | - ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
284 | - ->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 |
|
285 | - ->selectRaw(' SQRT(' . |
|
286 | - ' 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 |
|
287 | - ' - 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 |
|
288 | - ' ) AS stddev_gen_depth') |
|
289 | - ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
290 | - $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
291 | - ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
292 | - ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
293 | - }) |
|
294 | - ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
295 | - ->orderBy('stats_by_gen.root_ancestor') |
|
296 | - ->get()->keyBy('root_ancestor_sosa'); |
|
297 | - } |
|
282 | + return DB::table('maj_sosa AS sosa_list') |
|
283 | + ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
284 | + ->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 |
|
285 | + ->selectRaw(' SQRT(' . |
|
286 | + ' 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 |
|
287 | + ' - 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 |
|
288 | + ' ) AS stddev_gen_depth') |
|
289 | + ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
290 | + $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
291 | + ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
292 | + ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
293 | + }) |
|
294 | + ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
295 | + ->orderBy('stats_by_gen.root_ancestor') |
|
296 | + ->get()->keyBy('root_ancestor_sosa'); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Return a collection of the most duplicated root Sosa ancestors. |
|
301 | - * The number of ancestors to return is limited by the parameter $limit. |
|
302 | - * If several individuals are tied when reaching the limit, none of them are returned, |
|
303 | - * which means that there can be less individuals returned than requested. |
|
304 | - * |
|
305 | - * Format: |
|
306 | - * - value: |
|
307 | - * - sosa_i_id : sosa individual |
|
308 | - * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
309 | - * |
|
310 | - * @param int $limit |
|
311 | - * @return Collection |
|
312 | - */ |
|
313 | - public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
314 | - { |
|
315 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
316 | - $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
317 | - ->select('sosa.majs_i_id AS sosa_i_id') |
|
318 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
319 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
320 | - // Link to sosa's father |
|
321 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
322 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
323 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
324 | - }) |
|
325 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
326 | - // Link to sosa's mother |
|
327 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
328 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
329 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
330 | - }) |
|
331 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
332 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
333 | - ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
334 | - ->whereNull('sosa_mot.majs_sosa') |
|
335 | - ->groupBy('sosa.majs_i_id') |
|
336 | - ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
337 | - ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
338 | - ->limit($limit + 1) // We want to select one more than required, for ties |
|
339 | - ->get(); |
|
299 | + /** |
|
300 | + * Return a collection of the most duplicated root Sosa ancestors. |
|
301 | + * The number of ancestors to return is limited by the parameter $limit. |
|
302 | + * If several individuals are tied when reaching the limit, none of them are returned, |
|
303 | + * which means that there can be less individuals returned than requested. |
|
304 | + * |
|
305 | + * Format: |
|
306 | + * - value: |
|
307 | + * - sosa_i_id : sosa individual |
|
308 | + * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
309 | + * |
|
310 | + * @param int $limit |
|
311 | + * @return Collection |
|
312 | + */ |
|
313 | + public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
314 | + { |
|
315 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
316 | + $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
317 | + ->select('sosa.majs_i_id AS sosa_i_id') |
|
318 | + ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
319 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
320 | + // Link to sosa's father |
|
321 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
322 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
323 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
324 | + }) |
|
325 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
326 | + // Link to sosa's mother |
|
327 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
328 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
329 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
330 | + }) |
|
331 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
332 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
333 | + ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
334 | + ->whereNull('sosa_mot.majs_sosa') |
|
335 | + ->groupBy('sosa.majs_i_id') |
|
336 | + ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
337 | + ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
338 | + ->limit($limit + 1) // We want to select one more than required, for ties |
|
339 | + ->get(); |
|
340 | 340 | |
341 | - if ($multiple_ancestors->count() > $limit) { |
|
342 | - $last_count = $multiple_ancestors->last()->sosa_count; |
|
343 | - $multiple_ancestors = $multiple_ancestors->reject(function ($element) use ($last_count): bool { |
|
344 | - return $element->sosa_count == $last_count; |
|
345 | - }); |
|
346 | - } |
|
347 | - return $multiple_ancestors; |
|
348 | - } |
|
341 | + if ($multiple_ancestors->count() > $limit) { |
|
342 | + $last_count = $multiple_ancestors->last()->sosa_count; |
|
343 | + $multiple_ancestors = $multiple_ancestors->reject(function ($element) use ($last_count): bool { |
|
344 | + return $element->sosa_count == $last_count; |
|
345 | + }); |
|
346 | + } |
|
347 | + return $multiple_ancestors; |
|
348 | + } |
|
349 | 349 | |
350 | - /** |
|
351 | - * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
352 | - * at a specified generation. |
|
353 | - * |
|
354 | - * Format: |
|
355 | - * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
356 | - * For instance 3 represent the maternal grand father |
|
357 | - * 0 is used for shared ancestors |
|
358 | - * - values: number of ancestors exclusively in the ancestors of the ancestor in key |
|
359 | - * |
|
360 | - * For instance a result at generation 3 could be : |
|
361 | - * array ( 0 => 12 -> 12 ancestors are shared by the grand-parents |
|
362 | - * 1 => 32 -> 32 ancestors are exclusive to the paternal grand-father |
|
363 | - * 2 => 25 -> 25 ancestors are exclusive to the paternal grand-mother |
|
364 | - * 3 => 12 -> 12 ancestors are exclusive to the maternal grand-father |
|
365 | - * 4 => 30 -> 30 ancestors are exclusive to the maternal grand-mother |
|
366 | - * ) |
|
367 | - * |
|
368 | - * @param int $gen |
|
369 | - * @return Collection |
|
370 | - */ |
|
371 | - public function ancestorsDispersionForGeneration(int $gen): Collection |
|
372 | - { |
|
373 | - $ancestors_branches = DB::table('maj_sosa') |
|
374 | - ->select('majs_i_id AS i_id') |
|
375 | - ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
376 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
377 | - ->where('majs_user_id', '=', $this->user->id()) |
|
378 | - ->where('majs_gen', '>=', $gen) |
|
379 | - ->groupBy('majs_i_id', 'branch'); |
|
350 | + /** |
|
351 | + * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
352 | + * at a specified generation. |
|
353 | + * |
|
354 | + * Format: |
|
355 | + * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
356 | + * For instance 3 represent the maternal grand father |
|
357 | + * 0 is used for shared ancestors |
|
358 | + * - values: number of ancestors exclusively in the ancestors of the ancestor in key |
|
359 | + * |
|
360 | + * For instance a result at generation 3 could be : |
|
361 | + * array ( 0 => 12 -> 12 ancestors are shared by the grand-parents |
|
362 | + * 1 => 32 -> 32 ancestors are exclusive to the paternal grand-father |
|
363 | + * 2 => 25 -> 25 ancestors are exclusive to the paternal grand-mother |
|
364 | + * 3 => 12 -> 12 ancestors are exclusive to the maternal grand-father |
|
365 | + * 4 => 30 -> 30 ancestors are exclusive to the maternal grand-mother |
|
366 | + * ) |
|
367 | + * |
|
368 | + * @param int $gen |
|
369 | + * @return Collection |
|
370 | + */ |
|
371 | + public function ancestorsDispersionForGeneration(int $gen): Collection |
|
372 | + { |
|
373 | + $ancestors_branches = DB::table('maj_sosa') |
|
374 | + ->select('majs_i_id AS i_id') |
|
375 | + ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
376 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
377 | + ->where('majs_user_id', '=', $this->user->id()) |
|
378 | + ->where('majs_gen', '>=', $gen) |
|
379 | + ->groupBy('majs_i_id', 'branch'); |
|
380 | 380 | |
381 | 381 | |
382 | - $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
383 | - ->fromSub($ancestors_branches, 'indi_branch') |
|
384 | - ->select('i_id') |
|
385 | - ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
386 | - ->groupBy('i_id'); |
|
382 | + $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
383 | + ->fromSub($ancestors_branches, 'indi_branch') |
|
384 | + ->select('i_id') |
|
385 | + ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
386 | + ->groupBy('i_id'); |
|
387 | 387 | |
388 | - return DB::table('maj_sosa') |
|
389 | - ->fromSub($consolidated_ancestors_branches, 'indi_branch_consolidated') |
|
390 | - ->select('branches') |
|
391 | - ->selectRaw('COUNT(i_id) AS count_indi') |
|
392 | - ->groupBy('branches') |
|
393 | - ->get()->pluck('count_indi', 'branches'); |
|
394 | - } |
|
388 | + return DB::table('maj_sosa') |
|
389 | + ->fromSub($consolidated_ancestors_branches, 'indi_branch_consolidated') |
|
390 | + ->select('branches') |
|
391 | + ->selectRaw('COUNT(i_id) AS count_indi') |
|
392 | + ->groupBy('branches') |
|
393 | + ->get()->pluck('count_indi', 'branches'); |
|
394 | + } |
|
395 | 395 | } |
@@ -24,133 +24,133 @@ |
||
24 | 24 | */ |
25 | 25 | class SosaCalculatorService |
26 | 26 | { |
27 | - /** |
|
28 | - * Maximium size for the temporary Sosa table |
|
29 | - * @var int TMP_SOSA_TABLE_LIMIT |
|
30 | - */ |
|
31 | - private const TMP_SOSA_TABLE_LIMIT = 1000; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var SosaRecordsService $sosa_records_service |
|
35 | - */ |
|
36 | - private $sosa_records_service; |
|
37 | - |
|
38 | - /** |
|
39 | - * Reference user |
|
40 | - * @var User $user |
|
41 | - */ |
|
42 | - private $user; |
|
43 | - |
|
44 | - /** |
|
45 | - * Reference tree |
|
46 | - * @var Tree $tree |
|
47 | - */ |
|
48 | - private $tree; |
|
49 | - |
|
50 | - /** |
|
51 | - * Temporary Sosa table, used during construction |
|
52 | - * @var array<array<string,mixed>> $tmp_sosa_table |
|
53 | - */ |
|
54 | - private $tmp_sosa_table; |
|
55 | - |
|
56 | - /** |
|
57 | - * Constructor for the Sosa Calculator |
|
58 | - * |
|
59 | - * @param SosaRecordsService $sosa_records_service |
|
60 | - * @param Tree $tree |
|
61 | - * @param User $user |
|
62 | - */ |
|
63 | - public function __construct(SosaRecordsService $sosa_records_service, Tree $tree, User $user) |
|
64 | - { |
|
65 | - $this->sosa_records_service = $sosa_records_service; |
|
66 | - $this->tree = $tree; |
|
67 | - $this->user = $user; |
|
68 | - $this->tmp_sosa_table = array(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Compute all Sosa ancestors from the user's root individual. |
|
73 | - * |
|
74 | - * @return bool Result of the computation |
|
75 | - */ |
|
76 | - public function computeAll(): bool |
|
77 | - { |
|
78 | - $root_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
79 | - if (($indi = Registry::individualFactory()->make($root_id, $this->tree)) !== null) { |
|
80 | - $this->sosa_records_service->deleteAll($this->tree, $this->user); |
|
81 | - $this->addNode($indi, 1); |
|
82 | - $this->flushTmpSosaTable(true); |
|
83 | - return true; |
|
84 | - } |
|
85 | - return false; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Compute all Sosa Ancestors from a specified Individual |
|
90 | - * |
|
91 | - * @param Individual $indi |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - public function computeFromIndividual(Individual $indi): bool |
|
95 | - { |
|
96 | - $current_sosas = $this->sosa_records_service->getSosaNumbers($this->tree, $this->user, $indi); |
|
97 | - foreach ($current_sosas->keys() as $sosa) { |
|
98 | - $this->sosa_records_service->deleteAncestorsFrom($this->tree, $this->user, $sosa); |
|
99 | - $this->addNode($indi, $sosa); |
|
100 | - } |
|
101 | - $this->flushTmpSosaTable(true); |
|
102 | - return true; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Recursive method to add individual to the Sosa table, and flush it regularly |
|
107 | - * |
|
108 | - * @param Individual $indi Individual to add |
|
109 | - * @param int $sosa Individual's sosa |
|
110 | - */ |
|
111 | - private function addNode(Individual $indi, int $sosa): void |
|
112 | - { |
|
113 | - $birth_year = $indi->getBirthDate()->gregorianYear(); |
|
114 | - $birth_year_est = $birth_year === 0 ? $indi->getEstimatedBirthDate()->gregorianYear() : $birth_year; |
|
115 | - |
|
116 | - $death_year = $indi->getDeathDate()->gregorianYear(); |
|
117 | - $death_year_est = $death_year === 0 ? $indi->getEstimatedDeathDate()->gregorianYear() : $death_year; |
|
118 | - |
|
119 | - $this->tmp_sosa_table[] = [ |
|
120 | - 'indi' => $indi->xref(), |
|
121 | - 'sosa' => $sosa, |
|
122 | - 'birth_year' => $birth_year === 0 ? null : $birth_year, |
|
123 | - 'birth_year_est' => $birth_year_est === 0 ? null : $birth_year_est, |
|
124 | - 'death_year' => $death_year === 0 ? null : $death_year, |
|
125 | - 'death_year_est' => $death_year_est === 0 ? null : $death_year_est |
|
126 | - ]; |
|
127 | - |
|
128 | - $this->flushTmpSosaTable(); |
|
129 | - |
|
130 | - if (($fam = $indi->childFamilies()->first()) !== null) { |
|
131 | - /** @var \Fisharebest\Webtrees\Family $fam */ |
|
132 | - if (($husb = $fam->husband()) !== null) { |
|
133 | - $this->addNode($husb, 2 * $sosa); |
|
134 | - } |
|
135 | - if (($wife = $fam->wife()) !== null) { |
|
136 | - $this->addNode($wife, 2 * $sosa + 1); |
|
137 | - } |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Write sosas in the table, if the number of items is superior to the limit, or if forced. |
|
143 | - * |
|
144 | - * @param bool $force Should the flush be forced |
|
145 | - */ |
|
146 | - private function flushTmpSosaTable($force = false): void |
|
147 | - { |
|
148 | - if ( |
|
149 | - count($this->tmp_sosa_table) > 0 && |
|
150 | - ($force || count($this->tmp_sosa_table) >= self::TMP_SOSA_TABLE_LIMIT) |
|
151 | - ) { |
|
152 | - $this->sosa_records_service->insertOrUpdate($this->tree, $this->user, $this->tmp_sosa_table); |
|
153 | - $this->tmp_sosa_table = array(); |
|
154 | - } |
|
155 | - } |
|
27 | + /** |
|
28 | + * Maximium size for the temporary Sosa table |
|
29 | + * @var int TMP_SOSA_TABLE_LIMIT |
|
30 | + */ |
|
31 | + private const TMP_SOSA_TABLE_LIMIT = 1000; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var SosaRecordsService $sosa_records_service |
|
35 | + */ |
|
36 | + private $sosa_records_service; |
|
37 | + |
|
38 | + /** |
|
39 | + * Reference user |
|
40 | + * @var User $user |
|
41 | + */ |
|
42 | + private $user; |
|
43 | + |
|
44 | + /** |
|
45 | + * Reference tree |
|
46 | + * @var Tree $tree |
|
47 | + */ |
|
48 | + private $tree; |
|
49 | + |
|
50 | + /** |
|
51 | + * Temporary Sosa table, used during construction |
|
52 | + * @var array<array<string,mixed>> $tmp_sosa_table |
|
53 | + */ |
|
54 | + private $tmp_sosa_table; |
|
55 | + |
|
56 | + /** |
|
57 | + * Constructor for the Sosa Calculator |
|
58 | + * |
|
59 | + * @param SosaRecordsService $sosa_records_service |
|
60 | + * @param Tree $tree |
|
61 | + * @param User $user |
|
62 | + */ |
|
63 | + public function __construct(SosaRecordsService $sosa_records_service, Tree $tree, User $user) |
|
64 | + { |
|
65 | + $this->sosa_records_service = $sosa_records_service; |
|
66 | + $this->tree = $tree; |
|
67 | + $this->user = $user; |
|
68 | + $this->tmp_sosa_table = array(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Compute all Sosa ancestors from the user's root individual. |
|
73 | + * |
|
74 | + * @return bool Result of the computation |
|
75 | + */ |
|
76 | + public function computeAll(): bool |
|
77 | + { |
|
78 | + $root_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
79 | + if (($indi = Registry::individualFactory()->make($root_id, $this->tree)) !== null) { |
|
80 | + $this->sosa_records_service->deleteAll($this->tree, $this->user); |
|
81 | + $this->addNode($indi, 1); |
|
82 | + $this->flushTmpSosaTable(true); |
|
83 | + return true; |
|
84 | + } |
|
85 | + return false; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Compute all Sosa Ancestors from a specified Individual |
|
90 | + * |
|
91 | + * @param Individual $indi |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + public function computeFromIndividual(Individual $indi): bool |
|
95 | + { |
|
96 | + $current_sosas = $this->sosa_records_service->getSosaNumbers($this->tree, $this->user, $indi); |
|
97 | + foreach ($current_sosas->keys() as $sosa) { |
|
98 | + $this->sosa_records_service->deleteAncestorsFrom($this->tree, $this->user, $sosa); |
|
99 | + $this->addNode($indi, $sosa); |
|
100 | + } |
|
101 | + $this->flushTmpSosaTable(true); |
|
102 | + return true; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Recursive method to add individual to the Sosa table, and flush it regularly |
|
107 | + * |
|
108 | + * @param Individual $indi Individual to add |
|
109 | + * @param int $sosa Individual's sosa |
|
110 | + */ |
|
111 | + private function addNode(Individual $indi, int $sosa): void |
|
112 | + { |
|
113 | + $birth_year = $indi->getBirthDate()->gregorianYear(); |
|
114 | + $birth_year_est = $birth_year === 0 ? $indi->getEstimatedBirthDate()->gregorianYear() : $birth_year; |
|
115 | + |
|
116 | + $death_year = $indi->getDeathDate()->gregorianYear(); |
|
117 | + $death_year_est = $death_year === 0 ? $indi->getEstimatedDeathDate()->gregorianYear() : $death_year; |
|
118 | + |
|
119 | + $this->tmp_sosa_table[] = [ |
|
120 | + 'indi' => $indi->xref(), |
|
121 | + 'sosa' => $sosa, |
|
122 | + 'birth_year' => $birth_year === 0 ? null : $birth_year, |
|
123 | + 'birth_year_est' => $birth_year_est === 0 ? null : $birth_year_est, |
|
124 | + 'death_year' => $death_year === 0 ? null : $death_year, |
|
125 | + 'death_year_est' => $death_year_est === 0 ? null : $death_year_est |
|
126 | + ]; |
|
127 | + |
|
128 | + $this->flushTmpSosaTable(); |
|
129 | + |
|
130 | + if (($fam = $indi->childFamilies()->first()) !== null) { |
|
131 | + /** @var \Fisharebest\Webtrees\Family $fam */ |
|
132 | + if (($husb = $fam->husband()) !== null) { |
|
133 | + $this->addNode($husb, 2 * $sosa); |
|
134 | + } |
|
135 | + if (($wife = $fam->wife()) !== null) { |
|
136 | + $this->addNode($wife, 2 * $sosa + 1); |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Write sosas in the table, if the number of items is superior to the limit, or if forced. |
|
143 | + * |
|
144 | + * @param bool $force Should the flush be forced |
|
145 | + */ |
|
146 | + private function flushTmpSosaTable($force = false): void |
|
147 | + { |
|
148 | + if ( |
|
149 | + count($this->tmp_sosa_table) > 0 && |
|
150 | + ($force || count($this->tmp_sosa_table) >= self::TMP_SOSA_TABLE_LIMIT) |
|
151 | + ) { |
|
152 | + $this->sosa_records_service->insertOrUpdate($this->tree, $this->user, $this->tmp_sosa_table); |
|
153 | + $this->tmp_sosa_table = array(); |
|
154 | + } |
|
155 | + } |
|
156 | 156 | } |
@@ -27,231 +27,231 @@ |
||
27 | 27 | */ |
28 | 28 | class SosaRecordsService |
29 | 29 | { |
30 | - /** |
|
31 | - * Maximum number of generation the database is able to hold. |
|
32 | - * @var int MAX_DB_GENERATIONS |
|
33 | - */ |
|
34 | - public const MAX_DB_GENERATIONS = 64; |
|
30 | + /** |
|
31 | + * Maximum number of generation the database is able to hold. |
|
32 | + * @var int MAX_DB_GENERATIONS |
|
33 | + */ |
|
34 | + public const MAX_DB_GENERATIONS = 64; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Calculate the generation of a sosa |
|
38 | - * Sosa 1 is of generation 1. |
|
39 | - * |
|
40 | - * @param int $sosa |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function generation(int $sosa): int |
|
44 | - { |
|
45 | - return (int) log($sosa, 2) + 1; |
|
46 | - } |
|
36 | + /** |
|
37 | + * Calculate the generation of a sosa |
|
38 | + * Sosa 1 is of generation 1. |
|
39 | + * |
|
40 | + * @param int $sosa |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function generation(int $sosa): int |
|
44 | + { |
|
45 | + return (int) log($sosa, 2) + 1; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Returns all Sosa numbers associated to an Individual |
|
50 | - * |
|
51 | - * @param Tree $tree |
|
52 | - * @param User $user |
|
53 | - * @param Individual $indi |
|
54 | - * @return Collection |
|
55 | - */ |
|
56 | - public function getSosaNumbers(Tree $tree, User $user, Individual $indi): Collection |
|
57 | - { |
|
58 | - return DB::table('maj_sosa') |
|
59 | - ->select(['majs_sosa', 'majs_gen']) |
|
60 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
61 | - ->where('majs_user_id', '=', $user->id()) |
|
62 | - ->where('majs_i_id', '=', $indi->xref()) |
|
63 | - ->orderBy('majs_sosa') |
|
64 | - ->get()->pluck('majs_gen', 'majs_sosa'); |
|
65 | - } |
|
48 | + /** |
|
49 | + * Returns all Sosa numbers associated to an Individual |
|
50 | + * |
|
51 | + * @param Tree $tree |
|
52 | + * @param User $user |
|
53 | + * @param Individual $indi |
|
54 | + * @return Collection |
|
55 | + */ |
|
56 | + public function getSosaNumbers(Tree $tree, User $user, Individual $indi): Collection |
|
57 | + { |
|
58 | + return DB::table('maj_sosa') |
|
59 | + ->select(['majs_sosa', 'majs_gen']) |
|
60 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
61 | + ->where('majs_user_id', '=', $user->id()) |
|
62 | + ->where('majs_i_id', '=', $indi->xref()) |
|
63 | + ->orderBy('majs_sosa') |
|
64 | + ->get()->pluck('majs_gen', 'majs_sosa'); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Return a list of the Sosa ancestors at a given generation |
|
69 | - * |
|
70 | - * @param Tree $tree |
|
71 | - * @param User $user |
|
72 | - * @param int $gen |
|
73 | - * @return Collection |
|
74 | - */ |
|
75 | - public function listAncestorsAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
76 | - { |
|
77 | - return DB::table('maj_sosa') |
|
78 | - ->select(['majs_sosa', 'majs_i_id']) |
|
79 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
80 | - ->where('majs_user_id', '=', $user->id()) |
|
81 | - ->where('majs_gen', '=', $gen) |
|
82 | - ->orderBy('majs_sosa') |
|
83 | - ->get(); |
|
84 | - } |
|
67 | + /** |
|
68 | + * Return a list of the Sosa ancestors at a given generation |
|
69 | + * |
|
70 | + * @param Tree $tree |
|
71 | + * @param User $user |
|
72 | + * @param int $gen |
|
73 | + * @return Collection |
|
74 | + */ |
|
75 | + public function listAncestorsAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
76 | + { |
|
77 | + return DB::table('maj_sosa') |
|
78 | + ->select(['majs_sosa', 'majs_i_id']) |
|
79 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
80 | + ->where('majs_user_id', '=', $user->id()) |
|
81 | + ->where('majs_gen', '=', $gen) |
|
82 | + ->orderBy('majs_sosa') |
|
83 | + ->get(); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Return a list of the Sosa families at a given generation |
|
88 | - * |
|
89 | - * @param Tree $tree |
|
90 | - * @param User $user |
|
91 | - * @param int $gen |
|
92 | - * @return Collection |
|
93 | - */ |
|
94 | - public function listAncestorFamiliesAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
95 | - { |
|
96 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
97 | - return DB::table('families') |
|
98 | - ->join('maj_sosa AS sosa_husb', function (JoinClause $join) use ($tree, $user): void { |
|
99 | - // Link to family husband |
|
100 | - $join->on('families.f_file', '=', 'sosa_husb.majs_gedcom_id') |
|
101 | - ->on('families.f_husb', '=', 'sosa_husb.majs_i_id') |
|
102 | - ->where('sosa_husb.majs_gedcom_id', '=', $tree->id()) |
|
103 | - ->where('sosa_husb.majs_user_id', '=', $user->id()); |
|
104 | - }) |
|
105 | - ->join('maj_sosa AS sosa_wife', function (JoinClause $join) use ($tree, $user): void { |
|
106 | - // Link to family husband |
|
107 | - $join->on('families.f_file', '=', 'sosa_wife.majs_gedcom_id') |
|
108 | - ->on('families.f_wife', '=', 'sosa_wife.majs_i_id') |
|
109 | - ->where('sosa_wife.majs_gedcom_id', '=', $tree->id()) |
|
110 | - ->where('sosa_wife.majs_user_id', '=', $user->id()); |
|
111 | - }) |
|
112 | - ->select(['sosa_husb.majs_sosa', 'families.f_id']) |
|
113 | - ->where('sosa_husb.majs_gen', '=', $gen) |
|
114 | - ->whereRaw($table_prefix . 'sosa_husb.majs_sosa + 1 = ' . $table_prefix . 'sosa_wife.majs_sosa') |
|
115 | - ->orderBy('sosa_husb.majs_sosa') |
|
116 | - ->get(); |
|
117 | - } |
|
86 | + /** |
|
87 | + * Return a list of the Sosa families at a given generation |
|
88 | + * |
|
89 | + * @param Tree $tree |
|
90 | + * @param User $user |
|
91 | + * @param int $gen |
|
92 | + * @return Collection |
|
93 | + */ |
|
94 | + public function listAncestorFamiliesAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
95 | + { |
|
96 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
97 | + return DB::table('families') |
|
98 | + ->join('maj_sosa AS sosa_husb', function (JoinClause $join) use ($tree, $user): void { |
|
99 | + // Link to family husband |
|
100 | + $join->on('families.f_file', '=', 'sosa_husb.majs_gedcom_id') |
|
101 | + ->on('families.f_husb', '=', 'sosa_husb.majs_i_id') |
|
102 | + ->where('sosa_husb.majs_gedcom_id', '=', $tree->id()) |
|
103 | + ->where('sosa_husb.majs_user_id', '=', $user->id()); |
|
104 | + }) |
|
105 | + ->join('maj_sosa AS sosa_wife', function (JoinClause $join) use ($tree, $user): void { |
|
106 | + // Link to family husband |
|
107 | + $join->on('families.f_file', '=', 'sosa_wife.majs_gedcom_id') |
|
108 | + ->on('families.f_wife', '=', 'sosa_wife.majs_i_id') |
|
109 | + ->where('sosa_wife.majs_gedcom_id', '=', $tree->id()) |
|
110 | + ->where('sosa_wife.majs_user_id', '=', $user->id()); |
|
111 | + }) |
|
112 | + ->select(['sosa_husb.majs_sosa', 'families.f_id']) |
|
113 | + ->where('sosa_husb.majs_gen', '=', $gen) |
|
114 | + ->whereRaw($table_prefix . 'sosa_husb.majs_sosa + 1 = ' . $table_prefix . 'sosa_wife.majs_sosa') |
|
115 | + ->orderBy('sosa_husb.majs_sosa') |
|
116 | + ->get(); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Return a list of Sosa ancestors missing at a given generation. |
|
121 | - * It includes the reference of either parent if it is known. |
|
122 | - * |
|
123 | - * @param Tree $tree |
|
124 | - * @param User $user |
|
125 | - * @param int $gen |
|
126 | - * @return Collection |
|
127 | - */ |
|
128 | - public function listMissingAncestorsAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
129 | - { |
|
130 | - if ($gen == 1) { |
|
131 | - return collect(); |
|
132 | - } |
|
119 | + /** |
|
120 | + * Return a list of Sosa ancestors missing at a given generation. |
|
121 | + * It includes the reference of either parent if it is known. |
|
122 | + * |
|
123 | + * @param Tree $tree |
|
124 | + * @param User $user |
|
125 | + * @param int $gen |
|
126 | + * @return Collection |
|
127 | + */ |
|
128 | + public function listMissingAncestorsAtGeneration(Tree $tree, User $user, int $gen): Collection |
|
129 | + { |
|
130 | + if ($gen == 1) { |
|
131 | + return collect(); |
|
132 | + } |
|
133 | 133 | |
134 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
135 | - return DB::table('maj_sosa AS sosa') |
|
136 | - ->select(['sosa.majs_i_id', 'sosa_fat.majs_i_id AS majs_fat_id', 'sosa_mot.majs_i_id AS majs_mot_id']) |
|
137 | - ->selectRaw('MIN(' . $table_prefix . 'sosa.majs_sosa) AS majs_sosa') |
|
138 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
139 | - // Link to sosa's father |
|
140 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
141 | - ->where('sosa_fat.majs_gedcom_id', '=', $tree->id()) |
|
142 | - ->where('sosa_fat.majs_user_id', '=', $user->id()); |
|
143 | - }) |
|
144 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
145 | - // Link to sosa's mother |
|
146 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
147 | - ->where('sosa_mot.majs_gedcom_id', '=', $tree->id()) |
|
148 | - ->where('sosa_mot.majs_user_id', '=', $user->id()); |
|
149 | - }) |
|
150 | - ->where('sosa.majs_gedcom_id', '=', $tree->id()) |
|
151 | - ->where('sosa.majs_user_id', '=', $user->id()) |
|
152 | - ->where('sosa.majs_gen', '=', $gen - 1) |
|
153 | - ->where(function (Builder $query): void { |
|
154 | - $query->whereNull('sosa_fat.majs_i_id') |
|
155 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
156 | - }) |
|
157 | - ->groupBy('sosa.majs_i_id', 'sosa_fat.majs_i_id', 'sosa_mot.majs_i_id') |
|
158 | - ->orderByRaw('MIN(' . $table_prefix . 'sosa.majs_sosa)') |
|
159 | - ->get(); |
|
160 | - } |
|
134 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
135 | + return DB::table('maj_sosa AS sosa') |
|
136 | + ->select(['sosa.majs_i_id', 'sosa_fat.majs_i_id AS majs_fat_id', 'sosa_mot.majs_i_id AS majs_mot_id']) |
|
137 | + ->selectRaw('MIN(' . $table_prefix . 'sosa.majs_sosa) AS majs_sosa') |
|
138 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
139 | + // Link to sosa's father |
|
140 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
141 | + ->where('sosa_fat.majs_gedcom_id', '=', $tree->id()) |
|
142 | + ->where('sosa_fat.majs_user_id', '=', $user->id()); |
|
143 | + }) |
|
144 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($tree, $user, $table_prefix): void { |
|
145 | + // Link to sosa's mother |
|
146 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
147 | + ->where('sosa_mot.majs_gedcom_id', '=', $tree->id()) |
|
148 | + ->where('sosa_mot.majs_user_id', '=', $user->id()); |
|
149 | + }) |
|
150 | + ->where('sosa.majs_gedcom_id', '=', $tree->id()) |
|
151 | + ->where('sosa.majs_user_id', '=', $user->id()) |
|
152 | + ->where('sosa.majs_gen', '=', $gen - 1) |
|
153 | + ->where(function (Builder $query): void { |
|
154 | + $query->whereNull('sosa_fat.majs_i_id') |
|
155 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
156 | + }) |
|
157 | + ->groupBy('sosa.majs_i_id', 'sosa_fat.majs_i_id', 'sosa_mot.majs_i_id') |
|
158 | + ->orderByRaw('MIN(' . $table_prefix . 'sosa.majs_sosa)') |
|
159 | + ->get(); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Remove all Sosa entries related to the gedcom file and user |
|
164 | - * |
|
165 | - * @param Tree $tree |
|
166 | - * @param User $user |
|
167 | - */ |
|
168 | - public function deleteAll(Tree $tree, User $user): void |
|
169 | - { |
|
170 | - DB::table('maj_sosa') |
|
171 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
172 | - ->where('majs_user_id', '=', $user->id()) |
|
173 | - ->delete(); |
|
174 | - } |
|
162 | + /** |
|
163 | + * Remove all Sosa entries related to the gedcom file and user |
|
164 | + * |
|
165 | + * @param Tree $tree |
|
166 | + * @param User $user |
|
167 | + */ |
|
168 | + public function deleteAll(Tree $tree, User $user): void |
|
169 | + { |
|
170 | + DB::table('maj_sosa') |
|
171 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
172 | + ->where('majs_user_id', '=', $user->id()) |
|
173 | + ->delete(); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * |
|
178 | - * @param Tree $tree |
|
179 | - * @param User $user |
|
180 | - * @param int $sosa |
|
181 | - */ |
|
182 | - public function deleteAncestorsFrom(Tree $tree, User $user, int $sosa): void |
|
183 | - { |
|
184 | - DB::table('maj_sosa') |
|
185 | - ->where('majs_gedcom_id', '=', $tree->id()) |
|
186 | - ->where('majs_user_id', '=', $user->id()) |
|
187 | - ->where('majs_sosa', '>=', $sosa) |
|
188 | - ->whereRaw( |
|
189 | - 'FLOOR(majs_sosa / (POW(2, (majs_gen - ?)))) = ?', |
|
190 | - [$this->generation($sosa), $sosa] |
|
191 | - ) |
|
192 | - ->delete(); |
|
193 | - } |
|
176 | + /** |
|
177 | + * |
|
178 | + * @param Tree $tree |
|
179 | + * @param User $user |
|
180 | + * @param int $sosa |
|
181 | + */ |
|
182 | + public function deleteAncestorsFrom(Tree $tree, User $user, int $sosa): void |
|
183 | + { |
|
184 | + DB::table('maj_sosa') |
|
185 | + ->where('majs_gedcom_id', '=', $tree->id()) |
|
186 | + ->where('majs_user_id', '=', $user->id()) |
|
187 | + ->where('majs_sosa', '>=', $sosa) |
|
188 | + ->whereRaw( |
|
189 | + 'FLOOR(majs_sosa / (POW(2, (majs_gen - ?)))) = ?', |
|
190 | + [$this->generation($sosa), $sosa] |
|
191 | + ) |
|
192 | + ->delete(); |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Insert (or update if already existing) a list of Sosa individuals |
|
197 | - * |
|
198 | - * @param Tree $tree |
|
199 | - * @param User $user |
|
200 | - * @param array $sosa_records |
|
201 | - */ |
|
202 | - public function insertOrUpdate(Tree $tree, User $user, array $sosa_records): void |
|
203 | - { |
|
204 | - $mass_update = DB::connection()->getDriverName() === 'mysql'; |
|
195 | + /** |
|
196 | + * Insert (or update if already existing) a list of Sosa individuals |
|
197 | + * |
|
198 | + * @param Tree $tree |
|
199 | + * @param User $user |
|
200 | + * @param array $sosa_records |
|
201 | + */ |
|
202 | + public function insertOrUpdate(Tree $tree, User $user, array $sosa_records): void |
|
203 | + { |
|
204 | + $mass_update = DB::connection()->getDriverName() === 'mysql'; |
|
205 | 205 | |
206 | - $bindings_placeholders = $bindings_values = []; |
|
207 | - foreach ($sosa_records as $i => $row) { |
|
208 | - $gen = $this->generation($row['sosa']); |
|
209 | - if ($gen <= self::MAX_DB_GENERATIONS) { |
|
210 | - if ($mass_update) { |
|
211 | - $bindings_placeholders[] = '(:tree_id' . $i . ', :user_id' . $i . ', :sosa' . $i . ',' . |
|
212 | - ' :indi_id' . $i . ', :gen' . $i . ',' . |
|
213 | - ' :byear' . $i . ', :byearest' . $i . ', :dyear' . $i . ', :dyearest' . $i . ')'; |
|
214 | - $bindings_values = array_merge( |
|
215 | - $bindings_values, |
|
216 | - [ |
|
217 | - 'tree_id' . $i => $tree->id(), |
|
218 | - 'user_id' . $i => $user->id(), |
|
219 | - 'sosa' . $i => $row['sosa'], |
|
220 | - 'indi_id' . $i => $row['indi'], |
|
221 | - 'gen' . $i => $gen, |
|
222 | - 'byear' . $i => $row['birth_year'], |
|
223 | - 'byearest' . $i => $row['birth_year_est'], |
|
224 | - 'dyear' . $i => $row['death_year'], |
|
225 | - 'dyearest' . $i => $row['death_year_est'] |
|
226 | - ] |
|
227 | - ); |
|
228 | - } else { |
|
229 | - DB::table('maj_sosa')->updateOrInsert( |
|
230 | - [ 'majs_gedcom_id' => $tree->id(), 'majs_user_id' => $user->id(), 'majs_sosa' => $row['sosa']], |
|
231 | - [ |
|
232 | - 'majs_i_id' => $row['indi'], |
|
233 | - 'majs_gen' => $gen, |
|
234 | - 'majs_birth_year' => $row['birth_year'], |
|
235 | - 'majs_birth_year_est' => $row['birth_year_est'], |
|
236 | - 'majs_death_year' => $row['death_year'], |
|
237 | - 'majs_death_year_est' => $row['death_year_est'] |
|
238 | - ] |
|
239 | - ); |
|
240 | - } |
|
241 | - } |
|
242 | - } |
|
206 | + $bindings_placeholders = $bindings_values = []; |
|
207 | + foreach ($sosa_records as $i => $row) { |
|
208 | + $gen = $this->generation($row['sosa']); |
|
209 | + if ($gen <= self::MAX_DB_GENERATIONS) { |
|
210 | + if ($mass_update) { |
|
211 | + $bindings_placeholders[] = '(:tree_id' . $i . ', :user_id' . $i . ', :sosa' . $i . ',' . |
|
212 | + ' :indi_id' . $i . ', :gen' . $i . ',' . |
|
213 | + ' :byear' . $i . ', :byearest' . $i . ', :dyear' . $i . ', :dyearest' . $i . ')'; |
|
214 | + $bindings_values = array_merge( |
|
215 | + $bindings_values, |
|
216 | + [ |
|
217 | + 'tree_id' . $i => $tree->id(), |
|
218 | + 'user_id' . $i => $user->id(), |
|
219 | + 'sosa' . $i => $row['sosa'], |
|
220 | + 'indi_id' . $i => $row['indi'], |
|
221 | + 'gen' . $i => $gen, |
|
222 | + 'byear' . $i => $row['birth_year'], |
|
223 | + 'byearest' . $i => $row['birth_year_est'], |
|
224 | + 'dyear' . $i => $row['death_year'], |
|
225 | + 'dyearest' . $i => $row['death_year_est'] |
|
226 | + ] |
|
227 | + ); |
|
228 | + } else { |
|
229 | + DB::table('maj_sosa')->updateOrInsert( |
|
230 | + [ 'majs_gedcom_id' => $tree->id(), 'majs_user_id' => $user->id(), 'majs_sosa' => $row['sosa']], |
|
231 | + [ |
|
232 | + 'majs_i_id' => $row['indi'], |
|
233 | + 'majs_gen' => $gen, |
|
234 | + 'majs_birth_year' => $row['birth_year'], |
|
235 | + 'majs_birth_year_est' => $row['birth_year_est'], |
|
236 | + 'majs_death_year' => $row['death_year'], |
|
237 | + 'majs_death_year_est' => $row['death_year_est'] |
|
238 | + ] |
|
239 | + ); |
|
240 | + } |
|
241 | + } |
|
242 | + } |
|
243 | 243 | |
244 | - if ($mass_update) { |
|
245 | - DB::connection()->statement( |
|
246 | - 'INSERT INTO `' . DB::connection()->getTablePrefix() . 'maj_sosa`' . |
|
247 | - ' (majs_gedcom_id, majs_user_id, majs_sosa,' . |
|
248 | - ' majs_i_id, majs_gen, majs_birth_year, majs_birth_year_est, majs_death_year, majs_death_year_est)' . |
|
249 | - ' VALUES ' . implode(',', $bindings_placeholders) . |
|
250 | - ' ON DUPLICATE KEY UPDATE majs_i_id = VALUES(majs_i_id), majs_gen = VALUES(majs_gen),' . |
|
251 | - ' majs_birth_year = VALUES(majs_birth_year), majs_birth_year_est = VALUES(majs_birth_year_est),' . |
|
252 | - ' majs_death_year = VALUES(majs_death_year), majs_death_year_est = VALUES(majs_death_year_est)', |
|
253 | - $bindings_values |
|
254 | - ); |
|
255 | - } |
|
256 | - } |
|
244 | + if ($mass_update) { |
|
245 | + DB::connection()->statement( |
|
246 | + 'INSERT INTO `' . DB::connection()->getTablePrefix() . 'maj_sosa`' . |
|
247 | + ' (majs_gedcom_id, majs_user_id, majs_sosa,' . |
|
248 | + ' majs_i_id, majs_gen, majs_birth_year, majs_birth_year_est, majs_death_year, majs_death_year_est)' . |
|
249 | + ' VALUES ' . implode(',', $bindings_placeholders) . |
|
250 | + ' ON DUPLICATE KEY UPDATE majs_i_id = VALUES(majs_i_id), majs_gen = VALUES(majs_gen),' . |
|
251 | + ' majs_birth_year = VALUES(majs_birth_year), majs_birth_year_est = VALUES(majs_birth_year_est),' . |
|
252 | + ' majs_death_year = VALUES(majs_death_year), majs_death_year_est = VALUES(majs_death_year_est)', |
|
253 | + $bindings_values |
|
254 | + ); |
|
255 | + } |
|
256 | + } |
|
257 | 257 | } |
@@ -21,79 +21,79 @@ |
||
21 | 21 | */ |
22 | 22 | class MissingAncestor |
23 | 23 | { |
24 | - /** |
|
25 | - * @var Individual $individual |
|
26 | - */ |
|
27 | - private $individual; |
|
24 | + /** |
|
25 | + * @var Individual $individual |
|
26 | + */ |
|
27 | + private $individual; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var int $sosa |
|
31 | - */ |
|
32 | - private $sosa; |
|
29 | + /** |
|
30 | + * @var int $sosa |
|
31 | + */ |
|
32 | + private $sosa; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var bool $missing_father |
|
36 | - */ |
|
37 | - private $missing_father; |
|
34 | + /** |
|
35 | + * @var bool $missing_father |
|
36 | + */ |
|
37 | + private $missing_father; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var bool $missing_mother |
|
41 | - */ |
|
42 | - private $missing_mother; |
|
39 | + /** |
|
40 | + * @var bool $missing_mother |
|
41 | + */ |
|
42 | + private $missing_mother; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Constructor for MissingAncestor data class |
|
46 | - * |
|
47 | - * @param Individual $ancestor |
|
48 | - * @param int $sosa |
|
49 | - * @param bool $missing_father |
|
50 | - * @param bool $missing_mother |
|
51 | - */ |
|
52 | - public function __construct(Individual $ancestor, int $sosa, bool $missing_father, bool $missing_mother) |
|
53 | - { |
|
54 | - $this->individual = $ancestor; |
|
55 | - $this->sosa = $sosa; |
|
56 | - $this->missing_father = $missing_father; |
|
57 | - $this->missing_mother = $missing_mother; |
|
58 | - } |
|
44 | + /** |
|
45 | + * Constructor for MissingAncestor data class |
|
46 | + * |
|
47 | + * @param Individual $ancestor |
|
48 | + * @param int $sosa |
|
49 | + * @param bool $missing_father |
|
50 | + * @param bool $missing_mother |
|
51 | + */ |
|
52 | + public function __construct(Individual $ancestor, int $sosa, bool $missing_father, bool $missing_mother) |
|
53 | + { |
|
54 | + $this->individual = $ancestor; |
|
55 | + $this->sosa = $sosa; |
|
56 | + $this->missing_father = $missing_father; |
|
57 | + $this->missing_mother = $missing_mother; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Reference individual of the row |
|
62 | - * |
|
63 | - * @return Individual |
|
64 | - */ |
|
65 | - public function individual(): Individual |
|
66 | - { |
|
67 | - return $this->individual; |
|
68 | - } |
|
60 | + /** |
|
61 | + * Reference individual of the row |
|
62 | + * |
|
63 | + * @return Individual |
|
64 | + */ |
|
65 | + public function individual(): Individual |
|
66 | + { |
|
67 | + return $this->individual; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Minimum sosa of the reference individual |
|
72 | - * |
|
73 | - * @return int |
|
74 | - */ |
|
75 | - public function sosa(): int |
|
76 | - { |
|
77 | - return $this->sosa; |
|
78 | - } |
|
70 | + /** |
|
71 | + * Minimum sosa of the reference individual |
|
72 | + * |
|
73 | + * @return int |
|
74 | + */ |
|
75 | + public function sosa(): int |
|
76 | + { |
|
77 | + return $this->sosa; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Indicate whether the reference individual does not have a father |
|
82 | - * |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function isFatherMissing(): bool |
|
86 | - { |
|
87 | - return $this->missing_father; |
|
88 | - } |
|
80 | + /** |
|
81 | + * Indicate whether the reference individual does not have a father |
|
82 | + * |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function isFatherMissing(): bool |
|
86 | + { |
|
87 | + return $this->missing_father; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Indicate whether the reference individual does not have a mother |
|
92 | - * |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - public function isMotherMissing(): bool |
|
96 | - { |
|
97 | - return $this->missing_mother; |
|
98 | - } |
|
90 | + /** |
|
91 | + * Indicate whether the reference individual does not have a mother |
|
92 | + * |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + public function isMotherMissing(): bool |
|
96 | + { |
|
97 | + return $this->missing_mother; |
|
98 | + } |
|
99 | 99 | } |
@@ -31,65 +31,65 @@ |
||
31 | 31 | */ |
32 | 32 | class TaskEditPage implements RequestHandlerInterface |
33 | 33 | { |
34 | - use ViewResponseTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var AdminTasksModule $module |
|
38 | - */ |
|
39 | - private $module; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var TaskScheduleService $taskschedules_service |
|
43 | - */ |
|
44 | - private $taskschedules_service; |
|
45 | - |
|
46 | - /** |
|
47 | - * Constructor for TaskEditPage Request Handler |
|
48 | - * |
|
49 | - * @param ModuleService $module_service |
|
50 | - * @param TaskScheduleService $taskschedules_service |
|
51 | - */ |
|
52 | - public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
53 | - { |
|
54 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
55 | - $this->taskschedules_service = $taskschedules_service; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritDoc} |
|
60 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
61 | - */ |
|
62 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
63 | - { |
|
64 | - $this->layout = 'layouts/administration'; |
|
65 | - |
|
66 | - if ($this->module === null) { |
|
67 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
68 | - } |
|
69 | - |
|
70 | - $task_sched_id = (int) $request->getAttribute('task'); |
|
71 | - $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
72 | - |
|
73 | - if ($task_schedule === null) { |
|
74 | - throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.')); |
|
75 | - } |
|
76 | - |
|
77 | - $task = $this->taskschedules_service->findTask($task_schedule->taskId()); |
|
78 | - |
|
79 | - if ($task === null) { |
|
80 | - throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.')); |
|
81 | - } |
|
82 | - |
|
83 | - /** @var TaskInterface&ConfigurableTaskInterface $task */ |
|
84 | - $has_task_config = $task instanceof ConfigurableTaskInterface; |
|
85 | - |
|
86 | - return $this->viewResponse($this->module->name() . '::admin/tasks-edit', [ |
|
87 | - 'module' => $this->module, |
|
88 | - 'title' => I18N::translate('Edit the administrative task') . ' - ' . $task->name(), |
|
89 | - 'task_schedule' => $task_schedule, |
|
90 | - 'task' => $task, |
|
91 | - 'has_task_config' => $has_task_config, |
|
92 | - 'task_config_view' => $has_task_config ? $task->configView($request) : '' |
|
93 | - ]); |
|
94 | - } |
|
34 | + use ViewResponseTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var AdminTasksModule $module |
|
38 | + */ |
|
39 | + private $module; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var TaskScheduleService $taskschedules_service |
|
43 | + */ |
|
44 | + private $taskschedules_service; |
|
45 | + |
|
46 | + /** |
|
47 | + * Constructor for TaskEditPage Request Handler |
|
48 | + * |
|
49 | + * @param ModuleService $module_service |
|
50 | + * @param TaskScheduleService $taskschedules_service |
|
51 | + */ |
|
52 | + public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
53 | + { |
|
54 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
55 | + $this->taskschedules_service = $taskschedules_service; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritDoc} |
|
60 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
61 | + */ |
|
62 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
63 | + { |
|
64 | + $this->layout = 'layouts/administration'; |
|
65 | + |
|
66 | + if ($this->module === null) { |
|
67 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
68 | + } |
|
69 | + |
|
70 | + $task_sched_id = (int) $request->getAttribute('task'); |
|
71 | + $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
72 | + |
|
73 | + if ($task_schedule === null) { |
|
74 | + throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.')); |
|
75 | + } |
|
76 | + |
|
77 | + $task = $this->taskschedules_service->findTask($task_schedule->taskId()); |
|
78 | + |
|
79 | + if ($task === null) { |
|
80 | + throw new HttpNotFoundException(I18N::translate('The Task schedule could not be found.')); |
|
81 | + } |
|
82 | + |
|
83 | + /** @var TaskInterface&ConfigurableTaskInterface $task */ |
|
84 | + $has_task_config = $task instanceof ConfigurableTaskInterface; |
|
85 | + |
|
86 | + return $this->viewResponse($this->module->name() . '::admin/tasks-edit', [ |
|
87 | + 'module' => $this->module, |
|
88 | + 'title' => I18N::translate('Edit the administrative task') . ' - ' . $task->name(), |
|
89 | + 'task_schedule' => $task_schedule, |
|
90 | + 'task' => $task, |
|
91 | + 'has_task_config' => $has_task_config, |
|
92 | + 'task_config_view' => $has_task_config ? $task->configView($request) : '' |
|
93 | + ]); |
|
94 | + } |
|
95 | 95 | } |