summaryrefslogtreecommitdiff
path: root/includes/export_lib.php
blob: 7f6c0055e07f526418abbe19bf217357cd682c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
 * Copyright (c) 2004 bitweaver.org
 * Copyright (c) 2003 tikwiki.org
 * Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
 * All Rights Reserved. See below for details and a complete list of authors.
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
 *
 * @package wiki
 */

/**
 * required setup
 */
namespace Bitweaver\Wiki;

/**
 * @package wiki
 * @subpackage ExportLib
 */
class ExportLib extends \Bitweaver\BitBase {

	function MakeWikiZip( $pExportFile ) {
		global $gBitUser,$gBitSystem;
		include_once UTIL_PKG_INCLUDE_PATH."tar.class.php";
		$tar = new tar();
		$query = "SELECT wp.`page_id` from `".BIT_DB_PREFIX."wiki_pages` wp INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = wp.`content_id`) 
				  ORDER BY lc.".$this->mDb->convertSortmode("title_asc");
		$result = $this->mDb->query($query,[]);
		while ($res = $result->fetchRow()) {
			$page_id = $res["page_id"];
			$content = $this->export_wiki_page($page_id, 0);
			$tar->addData($page_id, $content, $gBitSystem->getUTCTime());
		}
		$tar->toTar( $pExportFile, false);
		return '';
	}

	function export_wiki_page($page_id, $nversions = 1) {
		global $gBitSystem;
		$head = '';
		$head .= "Date: " . $gBitSystem->mServerTimestamp->get_rfc2822_datetime(). "\r\n";
		$head .= sprintf("Mime-Version: 1.0 (Produced by Tiki)\r\n");
		$iter = $this->get_page_history($page_id);
		$gWikiPage = new BitPage( $page_id );
		$gWikiPage->load();
		$info = $gWikiPage->mInfo;
		$parts = [];
		$parts[] = MimeifyPageRevision($info);
		if ($nversions > 1 || $nversions == 0) {
			foreach ($iter as $revision) {
				$parts[] = MimeifyPageRevision($revision);
				if ($nversions > 0 && 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_id) {
		$query = "SELECT lc.`title`, th.`description`, th.`version`, th.`last_modified`, th.`user_id`, th.`ip`, th.`data`, th.`history_comment`, uu.`login` as `creator_user`, uu.`real_name` " .
				 "FROM `".BIT_DB_PREFIX."wiki_pages` wp " .
				 "INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = wp.`content_id`) " .
				 "INNER JOIN `".BIT_DB_PREFIX."liberty_content_history` th ON (th.`page_id` = th.`page_id`) " .
				 "INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = th.`user_id`) " .
				 "WHERE wp.`page_id`=? order by th.".$this->mDb->convertSortmode("version_desc");
		$result = $this->mDb->query($query,[$page_id]);
		$ret = [];
		while ($res = $result->fetchRow()) {
			array_push( $ret, $res );
		}
		return $ret;
	}
}
$exportlib = new ExportLib();