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