MediaWiki master
SpecialRestSandbox.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
18
26
27 private readonly ModuleManager $moduleManager;
28
29 public function __construct(
30 private readonly UrlUtils $urlUtils,
31 MessageFormatterFactory $messageFormatterFactory,
32 BagOStuff $srvCache,
33 ) {
34 parent::__construct( 'RestSandbox' );
35
36 $textFormatter = $messageFormatterFactory->getTextFormatter(
37 $this->getContentLanguage()->getCode()
38 );
39 $responseFactory = new ResponseFactory( [ $textFormatter ] );
40
41 $this->moduleManager = new ModuleManager(
42 new ServiceOptions( ModuleManager::CONSTRUCTOR_OPTIONS, $this->getConfig() ),
43 $srvCache,
44 $responseFactory
45 );
46 }
47
49 public function isListed() {
50 // Hide the special pages if there are no APIs to explore.
51 return $this->moduleManager->hasApiSpecs();
52 }
53
55 protected function getGroupName() {
56 return 'wiki';
57 }
58
59 private function getSpecUrl( array $apiSpecs, string $apiId ): ?string {
60 if ( $apiId !== '' ) {
61 $spec = $apiSpecs[$apiId] ?? null;
62 } else {
63 $spec = reset( $apiSpecs ) ?: null;
64 }
65
66 if ( !$spec ) {
67 return null;
68 }
69
70 return $this->urlUtils->expand( $spec['url'] );
71 }
72
74 public function execute( $subPage ) {
75 $this->setHeaders();
76 $out = $this->getOutput();
77 $this->addHelpLink( 'Help:RestSandbox' );
78
79 $apiId = $this->getRequest()->getRawVal( 'api' ) ?? $subPage ?? '';
80 $apiSpecs = $this->moduleManager->getApiSpecs();
81 $specUrl = $this->getSpecUrl( $apiSpecs, $apiId );
82
83 $out->addJsConfigVars( [
84 'specUrl' => $specUrl
85 ] );
86
87 $out->addModuleStyles( [
88 'mediawiki.special',
89 'mediawiki.hlist',
90 'mediawiki.special.restsandbox.styles'
91 ] );
92
93 if ( !$apiSpecs ) {
94 $out->addHTML( Html::errorBox(
95 $out->msg( 'restsandbox-no-specs-configured' )->parse()
96 ) );
97 return;
98 }
99
100 if ( $out->getLanguage()->getCode() !== 'en' ) {
101 $out->addHTML( Html::noticeBox( $out->msg( 'restsandbox-disclaimer' )->parse() ) );
102 }
103
104 $this->showForm( $apiSpecs, $apiId );
105
106 if ( !$specUrl ) {
107 $out->addHTML( Html::errorBox(
108 $out->msg( 'restsandbox-no-such-api', $apiId )->parse()
109 ) );
110 return;
111 }
112
113 $out->addModules( [
114 'mediawiki.codex.messagebox.styles',
115 'mediawiki.special.restsandbox'
116 ] );
117
118 $out->addHTML( Html::openElement( 'div', [ 'id' => 'mw-restsandbox' ] ) );
119
120 // Hidden when JS is available
121 $out->addHTML( Html::errorBox(
122 $out->msg( 'restsandbox-jsonly' )->parse(),
123 '',
124 'mw-restsandbox-client-nojs'
125 ) );
126
127 // To be replaced by Swagger UI.
128 $out->addElement( 'div', [
129 'id' => 'mw-restsandbox-swagger-ui',
130 // Force direction to "LTR" with swagger-ui.
131 // Since the swagger content is not internationalized, the information is always in English.
132 // We have to force the direction to "LTR" to avoid the content (specifically json strings)
133 // from being mangled.
134 'dir' => 'ltr',
135 'lang' => 'en',
136 // For dark mode compatibility
137 'class' => 'skin-invert'
138 ] );
139
140 $out->addHTML( Html::closeElement( 'div' ) ); // #mw-restsandbox
141 }
142
143 private function showForm( array $apiSpecs, string $apiId ) {
144 $apis = [];
145
146 foreach ( $apiSpecs as $key => $spec ) {
147 $apis[$spec['name']] = $key;
148 }
149
150 $formDescriptor = [
151 'intro' => [
152 'type' => 'info',
153 'raw' => true,
154 'default' => $this->msg( 'restsandbox-text' )->parseAsBlock()
155 ],
156 'api' => [
157 'type' => 'select',
158 'name' => 'api',
159 'label-message' => 'restsandbox-select-api',
160 'options' => $apis,
161 'default' => $apiId
162 ],
163 'title' => [
164 'type' => 'hidden',
165 'name' => 'title',
166 'default' => $this->getPageTitle()->getPrefixedDBkey()
167 ],
168 ];
169
170 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
171
172 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
173 $htmlForm->setAction( $action );
174 $htmlForm->setMethod( 'GET' );
175 $htmlForm->setId( 'mw-restsandbox-form' );
176 $htmlForm->prepareForm()->displayForm( false );
177 }
178}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
A class for passing options to services.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:214
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
The MediaWiki-specific implementation of IMessageFormatterFactory.
getTextFormatter(string $langCode)
Get a text message formatter for a given language.ITextFormatter
Manages information related to REST modules, such as routes and specs.
Generates standardized response objects.
Parent class for all special pages.
getConfig()
Shortcut to get main config object.
getContentLanguage()
Shortcut to get content language.
A special page showing a Swagger UI for exploring REST APIs.
isListed()
Whether this special page is listed in Special:SpecialPages.to override 1.3 (r3583) bool
execute( $subPage)
Default execute method Checks user permissions.This must be overridden by subclasses; it will be made...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(private readonly UrlUtils $urlUtils, MessageFormatterFactory $messageFormatterFactory, BagOStuff $srvCache,)
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73