34 private $mIsHtml, $mFormat;
35 private $mBuffer, $mDisabled =
false;
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";
93 return $this->mFormat;
103 return $this->mIsHtml;
112 return $this->mIsWrappedHtml;
121 $this->mDisabled =
true;
129 return $this->mDisabled;
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 ) {
197 if ( $this->
getIsHtml() && $this->
getMain()->getCacheMode() ===
'public' ) {
199 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
203 ?
'text/mediawiki-api-prettyprint-wrapped'
208 if (
$mime ===
null ) {
212 $this->
getMain()->getRequest()->response()->header(
"Content-Type: $mime; charset=utf-8" );
215 $apiFrameOptions = $this->
getConfig()->get( MainConfigNames::ApiFrameOptions );
216 if ( $apiFrameOptions ) {
217 $this->
getMain()->getRequest()->response()->header(
"X-Frame-Options: $apiFrameOptions" );
222 $header =
'Content-Disposition: inline';
224 $compatFilename = mb_convert_encoding( $filename,
'ISO-8859-1' );
225 if ( preg_match(
'/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) {
226 $header .=
'; filename=' . $compatFilename;
229 . preg_replace(
'/([\0-\x1f"\x5c\x7f])/',
'\\\\$1', $compatFilename ) .
'"';
231 if ( $compatFilename !== $filename ) {
232 $value =
"UTF-8''" . rawurlencode( $filename );
234 $value = strtr( $value, [
235 '%21' =>
'!',
'%23' =>
'#',
'%24' =>
'$',
'%26' =>
'&',
'%2B' =>
'+',
'%5E' =>
'^',
236 '%60' =>
'`',
'%7C' =>
'|',
238 $header .=
'; filename*=' . $value;
247 if ( $this->mDisabled ) {
254 $lcformat = strtolower( $format );
258 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
259 $context->setSkin( $skinFactory->makeSkin(
'apioutput' ) );
262 $context->setOutput( $out );
264 $out->setRobotPolicy(
'noindex,nofollow' );
265 $out->addModuleStyles(
'mediawiki.apipretty' );
266 $out->setPageTitle( $context->msg(
'api-format-title' ) );
272 $nonHtmlUrl = strtok( $this->
getRequest()->getFullRequestURL(),
'?' )
273 .
'?' . $this->
getRequest()->appendQueryValue(
'format', $lcformat );
274 $msg = $context->msg(
'api-format-prettyprint-header-hyperlinked' )
275 ->params( $format, $lcformat, $nonHtmlUrl );
277 $msg = $context->msg(
'api-format-prettyprint-header' )->params( $format, $lcformat );
280 $msg = $context->msg(
'api-format-prettyprint-header-only-html' )->params( $format );
283 $header = $msg->parseAsBlock();
285 Html::rawElement(
'div', [
'class' =>
'api-pretty-header' ],
290 if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
292 Html::rawElement(
'div', [
'class' => [
'api-pretty-header',
'api-pretty-status' ] ],
294 'api-format-prettyprint-status',
296 HttpStatus::getMessage( $this->mHttpStatus )
303 if ( $this->
getHookRunner()->onApiFormatHighlight( $context, $result,
$mime, $format ) ) {
305 Html::element(
'pre', [
'class' =>
'api-pretty-content' ], $result )
311 $time = $this->
getMain()->getRequest()->getElapsedTime();
312 echo FormatJson::encode(
314 'status' => (
int)( $this->mHttpStatus ?: 200 ),
315 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ),
316 'html' => $out->getHTML(),
317 'modules' => array_values( array_unique( array_merge(
319 $out->getModuleStyles()
321 'continue' => $this->
getResult()->getResultData(
'continue' ),
322 'time' => round( $time * 1000 ),
324 false, FormatJson::ALL_OK
329 $out->setPreventClickjacking(
false );
346 $this->mBuffer .= $text;
354 return $this->mBuffer;
360 $ret[
'wrappedhtml'] = [
361 ParamValidator::PARAM_DEFAULT =>
false,
370 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->
getModuleName()
371 => [
'apihelp-format-example-generic', $this->
getFormat() ]
376 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