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