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