DokuWiki

It's better when it's simple

User Tools

Site Tools


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
 * updated by Daniel "Nerun" Rodrigues in 2023-08-17
 */
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 ( isset($matches['timestamp']) && $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: 2023-08-17 19:17 by 2804:14c:6324:8204:3157:1995:f6:a0e2

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki