summaryrefslogtreecommitdiff
path: root/www/wiki/includes/tidy/RaggettExternal.php
blob: b59423ab3a9b33b9a4c597641f1b2c04b7c46236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace MediaWiki\Tidy;

class RaggettExternal extends RaggettBase {
	/**
	 * Spawn an external HTML tidy process and get corrected markup back from it.
	 * Also called in OutputHandler.php for full page validation
	 *
	 * @param string $text HTML to check
	 * @param bool $stderr Whether to read result from STDERR rather than STDOUT
	 * @param int &$retval Exit code (-1 on internal error)
	 * @return string|null
	 */
	protected function cleanWrapped( $text, $stderr = false, &$retval = null ) {
		$cleansource = '';
		$opts = ' -utf8';

		if ( $stderr ) {
			$descriptorspec = [
				0 => [ 'pipe', 'r' ],
				1 => [ 'file', wfGetNull(), 'a' ],
				2 => [ 'pipe', 'w' ]
			];
		} else {
			$descriptorspec = [
				0 => [ 'pipe', 'r' ],
				1 => [ 'pipe', 'w' ],
				2 => [ 'file', wfGetNull(), 'a' ]
			];
		}

		$readpipe = $stderr ? 2 : 1;
		$pipes = [];

		$process = proc_open(
			"{$this->config['tidyBin']} -config {$this->config['tidyConfigFile']} " .
			$this->config['tidyCommandLine'] . $opts, $descriptorspec, $pipes );

		// NOTE: At least on linux, the process will be created even if tidy is not installed.
		//      This means that missing tidy will be treated as a validation failure.

		if ( is_resource( $process ) ) {
			// Theoretically, this style of communication could cause a deadlock
			// here. If the stdout buffer fills up, then writes to stdin could
			// block. This doesn't appear to happen with tidy, because tidy only
			// writes to stdout after it's finished reading from stdin. Search
			// for tidyParseStdin and tidySaveStdout in console/tidy.c
			fwrite( $pipes[0], $text );
			fclose( $pipes[0] );
			while ( !feof( $pipes[$readpipe] ) ) {
				$cleansource .= fgets( $pipes[$readpipe], 1024 );
			}
			fclose( $pipes[$readpipe] );
			$retval = proc_close( $process );
		} else {
			wfWarn( "Unable to start external tidy process" );
			$retval = -1;
		}

		if ( !$stderr && $cleansource == '' && $text != '' ) {
			// Some kind of error happened, so we couldn't get the corrected text.
			// Just give up; we'll use the source text and append a warning.
			$cleansource = null;
		}

		return $cleansource;
	}

	public function supportsValidate() {
		return true;
	}
}