MediaWiki REL1_31
RaggettWrapper.php
Go to the documentation of this file.
1<?php
2namespace MediaWiki\Tidy;
3
5use Parser;
6
20
24 protected $mTokens;
25
29 protected $mMarkerIndex;
30
35 public function getWrapped( $text ) {
36 $this->mTokens = [];
37 $this->mMarkerIndex = 0;
38
39 // Replace <mw:editsection> elements with placeholders
40 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
41 [ $this, 'replaceCallback' ], $text );
42 // ...and <mw:toc> markers
43 $wrappedtext = preg_replace_callback( '/<\\/?mw:toc>/',
44 [ $this, 'replaceCallback' ], $wrappedtext );
45 // ... and <math> tags
46 $wrappedtext = preg_replace_callback( '/<math(.*?)<\\/math>/s',
47 [ $this, 'replaceCallback' ], $wrappedtext );
48 // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
49 // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
50 $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
51 // Similar for inline <style> tags, but those aren't empty.
52 $wrappedtext = preg_replace_callback( '!<style([^>]*)>(.*?)</style>!s', function ( $m ) {
53 return '<html-style' . $m[1] . '>'
54 . $this->replaceCallback( [ $m[2] ] )
55 . '</html-style>';
56 }, $wrappedtext );
57
58 // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
59 // The whitespace class is as in TY_(InitMap)
60 $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
61 '<li datafld="" class="mw-empty-elt">\1</li>', $wrappedtext );
62
63 // Wrap the whole thing in a doctype and body for Tidy.
64 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
65 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
66 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
67
68 return $wrappedtext;
69 }
70
75 private function replaceCallback( array $m ) {
76 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
77 $this->mMarkerIndex++;
78 $this->mTokens[$marker] = $m[0];
79 return $marker;
80 }
81
86 public function postprocess( $text ) {
87 // Revert <html-{link,meta,style}> back to <{link,meta,style}>
88 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
89 $text = preg_replace( '!<(/?)html-(style)([^>]*)>!', '<$1$2$3>', $text );
90
91 // Remove datafld
92 $text = str_replace( '<li datafld=""', '<li', $text );
93
94 // Restore the contents of placeholder tokens
95 $text = strtr( $text, $this->mTokens );
96
97 return $text;
98 }
99
100}
Class used to hide mw:editsection tokens from Tidy so that it doesn't break them or break on them.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
const MARKER_PREFIX
Definition Parser.php:135