2022-11-22 12:58:57 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2023-01-13 21:30:21 +00:00
|
|
|
namespace MediaWiki\Rest\Handler\Helper;
|
2022-11-22 12:58:57 +00:00
|
|
|
|
|
|
|
|
use LanguageCode;
|
|
|
|
|
use MediaWiki\Page\PageIdentity;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2022-11-22 12:58:57 +00:00
|
|
|
use MediaWiki\Rest\ResponseInterface;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-11-22 12:58:57 +00:00
|
|
|
use Message;
|
|
|
|
|
use Wikimedia\Parsoid\Utils\ContentUtils;
|
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.40
|
|
|
|
|
* @unstable
|
|
|
|
|
*/
|
|
|
|
|
class HtmlMessageOutputHelper implements HtmlOutputHelper {
|
|
|
|
|
|
|
|
|
|
private PageIdentity $page;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the helper with the given parameters like the page
|
|
|
|
|
* we're dealing with.
|
|
|
|
|
*
|
|
|
|
|
* @param PageIdentity $page
|
|
|
|
|
*/
|
|
|
|
|
public function init( PageIdentity $page ): void {
|
|
|
|
|
$this->page = $page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Message|null
|
|
|
|
|
*/
|
|
|
|
|
private function getDefaultSystemMessage(): ?Message {
|
|
|
|
|
$title = Title::castFromPageIdentity( $this->page );
|
|
|
|
|
|
|
|
|
|
return $title ? $title->getDefaultSystemMessage() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function getHtml(): ParserOutput {
|
|
|
|
|
$message = $this->getDefaultSystemMessage();
|
|
|
|
|
|
|
|
|
|
// NOTE: This class should be used only for system messages,
|
|
|
|
|
// so failing hard here is fine if we're not dealing with one.
|
|
|
|
|
$messageDom = DOMUtils::parseHTML( $message->parse() );
|
|
|
|
|
DOMUtils::appendToHead( $messageDom, 'meta', [
|
|
|
|
|
'http-equiv' => 'content-language',
|
|
|
|
|
'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$messageDocHtml = ContentUtils::toXML( $messageDom );
|
|
|
|
|
|
|
|
|
|
return new ParserOutput( $messageDocHtml );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function getETag( string $suffix = '' ): ?string {
|
|
|
|
|
// XXX: We end up generating the HTML twice. Would be nice to avoid that.
|
|
|
|
|
// But messages are small, and not hit a lot...
|
|
|
|
|
$output = $this->getHtml();
|
|
|
|
|
|
|
|
|
|
return '"message/' . sha1( $output->getRawText() ) . '/' . $suffix . '"';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*
|
|
|
|
|
* @note This is guaranteed to always return NULL since
|
|
|
|
|
* proper system messages (with no DB entry) have no
|
|
|
|
|
* revision, so they should have no last modified time.
|
|
|
|
|
*/
|
|
|
|
|
public function getLastModified(): ?string {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function getParamSettings(): array {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function setVariantConversionLanguage(
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$targetLanguage,
|
|
|
|
|
$sourceLanguage = null
|
2022-11-22 12:58:57 +00:00
|
|
|
): void {
|
|
|
|
|
// TODO: Set language in the response headers.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function putHeaders(
|
|
|
|
|
ResponseInterface $response,
|
|
|
|
|
bool $forHtml = true
|
|
|
|
|
): void {
|
|
|
|
|
// TODO: Set language in the response headers.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|