| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Rest\Handler\Helper; |
| 21 |
|
| 22 |
use InvalidArgumentException; |
| 23 |
use MediaWiki\Rest\ResponseInterface; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class ParsoidFormatHelper { |
| 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 = [ |
| 44 |
self::FORMAT_WIKITEXT, self::FORMAT_HTML, self::FORMAT_PAGEBUNDLE, self::FORMAT_LINT |
| 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 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 ) { |
| 68 |
case self::FORMAT_WIKITEXT: |
| 69 |
$contentType = 'text/plain'; |
| 70 |
|
| 71 |
|
| 72 |
$profile = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0'; |
| 73 |
break; |
| 74 |
case self::FORMAT_HTML: |
| 75 |
$contentType = 'text/html'; |
| 76 |
$profile = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $contentVersion; |
| 77 |
break; |
| 78 |
case self::FORMAT_PAGEBUNDLE: |
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
} |