RaggettInternalPHP.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace MediaWiki\Tidy;
  3. class RaggettInternalPHP extends RaggettBase {
  4. /**
  5. * Use the HTML tidy extension to use the tidy library in-process,
  6. * saving the overhead of spawning a new process.
  7. *
  8. * @param string $text HTML to check
  9. * @param bool $stderr Whether to read result from error status instead of output
  10. * @param int &$retval Exit code (-1 on internal error)
  11. * @return string|null
  12. */
  13. protected function cleanWrapped( $text, $stderr = false, &$retval = null ) {
  14. if ( !class_exists( 'tidy' ) ) {
  15. wfWarn( "Unable to load internal tidy class." );
  16. $retval = -1;
  17. return null;
  18. }
  19. $tidy = new \tidy;
  20. $tidy->parseString( $text, $this->config['tidyConfigFile'], 'utf8' );
  21. if ( $stderr ) {
  22. $retval = $tidy->getStatus();
  23. return $tidy->errorBuffer;
  24. }
  25. $tidy->cleanRepair();
  26. $retval = $tidy->getStatus();
  27. if ( $retval == 2 ) {
  28. // 2 is magic number for fatal error
  29. // https://secure.php.net/manual/en/tidy.getstatus.php
  30. $cleansource = null;
  31. } else {
  32. $cleansource = tidy_get_output( $tidy );
  33. if ( !empty( $this->config['debugComment'] ) && $retval > 0 ) {
  34. $cleansource .= "<!--\nTidy reports:\n" .
  35. str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
  36. "\n-->";
  37. }
  38. }
  39. return $cleansource;
  40. }
  41. public function supportsValidate() {
  42. return true;
  43. }
  44. }