Merge "Implement static public Parser::getExternalLinkRel"
[lhc/web/wiklou.git] / includes / parser / Tidy.php
index de99571..5cc1b0f 100644 (file)
@@ -2,7 +2,23 @@
 /**
  * HTML validation and correction
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
+ * @ingroup Parser
  */
 
 /**
  * we may create a real postprocessor or something that will replace this.
  * It's called wrapper because for now it basically takes over MWTidy::tidy's task
  * of wrapping the text in a xhtml block
- * 
+ *
  * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
  * duplicated. Perhaps we should create an abstract marker hiding class.
+ *
+ * @ingroup Parser
  */
 class MWTidyWrapper {
 
@@ -40,10 +58,16 @@ class MWTidyWrapper {
                $this->mUniqPrefix = "\x7fUNIQ" .
                        dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
                $this->mMarkerIndex = 0;
-       
+
+               // Replace <mw:editsection> elements with placeholders
                $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
                        array( &$this, 'replaceEditSectionLinksCallback' ), $text );
 
+               // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
+               // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
+               $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
+
+               // Wrap the whole thing in a doctype and body for Tidy.
                $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
                        ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
                        '<head><title>test</title></head><body>'.$wrappedtext.'</body></html>';
@@ -68,7 +92,13 @@ class MWTidyWrapper {
         * @return string
         */
        public function postprocess( $text ) {
-               return $this->mTokens->replace( $text );
+               // Revert <html-{link,meta}> back to <{link,meta}>
+               $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
+
+               // Restore the contents of placeholder tokens
+               $text = $this->mTokens->replace( $text );
+
+               return $text;
        }
 
 }
@@ -126,7 +156,7 @@ class MWTidy {
         */
        public static function checkErrors( $text, &$errorStr = null ) {
                global $wgTidyInternal;
-               
+
                $retval = 0;
                if( $wgTidyInternal ) {
                        $errorStr = self::execInternalTidy( $text, true, $retval );
@@ -143,7 +173,7 @@ class MWTidy {
         *
         * @param $text String: HTML to check
         * @param $stderr Boolean: Whether to read result from STDERR rather than STDOUT
-        * @param &$retval Exit code (-1 on internal error)
+        * @param &$retval int Exit code (-1 on internal error)
         * @return mixed String or null
         */
        private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
@@ -166,7 +196,7 @@ class MWTidy {
                                2 => array( 'file', wfGetNull(), 'a' )
                        );
                }
-               
+
                $readpipe = $stderr ? 2 : 1;
                $pipes = array();
 
@@ -207,7 +237,7 @@ class MWTidy {
         *
         * @param $text String: HTML to check
         * @param $stderr Boolean: Whether to read result from error status instead of output
-        * @param &$retval Exit code (-1 on internal error)
+        * @param &$retval int Exit code (-1 on internal error)
         * @return mixed String or null
         */
        private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
@@ -217,6 +247,8 @@ class MWTidy {
                if ( !MWInit::classExists( 'tidy' ) ) {
                        wfWarn( "Unable to load internal tidy class." );
                        $retval = -1;
+
+                       wfProfileOut( __METHOD__ );
                        return null;
                }
 
@@ -243,7 +275,7 @@ class MWTidy {
                                                "\n-->";
                                }
                        }
-       
+
                        wfProfileOut( __METHOD__ );
                        return $cleansource;
                }