summaryrefslogtreecommitdiff
path: root/includes/tidy/Html5Depurate.php
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-12-20 09:00:55 +0100
committerPierre Schmitz <pierre@archlinux.de>2015-12-20 09:00:55 +0100
commita2190ac74dd4d7080b12bab90e552d7aa81209ef (patch)
tree8b31f38de9882d18df54cf8d9e0de74167a094eb /includes/tidy/Html5Depurate.php
parent15e69f7b20b6596b9148030acce5b59993b95a45 (diff)
parent257401d8b2cf661adf36c84b0e3fd1cf85e33c22 (diff)
Merge branch 'mw-1.26'
Diffstat (limited to 'includes/tidy/Html5Depurate.php')
-rw-r--r--includes/tidy/Html5Depurate.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/includes/tidy/Html5Depurate.php b/includes/tidy/Html5Depurate.php
new file mode 100644
index 00000000..23e445fa
--- /dev/null
+++ b/includes/tidy/Html5Depurate.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace MediaWiki\Tidy;
+use MWHttpRequest;
+use Exception;
+
+class Html5Depurate extends TidyDriverBase {
+ public function __construct( array $config ) {
+ parent::__construct( $config + array(
+ 'url' => 'http://localhost:4339/document',
+ 'timeout' => 10,
+ 'connectTimeout' => 0.5,
+ ) );
+ }
+
+ public function tidy( $text ) {
+ $wrappedtext = '<!DOCTYPE html><html>' .
+ '<body>' . $text . '</body></html>';
+
+ $req = MWHttpRequest::factory( $this->config['url'],
+ array(
+ 'method' => 'POST',
+ 'timeout' => $this->config['timeout'],
+ 'connectTimeout' => $this->config['connectTimeout'],
+ 'postData' => array(
+ 'text' => $wrappedtext
+ )
+ ) );
+ $status = $req->execute();
+ if ( !$status->isOK() ) {
+ throw new Exception( "Error contacting depurate service: " . $status->getWikiText() );
+ } elseif ( $req->getStatus() !== 200 ) {
+ throw new Exception( "Depurate returned error: " . $status->getWikiText() );
+ }
+ $result = $req->getContent();
+ $startBody = strpos( $result, "<body>" );
+ $endBody = strrpos( $result, "</body>" );
+ if ( $startBody !== false && $endBody !== false && $endBody > $startBody ) {
+ $startBody += strlen( "<body>" );
+ return substr( $result, $startBody, $endBody - $startBody );
+ } else {
+ return $text . "\n<!-- Html5Depurate returned an invalid result -->";
+ }
+ }
+}