Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / includes / tidy / Html5Depurate.php
1 <?php
2
3 namespace MediaWiki\Tidy;
4 use MWHttpRequest;
5 use Exception;
6
7 class Html5Depurate extends TidyDriverBase {
8 public function __construct( array $config ) {
9 parent::__construct( $config + array(
10 'url' => 'http://localhost:4339/document',
11 'timeout' => 10,
12 'connectTimeout' => 0.5,
13 ) );
14 }
15
16 public function tidy( $text ) {
17 $wrappedtext = '<!DOCTYPE html><html>' .
18 '<body>' . $text . '</body></html>';
19
20 $req = MWHttpRequest::factory( $this->config['url'],
21 array(
22 'method' => 'POST',
23 'timeout' => $this->config['timeout'],
24 'connectTimeout' => $this->config['connectTimeout'],
25 'postData' => array(
26 'text' => $wrappedtext
27 )
28 ) );
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() );
34 }
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 );
41 } else {
42 return $text . "\n<!-- Html5Depurate returned an invalid result -->";
43 }
44 }
45 }