media version saving

This commit is contained in:
Kate Arzamastseva 2011-05-26 14:23:33 +03:00
parent f05345511b
commit e4f389ef17
4 changed files with 63 additions and 7 deletions

View File

@ -138,6 +138,7 @@ function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='',
// add changelog lines
$logline = implode("\t", $logline)."\n";
io_saveFile($conf['media_changelog'],$logline,true); //global media changelog cache
io_saveFile(mediaMetaFN($id,'.changes'),$logline,true); //media file's changelog
}
/**
@ -301,7 +302,7 @@ function _handleRecent($line,$ns,$flags,&$seen){
*
* @author Ben Coburn <btcoburn@silicodon.net>
*/
function getRevisionInfo($id, $rev, $chunk_size=8192) {
function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) {
global $cache_revinfo;
$cache =& $cache_revinfo;
if (!isset($cache[$id])) { $cache[$id] = array(); }
@ -312,7 +313,11 @@ function getRevisionInfo($id, $rev, $chunk_size=8192) {
return $cache[$id][$rev];
}
$file = metaFN($id, '.changes');
if ($media) {
$file = mediaMetaFN($id, '.changes');
} else {
$file = metaFN($id, '.changes');
}
if (!@file_exists($file)) { return false; }
if (filesize($file)<$chunk_size || $chunk_size==0) {
// read whole file
@ -398,7 +403,7 @@ function getRevisionInfo($id, $rev, $chunk_size=8192) {
*
* @author Ben Coburn <btcoburn@silicodon.net>
*/
function getRevisions($id, $first, $num, $chunk_size=8192) {
function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) {
global $cache_revinfo;
$cache =& $cache_revinfo;
if (!isset($cache[$id])) { $cache[$id] = array(); }
@ -406,11 +411,15 @@ function getRevisions($id, $first, $num, $chunk_size=8192) {
$revs = array();
$lines = array();
$count = 0;
$file = metaFN($id, '.changes');
if ($media) {
$file = mediaMetaFN($id, '.changes');
} else {
$file = metaFN($id, '.changes');
}
$num = max($num, 0);
$chunk_size = max($chunk_size, 0);
if ($first<0) { $first = 0; }
else if (@file_exists(wikiFN($id))) {
else if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) {
// skip current revision if the page exists
$first = max($first+1, 0);
}

View File

@ -230,7 +230,9 @@ function init_paths(){
$paths = array('datadir' => 'pages',
'olddir' => 'attic',
'mediadir' => 'media',
'mediaolddir' => 'media_attic',
'metadir' => 'meta',
'mediametadir' => 'media_meta',
'cachedir' => 'cache',
'indexdir' => 'index',
'lockdir' => 'locks',

View File

@ -346,6 +346,13 @@ function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'mov
global $conf;
global $lang;
$old = @filemtime($fn);
//
if(!@file_exists(mediaFN($id, $old)) && @file_exists($fn)) {
// add old revision to the attic if missing
saveOldMediaRevision($id);
}
// prepare directory
io_createNamespace($id, 'media');
@ -368,6 +375,26 @@ function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'mov
}
}
/**
* moves the current version to the media_attic and returns its
* revision date
*/
function saveOldMediaRevision($id, $move = 'copy'){
global $conf;
$oldf = mediaFN($id);
if(!@file_exists($oldf)) return '';
$date = filemtime($oldf);
$newf = mediaFN($id,$date);
io_makeFileDir($newf);
if($move($oldf, $newf)) {
// Set the correct permission here.
// Always chmod media because they may be saved with different permissions than expected from the php umask.
// (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
chmod($newf, $conf['fmode']);
}
return $date;
}
/**
* This function checks if the uploaded content is really what the
* mimetype says it is. We also do spam checking for text types here.

View File

@ -307,6 +307,19 @@ function metaFN($id,$ext){
return $fn;
}
/**
* returns the full path to the media's meta file specified by ID and extension
*
* The filename is URL encoded to protect Unicode chars
*/
function mediaMetaFN($id,$ext){
global $conf;
$id = cleanID($id);
$id = str_replace(':','/',$id);
$fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
return $fn;
}
/**
* returns an array of full paths to all metafiles of a given ID
*
@ -327,11 +340,16 @@ function metaFiles($id){
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function mediaFN($id){
function mediaFN($id, $rev=''){
global $conf;
$id = cleanID($id);
$id = str_replace(':','/',$id);
$fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
if(empty($rev)){
$fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
}else{
list($name, $ext) = explode(".", $id);
$fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name).'.'.$rev.'.'.utf8_encodeFN($ext);
}
return $fn;
}