xref: /webtrees/app/Services/ModuleService.php (revision ad3143cc1f5191f21ec566a4ccc46aa1a5e4f909)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Closure;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\FlashMessages;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Module\AhnentafelReportModule;
28use Fisharebest\Webtrees\Module\AlbumModule;
29use Fisharebest\Webtrees\Module\AncestorsChartModule;
30use Fisharebest\Webtrees\Module\AustrianHistoricEvents;
31use Fisharebest\Webtrees\Module\AustrianPresidents;
32use Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
33use Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
34use Fisharebest\Webtrees\Module\BirthReportModule;
35use Fisharebest\Webtrees\Module\BranchesListModule;
36use Fisharebest\Webtrees\Module\BritishMonarchs;
37use Fisharebest\Webtrees\Module\BritishPrimeMinisters;
38use Fisharebest\Webtrees\Module\BritishSocialHistory;
39use Fisharebest\Webtrees\Module\CalendarMenuModule;
40use Fisharebest\Webtrees\Module\CemeteryReportModule;
41use Fisharebest\Webtrees\Module\CensusAssistantModule;
42use Fisharebest\Webtrees\Module\ChangeReportModule;
43use Fisharebest\Webtrees\Module\ChartsBlockModule;
44use Fisharebest\Webtrees\Module\ChartsMenuModule;
45use Fisharebest\Webtrees\Module\CkeditorModule;
46use Fisharebest\Webtrees\Module\ClippingsCartModule;
47use Fisharebest\Webtrees\Module\CloudsTheme;
48use Fisharebest\Webtrees\Module\ColorsTheme;
49use Fisharebest\Webtrees\Module\CompactTreeChartModule;
50use Fisharebest\Webtrees\Module\ContactsFooterModule;
51use Fisharebest\Webtrees\Module\CustomCssJsModule;
52use Fisharebest\Webtrees\Module\CustomTagsAldfaer;
53use Fisharebest\Webtrees\Module\CustomTagsBrothersKeeper;
54use Fisharebest\Webtrees\Module\CustomTagsFamilyTreeBuilder;
55use Fisharebest\Webtrees\Module\CustomTagsFamilyTreeMaker;
56use Fisharebest\Webtrees\Module\CustomTagsGedcom53;
57use Fisharebest\Webtrees\Module\CustomTagsGedcom55;
58use Fisharebest\Webtrees\Module\CustomTagsGedcomL;
59use Fisharebest\Webtrees\Module\CustomTagsGenPluswin;
60use Fisharebest\Webtrees\Module\CustomTagsLegacy;
61use Fisharebest\Webtrees\Module\CustomTagsPersonalAncestralFile;
62use Fisharebest\Webtrees\Module\CustomTagsPhpGedView;
63use Fisharebest\Webtrees\Module\CustomTagsReunion;
64use Fisharebest\Webtrees\Module\CustomTagsRootsMagic;
65use Fisharebest\Webtrees\Module\CustomTagsWebtrees;
66use Fisharebest\Webtrees\Module\CzechMonarchsAndPresidents;
67use Fisharebest\Webtrees\Module\DeathReportModule;
68use Fisharebest\Webtrees\Module\DescendancyChartModule;
69use Fisharebest\Webtrees\Module\DescendancyModule;
70use Fisharebest\Webtrees\Module\DescendancyReportModule;
71use Fisharebest\Webtrees\Module\DutchMonarchs;
72use Fisharebest\Webtrees\Module\DutchPrimeMinisters;
73use Fisharebest\Webtrees\Module\EsriMaps;
74use Fisharebest\Webtrees\Module\FabTheme;
75use Fisharebest\Webtrees\Module\FactSourcesReportModule;
76use Fisharebest\Webtrees\Module\FamilyBookChartModule;
77use Fisharebest\Webtrees\Module\FamilyGroupReportModule;
78use Fisharebest\Webtrees\Module\FamilyListModule;
79use Fisharebest\Webtrees\Module\FamilyNavigatorModule;
80use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
81use Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
82use Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
83use Fisharebest\Webtrees\Module\FanChartModule;
84use Fisharebest\Webtrees\Module\FixCemeteryTag;
85use Fisharebest\Webtrees\Module\FixDuplicateLinks;
86use Fisharebest\Webtrees\Module\FixMissingDeaths;
87use Fisharebest\Webtrees\Module\FixNameSlashesAndSpaces;
88use Fisharebest\Webtrees\Module\FixNameTags;
89use Fisharebest\Webtrees\Module\FixPlaceNames;
90use Fisharebest\Webtrees\Module\FixPrimaryTag;
91use Fisharebest\Webtrees\Module\FixSearchAndReplace;
92use Fisharebest\Webtrees\Module\FrenchHistory;
93use Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
94use Fisharebest\Webtrees\Module\GeonamesAutocomplete;
95use Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
96use Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
97use Fisharebest\Webtrees\Module\HereMaps;
98use Fisharebest\Webtrees\Module\HitCountFooterModule;
99use Fisharebest\Webtrees\Module\HourglassChartModule;
100use Fisharebest\Webtrees\Module\HtmlBlockModule;
101use Fisharebest\Webtrees\Module\IndividualFactsTabModule;
102use Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
103use Fisharebest\Webtrees\Module\IndividualListModule;
104use Fisharebest\Webtrees\Module\IndividualMetadataModule;
105use Fisharebest\Webtrees\Module\IndividualReportModule;
106use Fisharebest\Webtrees\Module\InteractiveTreeModule;
107use Fisharebest\Webtrees\Module\LanguageAfrikaans;
108use Fisharebest\Webtrees\Module\LanguageAlbanian;
109use Fisharebest\Webtrees\Module\LanguageArabic;
110use Fisharebest\Webtrees\Module\LanguageBosnian;
111use Fisharebest\Webtrees\Module\LanguageBulgarian;
112use Fisharebest\Webtrees\Module\LanguageCatalan;
113use Fisharebest\Webtrees\Module\LanguageChineseSimplified;
114use Fisharebest\Webtrees\Module\LanguageChineseTraditional;
115use Fisharebest\Webtrees\Module\LanguageCroatian;
116use Fisharebest\Webtrees\Module\LanguageCzech;
117use Fisharebest\Webtrees\Module\LanguageDanish;
118use Fisharebest\Webtrees\Module\LanguageDivehi;
119use Fisharebest\Webtrees\Module\LanguageDutch;
120use Fisharebest\Webtrees\Module\LanguageEnglishAustralia;
121use Fisharebest\Webtrees\Module\LanguageEnglishGreatBritain;
122use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
123use Fisharebest\Webtrees\Module\LanguageEstonian;
124use Fisharebest\Webtrees\Module\LanguageFaroese;
125use Fisharebest\Webtrees\Module\LanguageFarsi;
126use Fisharebest\Webtrees\Module\LanguageFinnish;
127use Fisharebest\Webtrees\Module\LanguageFrench;
128use Fisharebest\Webtrees\Module\LanguageFrenchCanada;
129use Fisharebest\Webtrees\Module\LanguageGalician;
130use Fisharebest\Webtrees\Module\LanguageGeorgian;
131use Fisharebest\Webtrees\Module\LanguageGerman;
132use Fisharebest\Webtrees\Module\LanguageGreek;
133use Fisharebest\Webtrees\Module\LanguageHebrew;
134use Fisharebest\Webtrees\Module\LanguageHindi;
135use Fisharebest\Webtrees\Module\LanguageHungarian;
136use Fisharebest\Webtrees\Module\LanguageIcelandic;
137use Fisharebest\Webtrees\Module\LanguageIndonesian;
138use Fisharebest\Webtrees\Module\LanguageItalian;
139use Fisharebest\Webtrees\Module\LanguageJapanese;
140use Fisharebest\Webtrees\Module\LanguageJavanese;
141use Fisharebest\Webtrees\Module\LanguageKazhak;
142use Fisharebest\Webtrees\Module\LanguageKorean;
143use Fisharebest\Webtrees\Module\LanguageKurdish;
144use Fisharebest\Webtrees\Module\LanguageLatvian;
145use Fisharebest\Webtrees\Module\LanguageLingala;
146use Fisharebest\Webtrees\Module\LanguageLithuanian;
147use Fisharebest\Webtrees\Module\LanguageMalay;
148use Fisharebest\Webtrees\Module\LanguageMaori;
149use Fisharebest\Webtrees\Module\LanguageMarathi;
150use Fisharebest\Webtrees\Module\LanguageNepalese;
151use Fisharebest\Webtrees\Module\LanguageNorwegianBokmal;
152use Fisharebest\Webtrees\Module\LanguageNorwegianNynorsk;
153use Fisharebest\Webtrees\Module\LanguageOccitan;
154use Fisharebest\Webtrees\Module\LanguagePolish;
155use Fisharebest\Webtrees\Module\LanguagePortuguese;
156use Fisharebest\Webtrees\Module\LanguagePortugueseBrazil;
157use Fisharebest\Webtrees\Module\LanguageRomanian;
158use Fisharebest\Webtrees\Module\LanguageRussian;
159use Fisharebest\Webtrees\Module\LanguageSerbian;
160use Fisharebest\Webtrees\Module\LanguageSerbianLatin;
161use Fisharebest\Webtrees\Module\LanguageSlovakian;
162use Fisharebest\Webtrees\Module\LanguageSlovenian;
163use Fisharebest\Webtrees\Module\LanguageSpanish;
164use Fisharebest\Webtrees\Module\LanguageSundanese;
165use Fisharebest\Webtrees\Module\LanguageSwahili;
166use Fisharebest\Webtrees\Module\LanguageSwedish;
167use Fisharebest\Webtrees\Module\LanguageTagalog;
168use Fisharebest\Webtrees\Module\LanguageTamil;
169use Fisharebest\Webtrees\Module\LanguageTatar;
170use Fisharebest\Webtrees\Module\LanguageThai;
171use Fisharebest\Webtrees\Module\LanguageTurkish;
172use Fisharebest\Webtrees\Module\LanguageUkranian;
173use Fisharebest\Webtrees\Module\LanguageUrdu;
174use Fisharebest\Webtrees\Module\LanguageVietnamese;
175use Fisharebest\Webtrees\Module\LanguageWelsh;
176use Fisharebest\Webtrees\Module\LanguageYiddish;
177use Fisharebest\Webtrees\Module\LifespansChartModule;
178use Fisharebest\Webtrees\Module\ListsMenuModule;
179use Fisharebest\Webtrees\Module\LocationListModule;
180use Fisharebest\Webtrees\Module\LoggedInUsersModule;
181use Fisharebest\Webtrees\Module\LoginBlockModule;
182use Fisharebest\Webtrees\Module\LowCountriesRulers;
183use Fisharebest\Webtrees\Module\MapBox;
184use Fisharebest\Webtrees\Module\MapLinkBing;
185use Fisharebest\Webtrees\Module\MapLinkGoogle;
186use Fisharebest\Webtrees\Module\MapLinkOpenStreetMap;
187use Fisharebest\Webtrees\Module\MapGeoLocationGeonames;
188use Fisharebest\Webtrees\Module\MapGeoLocationNominatim;
189use Fisharebest\Webtrees\Module\MapGeoLocationOpenRouteService;
190use Fisharebest\Webtrees\Module\BingMaps;
191use Fisharebest\Webtrees\Module\GoogleMaps;
192use Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface;
193use Fisharebest\Webtrees\Module\OpenRouteServiceAutocomplete;
194use Fisharebest\Webtrees\Module\OpenStreetMap;
195use Fisharebest\Webtrees\Module\MarriageReportModule;
196use Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
197use Fisharebest\Webtrees\Module\MediaListModule;
198use Fisharebest\Webtrees\Module\MediaTabModule;
199use Fisharebest\Webtrees\Module\MinimalTheme;
200use Fisharebest\Webtrees\Module\MissingFactsReportModule;
201use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
202use Fisharebest\Webtrees\Module\ModuleBlockInterface;
203use Fisharebest\Webtrees\Module\ModuleChartInterface;
204use Fisharebest\Webtrees\Module\ModuleCustomInterface;
205use Fisharebest\Webtrees\Module\ModuleCustomTagsInterface;
206use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
207use Fisharebest\Webtrees\Module\ModuleFooterInterface;
208use Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
209use Fisharebest\Webtrees\Module\ModuleInterface;
210use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
211use Fisharebest\Webtrees\Module\ModuleListInterface;
212use Fisharebest\Webtrees\Module\ModuleMapLinkInterface;
213use Fisharebest\Webtrees\Module\ModuleMapGeoLocationInterface;
214use Fisharebest\Webtrees\Module\ModuleMapProviderInterface;
215use Fisharebest\Webtrees\Module\ModuleMenuInterface;
216use Fisharebest\Webtrees\Module\ModuleReportInterface;
217use Fisharebest\Webtrees\Module\ModuleShareInterface;
218use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
219use Fisharebest\Webtrees\Module\ModuleTabInterface;
220use Fisharebest\Webtrees\Module\ModuleThemeInterface;
221use Fisharebest\Webtrees\Module\NoteListModule;
222use Fisharebest\Webtrees\Module\NotesTabModule;
223use Fisharebest\Webtrees\Module\OccupationReportModule;
224use Fisharebest\Webtrees\Module\OnThisDayModule;
225use Fisharebest\Webtrees\Module\OrdnanceSurveyHistoricMaps;
226use Fisharebest\Webtrees\Module\PedigreeChartModule;
227use Fisharebest\Webtrees\Module\PedigreeMapModule;
228use Fisharebest\Webtrees\Module\PedigreeReportModule;
229use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
230use Fisharebest\Webtrees\Module\PlacesModule;
231use Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
232use Fisharebest\Webtrees\Module\PrivacyPolicy;
233use Fisharebest\Webtrees\Module\RecentChangesModule;
234use Fisharebest\Webtrees\Module\RedirectLegacyUrlsModule;
235use Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
236use Fisharebest\Webtrees\Module\RelationshipsChartModule;
237use Fisharebest\Webtrees\Module\RelativesTabModule;
238use Fisharebest\Webtrees\Module\ReportsMenuModule;
239use Fisharebest\Webtrees\Module\RepositoryListModule;
240use Fisharebest\Webtrees\Module\ResearchTaskModule;
241use Fisharebest\Webtrees\Module\ReviewChangesModule;
242use Fisharebest\Webtrees\Module\SearchMenuModule;
243use Fisharebest\Webtrees\Module\ShareAnniversaryModule;
244use Fisharebest\Webtrees\Module\ShareUrlModule;
245use Fisharebest\Webtrees\Module\SiteMapModule;
246use Fisharebest\Webtrees\Module\SlideShowModule;
247use Fisharebest\Webtrees\Module\SourceListModule;
248use Fisharebest\Webtrees\Module\SourcesTabModule;
249use Fisharebest\Webtrees\Module\StatcounterModule;
250use Fisharebest\Webtrees\Module\StatisticsChartModule;
251use Fisharebest\Webtrees\Module\StoriesModule;
252use Fisharebest\Webtrees\Module\SubmitterListModule;
253use Fisharebest\Webtrees\Module\ThemeSelectModule;
254use Fisharebest\Webtrees\Module\TimelineChartModule;
255use Fisharebest\Webtrees\Module\TopGivenNamesModule;
256use Fisharebest\Webtrees\Module\TopPageViewsModule;
257use Fisharebest\Webtrees\Module\TopSurnamesModule;
258use Fisharebest\Webtrees\Module\TreesMenuModule;
259use Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
260use Fisharebest\Webtrees\Module\UserFavoritesModule;
261use Fisharebest\Webtrees\Module\UserJournalModule;
262use Fisharebest\Webtrees\Module\UserMessagesModule;
263use Fisharebest\Webtrees\Module\UserWelcomeModule;
264use Fisharebest\Webtrees\Module\USPresidents;
265use Fisharebest\Webtrees\Module\WebtreesTheme;
266use Fisharebest\Webtrees\Module\WelcomeBlockModule;
267use Fisharebest\Webtrees\Module\XeneaTheme;
268use Fisharebest\Webtrees\Module\YahrzeitModule;
269use Fisharebest\Webtrees\Registry;
270use Fisharebest\Webtrees\Tree;
271use Fisharebest\Webtrees\Webtrees;
272use Illuminate\Database\Capsule\Manager as DB;
273use Illuminate\Support\Collection;
274use stdClass;
275use Throwable;
276
277use function app;
278use function str_contains;
279use function strlen;
280
281/**
282 * Functions for managing and maintaining modules.
283 */
284class ModuleService
285{
286    // Components are pieces of user-facing functionality, are managed together in the control panel.
287    private const COMPONENTS = [
288        ModuleAnalyticsInterface::class,
289        ModuleBlockInterface::class,
290        ModuleChartInterface::class,
291        ModuleCustomTagsInterface::class,
292        ModuleDataFixInterface::class,
293        ModuleFooterInterface::class,
294        ModuleHistoricEventsInterface::class,
295        ModuleLanguageInterface::class,
296        ModuleListInterface::class,
297        ModuleMapAutocompleteInterface::class,
298        ModuleMapLinkInterface::class,
299        ModuleMapProviderInterface::class,
300        ModuleMapGeoLocationInterface::class,
301        ModuleMenuInterface::class,
302        ModuleReportInterface::class,
303        ModuleShareInterface::class,
304        ModuleSidebarInterface::class,
305        ModuleTabInterface::class,
306        ModuleThemeInterface::class,
307    ];
308
309    // Components that have access levels.
310    private const COMPONENTS_WITH_ACCESS = [
311        ModuleBlockInterface::class,
312        ModuleChartInterface::class,
313        ModuleListInterface::class,
314        ModuleMenuInterface::class,
315        ModuleReportInterface::class,
316        ModuleSidebarInterface::class,
317        ModuleTabInterface::class,
318    ];
319
320    // Components that are displayed in a particular order
321    private const COMPONENTS_WITH_SORT = [
322        ModuleFooterInterface::class,
323        ModuleMenuInterface::class,
324        ModuleSidebarInterface::class,
325        ModuleTabInterface::class,
326    ];
327
328    // Array keys are module names, and should match module names from earlier versions of webtrees.
329    private const CORE_MODULES = [
330        'GEDFact_assistant'       => CensusAssistantModule::class,
331        'ahnentafel_report'       => AhnentafelReportModule::class,
332        'ancestors_chart'         => AncestorsChartModule::class,
333        'austrian-history'        => AustrianHistoricEvents::class,
334        'austrian-presidents'     => AustrianPresidents::class,
335        'bdm_report'              => BirthDeathMarriageReportModule::class,
336        'bing-maps'               => BingMaps::class,
337        'bing-webmaster-tools'    => BingWebmasterToolsModule::class,
338        'birth_report'            => BirthReportModule::class,
339        'branches_list'           => BranchesListModule::class,
340        'british-monarchs'        => BritishMonarchs::class,
341        'british-prime-ministers' => BritishPrimeMinisters::class,
342        'british-social-history'  => BritishSocialHistory::class,
343        'calendar-menu'           => CalendarMenuModule::class,
344        'cemetery_report'         => CemeteryReportModule::class,
345        'change_report'           => ChangeReportModule::class,
346        'charts'                  => ChartsBlockModule::class,
347        'charts-menu'             => ChartsMenuModule::class,
348        'ckeditor'                => CkeditorModule::class,
349        'clippings'               => ClippingsCartModule::class,
350        'clouds'                  => CloudsTheme::class,
351        'colors'                  => ColorsTheme::class,
352        'compact-chart'           => CompactTreeChartModule::class,
353        'contact-links'           => ContactsFooterModule::class,
354        'czech-leaders'           => CzechMonarchsAndPresidents::class,
355        'custom-css-js'           => CustomCssJsModule::class,
356        'custom-tags-aldfaer'     => CustomTagsAldfaer::class,
357        'custom-tags-bk'          => CustomTagsBrothersKeeper::class,
358        'custom-tags-gedcom-53'   => CustomTagsGedcom53::class,
359        'custom-tags-gedcom-55'   => CustomTagsGedcom55::class,
360        'custom-tags-gedcom-l'    => CustomTagsGedcomL::class,
361        'custom-tags-gedpluswin'  => CustomTagsGenPluswin::class,
362        'custom-tags-legacy'      => CustomTagsLegacy::class,
363        'custom-tags-ftb'         => CustomTagsFamilyTreeBuilder::class,
364        'custom-tags-ftm'         => CustomTagsFamilyTreeMaker::class,
365        'custom-tags-paf'         => CustomTagsPersonalAncestralFile::class,
366        'custom-tags-phpgedview'  => CustomTagsPhpGedView::class,
367        'custom-tags-reunion'     => CustomTagsReunion::class,
368        'custom-tags-roots-magic' => CustomTagsRootsMagic::class,
369        'custom-tags-webtrees'    => CustomTagsWebtrees::class,
370        'death_report'            => DeathReportModule::class,
371        'descendancy'             => DescendancyModule::class,
372        'descendancy_chart'       => DescendancyChartModule::class,
373        'descendancy_report'      => DescendancyReportModule::class,
374        'dutch_monarchs'          => DutchMonarchs::class,
375        'dutch_prime_ministers'   => DutchPrimeMinisters::class,
376        'esri-maps'               => EsriMaps::class,
377        'extra_info'              => IndividualMetadataModule::class,
378        'fab'                     => FabTheme::class,
379        'fact_sources'            => FactSourcesReportModule::class,
380        'family_book_chart'       => FamilyBookChartModule::class,
381        'family_group_report'     => FamilyGroupReportModule::class,
382        'family_list'             => FamilyListModule::class,
383        'family_nav'              => FamilyNavigatorModule::class,
384        'fan_chart'               => FanChartModule::class,
385        'faq'                     => FrequentlyAskedQuestionsModule::class,
386        'french-history'          => FrenchHistory::class,
387        'fix-add-death'           => FixMissingDeaths::class,
388        'fix-ceme-tag'            => FixCemeteryTag::class,
389        'fix-duplicate-links'     => FixDuplicateLinks::class,
390        'fix-name-slashes-spaces' => FixNameSlashesAndSpaces::class,
391        'fix-name-tags'           => FixNameTags::class,
392        'fix-place-names'         => FixPlaceNames::class,
393        'fix-prim-tag'            => FixPrimaryTag::class,
394        'fix-search-and-replace'  => FixSearchAndReplace::class,
395        'gedcom_block'            => WelcomeBlockModule::class,
396        'gedcom_favorites'        => FamilyTreeFavoritesModule::class,
397        'gedcom_news'             => FamilyTreeNewsModule::class,
398        'gedcom_stats'            => FamilyTreeStatisticsModule::class,
399        'geonames'                => GeonamesAutocomplete::class,
400        'google-analytics'        => GoogleAnalyticsModule::class,
401        'google-maps'             => GoogleMaps::class,
402        'google-webmaster-tools'  => GoogleWebmasterToolsModule::class,
403        'here-maps'               => HereMaps::class,
404        'hit-counter'             => HitCountFooterModule::class,
405        'hourglass_chart'         => HourglassChartModule::class,
406        'html'                    => HtmlBlockModule::class,
407        'individual_ext_report'   => IndividualFamiliesReportModule::class,
408        'individual_list'         => IndividualListModule::class,
409        'individual_report'       => IndividualReportModule::class,
410        'language-af'             => LanguageAfrikaans::class,
411        'language-ar'             => LanguageArabic::class,
412        'language-bg'             => LanguageBulgarian::class,
413        'language-bs'             => LanguageBosnian::class,
414        'language-ca'             => LanguageCatalan::class,
415        'language-cs'             => LanguageCzech::class,
416        'language-cy'             => LanguageWelsh::class,
417        'language-da'             => LanguageDanish::class,
418        'language-de'             => LanguageGerman::class,
419        'language-dv'             => LanguageDivehi::class,
420        'language-el'             => LanguageGreek::class,
421        'language-en-AU'          => LanguageEnglishAustralia::class,
422        'language-en-GB'          => LanguageEnglishGreatBritain::class,
423        'language-en-US'          => LanguageEnglishUnitedStates::class,
424        'language-es'             => LanguageSpanish::class,
425        'language-et'             => LanguageEstonian::class,
426        'language-fa'             => LanguageFarsi::class,
427        'language-fi'             => LanguageFinnish::class,
428        'language-fo'             => LanguageFaroese::class,
429        'language-fr'             => LanguageFrench::class,
430        'language-fr-CA'          => LanguageFrenchCanada::class,
431        'language-gl'             => LanguageGalician::class,
432        'language-he'             => LanguageHebrew::class,
433        'language-hi'             => LanguageHindi::class,
434        'language-hr'             => LanguageCroatian::class,
435        'language-hu'             => LanguageHungarian::class,
436        'language-id'             => LanguageIndonesian::class,
437        'language-is'             => LanguageIcelandic::class,
438        'language-it'             => LanguageItalian::class,
439        'language-ja'             => LanguageJapanese::class,
440        'language-jv'             => LanguageJavanese::class,
441        'language-ka'             => LanguageGeorgian::class,
442        'language-kk'             => LanguageKazhak::class,
443        'language-ko'             => LanguageKorean::class,
444        'language-ku'             => LanguageKurdish::class,
445        'language-ln'             => LanguageLingala::class,
446        'language-lt'             => LanguageLithuanian::class,
447        'language-lv'             => LanguageLatvian::class,
448        'language-mi'             => LanguageMaori::class,
449        'language-mr'             => LanguageMarathi::class,
450        'language-ms'             => LanguageMalay::class,
451        'language-nb'             => LanguageNorwegianBokmal::class,
452        'language-ne'             => LanguageNepalese::class,
453        'language-nl'             => LanguageDutch::class,
454        'language-nn'             => LanguageNorwegianNynorsk::class,
455        'language-oc'             => LanguageOccitan::class,
456        'language-pl'             => LanguagePolish::class,
457        'language-pt'             => LanguagePortuguese::class,
458        'language-pt-BR'          => LanguagePortugueseBrazil::class,
459        'language-ro'             => LanguageRomanian::class,
460        'language-ru'             => LanguageRussian::class,
461        'language-sk'             => LanguageSlovakian::class,
462        'language-sl'             => LanguageSlovenian::class,
463        'language-sq'             => LanguageAlbanian::class,
464        'language-sr'             => LanguageSerbian::class,
465        'language-sr-Latn'        => LanguageSerbianLatin::class,
466        'language-su'             => LanguageSundanese::class,
467        'language-sv'             => LanguageSwedish::class,
468        'language-sw'             => LanguageSwahili::class,
469        'language-ta'             => LanguageTamil::class,
470        'language-th'             => LanguageThai::class,
471        'language-tl'             => LanguageTagalog::class,
472        'language-tr'             => LanguageTurkish::class,
473        'language-tt'             => LanguageTatar::class,
474        'language-uk'             => LanguageUkranian::class,
475        'language-ur'             => LanguageUrdu::class,
476        'language-vi'             => LanguageVietnamese::class,
477        'language-yi'             => LanguageYiddish::class,
478        'language-zh-Hans'        => LanguageChineseSimplified::class,
479        'language-zh-Hant'        => LanguageChineseTraditional::class,
480        'legacy-urls'             => RedirectLegacyUrlsModule::class,
481        'lifespans_chart'         => LifespansChartModule::class,
482        'lightbox'                => AlbumModule::class,
483        'lists-menu'              => ListsMenuModule::class,
484        'location_list'           => LocationListModule::class,
485        'logged_in'               => LoggedInUsersModule::class,
486        'login_block'             => LoginBlockModule::class,
487        'low_countries_rulers'    => LowCountriesRulers::class,
488        'map-link-bing'           => MapLinkBing::class,
489        'map-link-google'         => MapLinkGoogle::class,
490        'map-link-openstreetmap'  => MapLinkOpenStreetMap::class,
491        'map-location-geonames'   => MapGeoLocationGeonames::class,
492        'map-location-nominatim'  => MapGeoLocationNominatim::class,
493        'map-location-ors'        => MapGeoLocationOpenRouteService::class,
494        'mapbox'                  => MapBox::class,
495        'marriage_report'         => MarriageReportModule::class,
496        'matomo-analytics'        => MatomoAnalyticsModule::class,
497        'media'                   => MediaTabModule::class,
498        'media_list'              => MediaListModule::class,
499        'minimal'                 => MinimalTheme::class,
500        'missing_facts_report'    => MissingFactsReportModule::class,
501        'notes'                   => NotesTabModule::class,
502        'note_list'               => NoteListModule::class,
503        'occupation_report'       => OccupationReportModule::class,
504        'openrouteservice'        => OpenRouteServiceAutocomplete::class,
505        'openstreetmap'           => OpenStreetMap::class,
506        'osgb-historic'           => OrdnanceSurveyHistoricMaps::class,
507        'pedigree-map'            => PedigreeMapModule::class,
508        'pedigree_chart'          => PedigreeChartModule::class,
509        'pedigree_report'         => PedigreeReportModule::class,
510        'personal_facts'          => IndividualFactsTabModule::class,
511        'places'                  => PlacesModule::class,
512        'places_list'             => PlaceHierarchyListModule::class,
513        'powered-by-webtrees'     => PoweredByWebtreesModule::class,
514        'privacy-policy'          => PrivacyPolicy::class,
515        'random_media'            => SlideShowModule::class,
516        'recent_changes'          => RecentChangesModule::class,
517        'relationships_chart'     => RelationshipsChartModule::class,
518        'relative_ext_report'     => RelatedIndividualsReportModule::class,
519        'relatives'               => RelativesTabModule::class,
520        'reports-menu'            => ReportsMenuModule::class,
521        'repository_list'         => RepositoryListModule::class,
522        'review_changes'          => ReviewChangesModule::class,
523        'search-menu'             => SearchMenuModule::class,
524        'share-anniversary'       => ShareAnniversaryModule::class,
525        'share-url'               => ShareUrlModule::class,
526        'sitemap'                 => SiteMapModule::class,
527        'source_list'             => SourceListModule::class,
528        'sources_tab'             => SourcesTabModule::class,
529        'statcounter'             => StatcounterModule::class,
530        'statistics_chart'        => StatisticsChartModule::class,
531        'stories'                 => StoriesModule::class,
532        'submitter_list'          => SubmitterListModule::class,
533        'theme_select'            => ThemeSelectModule::class,
534        'timeline_chart'          => TimelineChartModule::class,
535        'todays_events'           => OnThisDayModule::class,
536        'todo'                    => ResearchTaskModule::class,
537        'top10_givnnames'         => TopGivenNamesModule::class,
538        'top10_pageviews'         => TopPageViewsModule::class,
539        'top10_surnames'          => TopSurnamesModule::class,
540        'tree'                    => InteractiveTreeModule::class,
541        'trees-menu'              => TreesMenuModule::class,
542        'upcoming_events'         => UpcomingAnniversariesModule::class,
543        'us-presidents'           => USPresidents::class,
544        'user_blog'               => UserJournalModule::class,
545        'user_favorites'          => UserFavoritesModule::class,
546        'user_messages'           => UserMessagesModule::class,
547        'user_welcome'            => UserWelcomeModule::class,
548        'webtrees'                => WebtreesTheme::class,
549        'xenea'                   => XeneaTheme::class,
550        'yahrzeit'                => YahrzeitModule::class,
551    ];
552
553    /**
554     * A function to convert modules into their titles - to create option lists, etc.
555     *
556     * @return Closure
557     */
558    public function titleMapper(): Closure
559    {
560        return static function (ModuleInterface $module): string {
561            return $module->title();
562        };
563    }
564
565    /**
566     * Modules which (a) provide a specific function and (b) we have permission to see.
567     *
568     * @param string        $interface
569     * @param Tree          $tree
570     * @param UserInterface $user
571     *
572     * @return Collection<ModuleInterface>
573     */
574    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
575    {
576        return $this->findByInterface($interface, false, true)
577            ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool {
578                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
579            });
580    }
581
582    /**
583     * All modules which provide a specific function.
584     *
585     * @param string $interface
586     * @param bool   $include_disabled
587     * @param bool   $sort
588     *
589     * @return Collection<ModuleInterface>
590     */
591    public function findByInterface(string $interface, bool $include_disabled = false, bool $sort = false): Collection
592    {
593        $modules = $this->all($include_disabled)
594            ->filter($this->interfaceFilter($interface));
595
596        switch ($interface) {
597            case ModuleFooterInterface::class:
598                return $modules->sort($this->footerComparator());
599
600            case ModuleMenuInterface::class:
601                return $modules->sort($this->menuComparator());
602
603            case ModuleSidebarInterface::class:
604                return $modules->sort($this->sidebarComparator());
605
606            case ModuleTabInterface::class:
607                return $modules->sort($this->tabComparator());
608
609            default:
610                if ($sort) {
611                    return $modules->sort($this->moduleComparator());
612                }
613
614                return $modules;
615        }
616    }
617
618    /**
619     * All modules.
620     *
621     * @param bool $include_disabled
622     *
623     * @return Collection<ModuleInterface>
624     */
625    public function all(bool $include_disabled = false): Collection
626    {
627        return Registry::cache()->array()->remember('all-modules', function (): Collection {
628            // Modules have a default status, order etc.
629            // We can override these from database settings.
630            $module_info = DB::table('module')
631                ->get()
632                ->mapWithKeys(static function (stdClass $row): array {
633                    return [$row->module_name => $row];
634                });
635
636            return $this->coreModules()
637                ->merge($this->customModules())
638                ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface {
639                    $info = $module_info->get($module->name());
640
641                    if ($info instanceof stdClass) {
642                        $module->setEnabled($info->status === 'enabled');
643
644                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
645                            $module->setFooterOrder((int) $info->footer_order);
646                        }
647
648                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
649                            $module->setMenuOrder((int) $info->menu_order);
650                        }
651
652                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
653                            $module->setSidebarOrder((int) $info->sidebar_order);
654                        }
655
656                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
657                            $module->setTabOrder((int) $info->tab_order);
658                        }
659                    } else {
660                        $module->setEnabled($module->isEnabledByDefault());
661
662                        DB::table('module')->insert([
663                            'module_name' => $module->name(),
664                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
665                        ]);
666                    }
667
668                    return $module;
669                });
670        })->filter($this->enabledFilter($include_disabled));
671    }
672
673    /**
674     * All core modules in the system.
675     *
676     * @return Collection<ModuleInterface>
677     */
678    private function coreModules(): Collection
679    {
680        return Collection::make(self::CORE_MODULES)
681            ->map(static function (string $class, string $name): ModuleInterface {
682                $module = app($class);
683
684                $module->setName($name);
685
686                return $module;
687            });
688    }
689
690    /**
691     * All custom modules in the system.  Custom modules are defined in modules_v4/
692     *
693     * @return Collection<ModuleCustomInterface>
694     */
695    private function customModules(): Collection
696    {
697        $pattern   = Webtrees::MODULES_DIR . '*/module.php';
698        $filenames = glob($pattern, GLOB_NOSORT);
699
700        return Collection::make($filenames)
701            ->filter(static function (string $filename): bool {
702                // Special characters will break PHP variable names.
703                // This also allows us to ignore modules called "foo.example" and "foo.disable"
704                $module_name = basename(dirname($filename));
705
706                foreach (['.', ' ', '[', ']'] as $character) {
707                    if (str_contains($module_name, $character)) {
708                        return false;
709                    }
710                }
711
712                return strlen($module_name) <= 30;
713            })
714            ->map(static function (string $filename): ?ModuleCustomInterface {
715                try {
716                    $module = self::load($filename);
717
718                    if ($module instanceof ModuleCustomInterface) {
719                        $module_name = '_' . basename(dirname($filename)) . '_';
720
721                        $module->setName($module_name);
722                    } else {
723                        return null;
724                    }
725
726                    return $module;
727                } catch (Throwable $ex) {
728                    // It would be nice to show this error in a flash-message or similar, but the framework
729                    // has not yet been initialised so we have no themes, languages, sessions, etc.
730                    throw $ex;
731                }
732            })
733            ->filter()
734            ->mapWithKeys(static function (ModuleCustomInterface $module): array {
735                return [$module->name() => $module];
736            });
737    }
738
739    /**
740     * Load a custom module in a static scope, to prevent it from modifying local or object variables.
741     *
742     * @param string $filename
743     *
744     * @return ModuleInterface|null
745     */
746    private static function load(string $filename): ?ModuleInterface
747    {
748        try {
749            return include $filename;
750        } catch (Throwable $exception) {
751            $module_name = basename(dirname($filename));
752            $message     = 'Fatal error in module: ' . $module_name;
753            $message     .= '<br>' . $exception;
754            FlashMessages::addMessage($message, 'danger');
755        }
756
757        return null;
758    }
759
760    /**
761     * A function filter modules by enabled/disabled
762     *
763     * @param bool $include_disabled
764     *
765     * @return Closure
766     */
767    private function enabledFilter(bool $include_disabled): Closure
768    {
769        return static function (ModuleInterface $module) use ($include_disabled): bool {
770            return $include_disabled || $module->isEnabled();
771        };
772    }
773
774    /**
775     * A function filter modules by type
776     *
777     * @param string $interface
778     *
779     * @return Closure
780     */
781    private function interfaceFilter(string $interface): Closure
782    {
783        return static function (ModuleInterface $module) use ($interface): bool {
784            return $module instanceof $interface;
785        };
786    }
787
788    /**
789     * A function to sort footers
790     *
791     * @return Closure
792     */
793    private function footerComparator(): Closure
794    {
795        return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
796            return $x->getFooterOrder() <=> $y->getFooterOrder();
797        };
798    }
799
800    /**
801     * A function to sort menus
802     *
803     * @return Closure
804     */
805    private function menuComparator(): Closure
806    {
807        return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
808            return $x->getMenuOrder() <=> $y->getMenuOrder();
809        };
810    }
811
812    /**
813     * A function to sort menus
814     *
815     * @return Closure
816     */
817    private function sidebarComparator(): Closure
818    {
819        return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
820            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
821        };
822    }
823
824    /**
825     * A function to sort menus
826     *
827     * @return Closure
828     */
829    private function tabComparator(): Closure
830    {
831        return static function (ModuleTabInterface $x, ModuleTabInterface $y): int {
832            return $x->getTabOrder() <=> $y->getTabOrder();
833        };
834    }
835
836    /**
837     * A function to sort modules by name.
838     *
839     * Languages have a "sortable" name, so that "British English" sorts as "English, British".
840     * This provides a more natural order in the language menu.
841     *
842     * @return Closure
843     */
844    private function moduleComparator(): Closure
845    {
846        return static function (ModuleInterface $x, ModuleInterface $y): int {
847            $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();
848            $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();
849
850            return I18N::comparator()($title1, $title2);
851        };
852    }
853
854    /**
855     * During setup, we'll need access to some languages.
856     *
857     * @return Collection<ModuleLanguageInterface>
858     */
859    public function setupLanguages(): Collection
860    {
861        return $this->coreModules()
862            ->filter(static function (ModuleInterface $module): bool {
863                return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();
864            })
865            ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int {
866                return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();
867            });
868    }
869
870    /**
871     * Find a specified module, if it is currently active.
872     *
873     * @param string $module_name
874     * @param bool   $include_disabled
875     *
876     * @return ModuleInterface|null
877     */
878    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
879    {
880        return $this->all($include_disabled)
881            ->first(static function (ModuleInterface $module) use ($module_name): bool {
882                return $module->name() === $module_name;
883            });
884    }
885
886    /**
887     * Configuration settings are available through the various "module component" pages.
888     * For modules that do not provide a component, we need to list them separately.
889     *
890     * @param bool $include_disabled
891     *
892     * @return Collection<ModuleInterface>
893     */
894    public function otherModules(bool $include_disabled = false): Collection
895    {
896        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
897            ->filter(static function (ModuleInterface $module): bool {
898                foreach (self::COMPONENTS as $interface) {
899                    if ($module instanceof $interface) {
900                        return false;
901                    }
902                }
903
904                return true;
905            });
906    }
907
908    /**
909     * Generate a list of module names which exist in the database but not on disk.
910     *
911     * @return Collection<string>
912     */
913    public function deletedModules(): Collection
914    {
915        $database_modules = DB::table('module')->pluck('module_name');
916
917        $disk_modules = $this->all(true)
918            ->map(static function (ModuleInterface $module): string {
919                return $module->name();
920            });
921
922        return $database_modules->diff($disk_modules);
923    }
924
925    /**
926     * Boot all the modules.
927     *
928     * @param ModuleThemeInterface $current_theme
929     */
930    public function bootModules(ModuleThemeInterface $current_theme): void
931    {
932        foreach ($this->all() as $module) {
933            // Only bootstrap the current theme.
934            if ($module instanceof ModuleThemeInterface && $module !== $current_theme) {
935                continue;
936            }
937
938            $module->boot();
939        }
940    }
941
942    /**
943     * @return Collection<string>
944     */
945    public function componentsWithAccess(): Collection
946    {
947        return new Collection(self::COMPONENTS_WITH_ACCESS);
948    }
949
950    /**
951     * @return Collection<string>
952     */
953    public function componentsWithOrder(): Collection
954    {
955        return new Collection(self::COMPONENTS_WITH_SORT);
956    }
957}
958