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