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