MediaWiki master
ParsoidFormatHelper.php
Go to the documentation of this file.
1<?php
9
10use InvalidArgumentException;
12
18
19 public const FORMAT_WIKITEXT = 'wikitext';
20 public const FORMAT_HTML = 'html';
21 public const FORMAT_PAGEBUNDLE = 'pagebundle';
22 public const FORMAT_LINT = 'lint';
23
24 public const ERROR_ENCODING = [
25 self::FORMAT_WIKITEXT => 'plain',
26 self::FORMAT_HTML => 'html',
27 self::FORMAT_PAGEBUNDLE => 'json',
28 self::FORMAT_LINT => 'json',
29 ];
30
31 public const VALID_PAGE = [
33 ];
34
35 public const VALID_TRANSFORM = [
36 self::FORMAT_WIKITEXT => [ self::FORMAT_HTML, self::FORMAT_PAGEBUNDLE, self::FORMAT_LINT ],
37 self::FORMAT_HTML => [ self::FORMAT_WIKITEXT ],
38 self::FORMAT_PAGEBUNDLE => [ self::FORMAT_WIKITEXT, self::FORMAT_PAGEBUNDLE ],
39 ];
40
48 public static function getContentType(
49 string $format, ?string $contentVersion = null
50 ): string {
51 if ( $format !== self::FORMAT_WIKITEXT && !$contentVersion ) {
52 throw new InvalidArgumentException( '$contentVersion is required for this format' );
53 }
54
55 switch ( $format ) {
57 $contentType = 'text/plain';
58 // PORT-FIXME in the original the version number is from MWParserEnvironment.wikitextVersion
59 // but it did not seem to be used anywhere
60 $profile = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0';
61 break;
63 $contentType = 'text/html';
64 $profile = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $contentVersion;
65 break;
67 $contentType = 'application/json';
68 $profile = 'https://www.mediawiki.org/wiki/Specs/pagebundle/' . $contentVersion;
69 break;
70 default:
71 throw new InvalidArgumentException( "Invalid format $format" );
72 }
73 return "$contentType; charset=utf-8; profile=\"$profile\"";
74 }
75
83 public static function setContentType(
84 ResponseInterface $response, string $format,
85 ?string $contentVersion = null
86 ): void {
87 $response->setHeader( 'Content-Type', self::getContentType( $format, $contentVersion ) );
88 }
89
98 public static function parseContentTypeHeader(
99 string $contentTypeHeader, ?string &$format = null
100 ): ?string {
101 $newProfileSyntax = 'https://www.mediawiki.org/wiki/Specs/(HTML|pagebundle)/';
102 $oldProfileSyntax = 'mediawiki.org/specs/(html)/';
103 $profileRegex = "#\bprofile=\"(?:$newProfileSyntax|$oldProfileSyntax)(\d+\.\d+\.\d+)\"#";
104 preg_match( $profileRegex, $contentTypeHeader, $m );
105 if ( $m ) {
106 switch ( $m[1] ?: $m[2] ) {
107 case 'HTML':
108 case 'html':
109 $format = self::FORMAT_HTML;
110 break;
111 case 'pagebundle':
112 $format = self::FORMAT_PAGEBUNDLE;
113 break;
114 }
115 return $m[3];
116 }
117 return null;
118 }
119
120}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
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.