wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / tidy / RaggettWrapper.php
blob4759023fbf0710ed94d526dd395cace29eeb23fa
1 <?php
2 namespace MediaWiki\Tidy;
4 use ReplacementArray;
5 use ParserOutput;
6 use Parser;
8 /**
9 * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them
10 * or break on them. This is a bit of a hack for now, but hopefully in the future
11 * we may create a real postprocessor or something that will replace this.
12 * It's called wrapper because for now it basically takes over MWTidy::tidy's task
13 * of wrapping the text in a xhtml block
15 * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
16 * duplicated. Perhaps we should create an abstract marker hiding class.
18 * @ingroup Parser
20 class RaggettWrapper {
22 /**
23 * @var ReplacementArray
25 protected $mTokens;
27 protected $mMarkerIndex;
29 public function __construct() {
30 $this->mTokens = null;
33 /**
34 * @param string $text
35 * @return string
37 public function getWrapped( $text ) {
38 $this->mTokens = new ReplacementArray;
39 $this->mMarkerIndex = 0;
41 // Replace <mw:editsection> elements with placeholders
42 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
43 array( &$this, 'replaceCallback' ), $text );
44 // ...and <mw:toc> markers
45 $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
46 array( &$this, 'replaceCallback' ), $wrappedtext );
47 // ... and <math> tags
48 $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
49 array( &$this, 'replaceCallback' ), $wrappedtext );
50 // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
51 // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
52 $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
54 // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
55 // The whitespace class is as in TY_(InitMap)
56 $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
57 '<li datafld="" class="mw-empty-li">\1</li>', $wrappedtext );
59 // Wrap the whole thing in a doctype and body for Tidy.
60 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
61 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
62 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
64 return $wrappedtext;
67 /**
68 * @param array $m
70 * @return string
72 public function replaceCallback( $m ) {
73 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
74 $this->mMarkerIndex++;
75 $this->mTokens->setPair( $marker, $m[0] );
76 return $marker;
79 /**
80 * @param string $text
81 * @return string
83 public function postprocess( $text ) {
84 // Revert <html-{link,meta}> back to <{link,meta}>
85 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
87 // Remove datafld
88 $text = str_replace( '<li datafld=""', '<li', $text );
90 // Restore the contents of placeholder tokens
91 $text = $this->mTokens->replace( $text );
93 return $text;