mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-20 02:30:50 +00:00
ab07a3253c
Finishes breaking the circular reference between Cite and Parser. This patch also demonstrates how evil it is to allow the error reporter to be called from anywhere, and have side-effects. At least it's explicit now. Also fixes a bug where the inner error message would not be in the interface language. Bug: T240431 Change-Id: Ic3325cafb503e78295d72231ac6da5c121402def
96 lines
2.4 KiB
PHP
96 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Cite;
|
|
|
|
use Html;
|
|
use Language;
|
|
use Parser;
|
|
use Sanitizer;
|
|
|
|
/**
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class ErrorReporter {
|
|
|
|
/**
|
|
* @var ReferenceMessageLocalizer
|
|
*/
|
|
private $messageLocalizer;
|
|
|
|
/**
|
|
* @var Language
|
|
*/
|
|
private $cachedInterfaceLanguage = null;
|
|
|
|
/**
|
|
* @param ReferenceMessageLocalizer $messageLocalizer
|
|
*/
|
|
public function __construct(
|
|
ReferenceMessageLocalizer $messageLocalizer
|
|
) {
|
|
$this->messageLocalizer = $messageLocalizer;
|
|
}
|
|
|
|
/**
|
|
* @param Parser $parser
|
|
* @param string $key Message name of the error or warning
|
|
* @param mixed ...$params
|
|
*
|
|
* @return string Half-parsed wikitext with extension's tags already being expanded
|
|
*/
|
|
public function halfParsed( Parser $parser, string $key, ...$params ) : string {
|
|
// FIXME: We suspect this is not necessary and can just be removed
|
|
return $parser->recursiveTagParse( $this->plain( $parser, $key, ...$params ) );
|
|
}
|
|
|
|
/**
|
|
* @param Parser $parser
|
|
* @param string $key Message name of the error or warning
|
|
* @param mixed ...$params
|
|
*
|
|
* @return string Plain, unparsed wikitext
|
|
* @return-taint tainted
|
|
*/
|
|
public function plain( Parser $parser, string $key, ...$params ) : string {
|
|
$interfaceLanguage = $this->getInterfaceLanguageAndSplitCache( $parser );
|
|
$msg = $this->messageLocalizer->msg( $key, ...$params )->inLanguage( $interfaceLanguage );
|
|
|
|
if ( strncmp( $msg->getKey(), 'cite_warning_', 13 ) === 0 ) {
|
|
$type = 'warning';
|
|
$id = substr( $msg->getKey(), 13 );
|
|
$extraClass = ' mw-ext-cite-warning-' . Sanitizer::escapeClass( $id );
|
|
} else {
|
|
$type = 'error';
|
|
$extraClass = '';
|
|
|
|
// Take care; this is a sideeffect that might not belong to this class.
|
|
$parser->addTrackingCategory( 'cite-tracking-category-cite-error' );
|
|
}
|
|
|
|
return Html::rawElement(
|
|
'span',
|
|
[
|
|
'class' => "$type mw-ext-cite-$type" . $extraClass,
|
|
'lang' => $interfaceLanguage->getHtmlCode(),
|
|
'dir' => $interfaceLanguage->getDir(),
|
|
],
|
|
$this->messageLocalizer->msg( "cite_$type", $msg->plain() )
|
|
->inLanguage( $interfaceLanguage )->plain()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Note the startling side effect of splitting ParserCache by user interface language!
|
|
*
|
|
* @param Parser $parser
|
|
* @return Language
|
|
*/
|
|
private function getInterfaceLanguageAndSplitCache( Parser $parser ): Language {
|
|
if ( $this->cachedInterfaceLanguage === null ) {
|
|
$this->cachedInterfaceLanguage = $parser->getOptions()->getUserLangObj();
|
|
}
|
|
return $this->cachedInterfaceLanguage;
|
|
}
|
|
|
|
}
|