Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
14 / 21 |
|
33.33% |
3 / 9 |
CRAP | |
0.00% |
0 / 1 |
| HtmlMessageOutputHelper | |
66.67% |
14 / 21 |
|
33.33% |
3 / 9 |
15.48 | |
0.00% |
0 / 1 |
| __construct | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| init | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getDefaultSystemMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getHtml | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getETag | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getLastModified | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParamSettings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setVariantConversionLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| putHeaders | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | namespace MediaWiki\Rest\Handler\Helper; |
| 7 | |
| 8 | use MediaWiki\Language\LanguageCode; |
| 9 | use MediaWiki\Message\Message; |
| 10 | use MediaWiki\Page\PageIdentity; |
| 11 | use MediaWiki\Parser\ParserOutput; |
| 12 | use MediaWiki\Rest\ResponseInterface; |
| 13 | use MediaWiki\Title\Title; |
| 14 | use Wikimedia\Parsoid\Utils\ContentUtils; |
| 15 | use Wikimedia\Parsoid\Utils\DOMUtils; |
| 16 | |
| 17 | /** |
| 18 | * @since 1.40 |
| 19 | * @unstable |
| 20 | */ |
| 21 | class HtmlMessageOutputHelper implements HtmlOutputHelper { |
| 22 | |
| 23 | private ?PageIdentity $page; |
| 24 | |
| 25 | /** |
| 26 | * @note Since 1.43 setting $page to null has been deprecated. |
| 27 | */ |
| 28 | public function __construct( ?PageIdentity $page = null ) { |
| 29 | if ( $page === null ) { |
| 30 | wfDeprecated( __METHOD__ . ' without $page', '1.43' ); |
| 31 | } |
| 32 | $this->page = $page; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Initializes the helper with the given parameters like the page |
| 37 | * we're dealing with. |
| 38 | * |
| 39 | * @param PageIdentity $page |
| 40 | * @deprecated since 1.43, use constructor argument instead |
| 41 | */ |
| 42 | public function init( PageIdentity $page ): void { |
| 43 | wfDeprecated( __METHOD__, '1.43' ); |
| 44 | $this->page = $page; |
| 45 | } |
| 46 | |
| 47 | private function getDefaultSystemMessage(): ?Message { |
| 48 | $title = Title::castFromPageIdentity( $this->page ); |
| 49 | |
| 50 | return $title ? $title->getDefaultSystemMessage() : null; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @inheritDoc |
| 55 | */ |
| 56 | public function getHtml(): ParserOutput { |
| 57 | $message = $this->getDefaultSystemMessage(); |
| 58 | |
| 59 | // NOTE: This class should be used only for system messages, |
| 60 | // so failing hard here is fine if we're not dealing with one. |
| 61 | $messageDom = DOMUtils::parseHTML( $message->parse() ); |
| 62 | DOMUtils::appendToHead( $messageDom, 'meta', [ |
| 63 | 'http-equiv' => 'content-language', |
| 64 | 'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ), |
| 65 | ] ); |
| 66 | |
| 67 | $messageDocHtml = ContentUtils::toXML( $messageDom ); |
| 68 | |
| 69 | return new ParserOutput( $messageDocHtml ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritDoc |
| 74 | */ |
| 75 | public function getETag( string $suffix = '' ): ?string { |
| 76 | // XXX: We end up generating the HTML twice. Would be nice to avoid that. |
| 77 | // But messages are small, and not hit a lot... |
| 78 | $output = $this->getHtml(); |
| 79 | |
| 80 | return '"message/' . sha1( $output->getRawText() ) . '/' . $suffix . '"'; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @inheritDoc |
| 85 | * |
| 86 | * @note This is guaranteed to always return NULL since |
| 87 | * proper system messages (with no DB entry) have no |
| 88 | * revision, so they should have no last modified time. |
| 89 | */ |
| 90 | public function getLastModified(): ?string { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | */ |
| 97 | public static function getParamSettings(): array { |
| 98 | return []; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @inheritDoc |
| 103 | */ |
| 104 | public function setVariantConversionLanguage( |
| 105 | $targetLanguage, |
| 106 | $sourceLanguage = null |
| 107 | ): void { |
| 108 | // TODO: Set language in the response headers. |
| 109 | } |
| 110 | |
| 111 | public function putHeaders( |
| 112 | ResponseInterface $response, |
| 113 | bool $forHtml = true |
| 114 | ): void { |
| 115 | // TODO: Set language in the response headers. |
| 116 | } |
| 117 | |
| 118 | } |