DokuWiki

It's better when it's simple

User Tools

Site Tools


Sidebar

Translations of this page?:

Learn about DokuWiki

Advanced Use

Corporate Use

Our Community


Follow us on Facebook, Twitter and other social networks.

tips:fixmtime

Fix modification times based on timestamps

Occasionally you may want to move your dokuwikis to another host. To move your data, you will use things like ftp and tar/untar. During this process the file modification times of your pages may be modified, and they may no longer match the timestamps that are created by dokuwiki as meta-information.

The resulting symptom is that the actual author of a page will no longer be displayed. You will see an external edit instead.

To fix this, the file modification times have to be updated according to the timestamps.

Python Script

The following is a (throw-away) python script that does this. Run it from within the data/pages directory.

import os
path = os.path
 
os.stat_float_times(False)
 
meta = path.join(os.pardir, 'meta')
assert path.isdir(meta)
 
for root, dirs, filenames in os.walk(os.curdir):
    for filename in filenames:
        base, ext = path.splitext(filename)
        changelog = path.join(meta, root, base + ".changes")
        if not path.isfile(changelog):
            print "Changelog not found: %s" % changelog
            continue
        timestamp = 0
        for line in open(changelog):
            timestamp = int(line.split()[0])
        file = path.join(root, filename)
        print "updating %s" % file
        print "    timestamp: %s" % timestamp
        print "    old modification time: %s" % path.getmtime(file)
        os.utime(file, (timestamp, timestamp))
        print "    new modification time: %s" %  path.getmtime(file)

PHP script

The following is a PHP script that fixes modification times based on changelog timestamps this. Run it from within DokuWiki installation directory.

<?php 
/**
 * Fix modification times based on timestamps. Run from within DokuWiki installation directory.
 * @Author: dreamlusion <http://dreamlusion.eucoding.com>
  * last modified: 2008-09-05 4:15:00
 */
function WalkDirectory($parentDirectory) {
	global $_weeds;
 
	foreach(array_diff(scandir($parentDirectory), $_weeds) as $directory)
	{
		$path = $parentDirectory . '/'. $directory;
 
		if(is_dir($path)) 
		{
			WalkDirectory($path);
		}
		else 
		{
			// Calculate changes file path.
			$path_parts = pathinfo($path);
 
			// Remove pages path.
			global $_pagesPath;
			$relativePath = substr($path_parts['dirname'], strlen($_pagesPath), strlen($path_parts['dirname']));
 
			// Add <filename>.changes
			$filename = $path_parts['filename']; // Requires PHP 5.2.0 (http://gr2.php.net/manual/en/function.pathinfo.php)
			$relativePath .= '/' . $filename . '.' . 'changes';
 
			global $_metaPath;
			$changelog = $_metaPath . '/' . $relativePath;
 
			if (is_file($changelog))
			{
				$handle = @fopen($changelog, "r");
				if ($handle) {
				    while (!feof($handle)) {
				        $buffer = fgets($handle);
				        preg_match('/(?<timestamp>\d+)/', $buffer, $matches);
 
						if ($matches['timestamp'] != '')
							$timestamp = $matches['timestamp'];
 
				    }
				    fclose($handle);
				}
 
				// At this point we have our timestamp.
				echo 'Updating ' . $path . '<br />';
				echo '&nbsp;&nbsp;&nbsp;&nbsp;Timestamp in changelog: ' . $timestamp . '<br />';
				echo '&nbsp;&nbsp;&nbsp;&nbsp;Old modification time: ' . filemtime($path) . '<br />';
				if (touch($path, $timestamp))
				{
					// In my host, although the timestamp had changed successfully (checked manually), running filemtime($path) at this point 
					// did not return the correct timestamp, so use I use $timestamp instead of filemtime($path) to avoid confusing the user.
					echo '&nbsp;&nbsp;&nbsp;&nbsp;New modification time: ' . $timestamp . '<br />';
				}
				else
				{
					echo '&nbsp;&nbsp;&nbsp;&nbsp;Could not change modification time for page ' . $filename;
				}
			}
			else
			{
				echo 'Changelog not found: ' . $changelog . '<br />';
			}
		}
	} 
}
 
$_weeds = array('.', '..');
$_pagesPath = getcwd() . '/data/pages';
$_metaPath = getcwd() . '/data/meta';
 
WalkDirectory($_pagesPath);
 
?>
tips/fixmtime.txt · Last modified: 2008-09-01 03:21 by 89.210.228.96