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