Revision [16229]
This is an old revision of FileexportAction made by DomBonj on 2007-02-27 17:23:06.
Fileexport Action
NOT included in any Wikka versionThis is the development page for the Fileexport action.
Installation
- Save the first code block below as action/fileexport.php
- Give it the same file permissions as the other php files in that directory.
- Save the second code block below as handlers/page/fileexport.xml.php
Code
actions/fileexport.php
<?php
#
# Displays a link to download a selection of the wiki content as a single HTML or XML file
#
# @package Actions
# @name fileexport
#
# @authors DomBonj
# @version 0.9
# @input Parameters = [html=('html'|'xml')] [sort=('tag'|'type'|'cnt')] [opts=['o']['p']] [cats='Category1','Category2'..]
# default values: type='html', sort='tag'
#
# @uses Wakka::Href()
#
if (!defined('FE_DOWNLOAD_LINK_TITLE')) define('FE_DOWNLOAD_LINK_TITLE', 'Download');
if (!defined('FE_DOWNLOAD_LINK_TEXT')) define('FE_DOWNLOAD_LINK_TEXT', 'Download %s file');
if (!function_exists('FEerror'))
{
function FEerror ($msg) {
return ("<em class='error'>$msg</em><br />");
}
}
$output = "";
if (isset($vars['cats'])) $categories = preg_split("/\s*,\s*/i", $vars['cats']);
if ( (isset($vars['opts']) && (!preg_match("/^[op]{1,2}$/i", $vars['opts'])))
|| (isset($vars['type']) && (!preg_match("/^(html|xml)$/i", $vars['type'])))
|| (isset($vars['sort']) && (!preg_match("/^(time|tag|title)$/i", $vars['sort']))))
$output .= FEerror("Usage: fileexport [own=\"Y|N\"] [sys=\"Y|N\"] [sort=\"tag|time|title\"] [cats=\"<i>category list</i>\"]");
else {
$type = (isset($vars['type']) ? strtolower($vars['type']) : 'html');
$query = "&type=$type";
$query .= "&own=". (preg_match("/[o]/i", $vars['opts']) ? 'n' : 'y');
$query .= "&sys=". (preg_match("/[p]/i", $vars['opts']) ? 'y' : 'n');
$query .= "&sort=". ((isset($vars['sort'])) ? $vars['sort'] : 'tag');
$query .= "&cats=";
$first = true;
if ($categories) {
foreach ($categories as $lacat) {
// sanitize categories
if (preg_match("/^(Category[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*)\s*$/", $lacat, $matches)) {
if ($first) {
$query .= $matches[1];
$first = false;
}
else $query .= "_".$lacat;
}
}
}
$output .= '<a href="'. $this->Href('fileexport.xml'.$query, $this->tag, ''). '" title="'.FE_DOWNLOAD_LINK_TITLE.'">'. sprintf(FE_DOWNLOAD_LINK_TEXT, strtoupper($type)). '</a>';
}
echo $output;
?>
#
# Displays a link to download a selection of the wiki content as a single HTML or XML file
#
# @package Actions
# @name fileexport
#
# @authors DomBonj
# @version 0.9
# @input Parameters = [html=('html'|'xml')] [sort=('tag'|'type'|'cnt')] [opts=['o']['p']] [cats='Category1','Category2'..]
# default values: type='html', sort='tag'
#
# @uses Wakka::Href()
#
if (!defined('FE_DOWNLOAD_LINK_TITLE')) define('FE_DOWNLOAD_LINK_TITLE', 'Download');
if (!defined('FE_DOWNLOAD_LINK_TEXT')) define('FE_DOWNLOAD_LINK_TEXT', 'Download %s file');
if (!function_exists('FEerror'))
{
function FEerror ($msg) {
return ("<em class='error'>$msg</em><br />");
}
}
$output = "";
if (isset($vars['cats'])) $categories = preg_split("/\s*,\s*/i", $vars['cats']);
if ( (isset($vars['opts']) && (!preg_match("/^[op]{1,2}$/i", $vars['opts'])))
|| (isset($vars['type']) && (!preg_match("/^(html|xml)$/i", $vars['type'])))
|| (isset($vars['sort']) && (!preg_match("/^(time|tag|title)$/i", $vars['sort']))))
$output .= FEerror("Usage: fileexport [own=\"Y|N\"] [sys=\"Y|N\"] [sort=\"tag|time|title\"] [cats=\"<i>category list</i>\"]");
else {
$type = (isset($vars['type']) ? strtolower($vars['type']) : 'html');
$query = "&type=$type";
$query .= "&own=". (preg_match("/[o]/i", $vars['opts']) ? 'n' : 'y');
$query .= "&sys=". (preg_match("/[p]/i", $vars['opts']) ? 'y' : 'n');
$query .= "&sort=". ((isset($vars['sort'])) ? $vars['sort'] : 'tag');
$query .= "&cats=";
$first = true;
if ($categories) {
foreach ($categories as $lacat) {
// sanitize categories
if (preg_match("/^(Category[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*)\s*$/", $lacat, $matches)) {
if ($first) {
$query .= $matches[1];
$first = false;
}
else $query .= "_".$lacat;
}
}
}
$output .= '<a href="'. $this->Href('fileexport.xml'.$query, $this->tag, ''). '" title="'.FE_DOWNLOAD_LINK_TITLE.'">'. sprintf(FE_DOWNLOAD_LINK_TEXT, strtoupper($type)). '</a>';
}
echo $output;
?>
handlers/page/fileexport.xml.php
<?php
#
# Exports part of the wiki content as a single HTML or XML file
#
# @package Handlers
# @name fileexport
#
# @authors DomBonj
# @version 0.91
#
# @uses Wakka::Format()
# @uses Wakka::GetConfigValue()
# @uses Wakka::GetUserName()
# @uses Wakka::HasAccess()
# @uses Wakka::LoadAll()
# @uses Wakka::LoadPage()
# @uses Wakka::SetPage()
#
// i18n
if (!defined('FE_TABLE_OF_CONTENTS')) define ('FE_TABLE_OF_CONTENTS', "Table of contents");
if (!function_exists('FEerror'))
{
function FEerror ($msg) {
return ("<em class='error'>$msg</em><br />");
}
function FEhtml ($this, $pages, $csspath="", $keyorder, $title="") {
$header = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n".
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />\n<style type='text/css'>";
if ($fcss = fopen($csspath, 'r')) {
if (!($stylesheet = file_get_contents($csspath)))
$stylesheet = "";
} else $stylesheet = "";
$header .= $stylesheet."\n</style><title>".$this->GetConfigValue("wakka_name")."</title></head><body>\n";
$toc = "";
$filename = $this->GetConfigValue("wakka_name")."_".date("Ymd");
foreach ($keyorder as $tag => $val) {
$toc .= "<li><a href='#{$pages[$tag]['i']}'>{$pages[$tag]['title']}</a></li>\n";
$output .= $pages[$tag]['body']."<hr />\n";
}
$output = "$header<div class='page'><h2>".FE_TABLE_OF_CONTENTS."</h2><ul>$toc</ul><hr />$output</div></body></html>";
header("Content-disposition: attachment; filename=$filename.html");
header('Content-Type: text/html; charset=ISO-8859-1');
header('Expires: 0');
header('Pragma: no-cache');
// header('Cache-Control: private');
print $output;
exit();
}
function FExml ($this, $pages, $keyorder, $title="") {
$filename = $this->GetConfigValue("wakka_name")."_".date("Ymd");
$output = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><wikka>';
foreach ($keyorder as $tag => $val) {
$mytab = $pages[$tag];
$output .= "<page tag=\"{$tag}\" lastchange=\"".strftime("%Y-%m-%dT%H:%M:%S", strtotime($mytab['lastchange']))."\"><owner>{$mytab['owner']}</owner>";
foreach (explode (' ', $mytab['cats']) as $mycat) {
if ($mycat) $output .= "<category>$mycat</category>";
}
$output .= "<title>".utf8_encode(htmlspecialchars($mytab['title'], ENT_COMPAT, "UTF-8"))."</title>\n";
$output .= "<raw_content>".utf8_encode(htmlspecialchars($mytab['raw'], ENT_COMPAT, "UTF-8")).".</raw_content>\n";
$output .= "<rendered_content>".utf8_encode(htmlspecialchars($mytab['body'], ENT_COMPAT, "UTF-8")).".</rendered_content>";
$output .= "</page>\n";
}
$output .= "</wikka>";
header("Content-disposition: attachment; filename=$filename.xml");
header('Content-Type: text/xml; charset=UTF-8');
header('Expires: 0');
header('Pragma: no-cache');
print $output;
exit();
}
}
$pages = array();
$error_msg = "";
if (isset($_GET['cats'])) $categories = preg_split("/_/i", $_GET['cats']);
if ((isset($_GET['own']) && !(preg_match("/^[YN]$/i", $_GET['own'])))
|| (isset($_GET['sys']) && (!preg_match("/^[YN]$/i", $_GET['sys'])))
|| (isset($_GET['type']) && (!preg_match("/^(html|xml)$/i", $_GET['type'])))
|| (isset($_GET['sort']) && (!preg_match("/^(time|tag|title)$/i", $_GET['sort']))))
$error_msg .= FEerror("illegal request string, unable to export");
else {
$save_page = $this->page;
$save_tag = $this->tag;
$csspath = "css/".$this->GetConfigValue("stylesheet");
$root_url = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$pagecnt = 0;
$query = "SELECT * FROM ".$this->config['table_prefix']."pages WHERE ((latest = 'Y')";
if ('y' == strtolower($_GET['own']))
$query .= " AND (owner = '".$this->GetUserName()."')";
if ('y' != strtolower($_GET['sys']))
$query .= " AND (owner <> '(Public)')";
$query .= ")";
$rows = $this->LoadAll($query);
foreach ($rows as $row) {
$itscats = array();
if ($categories[0]) {
$match = false;
foreach ($categories as $mycat) {
if (preg_match("/$mycat/i", $row['body'])) {
$match = true;
$itscats[] = $mycat;
}
}
} else {
$match = true;
// sanitize categories
preg_match_all("/(Category[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*)\b/", $row['body'], $matches);
foreach ($matches[1] as $mycat)
$itscats[] = $mycat;
}
if ($match && $this->HasAccess("read", $row['tag'])) {
// this page is to be exported: pretend your are this page
$this->SetPage($this->LoadPage($row['tag']));
// prevent recursive calling
$page = preg_replace('/\{\{\s*fileexport\b(.*?)\}\}/', "[fileexport action, $1]", $this->page['body']);
// fill 'pages' associative array
$pages[$row['tag']]['i'] = $pagecnt;
$pages[$row['tag']]['raw'] = $this->page['body'];
$pages[$row['tag']]['cats'] = implode (' ', $itscats);
$pages[$row['tag']]['lastchange'] = $this->page['time'];
$pages[$row['tag']]['ts'] = strtotime($this->page['time']);
$pages[$row['tag']]['owner'] = $this->page['owner'];
$pages[$row['tag']]['body'] = "<a name='$pagecnt'></a>".$this->Format($page, 'wakka');
$pagecnt++;
// set title for Table of Contents
$title = "";
if (preg_match('/^(={2,6})([^=\n]+)\1/', $this->page['body'], $matches)) {
$title = $matches[2];
$formatting_tags = array("**", "//", "__", "##", "''", "++", "#%", "@@", "\"\"");
$title = str_replace($formatting_tags, "", $title);
$title = strip_tags($this->Format($title));
}
$pages[$row['tag']]['title'] = ($title) ? $title : $this->page['tag'];
}
}
foreach ($pages as $tag => $mytab) {
// prepare mapping table for translation of intra-wiki links
$pat[] = "/".preg_quote('http://'.$root_url, '/')."\?wakka=".$tag."([^\w\.\/])/";
$rep[] = "#".$pages[$tag]['i']."$1";
}
foreach ($pages as $tag => $mytab) {
// preserve intra-wiki links
$pages[$tag]['body'] = preg_replace($pat, $rep, $mytab['body']);
}
// restore original values
$this->tag = $save_tag;
$this->page = $save_page;
// now, sort the associative array's keys
if (isset($_GET['sort'])) {
if ($_GET['sort'] == 'time') { $field = 'ts'; $fn = 'arsort'; }
else if ($_GET['sort'] == 'title') { $field = 'title'; $fn= 'asort'; }
}
// default case: sort on tag
if (!$field) { $field = 'tag'; $fn = 'asort'; }
$keyorder = array();
foreach ($pages as $tag => $mytab) {
$keyorder[$tag] = ($field == 'tag') ? strtolower($tag) : strtolower($mytab[$field]);
}
// do the actual sorting
$fn($keyorder);
if (!isset($_GET['type']) || $_GET['type']=='html')
FEhtml ($this, $pages, $csspath, $keyorder);
else if ($_GET['type']=='xml')
FExml ($this, $pages, $keyorder);
}
// display error message, if any
if ($error_msg) {
header("Content-type: text/xml");
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= '<?xml-stylesheet href="' . $this->GetConfigValue("base_url") .'/css/xml.css" type="text/css"?' .">\n";
$xml .= '<item><title>Error message</title>';
$xml .= '<description>'. $error_msg. '</description></item>';
print $xml;
}
?>
#
# Exports part of the wiki content as a single HTML or XML file
#
# @package Handlers
# @name fileexport
#
# @authors DomBonj
# @version 0.91
#
# @uses Wakka::Format()
# @uses Wakka::GetConfigValue()
# @uses Wakka::GetUserName()
# @uses Wakka::HasAccess()
# @uses Wakka::LoadAll()
# @uses Wakka::LoadPage()
# @uses Wakka::SetPage()
#
// i18n
if (!defined('FE_TABLE_OF_CONTENTS')) define ('FE_TABLE_OF_CONTENTS', "Table of contents");
if (!function_exists('FEerror'))
{
function FEerror ($msg) {
return ("<em class='error'>$msg</em><br />");
}
function FEhtml ($this, $pages, $csspath="", $keyorder, $title="") {
$header = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n".
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />\n<style type='text/css'>";
if ($fcss = fopen($csspath, 'r')) {
if (!($stylesheet = file_get_contents($csspath)))
$stylesheet = "";
} else $stylesheet = "";
$header .= $stylesheet."\n</style><title>".$this->GetConfigValue("wakka_name")."</title></head><body>\n";
$toc = "";
$filename = $this->GetConfigValue("wakka_name")."_".date("Ymd");
foreach ($keyorder as $tag => $val) {
$toc .= "<li><a href='#{$pages[$tag]['i']}'>{$pages[$tag]['title']}</a></li>\n";
$output .= $pages[$tag]['body']."<hr />\n";
}
$output = "$header<div class='page'><h2>".FE_TABLE_OF_CONTENTS."</h2><ul>$toc</ul><hr />$output</div></body></html>";
header("Content-disposition: attachment; filename=$filename.html");
header('Content-Type: text/html; charset=ISO-8859-1');
header('Expires: 0');
header('Pragma: no-cache');
// header('Cache-Control: private');
print $output;
exit();
}
function FExml ($this, $pages, $keyorder, $title="") {
$filename = $this->GetConfigValue("wakka_name")."_".date("Ymd");
$output = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><wikka>';
foreach ($keyorder as $tag => $val) {
$mytab = $pages[$tag];
$output .= "<page tag=\"{$tag}\" lastchange=\"".strftime("%Y-%m-%dT%H:%M:%S", strtotime($mytab['lastchange']))."\"><owner>{$mytab['owner']}</owner>";
foreach (explode (' ', $mytab['cats']) as $mycat) {
if ($mycat) $output .= "<category>$mycat</category>";
}
$output .= "<title>".utf8_encode(htmlspecialchars($mytab['title'], ENT_COMPAT, "UTF-8"))."</title>\n";
$output .= "<raw_content>".utf8_encode(htmlspecialchars($mytab['raw'], ENT_COMPAT, "UTF-8")).".</raw_content>\n";
$output .= "<rendered_content>".utf8_encode(htmlspecialchars($mytab['body'], ENT_COMPAT, "UTF-8")).".</rendered_content>";
$output .= "</page>\n";
}
$output .= "</wikka>";
header("Content-disposition: attachment; filename=$filename.xml");
header('Content-Type: text/xml; charset=UTF-8');
header('Expires: 0');
header('Pragma: no-cache');
print $output;
exit();
}
}
$pages = array();
$error_msg = "";
if (isset($_GET['cats'])) $categories = preg_split("/_/i", $_GET['cats']);
if ((isset($_GET['own']) && !(preg_match("/^[YN]$/i", $_GET['own'])))
|| (isset($_GET['sys']) && (!preg_match("/^[YN]$/i", $_GET['sys'])))
|| (isset($_GET['type']) && (!preg_match("/^(html|xml)$/i", $_GET['type'])))
|| (isset($_GET['sort']) && (!preg_match("/^(time|tag|title)$/i", $_GET['sort']))))
$error_msg .= FEerror("illegal request string, unable to export");
else {
$save_page = $this->page;
$save_tag = $this->tag;
$csspath = "css/".$this->GetConfigValue("stylesheet");
$root_url = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$pagecnt = 0;
$query = "SELECT * FROM ".$this->config['table_prefix']."pages WHERE ((latest = 'Y')";
if ('y' == strtolower($_GET['own']))
$query .= " AND (owner = '".$this->GetUserName()."')";
if ('y' != strtolower($_GET['sys']))
$query .= " AND (owner <> '(Public)')";
$query .= ")";
$rows = $this->LoadAll($query);
foreach ($rows as $row) {
$itscats = array();
if ($categories[0]) {
$match = false;
foreach ($categories as $mycat) {
if (preg_match("/$mycat/i", $row['body'])) {
$match = true;
$itscats[] = $mycat;
}
}
} else {
$match = true;
// sanitize categories
preg_match_all("/(Category[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*)\b/", $row['body'], $matches);
foreach ($matches[1] as $mycat)
$itscats[] = $mycat;
}
if ($match && $this->HasAccess("read", $row['tag'])) {
// this page is to be exported: pretend your are this page
$this->SetPage($this->LoadPage($row['tag']));
// prevent recursive calling
$page = preg_replace('/\{\{\s*fileexport\b(.*?)\}\}/', "[fileexport action, $1]", $this->page['body']);
// fill 'pages' associative array
$pages[$row['tag']]['i'] = $pagecnt;
$pages[$row['tag']]['raw'] = $this->page['body'];
$pages[$row['tag']]['cats'] = implode (' ', $itscats);
$pages[$row['tag']]['lastchange'] = $this->page['time'];
$pages[$row['tag']]['ts'] = strtotime($this->page['time']);
$pages[$row['tag']]['owner'] = $this->page['owner'];
$pages[$row['tag']]['body'] = "<a name='$pagecnt'></a>".$this->Format($page, 'wakka');
$pagecnt++;
// set title for Table of Contents
$title = "";
if (preg_match('/^(={2,6})([^=\n]+)\1/', $this->page['body'], $matches)) {
$title = $matches[2];
$formatting_tags = array("**", "//", "__", "##", "''", "++", "#%", "@@", "\"\"");
$title = str_replace($formatting_tags, "", $title);
$title = strip_tags($this->Format($title));
}
$pages[$row['tag']]['title'] = ($title) ? $title : $this->page['tag'];
}
}
foreach ($pages as $tag => $mytab) {
// prepare mapping table for translation of intra-wiki links
$pat[] = "/".preg_quote('http://'.$root_url, '/')."\?wakka=".$tag."([^\w\.\/])/";
$rep[] = "#".$pages[$tag]['i']."$1";
}
foreach ($pages as $tag => $mytab) {
// preserve intra-wiki links
$pages[$tag]['body'] = preg_replace($pat, $rep, $mytab['body']);
}
// restore original values
$this->tag = $save_tag;
$this->page = $save_page;
// now, sort the associative array's keys
if (isset($_GET['sort'])) {
if ($_GET['sort'] == 'time') { $field = 'ts'; $fn = 'arsort'; }
else if ($_GET['sort'] == 'title') { $field = 'title'; $fn= 'asort'; }
}
// default case: sort on tag
if (!$field) { $field = 'tag'; $fn = 'asort'; }
$keyorder = array();
foreach ($pages as $tag => $mytab) {
$keyorder[$tag] = ($field == 'tag') ? strtolower($tag) : strtolower($mytab[$field]);
}
// do the actual sorting
$fn($keyorder);
if (!isset($_GET['type']) || $_GET['type']=='html')
FEhtml ($this, $pages, $csspath, $keyorder);
else if ($_GET['type']=='xml')
FExml ($this, $pages, $keyorder);
}
// display error message, if any
if ($error_msg) {
header("Content-type: text/xml");
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= '<?xml-stylesheet href="' . $this->GetConfigValue("base_url") .'/css/xml.css" type="text/css"?' .">\n";
$xml .= '<item><title>Error message</title>';
$xml .= '<description>'. $error_msg. '</description></item>';
print $xml;
}
?>
CategoryUserContributions