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