WeakWiki


modules\history\history_zlib.php

WeakWiki (WeakWiki zlib history implementation: history.zlib.php) Copyright (C) 2010 Alexander Lang This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . Project Home: http://weakwiki.robnet.wmweb.at/ Contact: robbiblubber@robnet.wmweb.at



/********************************************************************************
WeakWiki (WeakWiki zlib history implementation: history.zlib.php)
Copyright (C) 2010 Alexander Lang

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this
program; if not, see .

Project Home: http://weakwiki.robnet.wmweb.at/
Contact:      robbiblubber@robnet.wmweb.at
********************************************************************************/



require_once('./config/default.config.php');


define('_BREAKL', '*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~****');


/** Creates a history file for a wiki entry.
	@param $title	Wiki entry title.											*/
function _createHistory($title)
{
	$fn    = toTitle($title);
	$ctime = time();
	
	if(file_exists(_PATH_HISTORY . $fn . ".htable"))
	{
		rename(_PATH_HISTORY . $fn . ".htable", _PATH_HISTORY . $fn . "." . sprintf("%h", $ctime) . ".htable.keep");
	}
	if(file_exists(_PATH_HISTORY . $fn . ".hfile"))
	{
		rename(_PATH_HISTORY . $fn . ".hfile", _PATH_HISTORY . $fn . "." . sprintf("%h", $ctime) . ".hfile.keep");
	}
	
	$content = "|" . date("Y-m-d H:i:s") . "|" . currentUser()->getUID() . "|wiki entry created";
	
	file_put_contents(_PATH_HISTORY . toTitle($title) . '.htable', gzencode($content));
}


/** Gets the history file.
	@param $title	Title.														*/
function _getHistory($title, $expand = true)
{
	if($expand)
	{
		$f = _PATH_HISTORY . toTitle($title) . ".htable";
	}
	else
	{
		$f = _PATH_HISTORY . $title;
	}
	
	if(file_exists($f))
	{
		return "History: **" . trim($title) . "**\n\n\n|**timestamp** |**user** |**action** |**document** |\n" . _gzdecode(file_get_contents($f)) . "| - |\n";
	}
	
	return "\nHistory: **" . trim($title) . "**\n\n\nNo history.\n";
}


/** Gets a file from history.
	@param $title	Title.
	@param $id		File ID.													*/
function _getText($title, $id)
{
	$fn = toTitle($title);
	
	if(file_exists(_PATH_HISTORY . $fn . ".hfile"))
	{
		$m = _gzdecode(file_get_contents(_PATH_HISTORY . $fn . ".hfile"));
		
		$t = explode(_BREAKL, $m);
		
		for($i = 1; $i < count($t); $i++)
		{
			if($t[$i] == $id)
			{
				return $t[$i + 1];
			}
		}
	}
	
	return "";
}


/** Gets the file previous to a file from history.
	@param $title	Title.
	@param $id		File ID.													*/
function _getPrevious($title, $id)
{
	$fn = toTitle($title);
	
	if(file_exists(_PATH_HISTORY . $fn . ".hfile"))
	{
		$m = _gzdecode(file_get_contents(_PATH_HISTORY . $fn . ".hfile"));
		
		$t = explode(_BREAKL, $m);
		$u = explode("\n", $t[0]);
				
		$find = "";
		
		for($i = 0; $i < count($u) - 1; $i++)
		{
			if($u[$i] == $id)
			{
				$find = $u[$i + 1];
			}
		}
		
		if($find != "")
		{
			for($i = 1; $i < count($t); $i++)
			{
				if($t[$i] == $find)
				{
					return $t[$i + 1];
				}
			}
		}
	}
	
	return "";
}


/** Writes into a wiki entry history file.
	@param $title	Wiki entry title.
	@param $text	History log text.
	@return			Unique ID for associated wiki plaintext.					*/
function _writeHistory($title, $text)
{	
	$fn      = toTitle($title);
	$v       = sprintf("%x", rand(80, 1400)) . sprintf("%x", time());
	$content = "";
	
	$zip = null;
	
	if(file_exists(_PATH_HISTORY . $fn . ".htable"))
	{
		$content = _gzdecode(file_get_contents(_PATH_HISTORY . $fn . ".htable"));
		
		$content .= "|[[@H:" . $fn . "/" . $v . "]]|\n| " . date("Y-m-d H:i:s") . " | " . currentUser()->getUID() . " | " . $text;
		unlink(_PATH_HISTORY . $fn . ".htable");		 
	} 
	else
	{
		$content = "| ? | |" .
		           "|[[@H:" . $fn . "/" . $v . "]]|\n| " . date("Y-m-d H:i:s") . " | " . currentUser()->getUID() . " | " . $text;
	}
	
	file_put_contents(_PATH_HISTORY . $fn . '.htable', gzencode($content));
	
	
	$content = "";
	if(file_exists(_PATH_HISTORY . $fn . ".hfile"))
	{
		$content = $v . "\n" . _gzdecode(file_get_contents(_PATH_HISTORY . $fn . ".hfile"));
		unlink(_PATH_HISTORY . $fn . ".hfile");
	}
	else
	{
		$content = $v . _BREAKL;
	}
	
	$content .= $v . _BREAKL . file_get_contents(_PATH_CONTENT . $fn . ".wiki") . _BREAKL;
			
	file_put_contents(_PATH_HISTORY . $fn . '.hfile', gzencode($content, 9));
	
	return $v;
}


/** Renames history file.
	@param $oldTitle	Old title.
	@param $newTitle	New title.												*/
function _renameHistory($oldTitle, $newTitle)
{
	$oldTitle = toTitle($oldTitle);
	$newTitle = toTitle($newTitle);
	
	$ctime = time();
	
	if(file_exists(_PATH_HISTORY . $newTitle . ".htable"))
	{
		rename(_PATH_HISTORY . $newTitle . ".htable", _PATH_HISTORY . $newTitle . "." . $ctime . ".htable.keep");
	}
	if(file_exists(_PATH_HISTORY . $newTitle . ".hfile"))
	{
		rename(_PATH_HISTORY . $newTitle . ".hfile", _PATH_HISTORY . $newTitle . "." . $ctime . ".hfile.keep");
	}
	
	if(file_exists(_PATH_HISTORY . $oldTitle . ".htable"))
	{
		rename(_PATH_HISTORY . $oldTitle . ".htable", _PATH_HISTORY . $newTitle . ".htable");
	}
	if(file_exists(_PATH_HISTORY . $oldTitle . ".hfile"))
	{
		rename(_PATH_HISTORY . $oldTitle . ".hfile", _PATH_HISTORY . $newTitle . ".hfile");
	}
}

?>

WeakWiki