3 namespace MediaWiki\Tidy
;
8 class Html5Depurate
extends TidyDriverBase
{
9 public function __construct( array $config ) {
10 parent
::__construct( $config +
array(
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: " . $status->getWikiText() );
33 } elseif ( $req->getStatus() !== 200 ) {
34 throw new Exception( "Depurate returned error: " . $status->getWikiText() );
36 $result = $req->getContent();
37 $startBody = strpos( $result, "<body>" );
38 $endBody = strrpos( $result, "</body>" );
39 if ( $startBody !== false && $endBody !== false && $endBody > $startBody ) {
40 $startBody +
= strlen( "<body>" );
41 return substr( $result, $startBody, $endBody - $startBody );
43 return $text . "\n<!-- Html5Depurate returned an invalid result -->";