GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mediawiki-extensions-Cite/src/ErrorReporter.php at master · wikimedia/mediawiki-extensions-Cite

Expand file tree

/

ErrorReporter.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
126 lines (109 loc) · 3.64 KB
/

ErrorReporter.php

Copy path

File metadata and controls

126 lines (109 loc) · 3.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Cite;
use InvalidArgumentException;
use MediaWiki\Html\Html;
use MediaWiki\Language\Language;
use MediaWiki\Message\Message;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\Sanitizer;
use StatusValue;
/**
* @license GPL-2.0-or-later
*/
class ErrorReporter {
private ?Language $cachedInterfaceLanguage = null;
public function __construct(
private readonly Parser $parser,
private readonly ReferenceMessageLocalizer $messageLocalizer,
) {
}
/**
* @deprecated Intermediate helper function. Long-term all errors should be rendered, not only
* the first one.
*/
public function firstError( StatusValue $status ): string {
$firstError = $status->getMessages()[0];
return $this->halfParsed( $firstError->getKey(), ...$firstError->getParams() );
}
/**
* @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( string $key, ...$params ): string {
$msg = $this->msg( $key, ...$params );
$wikitext = $this->parser->recursiveTagParse( $msg->plain() );
return $this->wrapInHtmlContainer( $wikitext, $key, $msg->getLanguage() );
}
/**
* @param string $key Message name of the error or warning
* @param mixed ...$params
* @return string Plain, unparsed wikitext
* @return-taint tainted
*/
public function plain( string $key, ...$params ): string {
$msg = $this->msg( $key, ...$params );
$wikitext = $msg->plain();
return $this->wrapInHtmlContainer( $wikitext, $key, $msg->getLanguage() );
}
/**
* @param string $key
* @param mixed ...$params
* @return Message
*/
private function msg( string $key, ...$params ): Message {
$language = $this->getInterfaceLanguageAndSplitCache( $this->parser->getOptions() );
$msg = $this->messageLocalizer->msg( $key, ...$params )->inLanguage( $language );
[ $type ] = $this->parseTypeAndIdFromMessageKey( $key );
if ( $type === 'error' ) {
$wrapper = $this->messageLocalizer->msg( 'cite_error', $msg->plain() )->inLanguage( $language );
if ( !$wrapper->isDisabled() ) {
$msg = $wrapper;
}
// Take care; this is a sideeffect that might not belong to this class.
$this->parser->addTrackingCategory( 'cite-tracking-category-cite-error' );
}
return $msg;
}
/**
* Note the startling side effect of splitting ParserCache by user interface language!
*/
private function getInterfaceLanguageAndSplitCache( ParserOptions $parserOptions ): Language {
$this->cachedInterfaceLanguage ??= $parserOptions->getUserLangObj();
return $this->cachedInterfaceLanguage;
}
private function wrapInHtmlContainer(
string $wikitext,
string $key,
Language $language
): string {
[ $type, $id ] = $this->parseTypeAndIdFromMessageKey( $key );
$extraClass = $type === 'warning'
? ' mw-ext-cite-warning-' . Sanitizer::escapeClass( $id )
: '';
return Html::rawElement(
'span',
[
// The following classes are generated here:
// * mw-ext-cite-warning
// * mw-ext-cite-error
'class' => "$type mw-ext-cite-$type" . $extraClass,
'lang' => $language->getHtmlCode(),
'dir' => $language->getDir(),
],
$wikitext
);
}
/**
* @param string $messageKey Expected to be a message key like "cite_error_ref_numeric_key"
*
* @return string[] Two elements, e.g. "error" and "ref_numeric_key"
*/
private function parseTypeAndIdFromMessageKey( string $messageKey ): array {
if ( !preg_match( '/^cite.(error|warning).(.+)/i', $messageKey, $matches ) ) {
throw new InvalidArgumentException( 'Unexpected message key' );
}
return array_slice( $matches, 1 );
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.