5f97140ee464a93936e75655a64779ab3b3851de
3 * Performs transformations of HTML by wrapping around libxml2 and working
4 * around its countless bugs.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
30 private $itemsToRemove = array();
31 private $elementsToFlatten = array();
32 protected $removeMedia = false;
37 * @param string $html: Text to process
39 public function __construct( $html ) {
44 * Turns a chunk of HTML into a proper document
48 public static function wrapHTML( $html ) {
49 return '<!doctype html><html><head></head><body>' . $html . '</body></html>';
53 * Override this in descendant class to modify HTML after it has been converted from DOM tree
54 * @param string $html: HTML to process
55 * @return string: Processed HTML
57 protected function onHtmlReady( $html ) {
62 * @return DOMDocument: DOM to manipulate
64 public function getDoc() {
66 $html = mb_convert_encoding( $this->html
, 'HTML-ENTITIES', 'UTF-8' );
68 // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
69 $html = str_replace( ' <', ' <', $html );
71 libxml_use_internal_errors( true );
72 $this->doc
= new DOMDocument();
73 $this->doc
->strictErrorChecking
= false;
74 $this->doc
->loadHTML( $html );
75 libxml_use_internal_errors( false );
76 $this->doc
->encoding
= 'UTF-8';
82 * Sets whether images/videos/sounds should be removed from output
85 public function setRemoveMedia( $flag = true ) {
86 $this->removeMedia
= $flag;
90 * Adds one or more selector of content to remove
91 * @param Array|string $selectors: Selector(s) of stuff to remove
93 public function remove( $selectors ) {
94 $this->itemsToRemove
= array_merge( $this->itemsToRemove
, (array)$selectors );
98 * Adds one or more element name to the list to flatten (remove tag, but not its content)
99 * Can accept undelimited regexes
100 * @param Array|string $elements: Name(s) of tag(s) to flatten
102 public function flatten( $elements ) {
103 $this->elementsToFlatten
= array_merge( $this->elementsToFlatten
, (array)$elements );
107 * Instructs the formatter to flatten all tags
109 public function flattenAllTags() {
110 $this->flatten( '[?!]?[a-z0-9]+' );
114 * Removes content we've chosen to remove
116 public function filterContent() {
117 wfProfileIn( __METHOD__
);
118 $removals = $this->parseItemsToRemove();
124 $doc = $this->getDoc();
128 // You can't remove DOMNodes from a DOMNodeList as you're iterating
129 // over them in a foreach loop. It will seemingly leave the internal
130 // iterator on the foreach out of wack and results will be quite
131 // strange. Though, making a queue of items to remove seems to work.
132 $domElemsToRemove = array();
133 foreach ( $removals['TAG'] as $tagToRemove ) {
134 $tagToRemoveNodes = $doc->getElementsByTagName( $tagToRemove );
135 foreach ( $tagToRemoveNodes as $tagToRemoveNode ) {
136 if ( $tagToRemoveNode ) {
137 $domElemsToRemove[] = $tagToRemoveNode;
142 $this->removeElements( $domElemsToRemove );
144 // Elements with named IDs
145 $domElemsToRemove = array();
146 foreach ( $removals['ID'] as $itemToRemove ) {
147 $itemToRemoveNode = $doc->getElementById( $itemToRemove );
148 if ( $itemToRemoveNode ) {
149 $domElemsToRemove[] = $itemToRemoveNode;
152 $this->removeElements( $domElemsToRemove );
155 $domElemsToRemove = array();
156 $xpath = new DOMXpath( $doc );
157 foreach ( $removals['CLASS'] as $classToRemove ) {
158 $elements = $xpath->query( '//*[contains(@class, "' . $classToRemove . '")]' );
160 /** @var $element DOMElement */
161 foreach ( $elements as $element ) {
162 $classes = $element->getAttribute( 'class' );
163 if ( preg_match( "/\b$classToRemove\b/", $classes ) && $element->parentNode
) {
164 $domElemsToRemove[] = $element;
168 $this->removeElements( $domElemsToRemove );
170 // Tags with CSS Classes
171 foreach ( $removals['TAG_CLASS'] as $classToRemove ) {
172 $parts = explode( '.', $classToRemove );
174 $elements = $xpath->query(
175 '//' . $parts[0] . '[@class="' . $parts[1] . '"]'
178 $this->removeElements( $elements );
181 wfProfileOut( __METHOD__
);
185 * Removes a list of elelments from DOMDocument
186 * @param array|DOMNodeList $elements
188 private function removeElements( $elements ) {
190 if ( $elements instanceof DOMNodeList
) {
192 foreach ( $elements as $element ) {
196 /** @var $element DOMElement */
197 foreach ( $list as $element ) {
198 if ( $element->parentNode
) {
199 $element->parentNode
->removeChild( $element );
205 * libxml in its usual pointlessness converts many chars to entities - this function
206 * perfoms a reverse conversion
207 * @param string $html
210 private function fixLibXML( $html ) {
211 wfProfileIn( __METHOD__
);
212 static $replacements;
213 if ( ! $replacements ) {
214 // We don't include rules like '"' => '&quot;' because entities had already been
215 // normalized by libxml. Using this function with input not sanitized by libxml is UNSAFE!
216 $replacements = new ReplacementArray( array(
217 '"' => '&quot;',
218 '&' => '&amp;',
219 '<' => '&lt;',
220 '>' => '&gt;',
223 $html = $replacements->replace( $html );
224 $html = mb_convert_encoding( $html, 'UTF-8', 'HTML-ENTITIES' );
225 wfProfileOut( __METHOD__
);
230 * Performs final transformations and returns resulting HTML
232 * @param DOMElement|string|null $element: ID of element to get HTML from or false to get it from the whole tree
233 * @return string: Processed HTML
235 public function getText( $element = null ) {
236 wfProfileIn( __METHOD__
);
239 if ( $element !== null && !( $element instanceof DOMElement
) ) {
240 $element = $this->doc
->getElementById( $element );
243 $body = $this->doc
->getElementsByTagName( 'body' )->item( 0 );
244 $nodesArray = array();
245 foreach ( $body->childNodes
as $node ) {
246 $nodesArray[] = $node;
248 foreach ( $nodesArray as $nodeArray ) {
249 $body->removeChild( $nodeArray );
251 $body->appendChild( $element );
253 $html = $this->doc
->saveHTML();
254 $html = $this->fixLibXml( $html );
258 if ( wfIsWindows() ) {
259 $html = str_replace( ' ', '', $html );
261 $html = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $html );
262 $html = $this->onHtmlReady( $html );
264 if ( $this->elementsToFlatten
) {
265 $elements = implode( '|', $this->elementsToFlatten
);
266 $html = preg_replace( "#</?($elements)\\b[^>]*>#is", '', $html );
269 wfProfileOut( __METHOD__
);
274 * @param $selector: CSS selector to parse
277 * @return bool: Whether the selector was successfully recognised
279 protected function parseSelector( $selector, &$type, &$rawName ) {
280 if ( strpos( $selector, '.' ) === 0 ) {
282 $rawName = substr( $selector, 1 );
283 } elseif ( strpos( $selector, '#' ) === 0 ) {
285 $rawName = substr( $selector, 1 );
286 } elseif ( strpos( $selector, '.' ) !== 0 &&
287 strpos( $selector, '.' ) !== false )
290 $rawName = $selector;
291 } elseif ( strpos( $selector, '[' ) === false
292 && strpos( $selector, ']' ) === false )
295 $rawName = $selector;
297 throw new MWException( __METHOD__
. "(): unrecognized selector '$selector'" );
304 * Transforms CSS selectors into an internal representation suitable for processing
307 protected function parseItemsToRemove() {
308 wfProfileIn( __METHOD__
);
313 'TAG_CLASS' => array(),
316 foreach ( $this->itemsToRemove
as $itemToRemove ) {
319 if ( $this->parseSelector( $itemToRemove, $type, $rawName ) ) {
320 $removals[$type][] = $rawName;
324 if ( $this->removeMedia
) {
325 $removals['TAG'][] = 'img';
326 $removals['TAG'][] = 'audio';
327 $removals['TAG'][] = 'video';
330 wfProfileOut( __METHOD__
);