43 parent::__construct( $main, $format );
45 $this->mIsHtml = ( substr( $format, -2, 2 ) ===
'fm' );
46 if ( $this->mIsHtml ) {
47 $this->mFormat = substr( $format, 0, -2 );
48 $this->mIsWrappedHtml = $this->
getMain()->getCheck(
'wrappedhtml' );
50 $this->mFormat = $format;
52 $this->mFormat = strtoupper( $this->mFormat );
74 return 'api-result-wrapped.json';
76 return 'api-result.html';
78 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
79 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $this->
getMimeType() )
80 ?? strtolower( $this->mFormat );
81 return "api-result.$ext";
118 $this->mDisabled =
true;
148 $this->mForceDefaultParams =
true;
157 if ( !$this->mForceDefaultParams ) {
158 return parent::getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
161 if ( !is_array( $paramSettings ) ) {
162 return $paramSettings;
174 if ( $this->mDisabled ) {
179 $this->mHttpStatus = $code;
181 $this->
getMain()->getRequest()->response()->statusHeader( $code );
190 if ( $this->mDisabled ) {
195 ?
'text/mediawiki-api-prettyprint-wrapped'
200 if (
$mime ===
null ) {
204 $this->
getMain()->getRequest()->response()->header(
"Content-Type: $mime; charset=utf-8" );
207 $apiFrameOptions = $this->
getConfig()->get(
'ApiFrameOptions' );
208 if ( $apiFrameOptions ) {
209 $this->
getMain()->getRequest()->response()->header(
"X-Frame-Options: $apiFrameOptions" );
214 $header =
'Content-Disposition: inline';
216 $compatFilename = mb_convert_encoding( $filename,
'ISO-8859-1' );
217 if ( preg_match(
'/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) {
218 $header .=
'; filename=' . $compatFilename;
221 . preg_replace(
'/([\0-\x1f"\x5c\x7f])/',
'\\\\$1', $compatFilename ) .
'"';
223 if ( $compatFilename !== $filename ) {
224 $value =
"UTF-8''" . rawurlencode( $filename );
226 $value = strtr( $value, [
227 '%21' =>
'!',
'%23' =>
'#',
'%24' =>
'$',
'%26' =>
'&',
'%2B' =>
'+',
'%5E' =>
'^',
228 '%60' =>
'`',
'%7C' =>
'|',
230 $header .=
'; filename*=' . $value;
239 if ( $this->mDisabled ) {
246 $lcformat = strtolower( $format );
250 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
251 $context->setSkin( $skinFactory->makeSkin(
'apioutput' ) );
256 $out->setRobotPolicy(
'noindex,nofollow' );
257 $out->addModuleStyles(
'mediawiki.apipretty' );
258 $out->setPageTitle(
$context->
msg(
'api-format-title' ) );
264 $nonHtmlUrl = strtok( $this->
getRequest()->getFullRequestURL(),
'?' )
265 .
'?' . $this->
getRequest()->appendQueryValue(
'format', $lcformat );
266 $msg =
$context->
msg(
'api-format-prettyprint-header-hyperlinked' )
267 ->params( $format, $lcformat, $nonHtmlUrl );
269 $msg =
$context->
msg(
'api-format-prettyprint-header' )->params( $format, $lcformat );
272 $msg =
$context->
msg(
'api-format-prettyprint-header-only-html' )->params( $format );
275 $header = $msg->parseAsBlock();
277 Html::rawElement(
'div', [
'class' =>
'api-pretty-header' ],
282 if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
284 Html::rawElement(
'div', [
'class' =>
'api-pretty-header api-pretty-status' ],
286 'api-format-prettyprint-status',
288 HttpStatus::getMessage( $this->mHttpStatus )
297 Html::element(
'pre', [
'class' =>
'api-pretty-content' ], $result )
303 $time = $this->
getMain()->getRequest()->getElapsedTime();
304 $json = FormatJson::encode(
306 'status' => (
int)( $this->mHttpStatus ?: 200 ),
307 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ),
308 'html' => $out->getHTML(),
309 'modules' => array_values( array_unique( array_merge(
311 $out->getModuleStyles()
313 'continue' => $this->
getResult()->getResultData(
'continue' ),
314 'time' => round( $time * 1000 ),
316 false, FormatJson::ALL_OK
322 if ( preg_match(
'/<\s*cross-domain-policy\s*>/i', $json ) ) {
323 $json = preg_replace(
324 '/<(\s*cross-domain-policy\s*)>/i',
'\\u003C$1\\u003E', $json
332 $out->allowClickjacking();
349 $this->mBuffer .= $text;
363 $ret[
'wrappedhtml'] = [
374 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->
getModuleName()
375 => [
'apihelp-format-example-generic', $this->
getFormat() ]
380 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats';
static onApiFormatHighlight(IContextSource $context, $text, $mime, $format)
Hook to provide syntax highlighting for API pretty-printed output.
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 ...
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,...
msg( $key,... $params)
This is the method for getting translated interface messages.
if(!is_readable( $file)) $ext