Debian Codegraph @

includes/Rest/Handler/DiscoveryHandler.php

raw symbols SCIP
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 * Core REST API endpoint that outputs discovery information, including a
13 * list of registered modules.
14 * Inspired by Google's API directory, see https://developers.google.com/discovery/v1/reference.
15 */
16 class DiscoveryHandler extends Handler {
17 /**
18 * @internal
19 */
20 private const CONSTRUCTOR_OPTIONS = [
21 MainConfigNames::RightsUrl,
22 MainConfigNames::RightsText,
23 MainConfigNames::EmergencyContact,
24 MainConfigNames::Sitename,
25 MainConfigNames::Server,
26 ];
27
28 /** @var ServiceOptions */
29 private ServiceOptions $options;
30
31 /**
32 * @param Config $config
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 // NOTE: must match docs/rest/discovery-1.0.json
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 // TODO: link to aggregated spec
49 // TODO: list of component schemas
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 // See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#server-object
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 // TODO: terms of service
83 // TODO: owner/operator
84 // TODO: link to Special:RestSandbox
85 // TODO: link to https://www.mediawiki.org/wiki/API:REST_API
86 ];
87 }
88
89 private function getLicenseSpec(): array {
90 // See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#license-object
91 // TODO: get terms-of-use URL, not content license.
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 // https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contact-object
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 }