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