From: <var...@us...> - 2021-08-11 17:21:40
|
Revision: 10495 http://sourceforge.net/p/phpwiki/code/10495 Author: vargenau Date: 2021-08-11 17:21:38 +0000 (Wed, 11 Aug 2021) Log Message: ----------- Rename class XmlParser as PhpWikiXmlParser due to PHP 8 final class XMLParser Modified Paths: -------------- trunk/lib/AtomParser.php trunk/lib/HtmlParser.php trunk/lib/RssParser.php trunk/lib/XmlElement.php trunk/lib/plugin/FoafViewer.php trunk/lib/stdlib.php trunk/locale/Makefile Added Paths: ----------- trunk/lib/PhpWikiXmlParser.php Removed Paths: ------------- trunk/lib/XmlParser.php Modified: trunk/lib/AtomParser.php =================================================================== --- trunk/lib/AtomParser.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/AtomParser.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -28,10 +28,10 @@ * * @author: Sébastien Le Callonnec */ -require_once 'lib/XmlParser.php'; +require_once 'lib/PhpWikiXmlParser.php'; class AtomParser - extends XmlParser + extends PhpWikiXmlParser { // Feed public $feed = array(); Modified: trunk/lib/HtmlParser.php =================================================================== --- trunk/lib/HtmlParser.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/HtmlParser.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -24,7 +24,7 @@ /** * HtmlParser Class: Conversion HTML => wikimarkup - * Requires XmlParser, XmlElement and the expat (or now the libxml) library. This is all in core. + * Requires PhpWikiXmlParser, XmlElement and the expat (or now the libxml) library. This is all in core. */ /** @@ -40,10 +40,10 @@ */ // RssParser contains the XML (expat) and url-grabber methods -require_once 'lib/XmlParser.php'; +require_once 'lib/PhpWikiXmlParser.php'; class HtmlParser - extends XmlParser + extends PhpWikiXmlParser { public $dialect, $_handlers, $root; @@ -51,13 +51,13 @@ { $this->dialect = new HtmlParser_PhpWiki(); $this->_handlers =& $this->dialect->_handlers; - $this->XmlParser($encoding); + $this->PhpWikiXmlParser($encoding); xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($this->_parser, XML_OPTION_SKIP_WHITE, 1); } // The three callbacks, called on walking through the HTML tree. - // No extensions needed from XmlParser. + // No extensions needed from PhpWikiXmlParser. /* function tag_open($parser, $name, $attrs='') { } Copied: trunk/lib/PhpWikiXmlParser.php (from rev 10494, trunk/lib/XmlParser.php) =================================================================== --- trunk/lib/PhpWikiXmlParser.php (rev 0) +++ trunk/lib/PhpWikiXmlParser.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -0,0 +1,181 @@ +<?php +/** + * This file is part of PhpWiki. + * + * PhpWiki is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * PhpWiki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with PhpWiki; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +/** + * Base PhpWikiXmlParser Class. + * Requires the expat.so/.dll, usually enabled by default. + * Used by HtmlParser and RssParser. + * + * @author: Reini Urban + * + * TODO: Convert more perl Html::Element style to our XmlElement style + * Needed additions to XmlElement: + * Html::Element::parent() <=> XmlElement::parent + * Html::Element::attr() <=> XmlElement::getAttr() + * Html::Element::tag <=> XmlElement::_tag + * Html::Element::content_list() <=> ->getContent() ??? or ->_children[] + * all_external_attr_names() <=> + * + * Problems: + * The HtmlParser object set by xml_parse() doesn't keep its parameters, + * esp. $this->root is lost. So we have to this into a global. + */ + +/** + * class PhpWikiXmlParser - Parse into a tree of XmlElement nodes. + * + * PHP Problems: + * inside the handlers no globals are transported, only class vars. + * when leaving the handler class all class vars are destroyed, so we + * have to copy the root to a global. + * + */ +class PhpWikiXmlParser +{ + public $_parser, $root, $current, $previous, $parent; + + function __construct($encoding = '') + { + if ($encoding) + $this->_parser = xml_parser_create($encoding); + else + $this->_parser = xml_parser_create(); + + xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); + + //This unfortunately does not work + //xml_set_object($this->_parser, &$this); + + xml_set_element_handler($this->_parser, + array(&$this, 'tag_open'), + array(&$this, 'tag_close')); + xml_set_character_data_handler($this->_parser, + array(&$this, 'cdata')); + //xml_set_element_handler($this->_parser, "tag_open", "tag_close"); + //xml_set_character_data_handler($this->_parser, "cdata"); + + // Hack: workaround php OO bug + unset($GLOBALS['xml_parser_root']); + } + + function __destruct() + { + global $xml_parser_root, $xml_parser_current; + + if (!empty($this->_parser)) xml_parser_free($this->_parser); + unset($this->_parser); + + if (isset($xml_parser_root)) { + $xml_parser_root->_destruct(); + unset($xml_parser_root); // nested parsing forbidden! + } + unset($xml_parser_current); + } + + function tag_open($parser, $name, $attrs = '') + { + $this->_tag = strtolower($name); + $node = new XmlElement($this->_tag); + if (is_string($attrs) and !empty($attrs)) { + // lowercase attr names + foreach (explode(' ', $attrs) as $pair) { + if (strstr($pair, "=")) { + list($key, $val) = explode('=', $pair); + $key = strtolower(trim($key)); + $val = str_replace(array('"', "'"), '', trim($val)); + $node->_attr[$key] = $val; + } else { + $key = str_replace(array('"', "'"), '', strtolower(trim($pair))); + $node->_attr[$key] = $key; + } + } + } elseif (!empty($attrs) and is_array($attrs)) { + foreach ($attrs as $key => $val) { + $key = strtolower(trim($key)); + $val = str_replace(array('"', "'"), '', trim($val)); + $node->_attr[$key] = $val; + } + } + if (!is_null($this->current)) { + $this->current->_content[] =& $node; // copy or ref? + $node->previous =& $this->current; // ref to parallel prev + } + $this->current =& $node; // ref + if (empty($this->root)) { + $this->root =& $node; // ref for === test below + $GLOBALS['xml_parser_root'] =& $this->root; // copy + } + } + + function tag_close($parser, $name, $attrs = '') + { + $this->current->parent = $this->current; // copy! + $this->current =& $this->current->parent; // ref! + //unset($this->current); + } + + function cdata($parser, $data) + { + if (isset($this->current)) { + $this->current->_content[] = $data; + } else { + trigger_error(sprintf("unparsed content outside tags: %s", $data), E_USER_WARNING); + } + if ($this->current === $this->root) { // workaround php OO bug: ref => copy + $GLOBALS['xml_parser_root'] =& $this->root; // copy! + //$this->root = $this->current; // copy? + } + } + + function parse($content, $is_final = true) + { + xml_parse($this->_parser, $content, $is_final) or + trigger_error(sprintf("XML error: %s at line %d", + xml_error_string(xml_get_error_code($this->_parser)), + xml_get_current_line_number($this->_parser)), + E_USER_WARNING); + } + + function parse_url($file, $debug = false) + { + if (get_cfg_var('allow_url_fopen')) { + if (!($fp = fopen("$file", "r"))) { + trigger_error("Error parse url $file"); + return; + } + $content = ""; + while ($data = fread($fp, 4096)) { + $content .= $data; + } + fclose($fp); + $this->parse($content); + } else { + // other url_fopen workarounds: curl, socket (http 80 only) + $data = url_get_contents($file); + if (empty($data)) { + trigger_error("Error parse url $file"); + return; + } + $this->parse($data); + } + } +} Modified: trunk/lib/RssParser.php =================================================================== --- trunk/lib/RssParser.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/RssParser.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -25,7 +25,7 @@ * Based on Duncan Gough RSSParser class * Copyleft Arnaud Fontaine * Licence : GPL - * See lib/plugin/RssFeed.php and lib/XmlParser.php + * See lib/plugin/RssFeed.php and lib/PhpWikiXmlParser.php * * The myth of RSS compatibility: * http://diveintomark.org/archives/2004/02/04/incompatible-rss @@ -41,10 +41,10 @@ * http://ws.audioscrobbler.com/rdf/ for example */ -require_once 'lib/XmlParser.php'; +require_once 'lib/PhpWikiXmlParser.php'; class RSSParser - extends XmlParser + extends PhpWikiXmlParser { public $title = ""; Modified: trunk/lib/XmlElement.php =================================================================== --- trunk/lib/XmlElement.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/XmlElement.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -263,7 +263,7 @@ $this->setContent($args); } - /** Methods only needed for XmlParser, + /** Methods only needed for PhpWikiXmlParser, * to be fully compatible to perl Html::Element */ // doesn't yet work with php5 as __destruct() @@ -289,7 +289,7 @@ return !empty($this->_children); } - /* End XmlParser Methods + /* End PhpWikiXmlParser Methods */ function getTag() Deleted: trunk/lib/XmlParser.php =================================================================== --- trunk/lib/XmlParser.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/XmlParser.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -1,181 +0,0 @@ -<?php -/** - * This file is part of PhpWiki. - * - * PhpWiki is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * PhpWiki is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with PhpWiki; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * SPDX-License-Identifier: GPL-2.0-or-later - * - */ - -/** - * Base XmlParser Class. - * Requires the expat.so/.dll, usually enabled by default. - * Used by HtmlParser and RssParser. - * - * @author: Reini Urban - * - * TODO: Convert more perl Html::Element style to our XmlElement style - * Needed additions to XmlElement: - * Html::Element::parent() <=> XmlElement::parent - * Html::Element::attr() <=> XmlElement::getAttr() - * Html::Element::tag <=> XmlElement::_tag - * Html::Element::content_list() <=> ->getContent() ??? or ->_children[] - * all_external_attr_names() <=> - * - * Problems: - * The HtmlParser object set by xml_parse() doesn't keep its parameters, - * esp. $this->root is lost. So we have to this into a global. - */ - -/** - * class XmlParser - Parse into a tree of XmlElement nodes. - * - * PHP Problems: - * inside the handlers no globals are transported, only class vars. - * when leaving the handler class all class vars are destroyed, so we - * have to copy the root to a global. - * - */ -class XmlParser -{ - public $_parser, $root, $current, $previous, $parent; - - function __construct($encoding = '') - { - if ($encoding) - $this->_parser = xml_parser_create($encoding); - else - $this->_parser = xml_parser_create(); - - xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); - - //This unfortunately does not work - //xml_set_object($this->_parser, &$this); - - xml_set_element_handler($this->_parser, - array(&$this, 'tag_open'), - array(&$this, 'tag_close')); - xml_set_character_data_handler($this->_parser, - array(&$this, 'cdata')); - //xml_set_element_handler($this->_parser, "tag_open", "tag_close"); - //xml_set_character_data_handler($this->_parser, "cdata"); - - // Hack: workaround php OO bug - unset($GLOBALS['xml_parser_root']); - } - - function __destruct() - { - global $xml_parser_root, $xml_parser_current; - - if (!empty($this->_parser)) xml_parser_free($this->_parser); - unset($this->_parser); - - if (isset($xml_parser_root)) { - $xml_parser_root->_destruct(); - unset($xml_parser_root); // nested parsing forbidden! - } - unset($xml_parser_current); - } - - function tag_open($parser, $name, $attrs = '') - { - $this->_tag = strtolower($name); - $node = new XmlElement($this->_tag); - if (is_string($attrs) and !empty($attrs)) { - // lowercase attr names - foreach (explode(' ', $attrs) as $pair) { - if (strstr($pair, "=")) { - list($key, $val) = explode('=', $pair); - $key = strtolower(trim($key)); - $val = str_replace(array('"', "'"), '', trim($val)); - $node->_attr[$key] = $val; - } else { - $key = str_replace(array('"', "'"), '', strtolower(trim($pair))); - $node->_attr[$key] = $key; - } - } - } elseif (!empty($attrs) and is_array($attrs)) { - foreach ($attrs as $key => $val) { - $key = strtolower(trim($key)); - $val = str_replace(array('"', "'"), '', trim($val)); - $node->_attr[$key] = $val; - } - } - if (!is_null($this->current)) { - $this->current->_content[] =& $node; // copy or ref? - $node->previous =& $this->current; // ref to parallel prev - } - $this->current =& $node; // ref - if (empty($this->root)) { - $this->root =& $node; // ref for === test below - $GLOBALS['xml_parser_root'] =& $this->root; // copy - } - } - - function tag_close($parser, $name, $attrs = '') - { - $this->current->parent = $this->current; // copy! - $this->current =& $this->current->parent; // ref! - //unset($this->current); - } - - function cdata($parser, $data) - { - if (isset($this->current)) { - $this->current->_content[] = $data; - } else { - trigger_error(sprintf("unparsed content outside tags: %s", $data), E_USER_WARNING); - } - if ($this->current === $this->root) { // workaround php OO bug: ref => copy - $GLOBALS['xml_parser_root'] =& $this->root; // copy! - //$this->root = $this->current; // copy? - } - } - - function parse($content, $is_final = true) - { - xml_parse($this->_parser, $content, $is_final) or - trigger_error(sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($this->_parser)), - xml_get_current_line_number($this->_parser)), - E_USER_WARNING); - } - - function parse_url($file, $debug = false) - { - if (get_cfg_var('allow_url_fopen')) { - if (!($fp = fopen("$file", "r"))) { - trigger_error("Error parse url $file"); - return; - } - $content = ""; - while ($data = fread($fp, 4096)) { - $content .= $data; - } - fclose($fp); - $this->parse($content); - } else { - // other url_fopen workarounds: curl, socket (http 80 only) - $data = url_get_contents($file); - if (empty($data)) { - trigger_error("Error parse url $file"); - return; - } - $this->parse($data); - } - } -} Modified: trunk/lib/plugin/FoafViewer.php =================================================================== --- trunk/lib/plugin/FoafViewer.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/plugin/FoafViewer.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -52,7 +52,7 @@ * TODO: * - use a template. * - use the phpwiki internal user foaf data (stored by a UserPreferences extension) - * - fix the pear FOAF Parser or we'll write our own (based on our XmlParser) + * - fix the pear FOAF Parser or we'll write our own (based on our PhpWikiXmlParser) */ class WikiPlugin_FoafViewer extends WikiPlugin Modified: trunk/lib/stdlib.php =================================================================== --- trunk/lib/stdlib.php 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/lib/stdlib.php 2021-08-11 17:21:38 UTC (rev 10495) @@ -1710,7 +1710,7 @@ /** * Workaround for allow_url_fopen, to get the content of an external URI. * It returns the contents in one slurp. Parsers might want to check for allow_url_fopen - * and use fopen, fread chunkwise. (see lib/XmlParser.php) + * and use fopen, fread chunkwise. (see lib/PhpWikiXmlParser.php) */ function url_get_contents($uri) { Modified: trunk/locale/Makefile =================================================================== --- trunk/locale/Makefile 2021-08-11 16:37:58 UTC (rev 10494) +++ trunk/locale/Makefile 2021-08-11 17:21:38 UTC (rev 10495) @@ -379,7 +379,7 @@ ${POT_FILE}: .././lib/WysiwygEdit/tinymce.php ${POT_FILE}: .././lib/WysiwygEdit/Wikiwyg.php ${POT_FILE}: .././lib/XmlElement.php -${POT_FILE}: .././lib/XmlParser.php +${POT_FILE}: .././lib/PhpWikiXmlParser.php ${POT_FILE}: .././lib/XmlRpcClient.php ${POT_FILE}: .././lib/XmlRpcServer.php ${POT_FILE}: .././lib/XMLRPC/utils.php This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |