33 private $mIsHtml, $mFormat;
34 private $mBuffer, $mDisabled =
false;
35 private $mIsWrappedHtml =
false;
36 private $mHttpStatus =
false;
45 parent::__construct( $main, $format );
47 $this->mIsHtml = ( substr( $format, -2, 2 ) ===
'fm' );
48 if ( $this->mIsHtml ) {
49 $this->mFormat = substr( $format, 0, -2 );
50 $this->mIsWrappedHtml = $this->
getMain()->getCheck(
'wrappedhtml' );
52 $this->mFormat = $format;
54 $this->mFormat = strtoupper( $this->mFormat );
76 return 'api-result-wrapped.json';
78 return 'api-result.html';
80 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
81 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $this->
getMimeType() )
82 ?? strtolower( $this->mFormat );
83 return "api-result.$ext";
92 return $this->mFormat;
102 return $this->mIsHtml;
111 return $this->mIsWrappedHtml;
120 $this->mDisabled =
true;
128 return $this->mDisabled;
150 $this->mForceDefaultParams =
true;
159 if ( !$this->mForceDefaultParams ) {
160 return parent::getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
163 if ( !is_array( $paramSettings ) ) {
164 return $paramSettings;
167 return $paramSettings[ParamValidator::PARAM_DEFAULT] ??
null;
176 if ( $this->mDisabled ) {
181 $this->mHttpStatus = $code;
183 $this->
getMain()->getRequest()->response()->statusHeader( $code );
192 if ( $this->mDisabled ) {
197 ?
'text/mediawiki-api-prettyprint-wrapped'
202 if (
$mime ===
null ) {
206 $this->
getMain()->getRequest()->response()->header(
"Content-Type: $mime; charset=utf-8" );
209 $apiFrameOptions = $this->
getConfig()->get( MainConfigNames::ApiFrameOptions );
210 if ( $apiFrameOptions ) {
211 $this->
getMain()->getRequest()->response()->header(
"X-Frame-Options: $apiFrameOptions" );
216 $header =
'Content-Disposition: inline';
218 $compatFilename = mb_convert_encoding( $filename,
'ISO-8859-1' );
219 if ( preg_match(
'/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) {
220 $header .=
'; filename=' . $compatFilename;
223 . preg_replace(
'/([\0-\x1f"\x5c\x7f])/',
'\\\\$1', $compatFilename ) .
'"';
225 if ( $compatFilename !== $filename ) {
226 $value =
"UTF-8''" . rawurlencode( $filename );
228 $value = strtr( $value, [
229 '%21' =>
'!',
'%23' =>
'#',
'%24' =>
'$',
'%26' =>
'&',
'%2B' =>
'+',
'%5E' =>
'^',
230 '%60' =>
'`',
'%7C' =>
'|',
232 $header .=
'; filename*=' . $value;
241 if ( $this->mDisabled ) {
248 $lcformat = strtolower( $format );
252 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
253 $context->setSkin( $skinFactory->makeSkin(
'apioutput' ) );
256 $context->setOutput( $out );
258 $out->setRobotPolicy(
'noindex,nofollow' );
259 $out->addModuleStyles(
'mediawiki.apipretty' );
260 $out->setPageTitle( $context->msg(
'api-format-title' ) );
266 $nonHtmlUrl = strtok( $this->
getRequest()->getFullRequestURL(),
'?' )
267 .
'?' . $this->
getRequest()->appendQueryValue(
'format', $lcformat );
268 $msg = $context->msg(
'api-format-prettyprint-header-hyperlinked' )
269 ->params( $format, $lcformat, $nonHtmlUrl );
271 $msg = $context->msg(
'api-format-prettyprint-header' )->params( $format, $lcformat );
274 $msg = $context->msg(
'api-format-prettyprint-header-only-html' )->params( $format );
277 $header = $msg->parseAsBlock();
279 Html::rawElement(
'div', [
'class' =>
'api-pretty-header' ],
284 if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
286 Html::rawElement(
'div', [
'class' => [
'api-pretty-header',
'api-pretty-status' ] ],
288 'api-format-prettyprint-status',
290 HttpStatus::getMessage( $this->mHttpStatus )
297 if ( $this->
getHookRunner()->onApiFormatHighlight( $context, $result,
$mime, $format ) ) {
299 Html::element(
'pre', [
'class' =>
'api-pretty-content' ], $result )
305 $time = $this->
getMain()->getRequest()->getElapsedTime();
306 echo FormatJson::encode(
308 'status' => (
int)( $this->mHttpStatus ?: 200 ),
309 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ),
310 'html' => $out->getHTML(),
311 'modules' => array_values( array_unique( array_merge(
313 $out->getModuleStyles()
315 'continue' => $this->
getResult()->getResultData(
'continue' ),
316 'time' => round( $time * 1000 ),
318 false, FormatJson::ALL_OK
323 $out->setPreventClickjacking(
false );
340 $this->mBuffer .= $text;
348 return $this->mBuffer;
354 $ret[
'wrappedhtml'] = [
355 ParamValidator::PARAM_DEFAULT =>
false,
364 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->
getModuleName()
365 => [
'apihelp-format-example-generic', $this->
getFormat() ]
370 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.
getResult()
Get the result object.
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 ...
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