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