36 private $mIsWrappedHtml =
false;
37 private $mHttpStatus =
false;
46 parent::__construct( $main, $format );
48 $this->mIsHtml = str_ends_with( $format,
'fm' );
49 if ( $this->mIsHtml ) {
50 $this->mFormat = substr( $format, 0, -2 );
51 $this->mIsWrappedHtml = $this->
getMain()->getCheck(
'wrappedhtml' );
53 $this->mFormat = $format;
55 $this->mFormat = strtoupper( $this->mFormat );
77 return 'api-result-wrapped.json';
79 return 'api-result.html';
81 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
82 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $this->
getMimeType() )
83 ?? strtolower( $this->mFormat );
84 return "api-result.$ext";
103 return $this->mIsHtml;
112 return $this->mIsWrappedHtml;
121 $this->mDisabled =
true;
151 $this->mForceDefaultParams =
true;
160 if ( !$this->mForceDefaultParams ) {
161 return parent::getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
164 if ( !is_array( $paramSettings ) ) {
165 return $paramSettings;
168 return $paramSettings[ParamValidator::PARAM_DEFAULT] ??
null;
177 if ( $this->mDisabled ) {
182 $this->mHttpStatus = $code;
184 $this->
getMain()->getRequest()->response()->statusHeader( $code );
193 if ( $this->mDisabled ) {
198 ?
'text/mediawiki-api-prettyprint-wrapped'
203 if (
$mime ===
null ) {
207 $this->
getMain()->getRequest()->response()->header(
"Content-Type: $mime; charset=utf-8" );
210 $apiFrameOptions = $this->
getConfig()->get( MainConfigNames::ApiFrameOptions );
211 if ( $apiFrameOptions ) {
212 $this->
getMain()->getRequest()->response()->header(
"X-Frame-Options: $apiFrameOptions" );
217 $header =
'Content-Disposition: inline';
219 $compatFilename = mb_convert_encoding( $filename,
'ISO-8859-1' );
220 if ( preg_match(
'/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) {
221 $header .=
'; filename=' . $compatFilename;
224 . preg_replace(
'/([\0-\x1f"\x5c\x7f])/',
'\\\\$1', $compatFilename ) .
'"';
226 if ( $compatFilename !== $filename ) {
227 $value =
"UTF-8''" . rawurlencode( $filename );
229 $value = strtr( $value, [
230 '%21' =>
'!',
'%23' =>
'#',
'%24' =>
'$',
'%26' =>
'&',
'%2B' =>
'+',
'%5E' =>
'^',
231 '%60' =>
'`',
'%7C' =>
'|',
233 $header .=
'; filename*=' . $value;
242 if ( $this->mDisabled ) {
249 $lcformat = strtolower( $format );
253 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
254 $context->setSkin( $skinFactory->makeSkin(
'apioutput' ) );
257 $context->setOutput( $out );
259 $out->setRobotPolicy(
'noindex,nofollow' );
260 $out->addModuleStyles(
'mediawiki.apipretty' );
261 $out->setPageTitle( $context->msg(
'api-format-title' ) );
267 $nonHtmlUrl = strtok( $this->
getRequest()->getFullRequestURL(),
'?' )
268 .
'?' . $this->
getRequest()->appendQueryValue(
'format', $lcformat );
269 $msg = $context->msg(
'api-format-prettyprint-header-hyperlinked' )
270 ->params( $format, $lcformat, $nonHtmlUrl );
272 $msg = $context->msg(
'api-format-prettyprint-header' )->params( $format, $lcformat );
275 $msg = $context->msg(
'api-format-prettyprint-header-only-html' )->params( $format );
278 $header = $msg->parseAsBlock();
280 Html::rawElement(
'div', [
'class' =>
'api-pretty-header' ],
285 if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
287 Html::rawElement(
'div', [
'class' => [
'api-pretty-header',
'api-pretty-status' ] ],
289 'api-format-prettyprint-status',
298 if ( $this->
getHookRunner()->onApiFormatHighlight( $context, $result,
$mime, $format ) ) {
300 Html::element(
'pre', [
'class' =>
'api-pretty-content' ], $result )
306 $time = $this->
getMain()->getRequest()->getElapsedTime();
309 'status' => (
int)( $this->mHttpStatus ?: 200 ),
311 'html' => $out->getHTML(),
312 'modules' => array_values( array_unique( array_merge(
314 $out->getModuleStyles()
316 'continue' => $this->getResult()->getResultData(
'continue' ),
317 'time' => round( $time * 1000 ),
324 $out->setPreventClickjacking(
false );
341 $this->mBuffer .= $text;
349 return $this->mBuffer;
355 $ret[
'wrappedhtml'] = [
356 ParamValidator::PARAM_DEFAULT =>
false,
365 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->
getModuleName()
366 => [
'apihelp-format-example-generic', $this->
getFormat() ]
371 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats';
This abstract class implements many basic API functions, and is the base of all API classes.
getModuleManager()
Get the module manager, or null if this module has no sub-modules.
getMain()
Get the main module.
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
getModuleName()
Get the name of the module being executed by this instance.
getHookRunner()
Get an ApiHookRunner for running core API hooks.
static fixHelpLinks( $html, $helptitle=null, $localModules=[])
Replace Special:ApiHelp links with links to api.php.
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 ...
static getMessage( $code)
Get the message associated with an HTTP response status code.
A class containing constants representing the names of configuration variables.
This is one of the Core classes and should be read at least once by any new developers.
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,...
if(!is_readable( $file)) $ext