xref: /webtrees/app/Services/ModuleService.php (revision cb62cb3c3ddb419590d1c5813411dd49230199cd)
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<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<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<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<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
689                $module->setName($name);
690
691                return $module;
692            });
693    }
694
695    /**
696     * All custom modules in the system.  Custom modules are defined in modules_v4/
697     *
698     * @return Collection<ModuleCustomInterface>
699     */
700    private function customModules(): Collection
701    {
702        $pattern   = Webtrees::MODULES_DIR . '*/module.php';
703        $filenames = glob($pattern, GLOB_NOSORT);
704
705        return Collection::make($filenames)
706            ->filter(static function (string $filename): bool {
707                // Special characters will break PHP variable names.
708                // This also allows us to ignore modules called "foo.example" and "foo.disable"
709                $module_name = basename(dirname($filename));
710
711                foreach (['.', ' ', '[', ']'] as $character) {
712                    if (str_contains($module_name, $character)) {
713                        return false;
714                    }
715                }
716
717                return strlen($module_name) <= 30;
718            })
719            ->map(static function (string $filename): ?ModuleCustomInterface {
720                $module = self::load($filename);
721
722                if ($module instanceof ModuleCustomInterface) {
723                    $module->setName('_' . basename(dirname($filename)) . '_');
724
725                    return $module;
726                }
727
728                return null;
729            })
730            ->filter()
731            ->mapWithKeys(static function (ModuleCustomInterface $module): array {
732                return [$module->name() => $module];
733            });
734    }
735
736    /**
737     * Load a custom module in a static scope, to prevent it from modifying local or object variables.
738     *
739     * @param string $filename
740     *
741     * @return ModuleInterface|null
742     */
743    private static function load(string $filename): ?ModuleInterface
744    {
745        try {
746            return include $filename;
747        } catch (Throwable $exception) {
748            $module_name = basename(dirname($filename));
749            $message     = 'Fatal error in module: ' . $module_name . '<br>' . $exception;
750            FlashMessages::addMessage($message, 'danger');
751        }
752
753        return null;
754    }
755
756    /**
757     * A function filter modules by enabled/disabled
758     *
759     * @param bool $include_disabled
760     *
761     * @return Closure
762     */
763    private function enabledFilter(bool $include_disabled): Closure
764    {
765        return static function (ModuleInterface $module) use ($include_disabled): bool {
766            return $include_disabled || $module->isEnabled();
767        };
768    }
769
770    /**
771     * A function filter modules by type
772     *
773     * @param string $interface
774     *
775     * @return Closure
776     */
777    private function interfaceFilter(string $interface): Closure
778    {
779        return static function (ModuleInterface $module) use ($interface): bool {
780            return $module instanceof $interface;
781        };
782    }
783
784    /**
785     * A function to sort footers
786     *
787     * @return Closure
788     */
789    private function footerComparator(): Closure
790    {
791        return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
792            return $x->getFooterOrder() <=> $y->getFooterOrder();
793        };
794    }
795
796    /**
797     * A function to sort menus
798     *
799     * @return Closure
800     */
801    private function menuComparator(): Closure
802    {
803        return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
804            return $x->getMenuOrder() <=> $y->getMenuOrder();
805        };
806    }
807
808    /**
809     * A function to sort menus
810     *
811     * @return Closure
812     */
813    private function sidebarComparator(): Closure
814    {
815        return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
816            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
817        };
818    }
819
820    /**
821     * A function to sort menus
822     *
823     * @return Closure
824     */
825    private function tabComparator(): Closure
826    {
827        return static function (ModuleTabInterface $x, ModuleTabInterface $y): int {
828            return $x->getTabOrder() <=> $y->getTabOrder();
829        };
830    }
831
832    /**
833     * A function to sort modules by name.
834     *
835     * Languages have a "sortable" name, so that "British English" sorts as "English, British".
836     * This provides a more natural order in the language menu.
837     *
838     * @return Closure
839     */
840    private function moduleComparator(): Closure
841    {
842        return static function (ModuleInterface $x, ModuleInterface $y): int {
843            $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();
844            $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();
845
846            return I18N::comparator()($title1, $title2);
847        };
848    }
849
850    /**
851     * During setup, we'll need access to some languages.
852     *
853     * @return Collection<ModuleLanguageInterface>
854     */
855    public function setupLanguages(): Collection
856    {
857        return $this->coreModules()
858            ->filter(static function (ModuleInterface $module): bool {
859                return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();
860            })
861            ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int {
862                return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();
863            });
864    }
865
866    /**
867     * Find a specified module, if it is currently active.
868     *
869     * @param string $module_name
870     * @param bool   $include_disabled
871     *
872     * @return ModuleInterface|null
873     */
874    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
875    {
876        return $this->all($include_disabled)
877            ->first(static function (ModuleInterface $module) use ($module_name): bool {
878                return $module->name() === $module_name;
879            });
880    }
881
882    /**
883     * Configuration settings are available through the various "module component" pages.
884     * For modules that do not provide a component, we need to list them separately.
885     *
886     * @param bool $include_disabled
887     *
888     * @return Collection<ModuleInterface>
889     */
890    public function otherModules(bool $include_disabled = false): Collection
891    {
892        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
893            ->filter(static function (ModuleInterface $module): bool {
894                foreach (self::COMPONENTS as $interface) {
895                    if ($module instanceof $interface) {
896                        return false;
897                    }
898                }
899
900                return true;
901            });
902    }
903
904    /**
905     * Generate a list of module names which exist in the database but not on disk.
906     *
907     * @return Collection<string>
908     */
909    public function deletedModules(): Collection
910    {
911        $database_modules = DB::table('module')->pluck('module_name');
912
913        $disk_modules = $this->all(true)
914            ->map(static function (ModuleInterface $module): string {
915                return $module->name();
916            });
917
918        return $database_modules->diff($disk_modules);
919    }
920
921    /**
922     * Boot all the modules.
923     *
924     * @param ModuleThemeInterface $current_theme
925     */
926    public function bootModules(ModuleThemeInterface $current_theme): void
927    {
928        foreach ($this->all() as $module) {
929            // Only bootstrap the current theme.
930            if ($module instanceof ModuleThemeInterface && $module !== $current_theme) {
931                continue;
932            }
933
934            $module->boot();
935        }
936    }
937
938    /**
939     * @return Collection<string>
940     */
941    public function componentsWithAccess(): Collection
942    {
943        return new Collection(self::COMPONENTS_WITH_ACCESS);
944    }
945
946    /**
947     * @return Collection<string>
948     */
949    public function componentsWithOrder(): Collection
950    {
951        return new Collection(self::COMPONENTS_WITH_SORT);
952    }
953}
954