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