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