| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use MediaWiki\Config\Config; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Rest\Handler; |
| 9 |
use MediaWiki\Rest\Module\Module; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class DiscoveryHandler extends Handler { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private const CONSTRUCTOR_OPTIONS = [ |
| 21 |
MainConfigNames::RightsUrl, |
| 22 |
MainConfigNames::RightsText, |
| 23 |
MainConfigNames::EmergencyContact, |
| 24 |
MainConfigNames::Sitename, |
| 25 |
MainConfigNames::Server, |
| 26 |
]; |
| 27 |
|
| 28 |
|
| 29 |
private ServiceOptions $options; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( Config $config ) { |
| 35 |
$options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config ); |
| 36 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 37 |
$this->options = $options; |
| 38 |
} |
| 39 |
|
| 40 |
public function execute() { |
| 41 |
|
| 42 |
return [ |
| 43 |
'mw-discovery' => '1.0', |
| 44 |
'$schema' => 'https://www.mediawiki.org/schema/discovery-1.0', |
| 45 |
'info' => $this->getInfoSpec(), |
| 46 |
'servers' => $this->getServerList(), |
| 47 |
'modules' => $this->getModuleMap(), |
| 48 |
|
| 49 |
|
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
private function getModuleMap(): array { |
| 54 |
$modules = []; |
| 55 |
|
| 56 |
foreach ( $this->getRouter()->getModuleIds() as $moduleName ) { |
| 57 |
$module = $this->getRouter()->getModule( $moduleName ); |
| 58 |
|
| 59 |
if ( $module ) { |
| 60 |
$modules[$moduleName] = $this->getModuleSpec( $moduleName, $module ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $modules; |
| 65 |
} |
| 66 |
|
| 67 |
private function getServerList(): array { |
| 68 |
|
| 69 |
return [ |
| 70 |
[ |
| 71 |
'url' => $this->getRouter()->getRouteUrl( '' ), |
| 72 |
] |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
private function getInfoSpec() { |
| 77 |
return [ |
| 78 |
'title' => $this->options->get( MainConfigNames::Sitename ), |
| 79 |
'mediawiki' => MW_VERSION, |
| 80 |
'license' => $this->getLicenseSpec(), |
| 81 |
'contact' => $this->getContactSpec(), |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
private function getLicenseSpec(): array { |
| 90 |
|
| 91 |
|
| 92 |
return [ |
| 93 |
'name' => $this->options->get( MainConfigNames::RightsText ), |
| 94 |
'url' => $this->options->get( MainConfigNames::RightsUrl ), |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
private function getContactSpec(): array { |
| 99 |
|
| 100 |
return [ |
| 101 |
'email' => $this->options->get( MainConfigNames::EmergencyContact ), |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
private function getModuleSpec( string $moduleId, Module $module ): array { |
| 106 |
return $module->getModuleDescription(); |
| 107 |
} |
| 108 |
|
| 109 |
} |