r11909 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r11908‎ | r11909 | r11910 >
Date:15:33, 2 December 2005
Author:magnus_manske
Status:old
Tags:
Comment:
beginning of xml--to-odt converter
Modified paths:
  • /trunk/wiki2xml/php/content_provider.php (modified) (history)
  • /trunk/wiki2xml/php/w2x.php (modified) (history)
  • /trunk/wiki2xml/php/wiki2xml.php (modified) (history)
  • /trunk/wiki2xml/php/xml2odt.php (added) (history)
  • /trunk/wiki2xml/php/xml2tree.php (added) (history)

Diff [purge]

Index: trunk/wiki2xml/php/xml2odt.php
@@ -0,0 +1,64 @@
 2+<?php
 3+
 4+class element {
 5+ var $name = '';
 6+ var $attrs = array ();
 7+ var $children = array ();
 8+ function getSourceAttrs () {
 9+ return "" ;
 10+ }
 11+
 12+ function sub_parse(& $tree, $tag = '', $attr = '') {
 13+ $ret = '';
 14+
 15+ $attr2 = $this->getSourceAttrs();
 16+ if ($attr != '' AND $attr2 != '')
 17+ $attr .= ' ';
 18+ $attr .= $attr2;
 19+
 20+ if ($tag != '') {
 21+ $ret .= '<'.$tag;
 22+ if ($attr != '')
 23+ $ret .= ' '.$attr;
 24+ $ret .= '>';
 25+ }
 26+
 27+ foreach ($this->children as $key => $child) {
 28+ if (is_string($child)) {
 29+ $ret .= $child;
 30+ } elseif ($child->name != 'ATTRS') {
 31+ $ret .= $child->makeXHTML($tree);
 32+ }
 33+ }
 34+ if ($tag != '')
 35+ $ret .= '</'.$tag.">\n";
 36+ return $ret;
 37+ }
 38+
 39+
 40+ function parse ( &$tree ) {
 41+ $ret = '';
 42+ $n = $this->name; # Shortcut
 43+
 44+ $ret .= $this->sub_parse ( $tree ) . "\n" ;
 45+ return $ret ;
 46+ }
 47+}
 48+
 49+include_once ( "xml2tree.php" ) ;
 50+
 51+
 52+//_______________________________________________________________
 53+
 54+$infile = "Biology.xml" ;
 55+$xml = @file_get_contents ( $infile ) ;
 56+
 57+print htmlentities ( $xml ) . "<hr>" ;
 58+
 59+$x2t = new xml2php ;
 60+$tree = $x2t->scanString ( $xml ) ;
 61+
 62+$odt = new xml2odt ;
 63+$odt->parse ( $tree ) ;
 64+
 65+?>
Property changes on: trunk/wiki2xml/php/xml2odt.php
___________________________________________________________________
Added: svn:eol-style
166 + native
Added: svn:keywords
267 + Author Date Id Revision
Index: trunk/wiki2xml/php/w2x.php
@@ -23,7 +23,6 @@
2424
2525 $content_provider = new ContentProviderHTTP ;
2626 $xmlg["site_base_url"] = $_POST['site'] ;
27 -# $xmlg["namespace_template"] = $_POST['template'] ;
2827
2928 header('Content-type: text/xml; charset=utf-8');
3029 print "<?xml version='1.0' encoding='UTF-8' ?>\n" ;
@@ -47,7 +46,7 @@
4847 }
4948 }
5049 $t = microtime_float() - $t ;
51 -# xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"
 50+
5251 print "<articles xmlns:xhtml=\" \" rendertime='{$t} sec'>{$text}</articles>" ;
5352 } else if ( isset ( $_GET['showsource'] ) ) {
5453 header('Content-type: text/plain; charset=utf-8');
@@ -77,4 +76,4 @@
7877 </form></body></html>" ;
7978 }
8079
81 -?>
\ No newline at end of file
 80+?>
Index: trunk/wiki2xml/php/xml2tree.php
@@ -0,0 +1,90 @@
 2+<?php
 3+
 4+
 5+$ancStack = array (); // the stack with ancestral elements
 6+
 7+// START Three global functions needed for parsing, sorry guys
 8+/** @todo document */
 9+function wgXMLstartElement($parser, $name, $attrs) {
 10+ global $ancStack;
 11+
 12+ $newElem = new element;
 13+ $newElem->name = $name;
 14+ $newElem->attrs = $attrs;
 15+
 16+ array_push($ancStack, $newElem);
 17+}
 18+
 19+/** @todo document */
 20+function wgXMLendElement($parser, $name) {
 21+ global $ancStack, $rootElem;
 22+ // pop element off stack
 23+ $elem = array_pop($ancStack);
 24+ if (count($ancStack) == 0)
 25+ $rootElem = $elem;
 26+ else
 27+ // add it to its parent
 28+ array_push($ancStack[count($ancStack) - 1]->children, $elem);
 29+}
 30+
 31+/** @todo document */
 32+function wgXMLcharacterData($parser, $data) {
 33+ global $ancStack;
 34+ $data = trim($data); // Don't add blank lines, they're no use...
 35+ // add to parent if parent exists
 36+ if ($ancStack && $data != "") {
 37+ array_push($ancStack[count($ancStack) - 1]->children, $data);
 38+ }
 39+}
 40+// END Three global functions needed for parsing, sorry guys
 41+
 42+/**
 43+ * Here's the class that generates a nice tree
 44+ * @package MediaWiki
 45+ * @subpackage Experimental
 46+ */
 47+class xml2php {
 48+
 49+ /** @todo document */
 50+ function & scanFile($filename) {
 51+ global $ancStack, $rootElem;
 52+ $ancStack = array ();
 53+
 54+ $xml_parser = xml_parser_create();
 55+ xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
 56+ xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');
 57+ if (!($fp = fopen($filename, 'r'))) {
 58+ die('could not open XML input');
 59+ }
 60+ while ($data = fread($fp, 4096)) {
 61+ if (!xml_parse($xml_parser, $data, feof($fp))) {
 62+ die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
 63+ }
 64+ }
 65+ xml_parser_free($xml_parser);
 66+
 67+ // return the remaining root element we copied in the beginning
 68+ return $rootElem;
 69+ }
 70+
 71+ /** @todo document */
 72+ function scanString($input) {
 73+ global $ancStack, $rootElem;
 74+ $ancStack = array ();
 75+
 76+ $xml_parser = xml_parser_create();
 77+ xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
 78+ xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');
 79+
 80+ if (!xml_parse($xml_parser, $input, true)) {
 81+ die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
 82+ }
 83+ xml_parser_free($xml_parser);
 84+
 85+ // return the remaining root element we copied in the beginning
 86+ return $rootElem;
 87+ }
 88+
 89+}
 90+
 91+?>
Property changes on: trunk/wiki2xml/php/xml2tree.php
___________________________________________________________________
Added: svn:eol-style
192 + native
Added: svn:keywords
293 + Author Date Id Revision
Index: trunk/wiki2xml/php/content_provider.php
@@ -45,4 +45,4 @@
4646 }
4747 }
4848
49 -?>
\ No newline at end of file
 49+?>
Index: trunk/wiki2xml/php/wiki2xml.php
@@ -2,9 +2,6 @@
33 # Copyright by Magnus Manske (2005)
44 # Released under GPL
55
6 -# TODO :
7 -# The ";" thingy
8 -
96 class wiki2xml
107 {
118 var $protocols = array ( "http" , "https" , "news" , "ftp" , "irc" , "mailto" ) ;
@@ -1144,4 +1141,4 @@
11451142
11461143 }
11471144
1148 -?>
\ No newline at end of file
 1145+?>

Status & tagging log