X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiFormatBase.php;h=4348fc881d4b0853dc3a7e2116eb94442ab4a915;hb=e1aabf2f24aef20adc72db8a750704cbb33236c6;hp=06eaa19ca40ca20b4f9b66e28ea5c9ca3e34cff7;hpb=23f0ce46d46d06e4aa71b9bfe6d47bc5aaa63e68;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 06eaa19ca4..4348fc881d 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -64,6 +64,27 @@ abstract class ApiFormatBase extends ApiBase { */ abstract public function getMimeType(); + /** + * Return a filename for this module's output. + * @note If $this->getIsWrappedHtml() || $this->getIsHtml(), you'll very + * likely want to fall back to this class's version. + * @since 1.27 + * @return string Generally this should be "api-result.$ext", and must be + * encoded for inclusion in a Content-Disposition header's filename parameter. + */ + public function getFilename() { + if ( $this->getIsWrappedHtml() ) { + return 'api-result-wrapped.json'; + } elseif ( $this->getIsHtml() ) { + return 'api-result.html'; + } else { + $exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() + ->getExtensionsForType( $this->getMimeType() ); + $ext = $exts ? strtok( $exts, ' ' ) : strtolower( $this->mFormat ); + return "api-result.$ext"; + } + } + /** * Get the internal format name * @return string @@ -192,6 +213,13 @@ abstract class ApiFormatBase extends ApiBase { if ( $apiFrameOptions ) { $this->getMain()->getRequest()->response()->header( "X-Frame-Options: $apiFrameOptions" ); } + + // Set a Content-Disposition header so something downloading an API + // response uses a halfway-sensible filename (T128209). + $filename = $this->getFilename(); + $this->getMain()->getRequest()->response()->header( + "Content-Disposition: inline; filename=\"{$filename}\"" + ); } /**