Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.47% |
13 / 17 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
HtmlMessageOutputHelper | |
76.47% |
13 / 17 |
|
50.00% |
4 / 8 |
10.06 | |
0.00% |
0 / 1 |
init | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
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 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | namespace MediaWiki\Rest\Handler\Helper; |
21 | |
22 | use LanguageCode; |
23 | use MediaWiki\Message\Message; |
24 | use MediaWiki\Page\PageIdentity; |
25 | use MediaWiki\Parser\ParserOutput; |
26 | use MediaWiki\Rest\ResponseInterface; |
27 | use MediaWiki\Title\Title; |
28 | use Wikimedia\Parsoid\Utils\ContentUtils; |
29 | use Wikimedia\Parsoid\Utils\DOMUtils; |
30 | |
31 | /** |
32 | * @since 1.40 |
33 | * @unstable |
34 | */ |
35 | class HtmlMessageOutputHelper implements HtmlOutputHelper { |
36 | |
37 | private PageIdentity $page; |
38 | |
39 | /** |
40 | * Initializes the helper with the given parameters like the page |
41 | * we're dealing with. |
42 | * |
43 | * @param PageIdentity $page |
44 | */ |
45 | public function init( PageIdentity $page ): void { |
46 | $this->page = $page; |
47 | } |
48 | |
49 | /** |
50 | * @return Message|null |
51 | */ |
52 | private function getDefaultSystemMessage(): ?Message { |
53 | $title = Title::castFromPageIdentity( $this->page ); |
54 | |
55 | return $title ? $title->getDefaultSystemMessage() : null; |
56 | } |
57 | |
58 | /** |
59 | * @inheritDoc |
60 | */ |
61 | public function getHtml(): ParserOutput { |
62 | $message = $this->getDefaultSystemMessage(); |
63 | |
64 | // NOTE: This class should be used only for system messages, |
65 | // so failing hard here is fine if we're not dealing with one. |
66 | $messageDom = DOMUtils::parseHTML( $message->parse() ); |
67 | DOMUtils::appendToHead( $messageDom, 'meta', [ |
68 | 'http-equiv' => 'content-language', |
69 | 'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ), |
70 | ] ); |
71 | |
72 | $messageDocHtml = ContentUtils::toXML( $messageDom ); |
73 | |
74 | return new ParserOutput( $messageDocHtml ); |
75 | } |
76 | |
77 | /** |
78 | * @inheritDoc |
79 | */ |
80 | public function getETag( string $suffix = '' ): ?string { |
81 | // XXX: We end up generating the HTML twice. Would be nice to avoid that. |
82 | // But messages are small, and not hit a lot... |
83 | $output = $this->getHtml(); |
84 | |
85 | return '"message/' . sha1( $output->getRawText() ) . '/' . $suffix . '"'; |
86 | } |
87 | |
88 | /** |
89 | * @inheritDoc |
90 | * |
91 | * @note This is guaranteed to always return NULL since |
92 | * proper system messages (with no DB entry) have no |
93 | * revision, so they should have no last modified time. |
94 | */ |
95 | public function getLastModified(): ?string { |
96 | return null; |
97 | } |
98 | |
99 | /** |
100 | * @inheritDoc |
101 | */ |
102 | public function getParamSettings(): array { |
103 | return []; |
104 | } |
105 | |
106 | /** |
107 | * @inheritDoc |
108 | */ |
109 | public function setVariantConversionLanguage( |
110 | $targetLanguage, |
111 | $sourceLanguage = null |
112 | ): void { |
113 | // TODO: Set language in the response headers. |
114 | } |
115 | |
116 | public function putHeaders( |
117 | ResponseInterface $response, |
118 | bool $forHtml = true |
119 | ): void { |
120 | // TODO: Set language in the response headers. |
121 | } |
122 | |
123 | } |