TikiExportUpdate

Created by: mikelcu, Last modification: 16 May 2005 (20:12 UTC) by spiderr
(This appears to be code for exporting Wiki data.)

Here are 2 updated files, tiki-export_wiki_pages.php and exportlib.php. They have a couple minor hacks
1) new filename for a complete export: "tiki_export-<datestamp>.tar"
2) better headers (fixed wget, etc)
3) put directory and filenames into variables
4) tarfile is no longer passed as a Location: header, readfile() is used to send file contents to the client so file may be moved to some arbitrary point outside of the document root

Tested download with: Konqueror 3.1.3, Opera 7.23 Linux, Mozilla 1.6 Linux, Mozilla 1.4 Win, IE 6

tiki-export_wiki_pages.php


<?php

// $Header: /cvsroot/tikiwiki/tiki/tiki-export_wiki_pages.php,v 1.6 2003/12/04 10:40:53 mose Exp $

// Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.

// Modified by Mike Culbertson
// Tested with Opera 7.23 Linux, IE 6.0, Moz 1.4 Win, Moz 1.6 Linux, Konqueror 3.1.3

// Initialization
require_once ('setup.php');

include_once (
"lib/ziplib.php");
include_once (
'lib/wiki/exportlib.php');

/* I hate ifs without brackets ;) */
if ($tiki_p_admin_wiki != 'y')
{
        die;
}

// this should probably use a global date format
$datestamp date("dMY");

if (!isset(
$_REQUEST["page"])) {

        
$exportlib->MakeWikiZip();

        
/* This var should actually come from a global setting for a writable directory, but since there isn't one yet I'll just set it here */
        
$dumpdir "dump";

        
/*
                I'm not even sure yet if this next variable would show up here
                but we'll use it for now
        */
        
if(!empty($tikidomain))
        {
                
$filename "$dumpdir/$tikidomain/tiki_export-$datestamp.tar";
        }
        else
        {
                
$filename "$dumpdr/tiki_export-$datestamp.tar";
        }

        
/*
                if the tarfile doesn't get made, sneak a 404 in (probably needs better error checking there)
                otherwise send headers and read the file out to the client
                this way, when the dump dir gets moved, we will need only change the path
                also, name the file a bit more descriptively
        */

        
if(file_exists($filename))
        {
                
header("Pragma: public");
                
header("Expires: 0");
                
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                
header("Content-Type: application/octet-stream");
                
header("Content-Disposition: attachment; filename=\"tiki_export-$datestamp.tar\"");
                
header("Content-Description: File Transfer");

                @
readfile("$filename");
        }
        else
        {
                
header("Status: 404 Not Found");
        }

} else {
        if (isset(
$_REQUEST["all"]))
                
$all 0;
        else
                
$all 1;

        
$data $exportlib->export_wiki_page($_REQUEST["page"], $all);
        
$page $_REQUEST["page"];

        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
header("Content-Type: application/octet-stream");
        
header("Content-Disposition: inline; filename=\"$page-$datestamp\"");
        
header("Content-Description: File Transfer");

        echo 
$data;
}

?>


exportlib.php


<?php

class ExportLib extends TikiLib {
        function 
ExportLib($db) {
                
# this is probably uneeded now
                
if (!$db) {
                        die (
"Invalid db object passed to ExportLib constructor");
                }
                
$this->db $db;
        }

        function 
MakeWikiZip() {
                global 
$tikidomain;

                
// added datestamp, changed filenames - mkc

                
$datestamp date("dMY");
                
$zipname "wikidb-$datestamp.zip";
                include_once (
"lib/tar.class.php");
                
$tar = new tar();
                
$query "select `pageName` from `tiki_pages` order by ".$this->convert_sortmode("pageName_asc");
                
$result $this->query($query,array());

                while (
$res $result->fetchRow()) {
                        
$page $res["pageName"];
                        
$content $this->export_wiki_page($page0);
                        
$tar->addData($page$contentdate("U"));
                }

                
$tar->toTar("dump/$tikidomain/tiki_export-$datestamp.tar"FALSE);
                return 
'';
        }

        function 
export_wiki_page($pageName$nversions 1) {
                
$head '';
                
$head .= "Date: " $this->get_rfc2822_datetime(). "\r\n";
                
$head .= sprintf("Mime-Version: 1.0 (Produced by Tiki)\r\n");
                
$iter $this->get_page_history($pageName);
                
$info $this->get_page_info($pageName);
                
$parts = array();
                
$parts[] = MimeifyPageRevision($info);

                if (
$nversions || $nversions == 0) {
                        foreach (
$iter as $revision) {
                                
$parts[] = MimeifyPageRevision($revision);

                                if (
$nversions && count($parts) >= $nversions)
                                        break;
                        }
                }
                if (
count($parts) > 1)
                        return 
$head MimeMultipart($parts);

                
assert ($parts);
                return 
$head $parts[0];
        }

        
// Returns all the versions for this page
        // without the data itself
        
function get_page_history($page) {
                
$query "select `pageName`, `description`, `version`, `lastModif`, `user`, `ip`, `data`, `comment` from `tiki_history` where `pageName`=? order by ".$this->convert_sortmode("version_desc");
                
$result $this->query($query,array($page));
                
$ret = array();

                while (
$res $result->fetchRow()) {
                        
$aux = array();
                        
$aux["version"] = $res["version"];
                        
$aux["lastModif"] = $res["lastModif"];
                        
$aux["user"] = $res["user"];
                        
$aux["ip"] = $res["ip"];
                        
$aux["data"] = $res["data"];
                        
$aux["pageName"] = $res["pageName"];
                        
$aux["description"] = $res["description"];
                        
$aux["comment"] = $res["comment"];
                        
//$aux["percent"] = levenshtein($res["data"],$actual);
                        
$ret[] = $aux;
                }
                return 
$ret;
        }
}

$exportlib = new ExportLib($dbTiki);

?>
</datestamp>