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