FCKeditor/function strip

From Meta, a Wikimedia project coordination wiki

Note: the script contains tags and special characters, therefore it is not correctly displayed, for copying the code use the edit view instead.

	function strip( $text, &$state, $stripcomments = false ) {
		$render = ($this->mOutputType == OT_HTML);
		$html_content = array();
		$nowiki_content = array();
		$math_content = array();
		$pre_content = array();
		$comment_content = array();
		$ext_content = array();
		$ext_tags = array();
		$ext_params = array();
		$gallery_content = array();

		# Replace any instances of the placeholders
		$uniq_prefix = $this->mUniqPrefix;
		#$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );


		# patch fck editor
		global $wgUseEditor, $wgEditorToken;
		if ($wgUseEditor) {
			$taglist = array("nowiki", "math", "pre", "gallery");
	
			foreach ($taglist as $tag) {
				$text = preg_replace("/&lt;\s*$tag\s*&gt;/i", "<$tag>", $text);
				$text = preg_replace("/&lt;\s*\/\s*$tag\s*&gt;/i", "</$tag>", $text);
			}
			$text = preg_replace("/&lt;(\!\-\-.*?\-\-)&gt;/i", "<\\1>", $text);
		}



		# html
		global $wgRawHtml;
		if( $wgRawHtml ) {
			$text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
			foreach( $html_content as $marker => $content ) {
				if ($render ) {
					# Raw and unchecked for validity.
					$html_content[$marker] = $content;
				} else {
					$html_content[$marker] = '<html>'.$content.'</html>';
				}
			}
		}


		# nowiki
		$text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
		foreach( $nowiki_content as $marker => $content ) {
			if( $render ){
				$nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
			} else {
				$nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
			}
		}

		# math
		if( $this->mOptions->getUseTeX() ) {
			$text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
			foreach( $math_content as $marker => $content ){
				if( $render ) {
					$math_content[$marker] = renderMath( $content );
				} else {
					$math_content[$marker] = '<math>'.$content.'</math>';
				}
			}
		}

		# pre
		$text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
		foreach( $pre_content as $marker => $content ){
			if( $render ){
				$pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
			} else {
				$pre_content[$marker] = '<pre>'.$content.'</pre>';
			}
		}

		# gallery
		$text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
		foreach( $gallery_content as $marker => $content ) {
			require_once( 'ImageGallery.php' );
			if ( $render ) {
				$gallery_content[$marker] = $this->renderImageGallery( $content );
			} else {
				$gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
			}
		}

		# Comments
		$text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
		foreach( $comment_content as $marker => $content ){
			$comment_content[$marker] = '<!--'.$content.'-->';
		}

		# Extensions
		foreach ( $this->mTagHooks as $tag => $callback ) {
			$ext_content[$tag] = array();

			# patch
			if ($wgUseEditor) {
				$text = preg_replace("/&lt;\s*$tag\s*&gt;/i", "<$tag>", $text);
				$text = preg_replace("/&lt;\s*\/\s*$tag\s*&gt;/i", "</$tag>", $text);
			}

			$text = Parser::extractTagsAndParams( $tag, $text, $ext_content[$tag],
				$ext_tags[$tag], $ext_params[$tag], $uniq_prefix );
			foreach( $ext_content[$tag] as $marker => $content ) {
				$full_tag = $ext_tags[$tag][$marker];
				$params = $ext_params[$tag][$marker];
				if ( $render )
					$ext_content[$tag][$marker] = call_user_func_array( $callback, array( $content, $params, &$this ) );
				else {
					if ( is_null( $content ) ) {
						// Empty element tag
						$ext_content[$tag][$marker] = $full_tag;
					} else {
						$ext_content[$tag][$marker] = "$full_tag$content</$tag>";
					}
				}
			}
		}

		# Unstrip comments unless explicitly told otherwise.
		# (The comments are always stripped prior to this point, so as to
		# not invoke any extension tags / parser hooks contained within
		# a comment.)
		if ( !$stripcomments ) {
			$tempstate = array( 'comment' => $comment_content );
			$text = $this->unstrip( $text, $tempstate );
			$comment_content = array();
		}

		# Merge state with the pre-existing state, if there is one
		if ( $state ) {
			$state['html'] += $html_content;
			$state['nowiki'] += $nowiki_content;
			$state['math'] += $math_content;
			$state['pre'] += $pre_content;
			$state['gallery'] += $gallery_content;
			$state['comment'] += $comment_content;

			foreach( $ext_content as $tag => $array ) {
				if ( array_key_exists( $tag, $state ) ) {
					$state[$tag] += $array;
				}
			}
		} else {
			$state = array(
			  'html' => $html_content,
			  'nowiki' => $nowiki_content,
			  'math' => $math_content,
			  'pre' => $pre_content,
			  'gallery' => $gallery_content,
			  'comment' => $comment_content,
			) + $ext_content;
		}
		return $text;
	}