--- /dev/null
+CPPFLAGS=$(shell php-config --includes)
+CXXFLAGS=-std=gnu++0x $(shell xml2-config --cflags) -fPIC -g
+LDFLAGS=-shared
+LIBS=../c++11/xml-template.o
+EXT_DIR=$(DESTDIR)$(shell php-config --extension-dir)/
+
+XML_Template.so: xml-template_wrap.o $(LIBS)
+ $(CXX) $(LDFLAGS) -o $@ $< $(LIBS)
+
+xml-template_wrap.cxx XML_Template_SWIG.php php_XML_Template_SWIG.h : xml-template.swig
+ swig -c++ -php7 $<
+
+clean:
+ $(RM) XML_Template.so xml-template_wrap.o
+ $(RM) xml-template_wrap.cxx xml-template_wrap.cpp XML_Template_SWIG.php php_XML_Template_SWIG.h
+
+install:
+ mkdir -p $(EXT_DIR)
+ install XML_Template.so $(EXT_DIR)
+
+xml-template_wrap.cpp: xml-template_wrap.cxx
+ cp xml-template_wrap.cxx xml-template_wrap.cpp
<?php
-require('xml-template.php');
# NULL values are ignored, but having one helps pointing out that
# the array given is non-associative
$doc = XML_Template_process_file('../xml/clone.xml', array(
'color' => 'blue',
'#things' => array(NULL)
-));
-print $doc->dump_mem();
+), true);
+print XML_Template_convert_doc_to_string($doc, true);
?>
<?php
-require('xml-template.php');
-
$doc = XML_Template_process_file('../xml/clone.xml', array(
'color' => 'red',
'#things' => array(
array( 'li' => 'Bright copper kettles', 'li/class' => 'odd' ),
array( 'li' => 'Warm, woolen mittens', 'li/class' => 'even' )
)
-));
-print $doc->dump_mem();
+), true);
+print XML_Template_convert_doc_to_string($doc, true);
?>
<?php
-include("xml-template.php");
-
$doc = XML_Template_process_file('../xml/clone.xml', array(
'color' => 'blue',
'#things' => array(
array( 'li' => 'Warm, woolen mittens' )
)
), true);
-print XML_Template_convert_doc_to_string($doc);
+print XML_Template_convert_doc_to_string($doc, true);
?>
--- /dev/null
+php-xml-template (1.0) unstable; urgency=medium
+
+ * Initial release.
+
+ -- Sebastian Bugge <vsbugge@samfundet.no> Fri, 06 Sep 2024 11:20:14 +0200
--- /dev/null
+Source: php-xml-template
+Priority: extra
+Maintainer: Steinar H. Gunderson <sesse@samfundet.no>
+Build-Depends: debhelper (>= 13.3.4~)
+Standards-Version: 3.9.1
+Section: php
+
+Package: php-xml-template
+Section: php
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}, ${php:Depends}
+Description: PHP package that provides xml-template for use in PHP.
--- /dev/null
+#!/bin/sh
+
+phpenmod xml-template
--- /dev/null
+#!/bin/sh
+
+phpdismod xml-template
--- /dev/null
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+MODS_AVAILBALE=$(shell php-config --ini-path)/../mods-available
+DESTDIR=debian/php-xml-template
+
+clean:
+ dh_testdir
+ dh_clean
+ $(MAKE) clean
+
+build:
+ dh_testdir
+ $(MAKE)
+
+binary-indep:
+
+binary-arch:
+ dh_testroot
+ dh_prep
+ dh_installdirs
+ $(MAKE) install DESTDIR=$(DESTDIR)
+ install -D -m 644 -o root -g root debian/xml-template.ini $(DESTDIR)$(MODS_AVAILBALE)/xml-template.ini
+ dh_installdocs
+ dh_installchangelogs
+ dh_compress
+ dh_fixperms
+ dh_strip
+ dh_makeshlibs
+ dh_shlibdeps
+ echo "php:Depends=phpapi-$(shell php-config --phpapi)" >> debian/php-xml-template.substvars
+ dh_installdeb
+ dh_gencontrol
+ dh_md5sums
+ dh_builddeb
+
+binary: binary-arch
--- /dev/null
+; configuration for php xml-template module
+extension=XML_Template.so
<?php
-include("xml-template.php");
-
$doc = XML_Template_process_file('../xml/included.xml', array(
'color' => 'red'
), false);
<?php
-require('xml-template.php');
-
$doc = XML_Template_process_file('../xml/namespace.xml', array(
'title' => 'Namespace tests',
'#hello' => 'Hello world!',
'#test' => 'Replaced.',
'tagname' => 'foo',
'#moretest' => 'bar'
-));
-print $doc->saveXML();
+), true);
+print XML_Template_convert_doc_to_string($doc, true);
?>
<?php
-require('xml-template.php');
-
$doc = XML_Template_process_file('../xml/namespace2.xml', array(
'title' => 'Namespace tests',
'#hello' => 'Replaced.',
-));
-print $doc->dump_mem();
+), true);
+print XML_Template_convert_doc_to_string($doc, true);
?>
<?php
-include("xml-template.php");
-
$doc = XML_Template_process_file('../xml/passthru.xml', array(), true);
-print XML_Template_convert_doc_to_string($doc);
+print XML_Template_convert_doc_to_string($doc, true);
?>
<?php
-include("xml-template.php");
-
$doc = XML_Template_process_file('../xml/simple.xml', array(
'title' => 'A very basic example',
'#hello' => 'Hello world!'
<?php
-require('xml-template.php');
-
$doc = XML_Template_process_file('../xml/structure.xml', array(
'#outer' => array(
array( '#inner' => 'One' ),
array( '#inner' => 'Two' ),
array( '#inner' => 'Three' ),
)
-));
-print $doc->saveXML();
+), true);
+print XML_Template_convert_doc_to_string($doc, true);
?>
HashTable *ht = Z_ARRVAL_P(obj);
if (is_associative_array(ht)) {
std::unordered_map<std::string, Directive *> my_map;
- for (zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS; zend_hash_move_forward(ht)) {
- char *str_key;
- ulong num_key;
- zend_hash_get_current_key(ht, &str_key, &num_key, 0);
-
+ zend_string *str_key;
+ zval *zv;
+ ulong num_key;
+ ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
std::string key;
- if (zend_hash_get_current_key_type(ht) == HASH_KEY_IS_STRING) {
- key = str_key;
+ if (str_key) {
+ key.assign(str_key->val, str_key->len);
} else {
char buf[32];
sprintf(buf, "%lu", num_key);
key = buf;
}
- zval **data;
- zend_hash_get_current_data(ht, (void **)&data);
- my_map.insert(make_pair(key, convert_php_objects_to_directive(*data)));
- }
+ my_map.insert(make_pair(key, convert_php_objects_to_directive(zv)));
+ } ZEND_HASH_FOREACH_END();
return new Substitute(my_map);
} else {
std::vector<Directive *> subdirectives;
for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
- zval **data;
- zend_hash_index_find(ht, i, (void **)&data);
- subdirectives.push_back(convert_php_objects_to_directive(*data));
+ zval *data = zend_hash_index_find(ht, i);
+ subdirectives.push_back(convert_php_objects_to_directive(data));
}
return new Clone(subdirectives);
}
break;
}
case IS_STRING: {
- char *str = Z_STRVAL_P(obj);
+ std::string str(Z_STRVAL_P(obj), Z_STRLEN_P(obj));
return new Replace(str);
}
case IS_LONG: {
snprintf(str, sizeof(str), "%f", Z_DVAL_P(obj));
return new Replace(str);
}
- case IS_RESOURCE: {
+ case IS_OBJECT: {
XmlDocPtrWrapper *doc;
if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_XmlDocPtrWrapper, 0) < 0 || doc == NULL) {
return NULL;
}
return new ReplaceInclude(xmlCopyDoc((*doc)->ptr, 1));
}
+ case IS_REFERENCE:
+ return convert_php_objects_to_directive(&Z_REF_P(obj)->val);
case IS_NULL:
return new Replace { "" };
default:
%}
%typemap(in) Directive* {
- $1 = convert_php_objects_to_directive(*$input);
+ $1 = convert_php_objects_to_directive(&$input);
}
XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean);
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- NULL,
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' )
- )
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/included.xml', array(
- 'color' => 'red'
-), 0);
-$master = XML_Template_process_file('../xml/master.xml', array(
- 'title' => 'Main HTML title',
- 'h1' => 'Nice heading here',
- 'contents' => $doc
-));
-print $master->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/namespace.xml', array(
- 'title' => 'Namespace tests',
- '#hello' => 'Hello world!',
- '#test' => 'Replaced.',
- 'tagname' => 'foo',
- '#moretest' => 'bar'
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/passthru.xml', array());
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/simple.xml', array(
- 'title' => 'A very basic example',
- '#hello' => 'Hello world!'
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/structure.xml', array(
- '#outer' => array(
- array( '#inner' => 'One' ),
- array( '#inner' => 'Two' ),
- array( '#inner' => 'Three' ),
- )
-));
-print $doc->dump_mem();
-?>
+++ /dev/null
-<?php
-
-function XML_Template_process_file($filename, $obj, $clean = 1)
-{
- $doc = domxml_open_file($filename);
- XML_Template_process($doc, $obj, $clean);
- return $doc;
-}
-
-function XML_Template_process($node, $obj, $clean = 1)
-{
- if (is_a($obj, 'domnode')) { # overwrite
- foreach ($node->child_nodes() as $child) {
- $node->remove_child($child);
- }
-
- if (is_a($obj, 'domdocument')) {
- $obj = $obj->document_element();
- }
-
- $newobj = own_clone_node($obj, $node->owner_document());
- $node->append_child($newobj);
-
- XML_Template_process($newobj, array(), $clean);
- } else if (!is_array($obj)) { # overwrite
- foreach ($node->child_nodes() as $child) {
- $node->remove_child($child);
- }
- $doc = $node->owner_document();
- $node->add_child($doc->create_text_node($obj));
- } else if (is_associative_array($obj)) { # substitute
- foreach ($node->child_nodes() as $child) {
- $processed = false;
-
- if ($child->node_type() == XML_ELEMENT_NODE) {
- unset($id);
-
- $tag = $child->node_name();
- $attrs = $child->attributes();
- if (isset($attrs)) {
- foreach ($child->attributes() as $attr) {
- if ($attr->namespace_uri() == 'http://template.sesse.net/' && $attr->name() == 'id') {
- $id = $attr->value();
- if ($clean) {
- $attr->unlink_node();
- }
- }
- }
- }
-
- # check all substitutions to see if we found anything
- # appropriate
- foreach (array_keys($obj) as $key) {
- # FIXME: we would want something like \Q and \E here...
- if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
- (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
- $child->set_attribute($matches[1], $obj[$key]);
- }
-
- if ($processed) {
- continue;
- }
- if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
- XML_Template_process($child, $obj[$key], $clean);
- $processed = true;
- }
- }
- }
-
- if (!$processed) {
- XML_Template_process($child, $obj, $clean);
- }
- }
- } else { # repeat
- $doc = $node->owner_document();
- $frag = $doc->create_element("temporary-fragment"); # ugh
-
- foreach ($node->child_nodes() as $child) {
- $frag->append_child($child);
- $node->remove_child($child);
- }
-
- foreach ($obj as $instance) {
- if (!isset($instance)) {
- continue;
- }
-
- $newnode = own_clone_node($frag, $frag->owner_document());
- $node->append_child($newnode);
- XML_Template_process($newnode, $instance, $clean);
- if ($clean) {
- XML_Template_clean($newnode);
- }
- }
-
- # remove all the <fragment> tags
-
- foreach ($node->child_nodes() as $child) {
- if ($child->name() != 'temporary-fragment') {
- continue;
- }
- foreach ($child->child_nodes() as $child2) {
- $node->append_child($child2);
- }
- $node->remove_child($child);
- }
- }
-
- if ($clean) {
- XML_Template_clean($node);
- }
-}
-
-function XML_Template_clean($node)
-{
- if ($node->node_type() == XML_ELEMENT_NODE) {
- if ($node->namespace_uri() != 'http://template.sesse.net/') {
- return;
- }
-
- # as this is a dummy node, we want to remove it and move everything further up
- # after we've done any required replacements
- $doc = $node->owner_document();
- $parent = $node->parent_node();
- foreach ($node->child_nodes() as $child) {
- $node->remove_child($child);
- $node->insert_before($child, $node);
- }
- $parent->remove_child($node);
- }
-}
-
-# FIXME: use varargs here
-function XML_Template_alternate($tag, $array, $elems)
-{
- $num = count($elems);
-
- for ($i = 0, $j = 0; $i < count($array); $i++) {
- if (isset($array[$i])) {
- $array[$i][$tag] = $elems[$j++ % $num];
- }
- }
-
- return $array;
-}
-
-# Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
-# PHP is buggy (at least PHP4); it removes the prefix information from all attributes
-# during a clone. IOW, we'll have to clone evverything ourselves.
-function own_clone_node($node, $doc)
-{
- // we only need these two
- if ($node->node_type() == XML_ELEMENT_NODE) {
- $nsuri = $node->namespace_uri();
- if (isset($nsuri)) {
- $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
- } else {
- $newnode = $doc->create_element($node->node_name());
- }
-
- $attrs = $node->attributes();
- if (isset($attrs)) {
- foreach ($node->attributes() as $attr) {
- $attr2 = $doc->create_attribute($attr->name(), $attr->value());
- $nsuri = $attr->namespace_uri();
- if (isset($nsuri)) {
- $attr2->set_namespace($nsuri, $attr->prefix());
- }
- $newnode->append_child($attr2);
- }
- }
- foreach ($node->child_nodes() as $child) {
- $newnode->append_child(own_clone_node($child, $doc));
- }
- return $newnode;
- } else {
- return $node->clone_node(true);
- }
-}
-
-function is_associative_array($arr)
-{
- if (!is_array($arr)) {
- return false;
- }
- $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
- return (count($diff) > 0);
-}
-?>
+++ /dev/null
-CPPFLAGS=$(shell php-config --includes)
-CXXFLAGS=-std=gnu++0x $(shell xml2-config --cflags) -fPIC -g
-LDFLAGS=-shared
-LIBS=../c++11/xml-template.o
-
-XML_Template_SWIG.so: xml-template_wrap.o $(LIBS)
- $(CXX) $(LDFLAGS) -o $@ $< $(LIBS)
-
-xml-template_wrap.cpp XML_Template_SWIG.php php_XML_Template_SWIG.h : xml-template.swig
- swig -c++ -php $<
-
-clean:
- $(RM) XML_Template_SWIG.so xml-template_wrap.o
- $(RM) xml-template_wrap.cpp XML_Template_SWIG.php php_XML_Template_SWIG.h
-
+++ /dev/null
-<?php
-include("xml-template.php");
-
-# NULL values are ignored, but having one helps pointing out that
-# the array given is non-associative
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => array(NULL)
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'red',
- '#things' => array(
- array( 'li' => 'Raindrops on roses', 'li/class' => 'odd' ),
- array( 'li' => 'Whiskers on kittens', 'li/class' => 'even' ),
- array( 'li' => 'Bright copper kettles', 'li/class' => 'odd' ),
- array( 'li' => 'Warm, woolen mittens', 'li/class' => 'even' )
- )
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- NULL,
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/namespace.xml', array(
- 'title' => 'Namespace tests',
- '#hello' => 'Hello world!',
- '#test' => 'Replaced.',
- 'tagname' => 'foo',
- '#moretest' => 'bar'
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/namespace2.xml', array(
- 'title' => 'Namespace tests',
- '#hello' => 'Replaced.',
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-include("xml-template.php");
-
-$doc = XML_Template_process_file('../xml/structure.xml', array(
- '#outer' => array(
- array( '#inner' => 'One' ),
- array( '#inner' => 'Two' ),
- array( '#inner' => 'Three' ),
- )
-), true);
-print XML_Template_convert_doc_to_string($doc);
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-# NULL values are ignored, but having one helps pointing out that
-# the array given is non-associative
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => array(NULL)
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'red',
- '#things' => array(
- array( 'li' => 'Raindrops on roses', 'li/class' => 'odd' ),
- array( 'li' => 'Whiskers on kittens', 'li/class' => 'even' ),
- array( 'li' => 'Bright copper kettles', 'li/class' => 'odd' ),
- array( 'li' => 'Warm, woolen mittens', 'li/class' => 'even' )
- )
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => XML_Template_alternate('li/class', array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- NULL,
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' ),
- ), array('odd', 'even'))
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/attribute4.xml', array(
- '#foo/value' => 'bar',
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/clone.xml', array(
- 'color' => 'blue',
- '#things' => array(
- array( 'li' => 'Raindrops on roses' ),
- array( 'li' => 'Whiskers on kittens' ),
- array( 'li' => 'Bright copper kettles' ),
- array( 'li' => 'Warm, woolen mittens' )
- )
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/included.xml', array(
- 'color' => 'red'
-), 0);
-$master = XML_Template_process_file('../xml/master.xml', array(
- 'title' => 'Main HTML title',
- 'h1' => 'Nice heading here',
- 'contents' => $doc
-));
-print $master->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/namespace2.xml', array(
- 'title' => 'Namespace tests',
- '#hello' => 'Replaced.',
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/passthru.xml', array());
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-require('xml-template.php');
-
-$doc = XML_Template_process_file('../xml/simple.xml', array(
- 'title' => 'A very basic example',
- '#hello' => 'Hello world!'
-));
-print $doc->saveXML();
-?>
+++ /dev/null
-<?php
-
-function XML_Template_process_file($filename, $obj, $clean = 1)
-{
- $doc = new DOMDocument;
- $doc->load($filename);
- XML_Template_process($doc, $obj, $clean);
- return $doc;
-}
-
-function XML_Template_process($node, $obj, $clean = 1)
-{
- if (is_a($obj, 'DOMNode')) { # overwrite
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- }
-
- if (is_a($obj, 'DOMDocument')) {
- $obj = $obj->documentElement;
- }
-
- $frag = $node->ownerDocument->createDocumentFragment();
- $frag->appendChild($node->ownerDocument->importNode($obj, true));
- XML_Template_process($frag, array(), $clean);
- $node->appendChild($frag);
- } else if (!is_array($obj)) { # overwrite
- while ($node->childNodes->length > 0) {
- $node->removeChild($node->firstChild);
- }
- $doc = $node->ownerDocument;
- $node->appendChild($doc->createTextNode($obj));
- } else if (is_associative_array($obj)) { # substitute
- $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length;
- $children = array();
- for ($i = 0; $i < $num_children; ++$i) {
- $children[] = $node->childNodes->item($i);
- }
-
- for ($i = 0; $i < $num_children; ++$i) {
- $child = $children[$i];
- $processed = false;
-
- if ($child->nodeType == XML_ELEMENT_NODE) {
- unset($id);
-
- $tag = $child->localName;
- $attrs = $child->attributes;
-
- if (isset($attrs)) {
- $replace_child = 0;
- $id_node = $child->getAttributeNodeNS('http://template.sesse.net/', 'id');
- if (isset($id_node)) {
- $id = $id_node->value;
- if ($clean) {
- $child->removeAttributeNode($id_node);
- $replace_child = 1;
- }
- }
-
- # This seems to be the only way to reliably get rid of the excess
- # XML namespace declarations.
- if ($replace_child == 1) {
- $newchild = own_clone_element($child, $child->ownerDocument);
- while ($child->childNodes->length > 0) {
- $newchild->appendChild($child->firstChild);
- }
- $node->replaceChild($newchild, $child);
- $child = $newchild;
- }
- }
-
- # check all substitutions to see if we found anything
- # appropriate
- foreach (array_keys($obj) as $key) {
- # FIXME: we would want something like \Q and \E here...
- if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
- (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
- $child->setAttribute($matches[1], $obj[$key]);
- }
-
- if ($processed) {
- continue;
- }
- if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
- XML_Template_process($child, $obj[$key], $clean);
- $processed = true;
- }
- }
- }
-
- if (!$processed) {
- XML_Template_process($child, $obj, $clean);
- }
- }
- } else { # repeat
- $doc = $node->ownerDocument;
- $frag = $doc->createDocumentFragment();
-
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- $frag->appendChild($child);
- }
-
- foreach ($obj as $instance) {
- if (!isset($instance)) {
- continue;
- }
-
- $newnode = own_clone_node($frag, $frag->ownerDocument);
- XML_Template_process($newnode, $instance, $clean);
-
- $node->appendChild($newnode);
- if ($clean) {
- XML_Template_clean($newnode);
- }
- }
- }
-
- if ($clean) {
- XML_Template_clean($node);
- }
-}
-
-function XML_Template_clean($node)
-{
- if ($node->nodeType == XML_ELEMENT_NODE) {
- if ($node->namespaceURI != 'http://template.sesse.net/') {
- return;
- }
-
- # as this is a dummy node, we want to remove it and move everything further up
- # after we've done any required replacements
- $doc = $node->ownerDocument;
- $parent = $node->parentNode;
-
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- $parent->insertBefore(own_clone_node($child, $doc), $node);
- }
- $parent->removeChild($node);
- }
-}
-
-# FIXME: use varargs here
-function XML_Template_alternate($tag, $array, $elems)
-{
- $num = count($elems);
-
- for ($i = 0, $j = 0; $i < count($array); $i++) {
- if (isset($array[$i])) {
- $array[$i][$tag] = $elems[$j++ % $num];
- }
- }
-
- return $array;
-}
-
-# Ideally, this would be "return $obj->clone_node(true)". But surprise,
-# surprise, PHP is buggy; it does not preserve the prefix information on
-# attributes properly during a clone. (PHP4 and PHP5 are broken in different
-# ways here...) IOW, we'll have to clone everything ourselves.
-function own_clone_node($node, $doc)
-{
- // we only need these two
- if ($node->nodeType == XML_DOCUMENT_FRAG_NODE) {
- $newnode = $doc->createDocumentFragment();
-
- for ($i = 0; $i < $node->childNodes->length; ++$i) {
- $child = $node->childNodes->item($i);
- $newnode->appendChild(own_clone_node($child, $doc));
- }
- return $newnode;
- } else if ($node->nodeType == XML_ELEMENT_NODE) {
- $newnode = own_clone_element($node, $doc);
- for ($i = 0; $i < $node->childNodes->length; ++$i) {
- $child = $node->childNodes->item($i);
- $newnode->appendChild(own_clone_node($child, $doc));
- }
- return $newnode;
- } else {
- return $node->cloneNode(true);
- }
-}
-
-function own_clone_element($node, $doc)
-{
- $nsuri = $node->namespaceURI;
- if (isset($nsuri) && $node->prefix != "default") {
- $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
-
- // remove useless empty text child
- $newnode->removeChild($newnode->firstChild);
- } else {
- $newnode = $doc->createElement($node->localName);
- }
-
- $attrs = $node->attributes;
- if (isset($attrs)) {
- foreach ($node->attributes as $attr) {
- $nsuri = $attr->namespaceURI;
- if (isset($nsuri) && $attr->prefix != "default") {
- $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
- } else {
- $attr2 = $doc->createAttribute($attr->localName);
- }
-
- # You've got to be kidding me...
- $attr2->value = preg_replace("/&/", "&", $attr->value);
- $newnode->appendChild($attr2);
- }
- }
- return $newnode;
-}
-
-function is_associative_array($arr)
-{
- if (!is_array($arr)) {
- return false;
- }
- $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
- return (count($diff) > 0);
-}
-?>
if [ "$L" = "php5" ]; then
php5-cgi -q ../php5/$T.php > $TEMPFILE
fi
- if [ "$L" = "php5-swig" ]; then
- ( cd ../c++11 && make -s && cd ../php5-swig && make -s && sudo cp XML_Template_SWIG.so /usr/lib/php5/20090626+lfs ) && php5-cgi -q ../php5-swig/$T.php > $TEMPFILE
+ if [ "$L" = "php7-swig" ]; then
+ ( cd ../c++11 && make -s && cd ../php7-swig && make -s && sudo cp XML_Template_SWIG.so /usr/lib/php/20151012 ) && php7-cgi -q ../php7-swig/$T.php > $TEMPFILE
fi
if [ "$L" = "python" ]; then
python2 ../python/$T.py > $TEMPFILE