Debian Codegraph @

includes/Rest/Handler/Helper/HtmlMessageOutputHelper.php

raw symbols SCIP
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 MediaWiki\Language\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 * @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 /**
62 * @return Message|null
63 */
64 private function getDefaultSystemMessage(): ?Message {
65 $title = Title::castFromPageIdentity( $this->page );
66
67 return $title ? $title->getDefaultSystemMessage() : null;
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function getHtml(): ParserOutput {
74 $message = $this->getDefaultSystemMessage();
75
76 // NOTE: This class should be used only for system messages,
77 // so failing hard here is fine if we're not dealing with one.
78 $messageDom = DOMUtils::parseHTML( $message->parse() );
79 DOMUtils::appendToHead( $messageDom, 'meta', [
80 'http-equiv' => 'content-language',
81 'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ),
82 ] );
83
84 $messageDocHtml = ContentUtils::toXML( $messageDom );
85
86 return new ParserOutput( $messageDocHtml );
87 }
88
89 /**
90 * @inheritDoc
91 */
92 public function getETag( string $suffix = '' ): ?string {
93 // XXX: We end up generating the HTML twice. Would be nice to avoid that.
94 // But messages are small, and not hit a lot...
95 $output = $this->getHtml();
96
97 return '"message/' . sha1( $output->getRawText() ) . '/' . $suffix . '"';
98 }
99
100 /**
101 * @inheritDoc
102 *
103 * @note This is guaranteed to always return NULL since
104 * proper system messages (with no DB entry) have no
105 * revision, so they should have no last modified time.
106 */
107 public function getLastModified(): ?string {
108 return null;
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public static function getParamSettings(): array {
115 return [];
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function setVariantConversionLanguage(
122 $targetLanguage,
123 $sourceLanguage = null
124 ): void {
125 // TODO: Set language in the response headers.
126 }
127
128 public function putHeaders(
129 ResponseInterface $response,
130 bool $forHtml = true
131 ): void {
132 // TODO: Set language in the response headers.
133 }
134
135 }