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