@@ -41,127 +41,127 @@ |
||
| 41 | 41 | * Allow for tasks to be run on a (nearly-)regular schedule |
| 42 | 42 | */ |
| 43 | 43 | class AdminTasksModule extends AbstractModule implements |
| 44 | - ModuleMyArtJaubInterface, |
|
| 45 | - ModuleConfigInterface, |
|
| 46 | - ModuleGlobalInterface, |
|
| 47 | - ModuleTasksProviderInterface |
|
| 44 | + ModuleMyArtJaubInterface, |
|
| 45 | + ModuleConfigInterface, |
|
| 46 | + ModuleGlobalInterface, |
|
| 47 | + ModuleTasksProviderInterface |
|
| 48 | 48 | { |
| 49 | - use ModuleMyArtJaubTrait { |
|
| 50 | - boot as traitBoot; |
|
| 51 | - } |
|
| 52 | - use ModuleConfigTrait; |
|
| 53 | - use ModuleGlobalTrait; |
|
| 54 | - |
|
| 55 | - //How to update the database schema for this module |
|
| 56 | - private const SCHEMA_TARGET_VERSION = 2; |
|
| 57 | - private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
|
| 58 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * {@inheritDoc} |
|
| 62 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 63 | - */ |
|
| 64 | - public function title(): string |
|
| 65 | - { |
|
| 66 | - return I18N::translate('Administration Tasks'); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * {@inheritDoc} |
|
| 71 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 72 | - */ |
|
| 73 | - public function description(): string |
|
| 74 | - { |
|
| 75 | - return I18N::translate('Manage and run nearly-scheduled administration tasks.'); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * {@inheritDoc} |
|
| 80 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::boot() |
|
| 81 | - */ |
|
| 82 | - public function boot(): void |
|
| 83 | - { |
|
| 84 | - $this->traitBoot(); |
|
| 85 | - app(MigrationService::class)->updateSchema( |
|
| 86 | - self::SCHEMA_MIGRATION_PREFIX, |
|
| 87 | - self::SCHEMA_SETTING_NAME, |
|
| 88 | - self::SCHEMA_TARGET_VERSION |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritDoc} |
|
| 94 | - * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes() |
|
| 95 | - */ |
|
| 96 | - public function loadRoutes(Map $router): void |
|
| 97 | - { |
|
| 98 | - $router->attach('', '', static function (Map $router): void { |
|
| 99 | - |
|
| 100 | - $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 101 | - $router->tokens(['enable' => '[01]']); |
|
| 102 | - |
|
| 103 | - $router->attach('', '/admin', static function (Map $router): void { |
|
| 104 | - |
|
| 105 | - $router->extras([ |
|
| 106 | - 'middleware' => [ |
|
| 107 | - AuthAdministrator::class, |
|
| 108 | - ], |
|
| 109 | - ]); |
|
| 110 | - $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
|
| 111 | - |
|
| 112 | - $router->attach('', '/tasks', static function (Map $router): void { |
|
| 113 | - |
|
| 114 | - $router->get(TasksList::class, '', TasksList::class); |
|
| 115 | - $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
|
| 116 | - $router->post(TaskEditAction::class, '/{task}', TaskEditAction::class); |
|
| 117 | - $router->get(TaskStatusAction::class, '/{task}/status/{enable}', TaskStatusAction::class); |
|
| 118 | - }); |
|
| 119 | - }); |
|
| 120 | - |
|
| 121 | - $router->get(TaskTrigger::class, '/trigger{/task}', TaskTrigger::class) |
|
| 122 | - ->allows(RequestMethodInterface::METHOD_POST); |
|
| 123 | - |
|
| 124 | - $router->post(TokenGenerate::class, '/token', TokenGenerate::class) |
|
| 125 | - ->extras(['middleware' => [AuthAdministrator::class]]); |
|
| 126 | - }); |
|
| 127 | - }); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * {@inheritDoc} |
|
| 132 | - * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersion() |
|
| 133 | - */ |
|
| 134 | - public function customModuleVersion(): string |
|
| 135 | - { |
|
| 136 | - return '2.1.0-v.1'; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * {@inheritDoc} |
|
| 141 | - * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink() |
|
| 142 | - */ |
|
| 143 | - public function getConfigLink(): string |
|
| 144 | - { |
|
| 145 | - return route(AdminConfigPage::class); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * {@inheritDoc} |
|
| 150 | - * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::bodyContent() |
|
| 151 | - */ |
|
| 152 | - public function bodyContent(): string |
|
| 153 | - { |
|
| 154 | - return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * {@inheritDoc} |
|
| 159 | - * @see ModuleTasksProviderInterface::listTasks() |
|
| 160 | - */ |
|
| 161 | - public function listTasks(): array |
|
| 162 | - { |
|
| 163 | - return [ |
|
| 164 | - 'maj-healthcheck' => HealthCheckEmailTask::class |
|
| 165 | - ]; |
|
| 166 | - } |
|
| 49 | + use ModuleMyArtJaubTrait { |
|
| 50 | + boot as traitBoot; |
|
| 51 | + } |
|
| 52 | + use ModuleConfigTrait; |
|
| 53 | + use ModuleGlobalTrait; |
|
| 54 | + |
|
| 55 | + //How to update the database schema for this module |
|
| 56 | + private const SCHEMA_TARGET_VERSION = 2; |
|
| 57 | + private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
|
| 58 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * {@inheritDoc} |
|
| 62 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 63 | + */ |
|
| 64 | + public function title(): string |
|
| 65 | + { |
|
| 66 | + return I18N::translate('Administration Tasks'); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * {@inheritDoc} |
|
| 71 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 72 | + */ |
|
| 73 | + public function description(): string |
|
| 74 | + { |
|
| 75 | + return I18N::translate('Manage and run nearly-scheduled administration tasks.'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * {@inheritDoc} |
|
| 80 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::boot() |
|
| 81 | + */ |
|
| 82 | + public function boot(): void |
|
| 83 | + { |
|
| 84 | + $this->traitBoot(); |
|
| 85 | + app(MigrationService::class)->updateSchema( |
|
| 86 | + self::SCHEMA_MIGRATION_PREFIX, |
|
| 87 | + self::SCHEMA_SETTING_NAME, |
|
| 88 | + self::SCHEMA_TARGET_VERSION |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritDoc} |
|
| 94 | + * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes() |
|
| 95 | + */ |
|
| 96 | + public function loadRoutes(Map $router): void |
|
| 97 | + { |
|
| 98 | + $router->attach('', '', static function (Map $router): void { |
|
| 99 | + |
|
| 100 | + $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 101 | + $router->tokens(['enable' => '[01]']); |
|
| 102 | + |
|
| 103 | + $router->attach('', '/admin', static function (Map $router): void { |
|
| 104 | + |
|
| 105 | + $router->extras([ |
|
| 106 | + 'middleware' => [ |
|
| 107 | + AuthAdministrator::class, |
|
| 108 | + ], |
|
| 109 | + ]); |
|
| 110 | + $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
|
| 111 | + |
|
| 112 | + $router->attach('', '/tasks', static function (Map $router): void { |
|
| 113 | + |
|
| 114 | + $router->get(TasksList::class, '', TasksList::class); |
|
| 115 | + $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
|
| 116 | + $router->post(TaskEditAction::class, '/{task}', TaskEditAction::class); |
|
| 117 | + $router->get(TaskStatusAction::class, '/{task}/status/{enable}', TaskStatusAction::class); |
|
| 118 | + }); |
|
| 119 | + }); |
|
| 120 | + |
|
| 121 | + $router->get(TaskTrigger::class, '/trigger{/task}', TaskTrigger::class) |
|
| 122 | + ->allows(RequestMethodInterface::METHOD_POST); |
|
| 123 | + |
|
| 124 | + $router->post(TokenGenerate::class, '/token', TokenGenerate::class) |
|
| 125 | + ->extras(['middleware' => [AuthAdministrator::class]]); |
|
| 126 | + }); |
|
| 127 | + }); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * {@inheritDoc} |
|
| 132 | + * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersion() |
|
| 133 | + */ |
|
| 134 | + public function customModuleVersion(): string |
|
| 135 | + { |
|
| 136 | + return '2.1.0-v.1'; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * {@inheritDoc} |
|
| 141 | + * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink() |
|
| 142 | + */ |
|
| 143 | + public function getConfigLink(): string |
|
| 144 | + { |
|
| 145 | + return route(AdminConfigPage::class); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * {@inheritDoc} |
|
| 150 | + * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::bodyContent() |
|
| 151 | + */ |
|
| 152 | + public function bodyContent(): string |
|
| 153 | + { |
|
| 154 | + return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * {@inheritDoc} |
|
| 159 | + * @see ModuleTasksProviderInterface::listTasks() |
|
| 160 | + */ |
|
| 161 | + public function listTasks(): array |
|
| 162 | + { |
|
| 163 | + return [ |
|
| 164 | + 'maj-healthcheck' => HealthCheckEmailTask::class |
|
| 165 | + ]; |
|
| 166 | + } |
|
| 167 | 167 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | //How to update the database schema for this module |
| 56 | 56 | private const SCHEMA_TARGET_VERSION = 2; |
| 57 | 57 | private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
| 58 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 58 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__.'\Schema'; |
|
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * {@inheritDoc} |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function loadRoutes(Map $router): void |
| 97 | 97 | { |
| 98 | - $router->attach('', '', static function (Map $router): void { |
|
| 98 | + $router->attach('', '', static function(Map $router): void { |
|
| 99 | 99 | |
| 100 | - $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 100 | + $router->attach('', '/module-maj/admintasks', static function(Map $router): void { |
|
| 101 | 101 | $router->tokens(['enable' => '[01]']); |
| 102 | 102 | |
| 103 | - $router->attach('', '/admin', static function (Map $router): void { |
|
| 103 | + $router->attach('', '/admin', static function(Map $router): void { |
|
| 104 | 104 | |
| 105 | 105 | $router->extras([ |
| 106 | 106 | 'middleware' => [ |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | ]); |
| 110 | 110 | $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
| 111 | 111 | |
| 112 | - $router->attach('', '/tasks', static function (Map $router): void { |
|
| 112 | + $router->attach('', '/tasks', static function(Map $router): void { |
|
| 113 | 113 | |
| 114 | 114 | $router->get(TasksList::class, '', TasksList::class); |
| 115 | 115 | $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function bodyContent(): string |
| 153 | 153 | { |
| 154 | - return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 154 | + return view($this->name().'::snippet', ['url' => route(TaskTrigger::class)]); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -39,165 +39,165 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class HealthCheckEmailTask implements TaskInterface, ConfigurableTaskInterface |
| 41 | 41 | { |
| 42 | - /** |
|
| 43 | - * Name of the Tree preference to check if the task is enabled for that tree |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
| 47 | - |
|
| 48 | - private ?AdminTasksModule $module; |
|
| 49 | - private HealthCheckService $healthcheck_service; |
|
| 50 | - private EmailService $email_service; |
|
| 51 | - private UserService $user_service; |
|
| 52 | - private TreeService $tree_service; |
|
| 53 | - private UpgradeService $upgrade_service; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Constructor for HealthCheckTask |
|
| 57 | - * |
|
| 58 | - * @param ModuleService $module_service |
|
| 59 | - * @param HealthCheckService $healthcheck_service |
|
| 60 | - * @param EmailService $email_service |
|
| 61 | - * @param UserService $user_service |
|
| 62 | - * @param TreeService $tree_service |
|
| 63 | - * @param UpgradeService $upgrade_service |
|
| 64 | - */ |
|
| 65 | - public function __construct( |
|
| 66 | - ModuleService $module_service, |
|
| 67 | - HealthCheckService $healthcheck_service, |
|
| 68 | - EmailService $email_service, |
|
| 69 | - UserService $user_service, |
|
| 70 | - TreeService $tree_service, |
|
| 71 | - UpgradeService $upgrade_service |
|
| 72 | - ) { |
|
| 73 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 74 | - $this->healthcheck_service = $healthcheck_service; |
|
| 75 | - $this->email_service = $email_service; |
|
| 76 | - $this->user_service = $user_service; |
|
| 77 | - $this->tree_service = $tree_service; |
|
| 78 | - $this->upgrade_service = $upgrade_service; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * {@inheritDoc} |
|
| 84 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
| 85 | - */ |
|
| 86 | - public function name(): string |
|
| 87 | - { |
|
| 88 | - return I18N::translate('Healthcheck Email'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * {@inheritDoc} |
|
| 93 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
| 94 | - */ |
|
| 95 | - public function defaultFrequency(): int |
|
| 96 | - { |
|
| 97 | - return 10080; // = 1 week = 7 * 24 * 60 min |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * {@inheritDoc} |
|
| 102 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
| 103 | - */ |
|
| 104 | - public function run(TaskSchedule $task_schedule): bool |
|
| 105 | - { |
|
| 106 | - if ($this->module === null) { |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $res = true; |
|
| 111 | - |
|
| 112 | - // Compute the number of days to compute |
|
| 113 | - $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
| 114 | - //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 115 | - $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
| 116 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 117 | - |
|
| 118 | - $view_params_site = [ |
|
| 119 | - 'nb_days' => $nb_days, |
|
| 120 | - 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
| 121 | - 'latest_version' => $this->upgrade_service->latestVersion(), |
|
| 122 | - 'download_url' => $this->upgrade_service->downloadUrl(), |
|
| 123 | - 'all_users' => $this->user_service->all(), |
|
| 124 | - 'unapproved' => $this->user_service->unapproved(), |
|
| 125 | - 'unverified' => $this->user_service->unverified(), |
|
| 126 | - ]; |
|
| 127 | - |
|
| 128 | - foreach ($this->tree_service->all() as $tree) { |
|
| 129 | - /** @var Tree $tree */ |
|
| 130 | - |
|
| 131 | - if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 136 | - if ($webmaster === null) { |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - I18N::init($webmaster->getPreference('language')); |
|
| 140 | - |
|
| 141 | - $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
| 142 | - $nb_errors = $error_logs->sum('nblogs'); |
|
| 143 | - |
|
| 144 | - $view_params = array_merge($view_params_site, [ |
|
| 145 | - 'tree' => $tree, |
|
| 146 | - 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
| 147 | - 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
| 148 | - 'error_logs' => $error_logs, |
|
| 149 | - 'nb_errors' => $nb_errors |
|
| 150 | - ]); |
|
| 151 | - |
|
| 152 | - $res = $res && $this->email_service->send( |
|
| 153 | - new TreeUser($tree), |
|
| 154 | - $webmaster, |
|
| 155 | - new NoReplyUser(), |
|
| 156 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 157 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 158 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $res; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * {@inheritDoc} |
|
| 167 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
| 168 | - */ |
|
| 169 | - public function configView(ServerRequestInterface $request): string |
|
| 170 | - { |
|
| 171 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 172 | - 'all_trees' => $this->tree_service->all() |
|
| 173 | - ]); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * {@inheritDoc} |
|
| 178 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
| 179 | - */ |
|
| 180 | - public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
| 181 | - { |
|
| 182 | - try { |
|
| 183 | - $params = (array) $request->getParsedBody(); |
|
| 184 | - |
|
| 185 | - foreach ($this->tree_service->all() as $tree) { |
|
| 186 | - if (Auth::isManager($tree)) { |
|
| 187 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 188 | - $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - return true; |
|
| 192 | - } catch (Throwable $ex) { |
|
| 193 | - Log::addErrorLog( |
|
| 194 | - sprintf( |
|
| 195 | - 'Error while updating the Task schedule "%s". Exception: %s', |
|
| 196 | - $task_schedule->id(), |
|
| 197 | - $ex->getMessage() |
|
| 198 | - ) |
|
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 42 | + /** |
|
| 43 | + * Name of the Tree preference to check if the task is enabled for that tree |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
| 47 | + |
|
| 48 | + private ?AdminTasksModule $module; |
|
| 49 | + private HealthCheckService $healthcheck_service; |
|
| 50 | + private EmailService $email_service; |
|
| 51 | + private UserService $user_service; |
|
| 52 | + private TreeService $tree_service; |
|
| 53 | + private UpgradeService $upgrade_service; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Constructor for HealthCheckTask |
|
| 57 | + * |
|
| 58 | + * @param ModuleService $module_service |
|
| 59 | + * @param HealthCheckService $healthcheck_service |
|
| 60 | + * @param EmailService $email_service |
|
| 61 | + * @param UserService $user_service |
|
| 62 | + * @param TreeService $tree_service |
|
| 63 | + * @param UpgradeService $upgrade_service |
|
| 64 | + */ |
|
| 65 | + public function __construct( |
|
| 66 | + ModuleService $module_service, |
|
| 67 | + HealthCheckService $healthcheck_service, |
|
| 68 | + EmailService $email_service, |
|
| 69 | + UserService $user_service, |
|
| 70 | + TreeService $tree_service, |
|
| 71 | + UpgradeService $upgrade_service |
|
| 72 | + ) { |
|
| 73 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 74 | + $this->healthcheck_service = $healthcheck_service; |
|
| 75 | + $this->email_service = $email_service; |
|
| 76 | + $this->user_service = $user_service; |
|
| 77 | + $this->tree_service = $tree_service; |
|
| 78 | + $this->upgrade_service = $upgrade_service; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * {@inheritDoc} |
|
| 84 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
| 85 | + */ |
|
| 86 | + public function name(): string |
|
| 87 | + { |
|
| 88 | + return I18N::translate('Healthcheck Email'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * {@inheritDoc} |
|
| 93 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
| 94 | + */ |
|
| 95 | + public function defaultFrequency(): int |
|
| 96 | + { |
|
| 97 | + return 10080; // = 1 week = 7 * 24 * 60 min |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * {@inheritDoc} |
|
| 102 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
| 103 | + */ |
|
| 104 | + public function run(TaskSchedule $task_schedule): bool |
|
| 105 | + { |
|
| 106 | + if ($this->module === null) { |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $res = true; |
|
| 111 | + |
|
| 112 | + // Compute the number of days to compute |
|
| 113 | + $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
| 114 | + //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 115 | + $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
| 116 | + $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 117 | + |
|
| 118 | + $view_params_site = [ |
|
| 119 | + 'nb_days' => $nb_days, |
|
| 120 | + 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
| 121 | + 'latest_version' => $this->upgrade_service->latestVersion(), |
|
| 122 | + 'download_url' => $this->upgrade_service->downloadUrl(), |
|
| 123 | + 'all_users' => $this->user_service->all(), |
|
| 124 | + 'unapproved' => $this->user_service->unapproved(), |
|
| 125 | + 'unverified' => $this->user_service->unverified(), |
|
| 126 | + ]; |
|
| 127 | + |
|
| 128 | + foreach ($this->tree_service->all() as $tree) { |
|
| 129 | + /** @var Tree $tree */ |
|
| 130 | + |
|
| 131 | + if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 136 | + if ($webmaster === null) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + I18N::init($webmaster->getPreference('language')); |
|
| 140 | + |
|
| 141 | + $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
| 142 | + $nb_errors = $error_logs->sum('nblogs'); |
|
| 143 | + |
|
| 144 | + $view_params = array_merge($view_params_site, [ |
|
| 145 | + 'tree' => $tree, |
|
| 146 | + 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
| 147 | + 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
| 148 | + 'error_logs' => $error_logs, |
|
| 149 | + 'nb_errors' => $nb_errors |
|
| 150 | + ]); |
|
| 151 | + |
|
| 152 | + $res = $res && $this->email_service->send( |
|
| 153 | + new TreeUser($tree), |
|
| 154 | + $webmaster, |
|
| 155 | + new NoReplyUser(), |
|
| 156 | + I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 157 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 158 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $res; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * {@inheritDoc} |
|
| 167 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
| 168 | + */ |
|
| 169 | + public function configView(ServerRequestInterface $request): string |
|
| 170 | + { |
|
| 171 | + return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 172 | + 'all_trees' => $this->tree_service->all() |
|
| 173 | + ]); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * {@inheritDoc} |
|
| 178 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
| 179 | + */ |
|
| 180 | + public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
| 181 | + { |
|
| 182 | + try { |
|
| 183 | + $params = (array) $request->getParsedBody(); |
|
| 184 | + |
|
| 185 | + foreach ($this->tree_service->all() as $tree) { |
|
| 186 | + if (Auth::isManager($tree)) { |
|
| 187 | + $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 188 | + $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + return true; |
|
| 192 | + } catch (Throwable $ex) { |
|
| 193 | + Log::addErrorLog( |
|
| 194 | + sprintf( |
|
| 195 | + 'Error while updating the Task schedule "%s". Exception: %s', |
|
| 196 | + $task_schedule->id(), |
|
| 197 | + $ex->getMessage() |
|
| 198 | + ) |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | 203 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
| 137 | 137 | //@phpcs:ignore Generic.Files.LineLength.TooLong |
| 138 | 138 | $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
| 139 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 139 | + $nb_days = (int)$interval->ceilDay()->totalDays; |
|
| 140 | 140 | |
| 141 | 141 | $view_params_site = [ |
| 142 | 142 | 'nb_days' => $nb_days, |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 158 | + $webmaster = $this->user_service->find((int)$tree->getPreference('WEBMASTER_USER_ID')); |
|
| 159 | 159 | if ($webmaster === null) { |
| 160 | 160 | continue; |
| 161 | 161 | } |
@@ -176,9 +176,9 @@ discard block |
||
| 176 | 176 | new TreeUser($tree), |
| 177 | 177 | $webmaster, |
| 178 | 178 | new NoReplyUser(), |
| 179 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 180 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 181 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 179 | + I18N::translate('Health Check Report').' - '.I18N::translate('Tree %s', $tree->name()), |
|
| 180 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 181 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 182 | 182 | ); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | */ |
| 192 | 192 | public function configView(ServerRequestInterface $request): string |
| 193 | 193 | { |
| 194 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 194 | + return $this->module === null ? '' : view($this->module->name().'::tasks/healthcheck/config', [ |
|
| 195 | 195 | 'all_trees' => $this->tree_service->all() |
| 196 | 196 | ]); |
| 197 | 197 | } |
@@ -203,11 +203,11 @@ discard block |
||
| 203 | 203 | public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
| 204 | 204 | { |
| 205 | 205 | try { |
| 206 | - $params = (array) $request->getParsedBody(); |
|
| 206 | + $params = (array)$request->getParsedBody(); |
|
| 207 | 207 | |
| 208 | 208 | foreach ($this->tree_service->all() as $tree) { |
| 209 | 209 | if (Auth::isManager($tree)) { |
| 210 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 210 | + $tree_enabled = (bool)($params['HEALTHCHECK_ENABLED_'.$tree->id()] ?? false); |
|
| 211 | 211 | $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -28,15 +28,15 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | function hook(string $hook_interface, callable $apply, $default = null) |
| 30 | 30 | { |
| 31 | - try { |
|
| 32 | - $hook_collector = app(HookServiceInterface::class)->use($hook_interface); |
|
| 33 | - if ($hook_collector !== null) { |
|
| 34 | - return $apply($hook_collector); |
|
| 35 | - } |
|
| 36 | - } catch (BindingResolutionException $ex) { |
|
| 37 | - } |
|
| 31 | + try { |
|
| 32 | + $hook_collector = app(HookServiceInterface::class)->use($hook_interface); |
|
| 33 | + if ($hook_collector !== null) { |
|
| 34 | + return $apply($hook_collector); |
|
| 35 | + } |
|
| 36 | + } catch (BindingResolutionException $ex) { |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return $default; |
|
| 39 | + return $default; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -48,5 +48,5 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | function columnIndex(int $initial_index, Collection $new_column_indexes): int |
| 50 | 50 | { |
| 51 | - return $initial_index + $new_column_indexes->filter(fn(int $i) => $i <= $initial_index)->count(); |
|
| 51 | + return $initial_index + $new_column_indexes->filter(fn(int $i) => $i <= $initial_index)->count(); |
|
| 52 | 52 | } |
@@ -30,43 +30,43 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class AuthTreePreference implements MiddlewareInterface |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * {@inheritDoc} |
|
| 35 | - * @see \Psr\Http\Server\MiddlewareInterface::process() |
|
| 36 | - */ |
|
| 37 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 38 | - { |
|
| 39 | - $tree = $request->getAttribute('tree'); |
|
| 40 | - assert($tree instanceof Tree); |
|
| 41 | - /** @var Tree $tree */ |
|
| 33 | + /** |
|
| 34 | + * {@inheritDoc} |
|
| 35 | + * @see \Psr\Http\Server\MiddlewareInterface::process() |
|
| 36 | + */ |
|
| 37 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 38 | + { |
|
| 39 | + $tree = $request->getAttribute('tree'); |
|
| 40 | + assert($tree instanceof Tree); |
|
| 41 | + /** @var Tree $tree */ |
|
| 42 | 42 | |
| 43 | - $route = $request->getAttribute('route'); |
|
| 44 | - assert($route instanceof \Aura\Router\Route); |
|
| 45 | - /** @var \Aura\Router\Route $route */ |
|
| 43 | + $route = $request->getAttribute('route'); |
|
| 44 | + assert($route instanceof \Aura\Router\Route); |
|
| 45 | + /** @var \Aura\Router\Route $route */ |
|
| 46 | 46 | |
| 47 | - $user = $request->getAttribute('user'); |
|
| 47 | + $user = $request->getAttribute('user'); |
|
| 48 | 48 | |
| 49 | - $permission_preference = $route->extras['permission_preference'] ?? ''; |
|
| 50 | - $permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference); |
|
| 49 | + $permission_preference = $route->extras['permission_preference'] ?? ''; |
|
| 50 | + $permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference); |
|
| 51 | 51 | |
| 52 | - // Permissions are configured |
|
| 53 | - if (is_numeric($permission_level)) { |
|
| 54 | - // Logged in with the correct role? |
|
| 55 | - if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 56 | - return $handler->handle($request); |
|
| 57 | - } |
|
| 52 | + // Permissions are configured |
|
| 53 | + if (is_numeric($permission_level)) { |
|
| 54 | + // Logged in with the correct role? |
|
| 55 | + if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 56 | + return $handler->handle($request); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Logged in, but without the correct role? |
|
| 60 | - if ($user instanceof User) { |
|
| 61 | - throw new HttpAccessDeniedException(); |
|
| 62 | - } |
|
| 63 | - } |
|
| 59 | + // Logged in, but without the correct role? |
|
| 60 | + if ($user instanceof User) { |
|
| 61 | + throw new HttpAccessDeniedException(); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - // Permissions no configured, or not logged in |
|
| 66 | - if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
|
| 67 | - throw new HttpAccessDeniedException(); |
|
| 68 | - } |
|
| 65 | + // Permissions no configured, or not logged in |
|
| 66 | + if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
|
| 67 | + throw new HttpAccessDeniedException(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return redirect(route(LoginPage::class, ['tree' => $tree->name(), 'url' => $request->getUri()])); |
|
| 71 | - } |
|
| 70 | + return redirect(route(LoginPage::class, ['tree' => $tree->name(), 'url' => $request->getUri()])); |
|
| 71 | + } |
|
| 72 | 72 | } |
@@ -52,7 +52,7 @@ |
||
| 52 | 52 | // Permissions are configured |
| 53 | 53 | if (is_numeric($permission_level)) { |
| 54 | 54 | // Logged in with the correct role? |
| 55 | - if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 55 | + if (Auth::accessLevel($tree, $user) <= (int)$permission_level) { |
|
| 56 | 56 | return $handler->handle($request); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | */ |
| 58 | 58 | public function name(): string |
| 59 | 59 | { |
| 60 | - return $this->module->name() . '-' . |
|
| 60 | + return $this->module->name().'-'. |
|
| 61 | 61 | mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
| 62 | 62 | } |
| 63 | 63 | |
@@ -28,78 +28,78 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | abstract class AbstractHookCollector implements HookCollectorInterface, HookInterface |
| 30 | 30 | { |
| 31 | - /** @var Collection<THook> $hooks */ |
|
| 32 | - protected Collection $hooks; |
|
| 31 | + /** @var Collection<THook> $hooks */ |
|
| 32 | + protected Collection $hooks; |
|
| 33 | 33 | |
| 34 | - private ModuleInterface $module; |
|
| 34 | + private ModuleInterface $module; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Constructor for AbstractHookCollector |
|
| 38 | - * |
|
| 39 | - * @param ModuleInterface $module |
|
| 40 | - */ |
|
| 41 | - public function __construct(ModuleInterface $module) |
|
| 42 | - { |
|
| 43 | - $this->hooks = new Collection(); |
|
| 44 | - $this->module = $module; |
|
| 45 | - } |
|
| 36 | + /** |
|
| 37 | + * Constructor for AbstractHookCollector |
|
| 38 | + * |
|
| 39 | + * @param ModuleInterface $module |
|
| 40 | + */ |
|
| 41 | + public function __construct(ModuleInterface $module) |
|
| 42 | + { |
|
| 43 | + $this->hooks = new Collection(); |
|
| 44 | + $this->module = $module; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * {@inheritDoc} |
|
| 49 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
| 50 | - */ |
|
| 51 | - public function module(): ModuleInterface |
|
| 52 | - { |
|
| 53 | - return $this->module; |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * {@inheritDoc} |
|
| 49 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
| 50 | + */ |
|
| 51 | + public function module(): ModuleInterface |
|
| 52 | + { |
|
| 53 | + return $this->module; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * {@inheritDoc} |
|
| 58 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name() |
|
| 59 | - */ |
|
| 60 | - public function name(): string |
|
| 61 | - { |
|
| 62 | - return $this->module->name() . '-' . |
|
| 63 | - mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * {@inheritDoc} |
|
| 58 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name() |
|
| 59 | + */ |
|
| 60 | + public function name(): string |
|
| 61 | + { |
|
| 62 | + return $this->module->name() . '-' . |
|
| 63 | + mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * {@inheritDoc} |
|
| 68 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title() |
|
| 69 | - */ |
|
| 70 | - abstract public function title(): string; |
|
| 66 | + /** |
|
| 67 | + * {@inheritDoc} |
|
| 68 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title() |
|
| 69 | + */ |
|
| 70 | + abstract public function title(): string; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * {@inheritDoc} |
|
| 74 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description() |
|
| 75 | - */ |
|
| 76 | - abstract public function description(): string; |
|
| 72 | + /** |
|
| 73 | + * {@inheritDoc} |
|
| 74 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description() |
|
| 75 | + */ |
|
| 76 | + abstract public function description(): string; |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * {@inheritDoc} |
|
| 80 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface() |
|
| 81 | - */ |
|
| 82 | - abstract public function hookInterface(): string; |
|
| 78 | + /** |
|
| 79 | + * {@inheritDoc} |
|
| 80 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface() |
|
| 81 | + */ |
|
| 82 | + abstract public function hookInterface(): string; |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * {@inheritDoc} |
|
| 86 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register() |
|
| 87 | - */ |
|
| 88 | - public function register(HookInterface $hook_instance, int $order): void |
|
| 89 | - { |
|
| 90 | - if ($this->hooks->has($order)) { |
|
| 91 | - $this->hooks->splice($order + 1, 0, [$hook_instance]); |
|
| 92 | - } else { |
|
| 93 | - $this->hooks->put($order, $hook_instance); |
|
| 94 | - } |
|
| 95 | - } |
|
| 84 | + /** |
|
| 85 | + * {@inheritDoc} |
|
| 86 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register() |
|
| 87 | + */ |
|
| 88 | + public function register(HookInterface $hook_instance, int $order): void |
|
| 89 | + { |
|
| 90 | + if ($this->hooks->has($order)) { |
|
| 91 | + $this->hooks->splice($order + 1, 0, [$hook_instance]); |
|
| 92 | + } else { |
|
| 93 | + $this->hooks->put($order, $hook_instance); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * {@inheritDoc} |
|
| 99 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks() |
|
| 100 | - */ |
|
| 101 | - public function hooks(): Collection |
|
| 102 | - { |
|
| 103 | - return $this->hooks->sortKeys(); |
|
| 104 | - } |
|
| 97 | + /** |
|
| 98 | + * {@inheritDoc} |
|
| 99 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks() |
|
| 100 | + */ |
|
| 101 | + public function hooks(): Collection |
|
| 102 | + { |
|
| 103 | + return $this->hooks->sortKeys(); |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -20,67 +20,67 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class GeoAnalysisResultItem |
| 22 | 22 | { |
| 23 | - private GeoAnalysisPlace $place; |
|
| 24 | - private int $count; |
|
| 23 | + private GeoAnalysisPlace $place; |
|
| 24 | + private int $count; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor for GeoAnalysisResultItem |
|
| 28 | - * |
|
| 29 | - * @param GeoAnalysisPlace $place |
|
| 30 | - * @param int $count |
|
| 31 | - */ |
|
| 32 | - public function __construct(GeoAnalysisPlace $place, int $count = 0) |
|
| 33 | - { |
|
| 34 | - $this->place = $place; |
|
| 35 | - $this->count = $count; |
|
| 36 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor for GeoAnalysisResultItem |
|
| 28 | + * |
|
| 29 | + * @param GeoAnalysisPlace $place |
|
| 30 | + * @param int $count |
|
| 31 | + */ |
|
| 32 | + public function __construct(GeoAnalysisPlace $place, int $count = 0) |
|
| 33 | + { |
|
| 34 | + $this->place = $place; |
|
| 35 | + $this->count = $count; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get the item key. |
|
| 40 | - * |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function key(): string |
|
| 44 | - { |
|
| 45 | - return $this->place->key(); |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * Get the item key. |
|
| 40 | + * |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function key(): string |
|
| 44 | + { |
|
| 45 | + return $this->place->key(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get the referenced GeoAnalysis Place |
|
| 50 | - * |
|
| 51 | - * @return GeoAnalysisPlace |
|
| 52 | - */ |
|
| 53 | - public function place(): GeoAnalysisPlace |
|
| 54 | - { |
|
| 55 | - return $this->place; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Get the referenced GeoAnalysis Place |
|
| 50 | + * |
|
| 51 | + * @return GeoAnalysisPlace |
|
| 52 | + */ |
|
| 53 | + public function place(): GeoAnalysisPlace |
|
| 54 | + { |
|
| 55 | + return $this->place; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get the count of occurences of the GeoAnalysis Place in the analysis |
|
| 60 | - * |
|
| 61 | - * @return int |
|
| 62 | - */ |
|
| 63 | - public function count(): int |
|
| 64 | - { |
|
| 65 | - return $this->count; |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Get the count of occurences of the GeoAnalysis Place in the analysis |
|
| 60 | + * |
|
| 61 | + * @return int |
|
| 62 | + */ |
|
| 63 | + public function count(): int |
|
| 64 | + { |
|
| 65 | + return $this->count; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Increment the count of occurrences of the GeoAnalysis Place in the analysis |
|
| 70 | - * |
|
| 71 | - * @return $this |
|
| 72 | - */ |
|
| 73 | - public function increment(): self |
|
| 74 | - { |
|
| 75 | - $this->count++; |
|
| 76 | - return $this; |
|
| 77 | - } |
|
| 68 | + /** |
|
| 69 | + * Increment the count of occurrences of the GeoAnalysis Place in the analysis |
|
| 70 | + * |
|
| 71 | + * @return $this |
|
| 72 | + */ |
|
| 73 | + public function increment(): self |
|
| 74 | + { |
|
| 75 | + $this->count++; |
|
| 76 | + return $this; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Clone the item object |
|
| 81 | - */ |
|
| 82 | - public function __clone() |
|
| 83 | - { |
|
| 84 | - $this->place = clone $this->place; |
|
| 85 | - } |
|
| 79 | + /** |
|
| 80 | + * Clone the item object |
|
| 81 | + */ |
|
| 82 | + public function __clone() |
|
| 83 | + { |
|
| 84 | + $this->place = clone $this->place; |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -28,137 +28,137 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class GeoAnalysisPlace |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * The default place name for invalid places |
|
| 33 | - * @var string INVALID_PLACE |
|
| 34 | - */ |
|
| 35 | - private const INVALID_PLACE = '##INVALID##'; |
|
| 36 | - |
|
| 37 | - private Place $place; |
|
| 38 | - private int $depth; |
|
| 39 | - private bool $strict_depth; |
|
| 40 | - private bool $is_excluded; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Constructor for GeoAnalysisPlace |
|
| 44 | - * |
|
| 45 | - * @param Tree $tree Default tree |
|
| 46 | - * @param Place|null $place Place resulting from the analysis |
|
| 47 | - * @param int $depth Place hierarchy depth defined by the geographical analysis view |
|
| 48 | - * @param bool $strict_depth Checks whether places with a lower depth than defined should be flagged as invalid |
|
| 49 | - */ |
|
| 50 | - public function __construct(Tree $tree, ?Place $place, int $depth, bool $strict_depth = false) |
|
| 51 | - { |
|
| 52 | - $this->depth = $depth; |
|
| 53 | - $this->strict_depth = $strict_depth; |
|
| 54 | - $this->place = $this->extractPlace($place, $depth, $strict_depth) ?? new Place('', $tree); |
|
| 55 | - $this->is_excluded = false; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Process the provided Place to determine its status for further usage |
|
| 60 | - * |
|
| 61 | - * @param Place|null $place |
|
| 62 | - * @param int $depth |
|
| 63 | - * @param bool $strict_depth |
|
| 64 | - * @return Place|NULL |
|
| 65 | - */ |
|
| 66 | - private function extractPlace(?Place $place, int $depth, bool $strict_depth): ?Place |
|
| 67 | - { |
|
| 68 | - if ($place === null) { |
|
| 69 | - return null; |
|
| 70 | - } |
|
| 71 | - if (mb_strlen($place->gedcomName()) === 0) { |
|
| 72 | - return null; |
|
| 73 | - } |
|
| 74 | - $parts = $place->lastParts($depth); |
|
| 75 | - if ($strict_depth && $parts->count() !== $depth) { |
|
| 76 | - return new Place(self::INVALID_PLACE, $place->tree()); |
|
| 77 | - } |
|
| 78 | - return new Place($parts->implode(', '), $place->tree()); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the GeoAnalysis Place key |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function key(): string |
|
| 87 | - { |
|
| 88 | - return $this->place->gedcomName(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the underlying Place object |
|
| 93 | - * |
|
| 94 | - * @return Place |
|
| 95 | - */ |
|
| 96 | - public function place(): Place |
|
| 97 | - { |
|
| 98 | - return $this->place; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Check if the GeoAnalysis Place is in the Known status |
|
| 103 | - * |
|
| 104 | - * @return bool |
|
| 105 | - */ |
|
| 106 | - public function isKnown(): bool |
|
| 107 | - { |
|
| 108 | - return !$this->isUnknown(); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Check if the GeoAnalysis Place is in the Unknown status |
|
| 113 | - * |
|
| 114 | - * @return bool |
|
| 115 | - */ |
|
| 116 | - public function isUnknown(): bool |
|
| 117 | - { |
|
| 118 | - return mb_strlen($this->place->gedcomName()) === 0; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Check if the GeoAnalysis Place is in the Invalid status |
|
| 123 | - * |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function isInvalid(): bool |
|
| 127 | - { |
|
| 128 | - return $this->place->gedcomName() === self::INVALID_PLACE; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Check if the GeoAnalysis Place is in the Excluded status |
|
| 133 | - * |
|
| 134 | - * @return bool |
|
| 135 | - */ |
|
| 136 | - public function isExcluded(): bool |
|
| 137 | - { |
|
| 138 | - return $this->isUnknown() || $this->isInvalid() || $this->is_excluded; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Set the GeoAnalysis Place status to Found, if the parameter is true |
|
| 143 | - * |
|
| 144 | - * @param bool $include |
|
| 145 | - * @return $this |
|
| 146 | - */ |
|
| 147 | - public function include(bool $include = true): self |
|
| 148 | - { |
|
| 149 | - $this->is_excluded = !$include; |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Set the GeoAnalysis Place status to Excluded, if the parameter is true |
|
| 155 | - * |
|
| 156 | - * @param bool $exclude |
|
| 157 | - * @return $this |
|
| 158 | - */ |
|
| 159 | - public function exclude(bool $exclude = true): self |
|
| 160 | - { |
|
| 161 | - $this->is_excluded = $exclude; |
|
| 162 | - return $this; |
|
| 163 | - } |
|
| 31 | + /** |
|
| 32 | + * The default place name for invalid places |
|
| 33 | + * @var string INVALID_PLACE |
|
| 34 | + */ |
|
| 35 | + private const INVALID_PLACE = '##INVALID##'; |
|
| 36 | + |
|
| 37 | + private Place $place; |
|
| 38 | + private int $depth; |
|
| 39 | + private bool $strict_depth; |
|
| 40 | + private bool $is_excluded; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Constructor for GeoAnalysisPlace |
|
| 44 | + * |
|
| 45 | + * @param Tree $tree Default tree |
|
| 46 | + * @param Place|null $place Place resulting from the analysis |
|
| 47 | + * @param int $depth Place hierarchy depth defined by the geographical analysis view |
|
| 48 | + * @param bool $strict_depth Checks whether places with a lower depth than defined should be flagged as invalid |
|
| 49 | + */ |
|
| 50 | + public function __construct(Tree $tree, ?Place $place, int $depth, bool $strict_depth = false) |
|
| 51 | + { |
|
| 52 | + $this->depth = $depth; |
|
| 53 | + $this->strict_depth = $strict_depth; |
|
| 54 | + $this->place = $this->extractPlace($place, $depth, $strict_depth) ?? new Place('', $tree); |
|
| 55 | + $this->is_excluded = false; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Process the provided Place to determine its status for further usage |
|
| 60 | + * |
|
| 61 | + * @param Place|null $place |
|
| 62 | + * @param int $depth |
|
| 63 | + * @param bool $strict_depth |
|
| 64 | + * @return Place|NULL |
|
| 65 | + */ |
|
| 66 | + private function extractPlace(?Place $place, int $depth, bool $strict_depth): ?Place |
|
| 67 | + { |
|
| 68 | + if ($place === null) { |
|
| 69 | + return null; |
|
| 70 | + } |
|
| 71 | + if (mb_strlen($place->gedcomName()) === 0) { |
|
| 72 | + return null; |
|
| 73 | + } |
|
| 74 | + $parts = $place->lastParts($depth); |
|
| 75 | + if ($strict_depth && $parts->count() !== $depth) { |
|
| 76 | + return new Place(self::INVALID_PLACE, $place->tree()); |
|
| 77 | + } |
|
| 78 | + return new Place($parts->implode(', '), $place->tree()); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the GeoAnalysis Place key |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function key(): string |
|
| 87 | + { |
|
| 88 | + return $this->place->gedcomName(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the underlying Place object |
|
| 93 | + * |
|
| 94 | + * @return Place |
|
| 95 | + */ |
|
| 96 | + public function place(): Place |
|
| 97 | + { |
|
| 98 | + return $this->place; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Check if the GeoAnalysis Place is in the Known status |
|
| 103 | + * |
|
| 104 | + * @return bool |
|
| 105 | + */ |
|
| 106 | + public function isKnown(): bool |
|
| 107 | + { |
|
| 108 | + return !$this->isUnknown(); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Check if the GeoAnalysis Place is in the Unknown status |
|
| 113 | + * |
|
| 114 | + * @return bool |
|
| 115 | + */ |
|
| 116 | + public function isUnknown(): bool |
|
| 117 | + { |
|
| 118 | + return mb_strlen($this->place->gedcomName()) === 0; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Check if the GeoAnalysis Place is in the Invalid status |
|
| 123 | + * |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function isInvalid(): bool |
|
| 127 | + { |
|
| 128 | + return $this->place->gedcomName() === self::INVALID_PLACE; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Check if the GeoAnalysis Place is in the Excluded status |
|
| 133 | + * |
|
| 134 | + * @return bool |
|
| 135 | + */ |
|
| 136 | + public function isExcluded(): bool |
|
| 137 | + { |
|
| 138 | + return $this->isUnknown() || $this->isInvalid() || $this->is_excluded; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Set the GeoAnalysis Place status to Found, if the parameter is true |
|
| 143 | + * |
|
| 144 | + * @param bool $include |
|
| 145 | + * @return $this |
|
| 146 | + */ |
|
| 147 | + public function include(bool $include = true): self |
|
| 148 | + { |
|
| 149 | + $this->is_excluded = !$include; |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Set the GeoAnalysis Place status to Excluded, if the parameter is true |
|
| 155 | + * |
|
| 156 | + * @param bool $exclude |
|
| 157 | + * @return $this |
|
| 158 | + */ |
|
| 159 | + public function exclude(bool $exclude = true): self |
|
| 160 | + { |
|
| 161 | + $this->is_excluded = $exclude; |
|
| 162 | + return $this; |
|
| 163 | + } |
|
| 164 | 164 | } |
@@ -24,225 +24,225 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class GeoAnalysisResult |
| 26 | 26 | { |
| 27 | - private string $description; |
|
| 28 | - private int $order; |
|
| 29 | - private int $unknown_count; |
|
| 30 | - /** |
|
| 31 | - * @var Collection<GeoAnalysisResultItem> |
|
| 32 | - */ |
|
| 33 | - private Collection $places; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Constructor for GeoAnalysisResult |
|
| 37 | - * |
|
| 38 | - * @param string $description |
|
| 39 | - * @param int $order |
|
| 40 | - * @param Collection<GeoAnalysisResultItem> $places |
|
| 41 | - * @param int $unknown |
|
| 42 | - */ |
|
| 43 | - final public function __construct( |
|
| 44 | - string $description, |
|
| 45 | - int $order = 0, |
|
| 46 | - Collection $places = null, |
|
| 47 | - int $unknown = 0 |
|
| 48 | - ) { |
|
| 49 | - $this->description = $description; |
|
| 50 | - $this->order = $order; |
|
| 51 | - $this->places = $places ?? new Collection(); |
|
| 52 | - $this->unknown_count = $unknown; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Get the category description |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function description(): string |
|
| 61 | - { |
|
| 62 | - return $this->description; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the category order |
|
| 67 | - * |
|
| 68 | - * @return int |
|
| 69 | - */ |
|
| 70 | - public function order(): int |
|
| 71 | - { |
|
| 72 | - return $this->order; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Add a place to the analysis result |
|
| 77 | - * |
|
| 78 | - * @param GeoAnalysisPlace $place |
|
| 79 | - */ |
|
| 80 | - public function addPlace(GeoAnalysisPlace $place): void |
|
| 81 | - { |
|
| 82 | - if ($place->isKnown()) { |
|
| 83 | - /** @var GeoAnalysisResultItem $item */ |
|
| 84 | - $item = $this->places->get($place->key(), new GeoAnalysisResultItem($place)); |
|
| 85 | - $this->places->put($item->key(), $item->increment()); |
|
| 86 | - } else { |
|
| 87 | - $this->addUnknown(); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Exclude a place from the analysis result |
|
| 93 | - * |
|
| 94 | - * @param GeoAnalysisPlace $place |
|
| 95 | - */ |
|
| 96 | - public function exclude(GeoAnalysisPlace $place): void |
|
| 97 | - { |
|
| 98 | - /** @var GeoAnalysisResultItem|null $item */ |
|
| 99 | - $item = $this->places->get($place->key()); |
|
| 100 | - if ($item !== null) { |
|
| 101 | - $item->place()->exclude(); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Add an unknown place to the analysis result |
|
| 107 | - */ |
|
| 108 | - public function addUnknown(): void |
|
| 109 | - { |
|
| 110 | - $this->unknown_count++; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Take a copy of the current analysis result |
|
| 115 | - * |
|
| 116 | - * @return static |
|
| 117 | - */ |
|
| 118 | - public function copy(): self |
|
| 119 | - { |
|
| 120 | - return new static( |
|
| 121 | - $this->description(), |
|
| 122 | - $this->order(), |
|
| 123 | - $this->places->map(fn(GeoAnalysisResultItem $item): GeoAnalysisResultItem => clone $item), |
|
| 124 | - $this->countUnknown() |
|
| 125 | - ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Merge the current analysis result with another. |
|
| 130 | - * The current object is modified, not the second one. |
|
| 131 | - * |
|
| 132 | - * @param GeoAnalysisResult $other |
|
| 133 | - * @return $this |
|
| 134 | - */ |
|
| 135 | - public function merge(GeoAnalysisResult $other): self |
|
| 136 | - { |
|
| 137 | - $this->places->each(function (GeoAnalysisResultItem $item) use ($other): void { |
|
| 138 | - if ($other->places->has($item->key())) { |
|
| 139 | - $item->place()->exclude( |
|
| 140 | - $item->place()->isExcluded() |
|
| 141 | - && $other->places->get($item->key())->place()->isExcluded() |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - }); |
|
| 145 | - |
|
| 146 | - $other->places->each(function (GeoAnalysisResultItem $item): void { |
|
| 147 | - if (!$this->places->has($item->key())) { |
|
| 148 | - $this->addPlace($item->place()); |
|
| 149 | - } |
|
| 150 | - }); |
|
| 151 | - |
|
| 152 | - return $this; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Get the count of Known places |
|
| 157 | - * |
|
| 158 | - * @return int |
|
| 159 | - */ |
|
| 160 | - public function countKnown(): int |
|
| 161 | - { |
|
| 162 | - return $this->places->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the count of Found places |
|
| 167 | - * |
|
| 168 | - * @return int |
|
| 169 | - */ |
|
| 170 | - public function countFound(): int |
|
| 171 | - { |
|
| 172 | - return $this->places |
|
| 173 | - ->reject(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()) |
|
| 174 | - ->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Get the count of Excluded places |
|
| 179 | - * |
|
| 180 | - * @return int |
|
| 181 | - */ |
|
| 182 | - public function countExcluded(): int |
|
| 183 | - { |
|
| 184 | - return $this->places |
|
| 185 | - ->filter(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()) |
|
| 186 | - ->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the count of Unknown places |
|
| 191 | - * |
|
| 192 | - * @return int |
|
| 193 | - */ |
|
| 194 | - public function countUnknown(): int |
|
| 195 | - { |
|
| 196 | - return $this->unknown_count; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get the count of the most represented Place in the analysis result |
|
| 201 | - * |
|
| 202 | - * @return int |
|
| 203 | - */ |
|
| 204 | - public function maxCount(): int |
|
| 205 | - { |
|
| 206 | - return $this->places->max(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the list of Known places with their associated count |
|
| 211 | - * |
|
| 212 | - * @param bool $exclude_other |
|
| 213 | - * @return Collection<GeoAnalysisResultItem> |
|
| 214 | - */ |
|
| 215 | - public function knownPlaces(bool $exclude_other = false): Collection |
|
| 216 | - { |
|
| 217 | - if ($exclude_other) { |
|
| 218 | - return $this->places->reject(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()); |
|
| 219 | - } |
|
| 220 | - return $this->places; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Get the list of Known places with their associated count. |
|
| 225 | - * The list is sorted first by descending count, then by ascending Place name |
|
| 226 | - * |
|
| 227 | - * @param bool $exclude_other |
|
| 228 | - * @return Collection<GeoAnalysisResultItem> |
|
| 229 | - */ |
|
| 230 | - public function sortedKnownPlaces(bool $exclude_other = false): Collection |
|
| 231 | - { |
|
| 232 | - return $this->knownPlaces($exclude_other)->sortBy([ |
|
| 233 | - fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => $b->count() <=> $a->count(), |
|
| 234 | - fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => |
|
| 235 | - I18N::comparator()($a->place()->place()->gedcomName(), $b->place()->place()->gedcomName()) |
|
| 236 | - ]); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Get the list of Excluded places |
|
| 241 | - * |
|
| 242 | - * @return Collection<GeoAnalysisResultItem> |
|
| 243 | - */ |
|
| 244 | - public function excludedPlaces(): Collection |
|
| 245 | - { |
|
| 246 | - return $this->places->filter(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()); |
|
| 247 | - } |
|
| 27 | + private string $description; |
|
| 28 | + private int $order; |
|
| 29 | + private int $unknown_count; |
|
| 30 | + /** |
|
| 31 | + * @var Collection<GeoAnalysisResultItem> |
|
| 32 | + */ |
|
| 33 | + private Collection $places; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Constructor for GeoAnalysisResult |
|
| 37 | + * |
|
| 38 | + * @param string $description |
|
| 39 | + * @param int $order |
|
| 40 | + * @param Collection<GeoAnalysisResultItem> $places |
|
| 41 | + * @param int $unknown |
|
| 42 | + */ |
|
| 43 | + final public function __construct( |
|
| 44 | + string $description, |
|
| 45 | + int $order = 0, |
|
| 46 | + Collection $places = null, |
|
| 47 | + int $unknown = 0 |
|
| 48 | + ) { |
|
| 49 | + $this->description = $description; |
|
| 50 | + $this->order = $order; |
|
| 51 | + $this->places = $places ?? new Collection(); |
|
| 52 | + $this->unknown_count = $unknown; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Get the category description |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function description(): string |
|
| 61 | + { |
|
| 62 | + return $this->description; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the category order |
|
| 67 | + * |
|
| 68 | + * @return int |
|
| 69 | + */ |
|
| 70 | + public function order(): int |
|
| 71 | + { |
|
| 72 | + return $this->order; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Add a place to the analysis result |
|
| 77 | + * |
|
| 78 | + * @param GeoAnalysisPlace $place |
|
| 79 | + */ |
|
| 80 | + public function addPlace(GeoAnalysisPlace $place): void |
|
| 81 | + { |
|
| 82 | + if ($place->isKnown()) { |
|
| 83 | + /** @var GeoAnalysisResultItem $item */ |
|
| 84 | + $item = $this->places->get($place->key(), new GeoAnalysisResultItem($place)); |
|
| 85 | + $this->places->put($item->key(), $item->increment()); |
|
| 86 | + } else { |
|
| 87 | + $this->addUnknown(); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Exclude a place from the analysis result |
|
| 93 | + * |
|
| 94 | + * @param GeoAnalysisPlace $place |
|
| 95 | + */ |
|
| 96 | + public function exclude(GeoAnalysisPlace $place): void |
|
| 97 | + { |
|
| 98 | + /** @var GeoAnalysisResultItem|null $item */ |
|
| 99 | + $item = $this->places->get($place->key()); |
|
| 100 | + if ($item !== null) { |
|
| 101 | + $item->place()->exclude(); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Add an unknown place to the analysis result |
|
| 107 | + */ |
|
| 108 | + public function addUnknown(): void |
|
| 109 | + { |
|
| 110 | + $this->unknown_count++; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Take a copy of the current analysis result |
|
| 115 | + * |
|
| 116 | + * @return static |
|
| 117 | + */ |
|
| 118 | + public function copy(): self |
|
| 119 | + { |
|
| 120 | + return new static( |
|
| 121 | + $this->description(), |
|
| 122 | + $this->order(), |
|
| 123 | + $this->places->map(fn(GeoAnalysisResultItem $item): GeoAnalysisResultItem => clone $item), |
|
| 124 | + $this->countUnknown() |
|
| 125 | + ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Merge the current analysis result with another. |
|
| 130 | + * The current object is modified, not the second one. |
|
| 131 | + * |
|
| 132 | + * @param GeoAnalysisResult $other |
|
| 133 | + * @return $this |
|
| 134 | + */ |
|
| 135 | + public function merge(GeoAnalysisResult $other): self |
|
| 136 | + { |
|
| 137 | + $this->places->each(function (GeoAnalysisResultItem $item) use ($other): void { |
|
| 138 | + if ($other->places->has($item->key())) { |
|
| 139 | + $item->place()->exclude( |
|
| 140 | + $item->place()->isExcluded() |
|
| 141 | + && $other->places->get($item->key())->place()->isExcluded() |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + }); |
|
| 145 | + |
|
| 146 | + $other->places->each(function (GeoAnalysisResultItem $item): void { |
|
| 147 | + if (!$this->places->has($item->key())) { |
|
| 148 | + $this->addPlace($item->place()); |
|
| 149 | + } |
|
| 150 | + }); |
|
| 151 | + |
|
| 152 | + return $this; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Get the count of Known places |
|
| 157 | + * |
|
| 158 | + * @return int |
|
| 159 | + */ |
|
| 160 | + public function countKnown(): int |
|
| 161 | + { |
|
| 162 | + return $this->places->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the count of Found places |
|
| 167 | + * |
|
| 168 | + * @return int |
|
| 169 | + */ |
|
| 170 | + public function countFound(): int |
|
| 171 | + { |
|
| 172 | + return $this->places |
|
| 173 | + ->reject(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()) |
|
| 174 | + ->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Get the count of Excluded places |
|
| 179 | + * |
|
| 180 | + * @return int |
|
| 181 | + */ |
|
| 182 | + public function countExcluded(): int |
|
| 183 | + { |
|
| 184 | + return $this->places |
|
| 185 | + ->filter(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()) |
|
| 186 | + ->sum(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the count of Unknown places |
|
| 191 | + * |
|
| 192 | + * @return int |
|
| 193 | + */ |
|
| 194 | + public function countUnknown(): int |
|
| 195 | + { |
|
| 196 | + return $this->unknown_count; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get the count of the most represented Place in the analysis result |
|
| 201 | + * |
|
| 202 | + * @return int |
|
| 203 | + */ |
|
| 204 | + public function maxCount(): int |
|
| 205 | + { |
|
| 206 | + return $this->places->max(fn(GeoAnalysisResultItem $item): int => $item->count()) ?? 0; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the list of Known places with their associated count |
|
| 211 | + * |
|
| 212 | + * @param bool $exclude_other |
|
| 213 | + * @return Collection<GeoAnalysisResultItem> |
|
| 214 | + */ |
|
| 215 | + public function knownPlaces(bool $exclude_other = false): Collection |
|
| 216 | + { |
|
| 217 | + if ($exclude_other) { |
|
| 218 | + return $this->places->reject(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()); |
|
| 219 | + } |
|
| 220 | + return $this->places; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Get the list of Known places with their associated count. |
|
| 225 | + * The list is sorted first by descending count, then by ascending Place name |
|
| 226 | + * |
|
| 227 | + * @param bool $exclude_other |
|
| 228 | + * @return Collection<GeoAnalysisResultItem> |
|
| 229 | + */ |
|
| 230 | + public function sortedKnownPlaces(bool $exclude_other = false): Collection |
|
| 231 | + { |
|
| 232 | + return $this->knownPlaces($exclude_other)->sortBy([ |
|
| 233 | + fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => $b->count() <=> $a->count(), |
|
| 234 | + fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => |
|
| 235 | + I18N::comparator()($a->place()->place()->gedcomName(), $b->place()->place()->gedcomName()) |
|
| 236 | + ]); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Get the list of Excluded places |
|
| 241 | + * |
|
| 242 | + * @return Collection<GeoAnalysisResultItem> |
|
| 243 | + */ |
|
| 244 | + public function excludedPlaces(): Collection |
|
| 245 | + { |
|
| 246 | + return $this->places->filter(fn(GeoAnalysisResultItem $item): bool => $item->place()->isExcluded()); |
|
| 247 | + } |
|
| 248 | 248 | } |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public function merge(GeoAnalysisResult $other): self |
| 136 | 136 | { |
| 137 | - $this->places->each(function (GeoAnalysisResultItem $item) use ($other): void { |
|
| 137 | + $this->places->each(function(GeoAnalysisResultItem $item) use ($other): void { |
|
| 138 | 138 | if ($other->places->has($item->key())) { |
| 139 | 139 | $item->place()->exclude( |
| 140 | 140 | $item->place()->isExcluded() |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | } |
| 144 | 144 | }); |
| 145 | 145 | |
| 146 | - $other->places->each(function (GeoAnalysisResultItem $item): void { |
|
| 146 | + $other->places->each(function(GeoAnalysisResultItem $item): void { |
|
| 147 | 147 | if (!$this->places->has($item->key())) { |
| 148 | 148 | $this->addPlace($item->place()); |
| 149 | 149 | } |
@@ -230,8 +230,8 @@ discard block |
||
| 230 | 230 | public function sortedKnownPlaces(bool $exclude_other = false): Collection |
| 231 | 231 | { |
| 232 | 232 | return $this->knownPlaces($exclude_other)->sortBy([ |
| 233 | - fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => $b->count() <=> $a->count(), |
|
| 234 | - fn (GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => |
|
| 233 | + fn(GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => $b->count() <=> $a->count(), |
|
| 234 | + fn(GeoAnalysisResultItem $a, GeoAnalysisResultItem $b): int => |
|
| 235 | 235 | I18N::comparator()($a->place()->place()->gedcomName(), $b->place()->place()->gedcomName()) |
| 236 | 236 | ]); |
| 237 | 237 | } |
@@ -27,59 +27,59 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class SimpleFilesystemMap implements MapDefinitionInterface |
| 29 | 29 | { |
| 30 | - private string $id; |
|
| 31 | - private string $title; |
|
| 32 | - private string $path; |
|
| 33 | - private FilesystemReader $filesystem; |
|
| 30 | + private string $id; |
|
| 31 | + private string $title; |
|
| 32 | + private string $path; |
|
| 33 | + private FilesystemReader $filesystem; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Constructor for SimpleFilesystemMap |
|
| 37 | - * |
|
| 38 | - * @param string $id |
|
| 39 | - * @param string $title |
|
| 40 | - * @param FilesystemReader $filesystem |
|
| 41 | - * @param string $path |
|
| 42 | - */ |
|
| 43 | - public function __construct(string $id, string $title, FilesystemReader $filesystem, string $path) |
|
| 44 | - { |
|
| 45 | - $this->id = $id; |
|
| 46 | - $this->title = $title; |
|
| 47 | - $this->filesystem = $filesystem; |
|
| 48 | - $this->path = $path; |
|
| 49 | - } |
|
| 35 | + /** |
|
| 36 | + * Constructor for SimpleFilesystemMap |
|
| 37 | + * |
|
| 38 | + * @param string $id |
|
| 39 | + * @param string $title |
|
| 40 | + * @param FilesystemReader $filesystem |
|
| 41 | + * @param string $path |
|
| 42 | + */ |
|
| 43 | + public function __construct(string $id, string $title, FilesystemReader $filesystem, string $path) |
|
| 44 | + { |
|
| 45 | + $this->id = $id; |
|
| 46 | + $this->title = $title; |
|
| 47 | + $this->filesystem = $filesystem; |
|
| 48 | + $this->path = $path; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritDoc} |
|
| 53 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::id() |
|
| 54 | - */ |
|
| 55 | - public function id(): string |
|
| 56 | - { |
|
| 57 | - return $this->id; |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritDoc} |
|
| 53 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::id() |
|
| 54 | + */ |
|
| 55 | + public function id(): string |
|
| 56 | + { |
|
| 57 | + return $this->id; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * {@inheritDoc} |
|
| 62 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::title() |
|
| 63 | - */ |
|
| 64 | - public function title(): string |
|
| 65 | - { |
|
| 66 | - return $this->title; |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * {@inheritDoc} |
|
| 62 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::title() |
|
| 63 | + */ |
|
| 64 | + public function title(): string |
|
| 65 | + { |
|
| 66 | + return $this->title; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * {@inheritDoc} |
|
| 71 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::features() |
|
| 72 | - */ |
|
| 73 | - public function features(): array |
|
| 74 | - { |
|
| 75 | - $reader = new GeoJSONReader(); |
|
| 76 | - try { |
|
| 77 | - $feature_collection = $reader->read($this->filesystem->read($this->path)); |
|
| 78 | - if ($feature_collection instanceof FeatureCollection) { |
|
| 79 | - return $feature_collection->getFeatures(); |
|
| 80 | - } |
|
| 81 | - } catch (Throwable $ex) { |
|
| 82 | - } |
|
| 83 | - return []; |
|
| 84 | - } |
|
| 69 | + /** |
|
| 70 | + * {@inheritDoc} |
|
| 71 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::features() |
|
| 72 | + */ |
|
| 73 | + public function features(): array |
|
| 74 | + { |
|
| 75 | + $reader = new GeoJSONReader(); |
|
| 76 | + try { |
|
| 77 | + $feature_collection = $reader->read($this->filesystem->read($this->path)); |
|
| 78 | + if ($feature_collection instanceof FeatureCollection) { |
|
| 79 | + return $feature_collection->getFeatures(); |
|
| 80 | + } |
|
| 81 | + } catch (Throwable $ex) { |
|
| 82 | + } |
|
| 83 | + return []; |
|
| 84 | + } |
|
| 85 | 85 | } |