Manipulating DokuWiki Dates

The Problem

The dates on my old blog entries were wrong. My old website ran on a database driven system, so the “date posted” on each blog entry was stored in a database column. When I switched to DokuWiki, I had to export my blog entries into separate text files. I didn't realize that DW relies on the file system's creation date as the equivalent to the posted date, so all my old entries had the wrong dates in DW (they had the date of the initial import, sometime in 2020). However, since the files I exported started with the posted date in yyyy-mm-dd format, I still had the date info available.

The Solution

There's no easy way I know of to manipulate file creation time in Linux, so I wrote an iPython notebook to change the system time and create new copies of the blog entries.

# Creation date manipulation
 
import os
import re
import datetime as dt
import shutil
 
def get_files_and_dates():
    files = []  # tuples of (filename, creation_date)
    for file in os.listdir('blog'):
        matches = re.search('(^\d\d\d\d)-(\d\d)-(\d\d)-.*\.txt$', file)
        if matches:
            group = matches.groups()            
            files.append((file, dt.date(int(group[0]), int(group[1]), int(group[2]))))
    return files
 
def get_date_cmd(date):
    return 'date -s {}-{}-{}'.format(date.year, date.month, date.day)
 
files = get_files_and_dates()
 
for f in files:
    # The original files are in a folder called blog, the new files will go in a folder
    # called goodtimes
    src = os.path.join('blog', f[0])
    dest = os.path.join('goodtimes', f[0])
    os.system(get_date_cmd(f[1]))
    shutil.copyfile(src, dest)
 
print("finished")

User Tools