3 namespace MediaWiki\Tidy
;
8 class Html5Depurate
extends TidyDriverBase
{
9 public function __construct( array $config ) {
10 parent
::__construct( $config +
[
11 'url' => 'http://localhost:4339/document',
13 'connectTimeout' => 0.5,
17 public function tidy( $text ) {
18 $wrappedtext = '<!DOCTYPE html><html>' .
19 '<body>' . $text . '</body></html>';
21 $req = MWHttpRequest
::factory( $this->config
['url'],
24 'timeout' => $this->config
['timeout'],
25 'connectTimeout' => $this->config
['connectTimeout'],
27 'text' => $wrappedtext
30 $status = $req->execute();
31 if ( !$status->isOK() ) {
32 throw new Exception( "Error contacting depurate service: "
33 . $status->getWikiText( false, false, 'en' ) );
34 } elseif ( $req->getStatus() !== 200 ) {
35 throw new Exception( "Depurate returned error: " . $status->getWikiText( false, false, 'en' ) );
37 $result = $req->getContent();
38 $startBody = strpos( $result, "<body>" );
39 $endBody = strrpos( $result, "</body>" );
40 if ( $startBody !== false && $endBody !== false && $endBody > $startBody ) {
41 $startBody +
= strlen( "<body>" );
42 return substr( $result, $startBody, $endBody - $startBody );
44 return $text . "\n<!-- Html5Depurate returned an invalid result -->";