xref: /webtrees/app/Services/ModuleService.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
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\BingMaps;
33use Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
34use Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
35use Fisharebest\Webtrees\Module\BirthReportModule;
36use Fisharebest\Webtrees\Module\BranchesListModule;
37use Fisharebest\Webtrees\Module\BritishMonarchs;
38use Fisharebest\Webtrees\Module\BritishPrimeMinisters;
39use Fisharebest\Webtrees\Module\BritishSocialHistory;
40use Fisharebest\Webtrees\Module\CalendarMenuModule;
41use Fisharebest\Webtrees\Module\CemeteryReportModule;
42use Fisharebest\Webtrees\Module\CensusAssistantModule;
43use Fisharebest\Webtrees\Module\ChangeReportModule;
44use Fisharebest\Webtrees\Module\ChartsBlockModule;
45use Fisharebest\Webtrees\Module\ChartsMenuModule;
46use Fisharebest\Webtrees\Module\CheckForNewVersion;
47use Fisharebest\Webtrees\Module\CkeditorModule;
48use Fisharebest\Webtrees\Module\ClippingsCartModule;
49use Fisharebest\Webtrees\Module\CloudsTheme;
50use Fisharebest\Webtrees\Module\ColorsTheme;
51use Fisharebest\Webtrees\Module\CompactTreeChartModule;
52use Fisharebest\Webtrees\Module\ContactsFooterModule;
53use Fisharebest\Webtrees\Module\CustomCssJsModule;
54use Fisharebest\Webtrees\Module\CzechMonarchsAndPresidents;
55use Fisharebest\Webtrees\Module\DeathReportModule;
56use Fisharebest\Webtrees\Module\DescendancyChartModule;
57use Fisharebest\Webtrees\Module\DescendancyModule;
58use Fisharebest\Webtrees\Module\DescendancyReportModule;
59use Fisharebest\Webtrees\Module\DutchMonarchs;
60use Fisharebest\Webtrees\Module\DutchPrimeMinisters;
61use Fisharebest\Webtrees\Module\EsriMaps;
62use Fisharebest\Webtrees\Module\FabTheme;
63use Fisharebest\Webtrees\Module\FactSourcesReportModule;
64use Fisharebest\Webtrees\Module\FamilyBookChartModule;
65use Fisharebest\Webtrees\Module\FamilyGroupReportModule;
66use Fisharebest\Webtrees\Module\FamilyListModule;
67use Fisharebest\Webtrees\Module\FamilyNavigatorModule;
68use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
69use Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
70use Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
71use Fisharebest\Webtrees\Module\FanChartModule;
72use Fisharebest\Webtrees\Module\FixCemeteryTag;
73use Fisharebest\Webtrees\Module\FixDuplicateLinks;
74use Fisharebest\Webtrees\Module\FixMissingDeaths;
75use Fisharebest\Webtrees\Module\FixNameSlashesAndSpaces;
76use Fisharebest\Webtrees\Module\FixNameTags;
77use Fisharebest\Webtrees\Module\FixPlaceNames;
78use Fisharebest\Webtrees\Module\FixPrimaryTag;
79use Fisharebest\Webtrees\Module\FixSearchAndReplace;
80use Fisharebest\Webtrees\Module\FixWtObjeSortTag;
81use Fisharebest\Webtrees\Module\FrenchHistory;
82use Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
83use Fisharebest\Webtrees\Module\GeonamesAutocomplete;
84use Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
85use Fisharebest\Webtrees\Module\GoogleMaps;
86use Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
87use Fisharebest\Webtrees\Module\HereMaps;
88use Fisharebest\Webtrees\Module\HitCountFooterModule;
89use Fisharebest\Webtrees\Module\HourglassChartModule;
90use Fisharebest\Webtrees\Module\HtmlBlockModule;
91use Fisharebest\Webtrees\Module\IndividualFactsTabModule;
92use Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
93use Fisharebest\Webtrees\Module\IndividualListModule;
94use Fisharebest\Webtrees\Module\IndividualMetadataModule;
95use Fisharebest\Webtrees\Module\IndividualReportModule;
96use Fisharebest\Webtrees\Module\InteractiveTreeModule;
97use Fisharebest\Webtrees\Module\LanguageAfrikaans;
98use Fisharebest\Webtrees\Module\LanguageAlbanian;
99use Fisharebest\Webtrees\Module\LanguageArabic;
100use Fisharebest\Webtrees\Module\LanguageBosnian;
101use Fisharebest\Webtrees\Module\LanguageBulgarian;
102use Fisharebest\Webtrees\Module\LanguageCatalan;
103use Fisharebest\Webtrees\Module\LanguageChineseSimplified;
104use Fisharebest\Webtrees\Module\LanguageChineseTraditional;
105use Fisharebest\Webtrees\Module\LanguageCroatian;
106use Fisharebest\Webtrees\Module\LanguageCzech;
107use Fisharebest\Webtrees\Module\LanguageDanish;
108use Fisharebest\Webtrees\Module\LanguageDivehi;
109use Fisharebest\Webtrees\Module\LanguageDutch;
110use Fisharebest\Webtrees\Module\LanguageEnglishAustralia;
111use Fisharebest\Webtrees\Module\LanguageEnglishGreatBritain;
112use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
113use Fisharebest\Webtrees\Module\LanguageEstonian;
114use Fisharebest\Webtrees\Module\LanguageFaroese;
115use Fisharebest\Webtrees\Module\LanguageFarsi;
116use Fisharebest\Webtrees\Module\LanguageFinnish;
117use Fisharebest\Webtrees\Module\LanguageFrench;
118use Fisharebest\Webtrees\Module\LanguageFrenchCanada;
119use Fisharebest\Webtrees\Module\LanguageGalician;
120use Fisharebest\Webtrees\Module\LanguageGeorgian;
121use Fisharebest\Webtrees\Module\LanguageGerman;
122use Fisharebest\Webtrees\Module\LanguageGreek;
123use Fisharebest\Webtrees\Module\LanguageHebrew;
124use Fisharebest\Webtrees\Module\LanguageHindi;
125use Fisharebest\Webtrees\Module\LanguageHungarian;
126use Fisharebest\Webtrees\Module\LanguageIcelandic;
127use Fisharebest\Webtrees\Module\LanguageIndonesian;
128use Fisharebest\Webtrees\Module\LanguageItalian;
129use Fisharebest\Webtrees\Module\LanguageJapanese;
130use Fisharebest\Webtrees\Module\LanguageJavanese;
131use Fisharebest\Webtrees\Module\LanguageKazhak;
132use Fisharebest\Webtrees\Module\LanguageKorean;
133use Fisharebest\Webtrees\Module\LanguageKurdish;
134use Fisharebest\Webtrees\Module\LanguageLatvian;
135use Fisharebest\Webtrees\Module\LanguageLingala;
136use Fisharebest\Webtrees\Module\LanguageLithuanian;
137use Fisharebest\Webtrees\Module\LanguageMalay;
138use Fisharebest\Webtrees\Module\LanguageMaori;
139use Fisharebest\Webtrees\Module\LanguageMarathi;
140use Fisharebest\Webtrees\Module\LanguageNepalese;
141use Fisharebest\Webtrees\Module\LanguageNorwegianBokmal;
142use Fisharebest\Webtrees\Module\LanguageNorwegianNynorsk;
143use Fisharebest\Webtrees\Module\LanguageOccitan;
144use Fisharebest\Webtrees\Module\LanguagePolish;
145use Fisharebest\Webtrees\Module\LanguagePortuguese;
146use Fisharebest\Webtrees\Module\LanguagePortugueseBrazil;
147use Fisharebest\Webtrees\Module\LanguageRomanian;
148use Fisharebest\Webtrees\Module\LanguageRussian;
149use Fisharebest\Webtrees\Module\LanguageSerbian;
150use Fisharebest\Webtrees\Module\LanguageSerbianLatin;
151use Fisharebest\Webtrees\Module\LanguageSlovakian;
152use Fisharebest\Webtrees\Module\LanguageSlovenian;
153use Fisharebest\Webtrees\Module\LanguageSpanish;
154use Fisharebest\Webtrees\Module\LanguageSundanese;
155use Fisharebest\Webtrees\Module\LanguageSwahili;
156use Fisharebest\Webtrees\Module\LanguageSwedish;
157use Fisharebest\Webtrees\Module\LanguageTagalog;
158use Fisharebest\Webtrees\Module\LanguageTamil;
159use Fisharebest\Webtrees\Module\LanguageTatar;
160use Fisharebest\Webtrees\Module\LanguageThai;
161use Fisharebest\Webtrees\Module\LanguageTurkish;
162use Fisharebest\Webtrees\Module\LanguageUkranian;
163use Fisharebest\Webtrees\Module\LanguageUrdu;
164use Fisharebest\Webtrees\Module\LanguageVietnamese;
165use Fisharebest\Webtrees\Module\LanguageWelsh;
166use Fisharebest\Webtrees\Module\LanguageYiddish;
167use Fisharebest\Webtrees\Module\LifespansChartModule;
168use Fisharebest\Webtrees\Module\ListsMenuModule;
169use Fisharebest\Webtrees\Module\LocationListModule;
170use Fisharebest\Webtrees\Module\LoggedInUsersModule;
171use Fisharebest\Webtrees\Module\LoginBlockModule;
172use Fisharebest\Webtrees\Module\LowCountriesRulers;
173use Fisharebest\Webtrees\Module\MapBox;
174use Fisharebest\Webtrees\Module\MapGeoLocationGeonames;
175use Fisharebest\Webtrees\Module\MapGeoLocationNominatim;
176use Fisharebest\Webtrees\Module\MapGeoLocationOpenRouteService;
177use Fisharebest\Webtrees\Module\MapLinkBing;
178use Fisharebest\Webtrees\Module\MapLinkGoogle;
179use Fisharebest\Webtrees\Module\MapLinkOpenStreetMap;
180use Fisharebest\Webtrees\Module\MarriageReportModule;
181use Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
182use Fisharebest\Webtrees\Module\MediaListModule;
183use Fisharebest\Webtrees\Module\MediaTabModule;
184use Fisharebest\Webtrees\Module\MinimalTheme;
185use Fisharebest\Webtrees\Module\MissingFactsReportModule;
186use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
187use Fisharebest\Webtrees\Module\ModuleBlockInterface;
188use Fisharebest\Webtrees\Module\ModuleChartInterface;
189use Fisharebest\Webtrees\Module\ModuleCustomInterface;
190use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
191use Fisharebest\Webtrees\Module\ModuleFooterInterface;
192use Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
193use Fisharebest\Webtrees\Module\ModuleInterface;
194use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
195use Fisharebest\Webtrees\Module\ModuleListInterface;
196use Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface;
197use Fisharebest\Webtrees\Module\ModuleMapGeoLocationInterface;
198use Fisharebest\Webtrees\Module\ModuleMapLinkInterface;
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\OpenRouteServiceAutocomplete;
211use Fisharebest\Webtrees\Module\OpenStreetMap;
212use Fisharebest\Webtrees\Module\OrdnanceSurveyHistoricMaps;
213use Fisharebest\Webtrees\Module\PedigreeChartModule;
214use Fisharebest\Webtrees\Module\PedigreeMapModule;
215use Fisharebest\Webtrees\Module\PedigreeReportModule;
216use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
217use Fisharebest\Webtrees\Module\PlacesModule;
218use Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
219use Fisharebest\Webtrees\Module\PrivacyPolicy;
220use Fisharebest\Webtrees\Module\RecentChangesModule;
221use Fisharebest\Webtrees\Module\RedirectLegacyUrlsModule;
222use Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
223use Fisharebest\Webtrees\Module\RelationshipsChartModule;
224use Fisharebest\Webtrees\Module\RelativesTabModule;
225use Fisharebest\Webtrees\Module\ReportsMenuModule;
226use Fisharebest\Webtrees\Module\RepositoryListModule;
227use Fisharebest\Webtrees\Module\ResearchTaskModule;
228use Fisharebest\Webtrees\Module\ReviewChangesModule;
229use Fisharebest\Webtrees\Module\SearchMenuModule;
230use Fisharebest\Webtrees\Module\ShareAnniversaryModule;
231use Fisharebest\Webtrees\Module\ShareUrlModule;
232use Fisharebest\Webtrees\Module\SiteMapModule;
233use Fisharebest\Webtrees\Module\SlideShowModule;
234use Fisharebest\Webtrees\Module\SourceListModule;
235use Fisharebest\Webtrees\Module\SourcesTabModule;
236use Fisharebest\Webtrees\Module\StatcounterModule;
237use Fisharebest\Webtrees\Module\StatisticsChartModule;
238use Fisharebest\Webtrees\Module\StoriesModule;
239use Fisharebest\Webtrees\Module\SubmitterListModule;
240use Fisharebest\Webtrees\Module\ThemeSelectModule;
241use Fisharebest\Webtrees\Module\TimelineChartModule;
242use Fisharebest\Webtrees\Module\TopGivenNamesModule;
243use Fisharebest\Webtrees\Module\TopPageViewsModule;
244use Fisharebest\Webtrees\Module\TopSurnamesModule;
245use Fisharebest\Webtrees\Module\TreesMenuModule;
246use Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
247use Fisharebest\Webtrees\Module\UserFavoritesModule;
248use Fisharebest\Webtrees\Module\UserJournalModule;
249use Fisharebest\Webtrees\Module\UserMessagesModule;
250use Fisharebest\Webtrees\Module\UserWelcomeModule;
251use Fisharebest\Webtrees\Module\USPresidents;
252use Fisharebest\Webtrees\Module\WebtreesTheme;
253use Fisharebest\Webtrees\Module\WelcomeBlockModule;
254use Fisharebest\Webtrees\Module\XeneaTheme;
255use Fisharebest\Webtrees\Module\YahrzeitModule;
256use Fisharebest\Webtrees\Registry;
257use Fisharebest\Webtrees\Tree;
258use Fisharebest\Webtrees\Webtrees;
259use Illuminate\Database\Capsule\Manager as DB;
260use Illuminate\Support\Collection;
261use Throwable;
262
263use function app;
264use function assert;
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-fa'             => LanguageFarsi::class,
407        'language-fi'             => LanguageFinnish::class,
408        'language-fo'             => LanguageFaroese::class,
409        'language-fr'             => LanguageFrench::class,
410        'language-fr-CA'          => LanguageFrenchCanada::class,
411        'language-gl'             => LanguageGalician::class,
412        'language-he'             => LanguageHebrew::class,
413        'language-hi'             => LanguageHindi::class,
414        'language-hr'             => LanguageCroatian::class,
415        'language-hu'             => LanguageHungarian::class,
416        'language-id'             => LanguageIndonesian::class,
417        'language-is'             => LanguageIcelandic::class,
418        'language-it'             => LanguageItalian::class,
419        'language-ja'             => LanguageJapanese::class,
420        'language-jv'             => LanguageJavanese::class,
421        'language-ka'             => LanguageGeorgian::class,
422        'language-kk'             => LanguageKazhak::class,
423        'language-ko'             => LanguageKorean::class,
424        'language-ku'             => LanguageKurdish::class,
425        'language-ln'             => LanguageLingala::class,
426        'language-lt'             => LanguageLithuanian::class,
427        'language-lv'             => LanguageLatvian::class,
428        'language-mi'             => LanguageMaori::class,
429        'language-mr'             => LanguageMarathi::class,
430        'language-ms'             => LanguageMalay::class,
431        'language-nb'             => LanguageNorwegianBokmal::class,
432        'language-ne'             => LanguageNepalese::class,
433        'language-nl'             => LanguageDutch::class,
434        'language-nn'             => LanguageNorwegianNynorsk::class,
435        'language-oc'             => LanguageOccitan::class,
436        'language-pl'             => LanguagePolish::class,
437        'language-pt'             => LanguagePortuguese::class,
438        'language-pt-BR'          => LanguagePortugueseBrazil::class,
439        'language-ro'             => LanguageRomanian::class,
440        'language-ru'             => LanguageRussian::class,
441        'language-sk'             => LanguageSlovakian::class,
442        'language-sl'             => LanguageSlovenian::class,
443        'language-sq'             => LanguageAlbanian::class,
444        'language-sr'             => LanguageSerbian::class,
445        'language-sr-Latn'        => LanguageSerbianLatin::class,
446        'language-su'             => LanguageSundanese::class,
447        'language-sv'             => LanguageSwedish::class,
448        'language-sw'             => LanguageSwahili::class,
449        'language-ta'             => LanguageTamil::class,
450        'language-th'             => LanguageThai::class,
451        'language-tl'             => LanguageTagalog::class,
452        'language-tr'             => LanguageTurkish::class,
453        'language-tt'             => LanguageTatar::class,
454        'language-uk'             => LanguageUkranian::class,
455        'language-ur'             => LanguageUrdu::class,
456        'language-vi'             => LanguageVietnamese::class,
457        'language-yi'             => LanguageYiddish::class,
458        'language-zh-Hans'        => LanguageChineseSimplified::class,
459        'language-zh-Hant'        => LanguageChineseTraditional::class,
460        'legacy-urls'             => RedirectLegacyUrlsModule::class,
461        'lifespans_chart'         => LifespansChartModule::class,
462        'lightbox'                => AlbumModule::class,
463        'lists-menu'              => ListsMenuModule::class,
464        'location_list'           => LocationListModule::class,
465        'logged_in'               => LoggedInUsersModule::class,
466        'login_block'             => LoginBlockModule::class,
467        'low_countries_rulers'    => LowCountriesRulers::class,
468        'map-link-bing'           => MapLinkBing::class,
469        'map-link-google'         => MapLinkGoogle::class,
470        'map-link-openstreetmap'  => MapLinkOpenStreetMap::class,
471        'map-location-geonames'   => MapGeoLocationGeonames::class,
472        'map-location-nominatim'  => MapGeoLocationNominatim::class,
473        'map-location-ors'        => MapGeoLocationOpenRouteService::class,
474        'mapbox'                  => MapBox::class,
475        'marriage_report'         => MarriageReportModule::class,
476        'matomo-analytics'        => MatomoAnalyticsModule::class,
477        'media'                   => MediaTabModule::class,
478        'media_list'              => MediaListModule::class,
479        'minimal'                 => MinimalTheme::class,
480        'missing_facts_report'    => MissingFactsReportModule::class,
481        'notes'                   => NotesTabModule::class,
482        'note_list'               => NoteListModule::class,
483        'occupation_report'       => OccupationReportModule::class,
484        'openrouteservice'        => OpenRouteServiceAutocomplete::class,
485        'openstreetmap'           => OpenStreetMap::class,
486        'osgb-historic'           => OrdnanceSurveyHistoricMaps::class,
487        'pedigree-map'            => PedigreeMapModule::class,
488        'pedigree_chart'          => PedigreeChartModule::class,
489        'pedigree_report'         => PedigreeReportModule::class,
490        'personal_facts'          => IndividualFactsTabModule::class,
491        'places'                  => PlacesModule::class,
492        'places_list'             => PlaceHierarchyListModule::class,
493        'powered-by-webtrees'     => PoweredByWebtreesModule::class,
494        'privacy-policy'          => PrivacyPolicy::class,
495        'random_media'            => SlideShowModule::class,
496        'recent_changes'          => RecentChangesModule::class,
497        'relationships_chart'     => RelationshipsChartModule::class,
498        'relative_ext_report'     => RelatedIndividualsReportModule::class,
499        'relatives'               => RelativesTabModule::class,
500        'reports-menu'            => ReportsMenuModule::class,
501        'repository_list'         => RepositoryListModule::class,
502        'review_changes'          => ReviewChangesModule::class,
503        'search-menu'             => SearchMenuModule::class,
504        'share-anniversary'       => ShareAnniversaryModule::class,
505        'share-url'               => ShareUrlModule::class,
506        'sitemap'                 => SiteMapModule::class,
507        'source_list'             => SourceListModule::class,
508        'sources_tab'             => SourcesTabModule::class,
509        'statcounter'             => StatcounterModule::class,
510        'statistics_chart'        => StatisticsChartModule::class,
511        'stories'                 => StoriesModule::class,
512        'submitter_list'          => SubmitterListModule::class,
513        'theme_select'            => ThemeSelectModule::class,
514        'timeline_chart'          => TimelineChartModule::class,
515        'todays_events'           => OnThisDayModule::class,
516        'todo'                    => ResearchTaskModule::class,
517        'top10_givnnames'         => TopGivenNamesModule::class,
518        'top10_pageviews'         => TopPageViewsModule::class,
519        'top10_surnames'          => TopSurnamesModule::class,
520        'tree'                    => InteractiveTreeModule::class,
521        'trees-menu'              => TreesMenuModule::class,
522        'upcoming_events'         => UpcomingAnniversariesModule::class,
523        'us-presidents'           => USPresidents::class,
524        'user_blog'               => UserJournalModule::class,
525        'user_favorites'          => UserFavoritesModule::class,
526        'user_messages'           => UserMessagesModule::class,
527        'user_welcome'            => UserWelcomeModule::class,
528        'webtrees'                => WebtreesTheme::class,
529        'xenea'                   => XeneaTheme::class,
530        'yahrzeit'                => YahrzeitModule::class,
531    ];
532
533    /**
534     * A function to convert modules into their titles - to create option lists, etc.
535     *
536     * @return Closure
537     */
538    public function titleMapper(): Closure
539    {
540        return static function (ModuleInterface $module): string {
541            return $module->title();
542        };
543    }
544
545    /**
546     * Modules which (a) provide a specific function and (b) we have permission to see.
547     *
548     * @param string        $interface
549     * @param Tree          $tree
550     * @param UserInterface $user
551     *
552     * @return Collection<string,ModuleInterface>
553     */
554    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
555    {
556        return $this->findByInterface($interface, false, true)
557            ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool {
558                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
559            });
560    }
561
562    /**
563     * All modules which provide a specific function.
564     *
565     * @param string $interface
566     * @param bool   $include_disabled
567     * @param bool   $sort
568     *
569     * @return Collection<string,ModuleInterface>
570     */
571    public function findByInterface(string $interface, bool $include_disabled = false, bool $sort = false): Collection
572    {
573        $modules = $this->all($include_disabled)
574            ->filter($this->interfaceFilter($interface));
575
576        switch ($interface) {
577            case ModuleFooterInterface::class:
578                return $modules->sort($this->footerComparator());
579
580            case ModuleMenuInterface::class:
581                return $modules->sort($this->menuComparator());
582
583            case ModuleSidebarInterface::class:
584                return $modules->sort($this->sidebarComparator());
585
586            case ModuleTabInterface::class:
587                return $modules->sort($this->tabComparator());
588
589            default:
590                if ($sort) {
591                    return $modules->sort($this->moduleComparator());
592                }
593
594                return $modules;
595        }
596    }
597
598    /**
599     * All modules.
600     *
601     * @param bool $include_disabled
602     *
603     * @return Collection<string,ModuleInterface>
604     */
605    public function all(bool $include_disabled = false): Collection
606    {
607        return Registry::cache()->array()->remember('all-modules', function (): Collection {
608            // Modules have a default status, order etc.
609            // We can override these from database settings.
610            $module_info = DB::table('module')
611                ->get()
612                ->mapWithKeys(static function (object $row): array {
613                    return [$row->module_name => $row];
614                });
615
616            return $this->coreModules()
617                ->merge($this->customModules())
618                ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface {
619                    $info = $module_info->get($module->name());
620
621                    if (is_object($info)) {
622                        $module->setEnabled($info->status === 'enabled');
623
624                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
625                            $module->setFooterOrder((int) $info->footer_order);
626                        }
627
628                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
629                            $module->setMenuOrder((int) $info->menu_order);
630                        }
631
632                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
633                            $module->setSidebarOrder((int) $info->sidebar_order);
634                        }
635
636                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
637                            $module->setTabOrder((int) $info->tab_order);
638                        }
639                    } else {
640                        $module->setEnabled($module->isEnabledByDefault());
641
642                        DB::table('module')->insert([
643                            'module_name' => $module->name(),
644                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
645                        ]);
646                    }
647
648                    return $module;
649                });
650        })->filter($this->enabledFilter($include_disabled));
651    }
652
653    /**
654     * All core modules in the system.
655     *
656     * @return Collection<string,ModuleInterface>
657     */
658    private function coreModules(): Collection
659    {
660        return Collection::make(self::CORE_MODULES)
661            ->map(static function (string $class, string $name): ModuleInterface {
662                $module = app($class);
663                assert($module instanceof ModuleInterface);
664
665                $module->setName($name);
666
667                return $module;
668            });
669    }
670
671    /**
672     * All custom modules in the system.  Custom modules are defined in modules_v4/
673     *
674     * @return Collection<string,ModuleCustomInterface>
675     */
676    private function customModules(): Collection
677    {
678        $pattern   = Webtrees::MODULES_DIR . '*/module.php';
679        $filenames = glob($pattern, GLOB_NOSORT);
680
681        return Collection::make($filenames)
682            ->filter(static function (string $filename): bool {
683                // Special characters will break PHP variable names.
684                // This also allows us to ignore modules called "foo.example" and "foo.disable"
685                $module_name = basename(dirname($filename));
686
687                foreach (['.', ' ', '[', ']'] as $character) {
688                    if (str_contains($module_name, $character)) {
689                        return false;
690                    }
691                }
692
693                return strlen($module_name) <= 30;
694            })
695            ->map(static function (string $filename): ?ModuleCustomInterface {
696                $module = self::load($filename);
697
698                if ($module instanceof ModuleCustomInterface) {
699                    $module->setName('_' . basename(dirname($filename)) . '_');
700
701                    return $module;
702                }
703
704                return null;
705            })
706            ->filter()
707            ->mapWithKeys(static function (ModuleCustomInterface $module): array {
708                return [$module->name() => $module];
709            });
710    }
711
712    /**
713     * Load a custom module in a static scope, to prevent it from modifying local or object variables.
714     *
715     * @param string $filename
716     *
717     * @return ModuleInterface|null
718     */
719    private static function load(string $filename): ?ModuleInterface
720    {
721        try {
722            return include $filename;
723        } catch (Throwable $exception) {
724            $module_name = basename(dirname($filename));
725            $message     = 'Fatal error in module: ' . $module_name . '<br>' . $exception;
726            FlashMessages::addMessage($message, 'danger');
727        }
728
729        return null;
730    }
731
732    /**
733     * A function filter modules by enabled/disabled
734     *
735     * @param bool $include_disabled
736     *
737     * @return Closure
738     */
739    private function enabledFilter(bool $include_disabled): Closure
740    {
741        return static function (ModuleInterface $module) use ($include_disabled): bool {
742            return $include_disabled || $module->isEnabled();
743        };
744    }
745
746    /**
747     * A function filter modules by type
748     *
749     * @param string $interface
750     *
751     * @return Closure
752     */
753    private function interfaceFilter(string $interface): Closure
754    {
755        return static function (ModuleInterface $module) use ($interface): bool {
756            return $module instanceof $interface;
757        };
758    }
759
760    /**
761     * A function to sort footers
762     *
763     * @return Closure
764     */
765    private function footerComparator(): Closure
766    {
767        return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
768            return $x->getFooterOrder() <=> $y->getFooterOrder();
769        };
770    }
771
772    /**
773     * A function to sort menus
774     *
775     * @return Closure
776     */
777    private function menuComparator(): Closure
778    {
779        return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
780            return $x->getMenuOrder() <=> $y->getMenuOrder();
781        };
782    }
783
784    /**
785     * A function to sort menus
786     *
787     * @return Closure
788     */
789    private function sidebarComparator(): Closure
790    {
791        return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
792            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
793        };
794    }
795
796    /**
797     * A function to sort menus
798     *
799     * @return Closure
800     */
801    private function tabComparator(): Closure
802    {
803        return static function (ModuleTabInterface $x, ModuleTabInterface $y): int {
804            return $x->getTabOrder() <=> $y->getTabOrder();
805        };
806    }
807
808    /**
809     * A function to sort modules by name.
810     *
811     * Languages have a "sortable" name, so that "British English" sorts as "English, British".
812     * This provides a more natural order in the language menu.
813     *
814     * @return Closure
815     */
816    private function moduleComparator(): Closure
817    {
818        return static function (ModuleInterface $x, ModuleInterface $y): int {
819            $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();
820            $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();
821
822            return I18N::comparator()($title1, $title2);
823        };
824    }
825
826    /**
827     * During setup, we'll need access to some languages.
828     *
829     * @return Collection<string,ModuleLanguageInterface>
830     */
831    public function setupLanguages(): Collection
832    {
833        return $this->coreModules()
834            ->filter(static function (ModuleInterface $module): bool {
835                return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();
836            })
837            ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int {
838                return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();
839            });
840    }
841
842    /**
843     * Find a specified module, if it is currently active.
844     *
845     * @param string $module_name
846     * @param bool   $include_disabled
847     *
848     * @return ModuleInterface|null
849     */
850    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
851    {
852        return $this->all($include_disabled)
853            ->first(static function (ModuleInterface $module) use ($module_name): bool {
854                return $module->name() === $module_name;
855            });
856    }
857
858    /**
859     * Configuration settings are available through the various "module component" pages.
860     * For modules that do not provide a component, we need to list them separately.
861     *
862     * @param bool $include_disabled
863     *
864     * @return Collection<string,ModuleInterface>
865     */
866    public function otherModules(bool $include_disabled = false): Collection
867    {
868        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
869            ->filter(static function (ModuleInterface $module): bool {
870                foreach (self::COMPONENTS as $interface) {
871                    if ($module instanceof $interface) {
872                        return false;
873                    }
874                }
875
876                return true;
877            });
878    }
879
880    /**
881     * Generate a list of module names which exist in the database but not on disk.
882     *
883     * @return Collection<int,string>
884     */
885    public function deletedModules(): Collection
886    {
887        $database_modules = DB::table('module')->pluck('module_name');
888
889        $disk_modules = $this->all(true)
890            ->map(static function (ModuleInterface $module): string {
891                return $module->name();
892            });
893
894        return $database_modules->diff($disk_modules);
895    }
896
897    /**
898     * Boot all the modules.
899     *
900     * @param ModuleThemeInterface $current_theme
901     */
902    public function bootModules(ModuleThemeInterface $current_theme): void
903    {
904        foreach ($this->all() as $module) {
905            // Only bootstrap the current theme.
906            if ($module instanceof ModuleThemeInterface && $module !== $current_theme) {
907                continue;
908            }
909
910            $module->boot();
911        }
912    }
913
914    /**
915     * @return Collection<int,string>
916     */
917    public function componentsWithAccess(): Collection
918    {
919        return new Collection(self::COMPONENTS_WITH_ACCESS);
920    }
921
922    /**
923     * @return Collection<int,string>
924     */
925    public function componentsWithOrder(): Collection
926    {
927        return new Collection(self::COMPONENTS_WITH_SORT);
928    }
929}
930