Oo-dom-export.php

From Organic Design wiki
Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.

<php><?

  1. Export article to OO format
  2. - only does document.php transform and its own wiki markup

$htdocs = $GLOBALS['_SERVER']['DOCUMENT_ROOT']; $error = false;

  1. Create a unique working folder and copy OO doc file-template to new file in it

mkdir($tmp = "$htdocs/wiki/tmp/".uniqid('export')); copy("$htdocs/wiki/xmlwiki/doc-template.sxw", $sxw = "$tmp/sxw");

  1. Zip compression library - www.phpconcept.net/pclzip/

define('PCLZIP_TEMPORARY_DIR', "$tmp/"); require_once("$htdocs/wiki/xmlwiki/lib/pclzip/pclzip.lib.php");

  1. Get OO content template article

$doc = xwDomificateArticle(xwArticleContent('oo-doc-template.xml'), 'OO-Template');

  1. Insert our document info into existing tokens

$nodes = xwXPathQuery($doc, '//p[@style-name="P1"]/text()'); $nodes[0]->set_content($title); if (0) { $nodes = xwXPathQuery($doc, '/document-content/body/p[@style-name="P2"]/text()'); $nodes[0]->set_content('Exported by '.$GLOBALS['xwUserName'].' on '.date('d M Y').' at '.date('H:i:s'));

  1. Append our article to document body
  2. Get document body node from template

$body = xwXPathQuery($doc, '/document-content/body'); $body = $body[0];

# Append a heading to the document body # - uses P9 style for H1 due to page-break-before function insertHeading(&$doc, $body, $content, $level = 1) { $text = $doc->create_element('text:h'); $text->set_content($content); $text->append_child($doc->create_attribute('text:style-name', $level > 1 ? "Heading $level" : 'P9')); $text->append_child($doc->create_attribute('text:level', $level)); $body->append_child($text); }

# Append a paragraph to the document body function insertParagraph(&$doc, $body, $content) { $text = $doc->create_element('text:p'); $text->set_content($content); $text->append_child($doc->create_attribute('text:style-name', "Text body")); $body->append_child($text); }

# Add style to passed text fragment - using DOM...? function addStyle($text, $style) { #<text:span text:style-name="T1">italic</text:span> #return; }

  1. Build mian document body
  2. - the match splits the article into sections at headings, extracting heading, level & text

if ($n = preg_match_all("/<h([1-9]).*?>(.+?)<\\/ *h\\1 *>(.+?)(?=(<h[1-9].*?>)|$)/ism", $article, $sections)) { for ($i = 1; $i < $n; $i++) { insertHeading($doc, $body, $sections[2][$i], $sections[1][$i]); insertParagraph($doc, $body, $sections[3][$i]); } } else { # No headings insertHeading($doc, $body, "Couldn't match any headings!"); insertParagraph($doc, $body, $article); } }

  1. Dump content doc to content.xml file in the working folder

$doc->dump_file($xml = "$tmp/content.xml");

  1. Add content.xml to the sxw archive

$zip = new PclZip($sxw); if ($zip->add($xml, PCLZIP_OPT_REMOVE_ALL_PATH)) { # Send raw SXW file back to browser and die ob_end_clean(); header('Content-type: application/zip'); header("Content-Disposition: attachment; filename=\"$title.sxw\""); @readfile($sxw); # Clean up and die @unlink("$tmp/content.xml"); @unlink("$tmp/sxw"); @rmdir($tmp); die; } else $error = $zip->errorInfo(true);

  1. Error, clean up files

if ($error) xwMessage($error,'red'); if (!@unlink("$tmp/content.xml")) xwMessage("Couldn't remove $tmp/content.xml!",'red'); if (!@unlink("$tmp/sxw")) xwMessage("Couldn't remove $tmp/sxw!",'red'); if (!@rmdir($tmp)) xwMessage("Couldn't remove $tmp",'red'); ?></php>