User:Jiangxin/Patch RawHtmlTag

Support raw html ouput from extension

edit
Extensions such as charinsert and User:Joncutrer/Extensions/Google Adsense Search generate raw html, which could be broken by other parser process. CharInsert 插件搜索引擎插件 直接生成 Raw HTML 代码,但是这些插件生成的 Raw HTML 代码并非像内置的 <html> 或者 <pre> 那样受到良好的支持。可能受到 Parser 的其它步骤的影响,导致输出和预期不符。
The following patch give extension hacker a chance to treat their extension's raw html output just as what <html>, or <pre> does. 下面的插件给开发者提供一个选项,使得 raw html 类型的插件如同系统默认的 raw html 插件一样。

patch for MediaWiki 1.6.2

edit

Index: includes/GlobalFunctions.php
===================================================================
--- includes/GlobalFunctions.php	(revision 51)
+++ includes/GlobalFunctions.php	(working copy)
@@ -1785,4 +1785,10 @@
 	}
 }
 
+// OpenSourceXpress.com Extension
+function wfRawHtmlTag( $tag, $enable=true ) {
+	global $wgRawHtmlTag;
+	$wgRawHtmlTag["$tag"] = (true == $enable);
+}
+
 ?>
Index: includes/Parser.php
===================================================================
--- includes/Parser.php	(revision 53)
+++ includes/Parser.php	(working copy)
@@ -551,6 +551,9 @@
 	 * @access private
 	 */
 	function unstrip( $text, &$state ) {
+ 		// extensions may output raw html, so give extension hack a chance to do that. (by http://www.worldhello.net)
+ 		global $wgRawHtmlTag;
+
 		if ( !is_array( $state ) ) {
 			return $text;
 		}
@@ -558,6 +561,9 @@
 		# Must expand in reverse order, otherwise nested tags will be corrupted
 		foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
 			if( $tag != 'nowiki' && $tag != 'html' ) {
+ 				if(is_array($wgRawHtmlTag) && isset($wgRawHtmlTag["$tag"]) && true === $wgRawHtmlTag["$tag"] ) {
+ 					continue;
+ 				}
 				foreach( array_reverse( $contentDict, true ) as $uniq => $content ) {
 					$text = str_replace( $uniq, $content, $text );
 				}
@@ -573,11 +579,37 @@
 	 * @access private
 	 */
 	function unstripNoWiki( $text, &$state ) {
+		# Must expand in reverse order, otherwise nested tags will be corrupted
+		global $wgRawHtmlTag;
+
 		if ( !is_array( $state ) ) {
 			return $text;
 		}
 
+		if( is_array($wgRawHtmlTag) )
+		{
+			foreach ( $wgRawHtmlTag AS $tag => $bNowiki )
+			{
+				if ( (true === $bNowiki) && is_array($state["$tag"]) )
+				{
+					for ( $content = end($state["$tag"]); $content !== false; $content = prev( $state["$tag"] ) ) {
+						$text = str_replace( key( $state["$tag"] ), $content, $text );
+					}
+				}
+			}
+		}
+
+		# Do the following unstrip again, because User defined RawHtmlTag has lower priority
 		# Must expand in reverse order, otherwise nested tags will be corrupted
+		foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
+			if( $tag != 'nowiki' && $tag != 'html' ) {
+				foreach( array_reverse( $contentDict, true ) as $uniq => $content ) {
+					$text = str_replace( $uniq, $content, $text );
+				}
+			}
+		}
+
+		# Must expand in reverse order, otherwise nested tags will be corrupted
 		for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
 			$text = str_replace( key( $state['nowiki'] ), $content, $text );
 		}

patch for MediaWiki 1.5.x

edit

--- mediawiki-1.5.2/includes/GlobalFunctions.php	2005-11-13 23:13:35.437500000 +0800
+++ mediawiki/includes/GlobalFunctions.php	2005-11-13 17:32:22.937500000 +0800
@@ -1383,4 +1383,11 @@
 function wfNoMsg( $msg, $wfMsgOut ) {
 	return $wfMsgOut === "<$msg>";
 }
+
+
+function wfRawHtmlTag( $tag, $enable=true ) {
+	global $wgRawHtmlTag;
+	$wgRawHtmlTag["$tag"] = (true == $enable);
+}
+
 ?>
--- mediawiki-1.5.2/includes/Parser.php	2005-11-13 23:13:35.453125000 +0800
+++ mediawiki/includes/Parser.php	2005-11-13 17:32:23.562500000 +0800
@@ -467,9 +467,15 @@
 	 * @access private
 	 */
 	function unstrip( $text, &$state ) {
+		// extensions may output raw html, so give extension hack a chance to do that. (by http://www.worldhello.net)
+		global $wgRawHtmlTag;
+		
 		# Must expand in reverse order, otherwise nested tags will be corrupted
 		foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
 			if( $tag != 'nowiki' && $tag != 'html' ) {
+				if(is_array($wgRawHtmlTag) && true === $wgRawHtmlTag["$tag"] ) {
+					continue;
+				}
 				foreach( array_reverse( $contentDict, true ) as $uniq => $content ) {
 					$text = str_replace( $uniq, $content, $text );
 				}
@@ -486,6 +492,31 @@
 	 */
 	function unstripNoWiki( $text, &$state ) {
 		# Must expand in reverse order, otherwise nested tags will be corrupted
+		global $wgRawHtmlTag;
+		if( is_array($wgRawHtmlTag) )
+		{
+			foreach ( $wgRawHtmlTag AS $tag => $bNowiki )
+			{
+				if ( (true === $bNowiki) && is_array($state["$tag"]) )
+				{
+					for ( $content = end($state["$tag"]); $content !== false; $content = prev( $state["$tag"] ) ) {
+						$text = str_replace( key( $state["$tag"] ), $content, $text );
+					}
+				}
+			}
+		}
+
+		# Do the following unstrip again, because User defined RawHtmlTag has lower priority
+		# Must expand in reverse order, otherwise nested tags will be corrupted
+		foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
+			if( $tag != 'nowiki' && $tag != 'html' ) {
+				foreach( array_reverse( $contentDict, true ) as $uniq => $content ) {
+					$text = str_replace( $uniq, $content, $text );
+				}
+			}
+		}
+
+		# Must expand in reverse order, otherwise nested tags will be corrupted
 		for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
 			$text = str_replace( key( $state['nowiki'] ), $content, $text );
 		}


How a extension can benefit from this feature? For example, extension charinsert only needs to add the new global function 'wfRawHtmlTag' in it's hook.


Index: CharInsert.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/extensions/charinsert/CharInsert.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharInsert.php      9 Nov 2005 08:16:11 -0000       1.1
+++ CharInsert.php      10 Nov 2005 18:38:58 -0000      1.2
@@ -40,6 +40,7 @@
 function setupSpecialChars() {
     global $wgParser;
     $wgParser->setHook( 'charinsert', 'charInsert' );
+    wfRawHtmlTag('charinsert');
 }

 function charInsert( $data ) {


-- JiangXin 01:47, 15 November 2005 (UTC)