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