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