41 private bool $mIsHtml;
42 private string $mFormat;
43 private string $mBuffer =
'';
44 private bool $mDisabled =
false;
46 private $mIsWrappedHtml =
false;
48 private $mHttpStatus =
false;
59 parent::__construct( $main, $format );
61 $this->mIsHtml = str_ends_with( $format,
'fm' );
62 if ( $this->mIsHtml ) {
63 $this->mFormat = substr( $format, 0, -2 );
64 $this->mIsWrappedHtml = $this->
getMain()->getCheck(
'wrappedhtml' );
66 $this->mFormat = $format;
68 $this->mFormat = strtoupper( $this->mFormat );
91 return 'api-result-wrapped.json';
95 return 'api-result.html';
99 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $this->
getMimeType() )
100 ?? strtolower( $this->mFormat );
101 return "api-result.$ext";
110 return $this->mFormat;
120 return $this->mIsHtml;
130 return $this->mIsWrappedHtml;
139 $this->mDisabled =
true;
148 return $this->mDisabled;
170 $this->mForceDefaultParams =
true;
179 if ( !$this->mForceDefaultParams ) {
180 return parent::getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
183 if ( !is_array( $paramSettings ) ) {
184 return $paramSettings;
187 return $paramSettings[ParamValidator::PARAM_DEFAULT] ??
null;
196 if ( $this->mDisabled ) {
201 $this->mHttpStatus = $code;
203 $this->
getMain()->getRequest()->response()->statusHeader( $code );
212 if ( $this->mDisabled ) {
216 if ( $this->
getIsHtml() && $this->
getMain()->getCacheMode() ===
'public' ) {
218 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
222 ?
'text/mediawiki-api-prettyprint-wrapped'
227 if ( $mime ===
null ) {
231 $this->
getMain()->getRequest()->response()->header(
"Content-Type: $mime; charset=utf-8" );
235 if ( $apiFrameOptions ) {
236 $this->
getMain()->getRequest()->response()->header(
"X-Frame-Options: $apiFrameOptions" );
241 $header =
'Content-Disposition: inline';
243 $compatFilename = mb_convert_encoding( $filename,
'ISO-8859-1' );
244 if ( preg_match(
'/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) {
245 $header .=
'; filename=' . $compatFilename;
248 . preg_replace(
'/([\0-\x1f"\x5c\x7f])/',
'\\\\$1', $compatFilename ) .
'"';
250 if ( $compatFilename !== $filename ) {
251 $value =
"UTF-8''" . rawurlencode( $filename );
253 $value = strtr( $value, [
254 '%21' =>
'!',
'%23' =>
'#',
'%24' =>
'$',
'%26' =>
'&',
'%2B' =>
'+',
'%5E' =>
'^',
255 '%60' =>
'`',
'%7C' =>
'|',
257 $header .=
'; filename*=' . $value;
266 if ( $this->mDisabled ) {
271 if ( $this->
getIsHtml() && $mime !==
null ) {
273 $lcformat = strtolower( $format );
278 $context->setSkin( $skinFactory->makeSkin(
'apioutput' ) );
281 $context->setOutput( $out );
283 $out->setRobotPolicy(
'noindex,nofollow' );
284 $out->addModuleStyles(
'mediawiki.apipretty' );
285 $out->setPageTitleMsg( $context->msg(
'api-format-title' ) );
291 $nonHtmlUrl = strtok( $this->
getRequest()->getFullRequestURL(),
'?' )
292 .
'?' . $this->
getRequest()->appendQueryValue(
'format', $lcformat );
293 $msg = $context->msg(
'api-format-prettyprint-header-hyperlinked' )
294 ->params( $format, $lcformat, $nonHtmlUrl );
296 $msg = $context->msg(
'api-format-prettyprint-header' )->params( $format, $lcformat );
299 $msg = $context->msg(
'api-format-prettyprint-header-only-html' )->params( $format );
302 $header = $msg->parseAsBlock();
304 Html::rawElement(
'div', [
'class' =>
'api-pretty-header' ],
309 if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
311 Html::rawElement(
'div', [
'class' => [
'api-pretty-header',
'api-pretty-status' ] ],
313 'api-format-prettyprint-status',
315 HttpStatus::getMessage( $this->mHttpStatus )
322 if ( $this->
getHookRunner()->onApiFormatHighlight( $context, $result, $mime, $format ) ) {
324 Html::element(
'pre', [
'class' =>
'api-pretty-content' ], $result )
330 $time = $this->
getMain()->getRequest()->getElapsedTime();
331 echo FormatJson::encode(
333 'status' => (
int)( $this->mHttpStatus ?: 200 ),
334 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ),
335 'html' => $out->getHTML(),
336 'modules' => array_values( array_unique( array_merge(
338 $out->getModuleStyles()
340 'continue' => $this->
getResult()->getResultData(
'continue' ),
341 'time' => round( $time * 1000 ),
343 false, FormatJson::ALL_OK
348 $out->getMetadata()->setPreventClickjacking(
false );
366 $this->mBuffer .= $text;
375 return $this->mBuffer;
381 $ret[
'wrappedhtml'] = [
382 ParamValidator::PARAM_DEFAULT =>
false,
391 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->
getModuleName()
392 => [
'apihelp-format-example-generic', $this->
getFormat() ]
397 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats';
408class_alias( ApiFormatBase::class,
'ApiFormatBase' );
This is the main API class, used for both external and internal processing.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
An IContextSource implementation which will inherit context from another source but allow individual ...
A class containing constants representing the names of configuration variables.
const ApiFrameOptions
Name constant for the ApiFrameOptions setting, for use with Config::get()
This is one of the Core classes and should be read at least once by any new developers.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...