Oo-dom-export.php
<php><?
- Export article to OO format
- - only does document.php transform and its own wiki markup
$htdocs = $GLOBALS['_SERVER']['DOCUMENT_ROOT']; $error = false;
- 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");
- Zip compression library - www.phpconcept.net/pclzip/
define('PCLZIP_TEMPORARY_DIR', "$tmp/"); require_once("$htdocs/wiki/xmlwiki/lib/pclzip/pclzip.lib.php");
- Get OO content template article
$doc = xwDomificateArticle(xwArticleContent('oo-doc-template.xml'), 'OO-Template');
- 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'));
- Append our article to document body
- 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; }
- Build mian document body
- - 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); } }
- Dump content doc to content.xml file in the working folder
$doc->dump_file($xml = "$tmp/content.xml");
- 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);
- 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>