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