MediaWiki master
ParsoidFormatHelper.php
Go to the documentation of this file.
1<?php
21
22use InvalidArgumentException;
24
30
31 public const FORMAT_WIKITEXT = 'wikitext';
32 public const FORMAT_HTML = 'html';
33 public const FORMAT_PAGEBUNDLE = 'pagebundle';
34 public const FORMAT_LINT = 'lint';
35
36 public const ERROR_ENCODING = [
37 self::FORMAT_WIKITEXT => 'plain',
38 self::FORMAT_HTML => 'html',
39 self::FORMAT_PAGEBUNDLE => 'json',
40 self::FORMAT_LINT => 'json',
41 ];
42
43 public const VALID_PAGE = [
45 ];
46
47 public const VALID_TRANSFORM = [
48 self::FORMAT_WIKITEXT => [ self::FORMAT_HTML, self::FORMAT_PAGEBUNDLE, self::FORMAT_LINT ],
49 self::FORMAT_HTML => [ self::FORMAT_WIKITEXT ],
50 self::FORMAT_PAGEBUNDLE => [ self::FORMAT_WIKITEXT, self::FORMAT_PAGEBUNDLE ],
51 ];
52
60 public static function getContentType(
61 string $format, ?string $contentVersion = null
62 ): string {
63 if ( $format !== self::FORMAT_WIKITEXT && !$contentVersion ) {
64 throw new InvalidArgumentException( '$contentVersion is required for this format' );
65 }
66
67 switch ( $format ) {
69 $contentType = 'text/plain';
70 // PORT-FIXME in the original the version number is from MWParserEnvironment.wikitextVersion
71 // but it did not seem to be used anywhere
72 $profile = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0';
73 break;
75 $contentType = 'text/html';
76 $profile = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $contentVersion;
77 break;
79 $contentType = 'application/json';
80 $profile = 'https://www.mediawiki.org/wiki/Specs/pagebundle/' . $contentVersion;
81 break;
82 default:
83 throw new InvalidArgumentException( "Invalid format $format" );
84 }
85 return "$contentType; charset=utf-8; profile=\"$profile\"";
86 }
87
95 public static function setContentType(
96 ResponseInterface $response, string $format,
97 ?string $contentVersion = null
98 ): void {
99 $response->setHeader( 'Content-Type', self::getContentType( $format, $contentVersion ) );
100 }
101
110 public static function parseContentTypeHeader(
111 string $contentTypeHeader, ?string &$format = null
112 ): ?string {
113 $newProfileSyntax = 'https://www.mediawiki.org/wiki/Specs/(HTML|pagebundle)/';
114 $oldProfileSyntax = 'mediawiki.org/specs/(html)/';
115 $profileRegex = "#\bprofile=\"(?:$newProfileSyntax|$oldProfileSyntax)(\d+\.\d+\.\d+)\"#";
116 preg_match( $profileRegex, $contentTypeHeader, $m );
117 if ( $m ) {
118 switch ( $m[1] ?: $m[2] ) {
119 case 'HTML':
120 case 'html':
121 $format = self::FORMAT_HTML;
122 break;
123 case 'pagebundle':
124 $format = self::FORMAT_PAGEBUNDLE;
125 break;
126 }
127 return $m[3];
128 }
129 return null;
130 }
131
132}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static getContentType(string $format, ?string $contentVersion=null)
Get the content type appropriate for a given response format.
static setContentType(ResponseInterface $response, string $format, ?string $contentVersion=null)
Set the Content-Type header appropriate for a given response format.
static parseContentTypeHeader(string $contentTypeHeader, ?string &$format=null)
Parse a Content-Type header and return the format type and version.
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Copyright (C) 2011-2020 Wikimedia Foundation and others.