parserxml.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 644 行 · 第 1/2 页

PHP
644
字号
				$parts = $this->sub_makeXHTML($parser);				$parts = explode('|', $parts);				$title = array_shift($parts);				$ret .= $this->getTemplateXHTML($title, $parts, & $parser);				break;			case 'TEMPLATEVAR':				$x = $this->sub_makeXHTML($parser);				if (isset ($parser->mCurrentTemplateOptions["{$x}"]))					$ret .= $parser->mCurrentTemplateOptions["{$x}"];				break;			# Internal use, not generated by wiki2xml parser			case 'IGNORE':				$ret .= $this->sub_makeXHTML($parser);			case 'NOWIKI':				$parser->nowiki++;				$ret .= $this->sub_makeXHTML($parser, '');				$parser->nowiki--;			# Unknown HTML extension			case 'EXTENSION': # This is currently a dummy!!!				$ext = $this->attrs['NAME'];				$ret .= '&lt;'.$ext.'&gt;';				$ret .= $this->sub_makeXHTML($parser);				$ret .= '&lt;/'.$ext.'&gt; ';				break;			# Table stuff			case 'TABLE':				$ret .= $this->sub_makeXHTML($parser, 'table');				break;			case 'TABLEROW':				$ret .= $this->sub_makeXHTML($parser, 'tr');				break;			case 'TABLECELL':				$ret .= $this->sub_makeXHTML($parser, 'td');				break;			case 'TABLEHEAD':				$ret .= $this->sub_makeXHTML($parser, 'th');				break;			case 'CAPTION':				$ret .= $this->sub_makeXHTML($parser, 'caption');				break;			case 'ATTRS': # SPECIAL CASE : returning attributes				return $this->getTheseAttrs();			# Lists stuff			case 'LISTITEM':				if ($parser->mListType == 'dl')					$ret .= $this->sub_makeXHTML($parser, 'dd');				else					$ret .= $this->sub_makeXHTML($parser, 'li');				break;			case 'LIST':					$type = 'ol'; # Default					if ($this->attrs['TYPE'] == 'bullet')						$type = 'ul';					else						if ($this->attrs['TYPE'] == 'indent')							$type = 'dl';					$oldtype = $parser->mListType;					$parser->mListType = $type;					$ret .= $this->sub_makeXHTML($parser, $type);					$parser->mListType = $oldtype;				break;			# Something else entirely			default:				$ret .= '&lt;'.$n.'&gt;';				$ret .= $this->sub_makeXHTML($parser);				$ret .= '&lt;/'.$n.'&gt; ';			} // switch($n)		$ret = "\n{$ret}\n";		$ret = str_replace("\n\n", "\n", $ret);		return $ret;	}	/**	 * A function for additional debugging output	 */	function myPrint() {		$ret = "<ul>\n";		$ret .= "<li> <b> Name: </b> $this->name </li>\n";		// print attributes		$ret .= '<li> <b> Attributes: </b>';		foreach ($this->attrs as $name => $value) {			$ret .= "$name => $value; ";		}		$ret .= " </li>\n";		// print children		foreach ($this->children as $child) {			if (is_string($child)) {				$ret .= "<li> $child </li>\n";			} else {				$ret .= $child->myPrint();			}		}		$ret .= "</ul>\n";		return $ret;	}}$ancStack = array (); // the stack with ancestral elements// START Three global functions needed for parsing, sorry guys/** @todo document */function wgXMLstartElement($parser, $name, $attrs) {	global $ancStack;	$newElem = new element;	$newElem->name = $name;	$newElem->attrs = $attrs;	array_push($ancStack, $newElem);}/** @todo document */function wgXMLendElement($parser, $name) {	global $ancStack, $rootElem;	// pop element off stack	$elem = array_pop($ancStack);	if (count($ancStack) == 0)		$rootElem = $elem;	else		// add it to its parent		array_push($ancStack[count($ancStack) - 1]->children, $elem);}/** @todo document */function wgXMLcharacterData($parser, $data) {	global $ancStack;	$data = trim($data); // Don't add blank lines, they're no use...	// add to parent if parent exists	if ($ancStack && $data != "") {		array_push($ancStack[count($ancStack) - 1]->children, $data);	}}// END Three global functions needed for parsing, sorry guys/** * Here's the class that generates a nice tree * @package MediaWiki * @subpackage Experimental */class xml2php {	/** @todo document */	function & scanFile($filename) {		global $ancStack, $rootElem;		$ancStack = array ();		$xml_parser = xml_parser_create();		xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');		xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');		if (!($fp = fopen($filename, 'r'))) {			die('could not open XML input');		}		while ($data = fread($fp, 4096)) {			if (!xml_parse($xml_parser, $data, feof($fp))) {				die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));			}		}		xml_parser_free($xml_parser);		// return the remaining root element we copied in the beginning		return $rootElem;	}	/** @todo document */	function scanString($input) {		global $ancStack, $rootElem;		$ancStack = array ();		$xml_parser = xml_parser_create();		xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');		xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');		if (!xml_parse($xml_parser, $input, true)) {			die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));		}		xml_parser_free($xml_parser);		// return the remaining root element we copied in the beginning		return $rootElem;	}}/** * @todo document * @package MediaWiki * @subpackage Experimental */class ParserXML extends Parser {	/**#@+	 * @private	 */	# Persistent:	var $mTagHooks, $mListType;	# Cleared with clearState():	var $mOutput, $mAutonumber, $mDTopen, $mStripState = array ();	var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;	# Temporary:	var $mOptions, $mTitle, $mOutputType, $mTemplates, // cache of already loaded templates, avoids	// multiple SQL queries for the same string	$mTemplatePath; // stores an unsorted hash of all the templates already loaded	// in this path. Used for loop detection.	var $nowikicount, $mCurrentTemplateOptions;	/**#@-*/	/**	 * Constructor	 *	 * @public	 */	function ParserXML() {		$this->mTemplates = array ();		$this->mTemplatePath = array ();		$this->mTagHooks = array ();		$this->clearState();	}	/**	 * Clear Parser state	 *	 * @private	 */	function clearState() {		$this->mOutput = new ParserOutput;		$this->mAutonumber = 0;		$this->mLastSection = "";		$this->mDTopen = false;		$this->mVariables = false;		$this->mIncludeCount = array ();		$this->mStripState = array ();		$this->mArgStack = array ();		$this->mInPre = false;	}	/**	* Turns the wikitext into XML by calling the external parser	*	*/	function html2xml(& $text) {		global $wgWiki2xml;		# generating html2xml command path		$a = $wgWiki2xml;		$a = explode('/', $a);		array_pop($a);		$a[] = 'html2xml';		$html2xml = implode('/', $a);		$a = array ();		$tmpfname = tempnam( wfTempDir(), 'FOO' );		$handle = fopen($tmpfname, 'w');		fwrite($handle, utf8_encode($text));		fclose($handle);		exec($html2xml.' < '.$tmpfname, $a);		$text = utf8_decode(implode("\n", $a));		unlink($tmpfname);	}	/** @todo document */	function runXMLparser(& $text) {		global $wgWiki2xml;		$this->html2xml($text);		$tmpfname = tempnam( wfTempDir(), 'FOO');		$handle = fopen($tmpfname, 'w');		fwrite($handle, $text);		fclose($handle);		exec($wgWiki2xml.' < '.$tmpfname, $a);		$text = utf8_decode(implode("\n", $a));		unlink($tmpfname);	}	/** @todo document */	function plain_parse(& $text, $inline = false, $templateOptions = array ()) {		$this->runXMLparser($text);		$nowikicount = 0;		$w = new xml2php;		$result = $w->scanString($text);		$oldTemplateOptions = $this->mCurrentTemplateOptions;		$this->mCurrentTemplateOptions = $templateOptions;		if ($inline) { # Inline rendering off for templates			if (count($result->children) == 1)				$result->children[0]->name = 'IGNORE';		}		if (1)			$text = $result->makeXHTML($this); # No debugging info		else			$text = $result->makeXHTML($this).'<hr>'.$text.'<hr>'.$result->myPrint();		$this->mCurrentTemplateOptions = $oldTemplateOptions;	}	/** @todo document */	function parse($text, & $title, $options, $linestart = true, $clearState = true) {		$this->plain_parse($text);		$this->mOutput->setText($text);		return $this->mOutput;	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?