(X)HTML parser * Based on work by Jan Hidders and Magnus Manske * To use, set * $wgUseXMLparser = true ; * $wgEnableParserCache = false ; * $wgWiki2xml to the path and executable of the command line version (cli) * in LocalSettings.php * @package MediaWiki * @subpackage Experimental */ /** * the base class for an element */ class element { var $name = ''; var $attrs = array(); var $children = array(); function getSourceAttrs () { $ret = "" ; foreach ($this->children as $child) { if ( !is_string($child) AND $child->name == "ATTRS" ) { $ret = $child->makeXHTML ( $parser ); } } return $ret ; } function getTheseAttrs () { $ret = array() ; foreach ($this->children as $child) { if ( !is_string($child) AND $child->name == "ATTR" ) { $s = $child->attrs["NAME"] . "='" . $child->children[0] . "'" ; # $s = "ATTRIBUTE=" . $child->attrs["NAME"] ; $ret[] = $s ; } } return implode ( " " , $ret ) ; } function sub_makeXHTML ( &$parser , $tag = "" , $attr = "" ) { $ret = "" ; $attr2 = $this->getSourceAttrs () ; if ( $attr != "" AND $attr2 != "" ) $attr .= " " ; $attr .= $attr2 ; if ( $tag != "" ) { $ret .= "<" . $tag ; if ( $attr != "" ) $ret .= " " . $attr ; $ret .= ">" ; } foreach ($this->children as $child) { if ( is_string($child) ) { $ret .= $child ; } else if ( $child->name != "ATTRS" ) { $ret .= $child->makeXHTML ( $parser ); } } if ( $tag != "" ) $ret .= "" . $tag . ">\n" ; return $ret ; } function createInternalLink ( &$parser , $target , $display_title , $options ) { $tp = explode ( ":" , $target ) ; # tp = target parts $title = "" ; # The plain title $language = "" ; # The language/meta/etc. part $namespace = "" ; # The namespace, if any $subtarget = "" ; # The '#' thingy if ( count ( $tp ) == 1 ) $title = $target ; # Plain and simple case else { # To be implemented } if ( $language != "" ) # External link within the WikiMedia project { return "{language link}" ; } else if ( $namespace != "" ) # Link to another namespace, check for image/media stuff { return "{namespace link}" ; } else { return "{internal link}" ; } } function makeInternalLink ( &$parser ) { $target = "" ; $option = array () ; foreach ($this->children as $child) { if ( is_string($child) ) { # This shouldn't be the case! } else { if ( $child->name == "LINKTARGET" ) $target = trim ( $child->makeXHTML ( $parser ) ) ; else $option[] = trim ( $child->makeXHTML ( $parser ) ) ; } } if ( count ( $option ) == 0 ) $option[] = $target ; # Create dummy display title $display_title = array_pop ( $option ) ; return $this->createInternalLink ( $parser , $target , $display_title , $option ) ; } function makeXHTML ( &$parser ) { $ret = "" ; $n = $this->name ; # Shortcut if ( $n == "EXTENSION" ) # Fix allowed HTML { $ext = strtoupper ( $this->attrs["NAME"] ) ; if ( $ext == "B" ) $n = "BOLD" ; if ( $ext == "I" ) $n = "ITALICS" ; if ( $ext == "P" ) $n = "PARAGRAPH" ; } if ( $n == "ARTICLE" ) $ret .= $this->sub_makeXHTML ( $parser ) ; else if ( $n == "HEADING" ) $ret .= $this->sub_makeXHTML ( $parser , "h" . $this->attrs["LEVEL"] ) ; else if ( $n == "PARAGRAPH" ) $ret .= $this->sub_makeXHTML ( $parser , "p" ) ; else if ( $n == "BOLD" ) $ret .= $this->sub_makeXHTML ( $parser , "strong" ) ; else if ( $n == "ITALICS" ) $ret .= $this->sub_makeXHTML ( $parser , "em" ) ; else if ( $n == "LINK" ) $ret .= $this->makeInternalLink ( $parser ) ; else if ( $n == "LINKTARGET" ) $ret .= $this->sub_makeXHTML ( $parser ) ; else if ( $n == "LINKOPTION" ) $ret .= $this->sub_makeXHTML ( $parser ) ; else if ( $n == "EXTENSION" ) # This is currently a dummy!!! { $ext = $this->attrs["NAME"] ; $ret .= "<" . $ext . ">" ; $ret .= $this->sub_makeXHTML ( $parser ) ; $ret .= "</" . $ext . "> " ; } else if ( $n == "TABLE" ) { $ret .= $this->sub_makeXHTML ( $parser , "table" ) ; } else if ( $n == "TABLEROW" ) { $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ; } else if ( $n == "TABLECELL" ) { $ret .= $this->sub_makeXHTML ( $parser , "td" ) ; } else if ( $n == "TABLEHEAD" ) { $ret .= $this->sub_makeXHTML ( $parser , "th" ) ; } else if ( $n == "CAPTION" ) { $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ; } else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes { return $this->getTheseAttrs () ; } else if ( $n == "LISTITEM" ) $ret .= $this->sub_makeXHTML ( $parser , "li" ) ; else if ( $n == "LIST" ) { $type = "ol" ; # Default if ( $this->attrs["TYPE"] == "bullet" ) $type = "ul" ; $ret .= $this->sub_makeXHTML ( $parser , $type ) ; } else { $ret .= "<" . $n . ">" ; $ret .= $this->sub_makeXHTML ( $parser ) ; $ret .= "</" . $n . "> " ; } $ret = "\n{$ret}\n" ; $ret = str_replace ( "\n\n" , "\n" , $ret ) ; return $ret ; } function myPrint() { $ret = "