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 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Services; 20 21use Closure; 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Module\AhnentafelReportModule; 26use Fisharebest\Webtrees\Module\AlbumModule; 27use Fisharebest\Webtrees\Module\AncestorsChartModule; 28use Fisharebest\Webtrees\Module\BatchUpdateModule; 29use Fisharebest\Webtrees\Module\BingWebmasterToolsModule; 30use Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule; 31use Fisharebest\Webtrees\Module\BirthReportModule; 32use Fisharebest\Webtrees\Module\BranchesListModule; 33use Fisharebest\Webtrees\Module\BritishMonarchs; 34use Fisharebest\Webtrees\Module\BritishPrimeMinisters; 35use Fisharebest\Webtrees\Module\BritishSocialHistory; 36use Fisharebest\Webtrees\Module\CalendarMenuModule; 37use Fisharebest\Webtrees\Module\CemeteryReportModule; 38use Fisharebest\Webtrees\Module\CensusAssistantModule; 39use Fisharebest\Webtrees\Module\ChangeReportModule; 40use Fisharebest\Webtrees\Module\ChartsBlockModule; 41use Fisharebest\Webtrees\Module\ChartsMenuModule; 42use Fisharebest\Webtrees\Module\CkeditorModule; 43use Fisharebest\Webtrees\Module\ClippingsCartModule; 44use Fisharebest\Webtrees\Module\CloudsTheme; 45use Fisharebest\Webtrees\Module\ColorsTheme; 46use Fisharebest\Webtrees\Module\CompactTreeChartModule; 47use Fisharebest\Webtrees\Module\ContactsFooterModule; 48use Fisharebest\Webtrees\Module\CookieWarningModule; 49use Fisharebest\Webtrees\Module\CustomCssJsModule; 50use Fisharebest\Webtrees\Module\DeathReportModule; 51use Fisharebest\Webtrees\Module\DescendancyChartModule; 52use Fisharebest\Webtrees\Module\DescendancyModule; 53use Fisharebest\Webtrees\Module\DescendancyReportModule; 54use Fisharebest\Webtrees\Module\FabTheme; 55use Fisharebest\Webtrees\Module\FactSourcesReportModule; 56use Fisharebest\Webtrees\Module\FamilyBookChartModule; 57use Fisharebest\Webtrees\Module\FamilyGroupReportModule; 58use Fisharebest\Webtrees\Module\FamilyListModule; 59use Fisharebest\Webtrees\Module\FamilyNavigatorModule; 60use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule; 61use Fisharebest\Webtrees\Module\FamilyTreeNewsModule; 62use Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule; 63use Fisharebest\Webtrees\Module\FanChartModule; 64use Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule; 65use Fisharebest\Webtrees\Module\GoogleAnalyticsModule; 66use Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule; 67use Fisharebest\Webtrees\Module\HitCountFooterModule; 68use Fisharebest\Webtrees\Module\HourglassChartModule; 69use Fisharebest\Webtrees\Module\HtmlBlockModule; 70use Fisharebest\Webtrees\Module\IndividualFactsTabModule; 71use Fisharebest\Webtrees\Module\IndividualFamiliesReportModule; 72use Fisharebest\Webtrees\Module\IndividualListModule; 73use Fisharebest\Webtrees\Module\IndividualMetadataModule; 74use Fisharebest\Webtrees\Module\IndividualReportModule; 75use Fisharebest\Webtrees\Module\InteractiveTreeModule; 76use Fisharebest\Webtrees\Module\LanguageAfrikaans; 77use Fisharebest\Webtrees\Module\LanguageAlbanian; 78use Fisharebest\Webtrees\Module\LanguageArabic; 79use Fisharebest\Webtrees\Module\LanguageBosnian; 80use Fisharebest\Webtrees\Module\LanguageBulgarian; 81use Fisharebest\Webtrees\Module\LanguageCatalan; 82use Fisharebest\Webtrees\Module\LanguageChineseSimplified; 83use Fisharebest\Webtrees\Module\LanguageChineseTraditional; 84use Fisharebest\Webtrees\Module\LanguageCroatian; 85use Fisharebest\Webtrees\Module\LanguageCzech; 86use Fisharebest\Webtrees\Module\LanguageDanish; 87use Fisharebest\Webtrees\Module\LanguageDivehi; 88use Fisharebest\Webtrees\Module\LanguageDutch; 89use Fisharebest\Webtrees\Module\LanguageEnglishAustralia; 90use Fisharebest\Webtrees\Module\LanguageEnglishGreatBritain; 91use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates; 92use Fisharebest\Webtrees\Module\LanguageEstonian; 93use Fisharebest\Webtrees\Module\LanguageFaroese; 94use Fisharebest\Webtrees\Module\LanguageFarsi; 95use Fisharebest\Webtrees\Module\LanguageFinnish; 96use Fisharebest\Webtrees\Module\LanguageFrench; 97use Fisharebest\Webtrees\Module\LanguageFrenchCanada; 98use Fisharebest\Webtrees\Module\LanguageGalician; 99use Fisharebest\Webtrees\Module\LanguageGeorgian; 100use Fisharebest\Webtrees\Module\LanguageGerman; 101use Fisharebest\Webtrees\Module\LanguageGreek; 102use Fisharebest\Webtrees\Module\LanguageHebrew; 103use Fisharebest\Webtrees\Module\LanguageHungarian; 104use Fisharebest\Webtrees\Module\LanguageIcelandic; 105use Fisharebest\Webtrees\Module\LanguageIndonesian; 106use Fisharebest\Webtrees\Module\LanguageItalian; 107use Fisharebest\Webtrees\Module\LanguageJapanese; 108use Fisharebest\Webtrees\Module\LanguageJavanese; 109use Fisharebest\Webtrees\Module\LanguageKazhak; 110use Fisharebest\Webtrees\Module\LanguageKorean; 111use Fisharebest\Webtrees\Module\LanguageLatvian; 112use Fisharebest\Webtrees\Module\LanguageLingala; 113use Fisharebest\Webtrees\Module\LanguageLithuanian; 114use Fisharebest\Webtrees\Module\LanguageMalay; 115use Fisharebest\Webtrees\Module\LanguageMaori; 116use Fisharebest\Webtrees\Module\LanguageMarathi; 117use Fisharebest\Webtrees\Module\LanguageNepalese; 118use Fisharebest\Webtrees\Module\LanguageNorwegianBokmal; 119use Fisharebest\Webtrees\Module\LanguageNorwegianNynorsk; 120use Fisharebest\Webtrees\Module\LanguageOccitan; 121use Fisharebest\Webtrees\Module\LanguagePolish; 122use Fisharebest\Webtrees\Module\LanguagePortuguese; 123use Fisharebest\Webtrees\Module\LanguagePortugueseBrazil; 124use Fisharebest\Webtrees\Module\LanguageRomanian; 125use Fisharebest\Webtrees\Module\LanguageRussian; 126use Fisharebest\Webtrees\Module\LanguageSerbian; 127use Fisharebest\Webtrees\Module\LanguageSerbianLatin; 128use Fisharebest\Webtrees\Module\LanguageSlovakian; 129use Fisharebest\Webtrees\Module\LanguageSlovenian; 130use Fisharebest\Webtrees\Module\LanguageSpanish; 131use Fisharebest\Webtrees\Module\LanguageSundanese; 132use Fisharebest\Webtrees\Module\LanguageSwahili; 133use Fisharebest\Webtrees\Module\LanguageSwedish; 134use Fisharebest\Webtrees\Module\LanguageTamil; 135use Fisharebest\Webtrees\Module\LanguageTatar; 136use Fisharebest\Webtrees\Module\LanguageThai; 137use Fisharebest\Webtrees\Module\LanguageTurkish; 138use Fisharebest\Webtrees\Module\LanguageUkranian; 139use Fisharebest\Webtrees\Module\LanguageVietnamese; 140use Fisharebest\Webtrees\Module\LanguageYiddish; 141use Fisharebest\Webtrees\Module\LifespansChartModule; 142use Fisharebest\Webtrees\Module\ListsMenuModule; 143use Fisharebest\Webtrees\Module\LoggedInUsersModule; 144use Fisharebest\Webtrees\Module\LoginBlockModule; 145use Fisharebest\Webtrees\Module\MarriageReportModule; 146use Fisharebest\Webtrees\Module\MatomoAnalyticsModule; 147use Fisharebest\Webtrees\Module\MediaListModule; 148use Fisharebest\Webtrees\Module\MediaTabModule; 149use Fisharebest\Webtrees\Module\MinimalTheme; 150use Fisharebest\Webtrees\Module\MissingFactsReportModule; 151use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface; 152use Fisharebest\Webtrees\Module\ModuleBlockInterface; 153use Fisharebest\Webtrees\Module\ModuleChartInterface; 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 static 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 */ 429 public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection 430 { 431 return $this->findByInterface($interface, false, true) 432 ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool { 433 return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user); 434 }); 435 } 436 437 /** 438 * All modules which provide a specific function. 439 * 440 * @param string $interface 441 * @param bool $include_disabled 442 * @param bool $sort 443 * 444 * @return Collection 445 */ 446 public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection 447 { 448 $modules = $this->all($include_disabled) 449 ->filter($this->interfaceFilter($interface)); 450 451 switch ($interface) { 452 case ModuleFooterInterface::class: 453 return $modules->sort($this->footerSorter()); 454 455 case ModuleMenuInterface::class: 456 return $modules->sort($this->menuSorter()); 457 458 case ModuleSidebarInterface::class: 459 return $modules->sort($this->sidebarSorter()); 460 461 case ModuleTabInterface::class: 462 return $modules->sort($this->tabSorter()); 463 464 default: 465 if ($sort) { 466 return $modules->sort($this->moduleSorter()); 467 } 468 469 return $modules; 470 } 471 } 472 473 /** 474 * All modules. 475 * 476 * @param bool $include_disabled 477 * 478 * @return Collection 479 */ 480 public function all(bool $include_disabled = false): Collection 481 { 482 return app('cache.array')->rememberForever('all_modules', function (): Collection { 483 // Modules have a default status, order etc. 484 // We can override these from database settings. 485 $module_info = DB::table('module') 486 ->get() 487 ->mapWithKeys(static function (stdClass $row): array { 488 return [$row->module_name => $row]; 489 }); 490 491 return $this->coreModules() 492 ->merge($this->customModules()) 493 ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface { 494 $info = $module_info->get($module->name()); 495 496 if ($info instanceof stdClass) { 497 $module->setEnabled($info->status === 'enabled'); 498 499 if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) { 500 $module->setFooterOrder((int) $info->footer_order); 501 } 502 503 if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) { 504 $module->setMenuOrder((int) $info->menu_order); 505 } 506 507 if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) { 508 $module->setSidebarOrder((int) $info->sidebar_order); 509 } 510 511 if ($module instanceof ModuleTabInterface && $info->tab_order !== null) { 512 $module->setTabOrder((int) $info->tab_order); 513 } 514 } else { 515 $module->setEnabled($module->isEnabledByDefault()); 516 517 DB::table('module')->insert([ 518 'module_name' => $module->name(), 519 'status' => $module->isEnabled() ? 'enabled' : 'disabled', 520 ]); 521 } 522 523 return $module; 524 }); 525 })->filter($this->enabledFilter($include_disabled)); 526 } 527 528 /** 529 * All core modules in the system. 530 * 531 * @return Collection 532 */ 533 private function coreModules(): Collection 534 { 535 return Collection::make(self::CORE_MODULES) 536 ->map(static function (string $class, string $name): ModuleInterface { 537 $module = app($class); 538 539 $module->setName($name); 540 541 return $module; 542 }); 543 } 544 545 /** 546 * All custom modules in the system. Custom modules are defined in modules_v4/ 547 * 548 * @return Collection 549 */ 550 private function customModules(): Collection 551 { 552 $pattern = Webtrees::MODULES_DIR . '*/module.php'; 553 $filenames = glob($pattern, GLOB_NOSORT); 554 555 return Collection::make($filenames) 556 ->filter(static function (string $filename): bool { 557 // Special characters will break PHP variable names. 558 // This also allows us to ignore modules called "foo.example" and "foo.disable" 559 $module_name = basename(dirname($filename)); 560 561 return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30; 562 }) 563 ->map(static function (string $filename): ?ModuleCustomInterface { 564 try { 565 $module = self::load($filename); 566 567 if ($module instanceof ModuleCustomInterface) { 568 $module_name = '_' . basename(dirname($filename)) . '_'; 569 570 $module->setName($module_name); 571 } else { 572 return null; 573 } 574 575 return $module; 576 } catch (Throwable $ex) { 577 // It would be nice to show this error in a flash-message or similar, but the framework 578 // has not yet been initialised so we have no themes, languages, sessions, etc. 579 throw $ex; 580 } 581 }) 582 ->filter() 583 ->mapWithKeys(static function (ModuleCustomInterface $module): array { 584 return [$module->name() => $module]; 585 }); 586 } 587 588 /** 589 * During setup, we'll need access to some languages. 590 * 591 * @return Collection 592 */ 593 public function setupLanguages(): Collection 594 { 595 return $this->coreModules() 596 ->filter(static function (ModuleInterface $module): bool { 597 return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault(); 598 }) 599 ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int { 600 return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable(); 601 }); 602 } 603 604 /** 605 * Load a module in a static scope, to prevent it from modifying local or object variables. 606 * 607 * @param string $filename 608 * 609 * @return mixed 610 */ 611 private static function load(string $filename) 612 { 613 return include $filename; 614 } 615 616 /** 617 * A function filter modules by enabled/disabled 618 * 619 * @param bool $include_disabled 620 * 621 * @return Closure 622 */ 623 private function enabledFilter(bool $include_disabled): Closure 624 { 625 return static function (ModuleInterface $module) use ($include_disabled): bool { 626 return $include_disabled || $module->isEnabled(); 627 }; 628 } 629 630 /** 631 * A function filter modules by type 632 * 633 * @param string $interface 634 * 635 * @return Closure 636 */ 637 private function interfaceFilter(string $interface): Closure 638 { 639 return static function (ModuleInterface $module) use ($interface): bool { 640 return $module instanceof $interface; 641 }; 642 } 643 644 /** 645 * A function to sort footers 646 * 647 * @return Closure 648 */ 649 private function footerSorter(): Closure 650 { 651 return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int { 652 return $x->getFooterOrder() <=> $y->getFooterOrder(); 653 }; 654 } 655 656 /** 657 * A function to sort menus 658 * 659 * @return Closure 660 */ 661 private function menuSorter(): Closure 662 { 663 return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int { 664 return $x->getMenuOrder() <=> $y->getMenuOrder(); 665 }; 666 } 667 668 /** 669 * A function to sort menus 670 * 671 * @return Closure 672 */ 673 private function sidebarSorter(): Closure 674 { 675 return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int { 676 return $x->getSidebarOrder() <=> $y->getSidebarOrder(); 677 }; 678 } 679 680 /** 681 * A function to sort menus 682 * 683 * @return Closure 684 */ 685 private function tabSorter(): Closure 686 { 687 return static function (ModuleTabInterface $x, ModuleTabInterface $y): int { 688 return $x->getTabOrder() <=> $y->getTabOrder(); 689 }; 690 } 691 692 /** 693 * A function to sort modules by name. 694 * 695 * Languages have a "sortable" name, so that "British English" sorts as "English, British". 696 * This provides a more natural order in the language menu. 697 * 698 * @return Closure 699 */ 700 private function moduleSorter(): Closure 701 { 702 return static function (ModuleInterface $x, ModuleInterface $y): int { 703 $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title(); 704 $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title(); 705 706 return I18N::strcasecmp($title1, $title2); 707 }; 708 } 709 710 /** 711 * Find a specified module, if it is currently active. 712 * 713 * @param string $module_name 714 * @param bool $include_disabled 715 * 716 * @return ModuleInterface|null 717 */ 718 public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface 719 { 720 return $this->all($include_disabled) 721 ->first(static function (ModuleInterface $module) use ($module_name): bool { 722 return $module->name() === $module_name; 723 }); 724 } 725 726 /** 727 * Configuration settings are available through the various "module component" pages. 728 * For modules that do not provide a component, we need to list them separately. 729 * 730 * @param bool $include_disabled 731 * 732 * @return Collection 733 */ 734 public function otherModules(bool $include_disabled = false): Collection 735 { 736 return $this->findByInterface(ModuleInterface::class, $include_disabled, true) 737 ->filter(static function (ModuleInterface $module): bool { 738 foreach (self::COMPONENTS as $interface) { 739 if ($module instanceof $interface) { 740 return false; 741 } 742 } 743 744 return true; 745 }); 746 } 747 748 /** 749 * Generate a list of module names which exist in the database but not on disk. 750 * 751 * @return Collection 752 */ 753 public function deletedModules(): Collection 754 { 755 $database_modules = DB::table('module')->pluck('module_name'); 756 757 $disk_modules = $this->all(true) 758 ->map(static function (ModuleInterface $module): string { 759 return $module->name(); 760 }); 761 762 return $database_modules->diff($disk_modules); 763 } 764} 765