forked from wikimedia/mediawiki-extensions-VisualEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiParsoidTrait.php
211 lines (186 loc) · 6.65 KB
/
ApiParsoidTrait.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Helper functions for contacting Parsoid/RESTBase from the action API.
*
* @file
* @ingroup Extensions
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
* @license MIT
*/
namespace MediaWiki\Extension\VisualEditor;
use ApiUsageException;
use Language;
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use MediaWiki\MediaWikiServices;
use MediaWiki\Message\Message;
use MediaWiki\Request\WebRequest;
use MediaWiki\Rest\HttpException;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Title\Title;
use NullStatsdDataFactory;
use PrefixingStatsdDataFactoryProxy;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
trait ApiParsoidTrait {
private ?LoggerInterface $logger = null;
private ?StatsdDataFactoryInterface $stats = null;
protected function getLogger(): LoggerInterface {
return $this->logger ?: new NullLogger();
}
protected function setLogger( LoggerInterface $logger ): void {
$this->logger = $logger;
}
protected function getStats(): StatsdDataFactoryInterface {
return $this->stats ?: new NullStatsdDataFactory();
}
protected function setStats( StatsdDataFactoryInterface $stats ): void {
$this->stats = new PrefixingStatsdDataFactoryProxy( $stats, 'VE' );
}
/**
* @return float Return a start time for use with statsRecordTiming()
*/
private function statsGetStartTime(): float {
return microtime( true );
}
/**
* @param string $key
* @param float $startTime from statsGetStartTime()
*/
private function statsRecordTiming( string $key, float $startTime ): void {
$duration = ( microtime( true ) - $startTime ) * 1000;
$this->getStats()->timing( $key, $duration );
}
/**
* @return never
* @throws ApiUsageException
*/
private function dieWithRestHttpException( HttpException $ex ): void {
if ( $ex instanceof LocalizedHttpException ) {
$converter = new \MediaWiki\Message\Converter();
$msg = $converter->convertMessageValue( $ex->getMessageValue() );
$this->dieWithError( $msg, null, $ex->getErrorData() );
} else {
$this->dieWithException( $ex, [ 'data' => $ex->getErrorData() ] );
}
}
/**
* Request page HTML from Parsoid.
*
* @param RevisionRecord $revision Page revision
* @return array An array mimicking a RESTbase server's response, with keys: 'headers' and 'body'
* @phan-return array{body:string,headers:array<string,string>}
* @throws ApiUsageException
*/
protected function requestRestbasePageHtml( RevisionRecord $revision ): array {
$title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
$lang = self::getPageLanguage( $title );
$startTime = $this->statsGetStartTime();
try {
$response = $this->getParsoidClient()->getPageHtml( $revision, $lang );
} catch ( HttpException $ex ) {
$this->dieWithRestHttpException( $ex );
}
$this->statsRecordTiming( 'ApiVisualEditor.ParsoidClient.getPageHtml', $startTime );
return $response;
}
/**
* Transform HTML to wikitext with Parsoid.
*
* @param Title $title The title of the page
* @param string $html The HTML of the page to be transformed
* @param int|null $oldid What oldid revision, if any, to base the request from (default: `null`)
* @param string|null $etag The ETag to set in the HTTP request header
* @return array An array mimicking a RESTbase server's response, with keys: 'headers' and 'body'
* @phan-return array{body:string,headers:array<string,string>}
* @throws ApiUsageException
*/
protected function transformHTML(
Title $title, string $html, int $oldid = null, string $etag = null
): array {
$lang = self::getPageLanguage( $title );
$startTime = $this->statsGetStartTime();
try {
$response = $this->getParsoidClient()->transformHTML( $title, $lang, $html, $oldid, $etag );
} catch ( HttpException $ex ) {
$this->dieWithRestHttpException( $ex );
}
$this->statsRecordTiming( 'ApiVisualEditor.ParsoidClient.transformHTML', $startTime );
return $response;
}
/**
* Transform wikitext to HTML with Parsoid.
*
* @param Title $title The title of the page to use as the parsing context
* @param string $wikitext The wikitext fragment to parse
* @param bool $bodyOnly Whether to provide only the contents of the `<body>` tag
* @param int|null $oldid What oldid revision, if any, to base the request from (default: `null`)
* @param bool $stash Whether to stash the result in the server-side cache (default: `false`)
* @return array An array mimicking a RESTbase server's response, with keys: 'headers' and 'body'
* @phan-return array{body:string,headers:array<string,string>}
* @throws ApiUsageException
*/
protected function transformWikitext(
Title $title, string $wikitext, bool $bodyOnly, int $oldid = null, bool $stash = false
): array {
$lang = self::getPageLanguage( $title );
$startTime = $this->statsGetStartTime();
try {
$response = $this->getParsoidClient()->transformWikitext(
$title,
$lang,
$wikitext,
$bodyOnly,
$oldid,
$stash
);
} catch ( HttpException $ex ) {
$this->dieWithRestHttpException( $ex );
}
$this->statsRecordTiming( 'ApiVisualEditor.ParsoidClient.transformWikitext', $startTime );
return $response;
}
/**
* Get the page language from a title, using the content language as fallback on special pages
*
* @param Title $title
* @return Language Content language
*/
public static function getPageLanguage( Title $title ): Language {
if ( $title->isSpecial( 'CollabPad' ) ) {
// Use the site language for CollabPad, as getPageLanguage just
// returns the interface language for special pages.
// TODO: Let the user change the document language on multi-lingual sites.
return MediaWikiServices::getInstance()->getContentLanguage();
} else {
return $title->getPageLanguage();
}
}
/**
* @see VisualEditorParsoidClientFactory
*/
abstract protected function getParsoidClient(): ParsoidClient;
/**
* @see ApiBase
* @param string|array|Message $msg See ApiErrorFormatter::addError()
* @param string|null $code See ApiErrorFormatter::addError()
* @param array|null $data See ApiErrorFormatter::addError()
* @param int|null $httpCode HTTP error code to use
* @return never
* @throws ApiUsageException
*/
abstract public function dieWithError( $msg, $code = null, $data = null, $httpCode = null );
/**
* @see ApiBase
* @param Throwable $exception See ApiErrorFormatter::getMessageFromException()
* @param array $options See ApiErrorFormatter::getMessageFromException()
* @return never
*/
abstract public function dieWithException( Throwable $exception, array $options = [] );
/**
* @see ContextSource
* @return WebRequest
*/
abstract public function getRequest();
}