requarks / wiki

Wiki.js | A modern and powerful wiki app built on Node.js
https://js.wiki
GNU Affero General Public License v3.0
24.68k stars 2.73k forks source link

Importing from Local File System is ignoring dateCreated and date fields #4631

Open rahul-r opened 3 years ago

rahul-r commented 3 years ago

Actual behavior

I setup local file system storage in wiki.js settings. I created a markdown file with the following content and imported everything to wiki.js (clicked the Run button on Import Everything). --- title: Note-1 description: published: true date: 2012-07-14T22:31:05Z tags: tag1, tag2 editor: markdown dateCreated: 2012-07-14T22:24:50Z --- Note content

The file got imported into wiki.js as expected but the 'last edited by' time in the imported page shows the current time instead of time from the 'date' field of imported file and the page history shows the creation time as current time instead of 'dateCreated' from the imported file

Expected behavior

Imported pages should use the creation time and last edited time from the imported file instead of current time.

Steps to reproduce the behavior

  1. Enable local file system storage wiki.js (Settings > Storage > Local File System)
  2. Create a markdown file with the aforementioned content in the backup location
  3. Import the file into wiki.js by running 'Import Everything'
  4. Open the imported page in wiki.js and navigate to the page history page and note the creation and last edited times.
rtpt-romankarwacik commented 9 months ago

I used the following patch which disables the automatic updatedAt/createdAt setting, and imports the fields you mentioned (not fully tested though):

From 7adf73f65b88ac39c2c502032f9083b9646eec3f Mon Sep 17 00:00:00 2001
From: Roman Karwacik <roman.karwacik@redteam-pentesting.de>
Date: Tue, 23 Jan 2024 11:53:30 +0100
Subject: [PATCH] Fix creationDate import

---
 server/models/pages.js                | 11 ++++-------
 server/modules/storage/disk/common.js |  8 ++++++--
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/server/models/pages.js b/server/models/pages.js
index bb5b6585..89e07c13 100644
--- a/server/models/pages.js
+++ b/server/models/pages.js
@@ -115,13 +115,6 @@ module.exports = class Page extends Model {
     }
   }

-  $beforeUpdate() {
-    this.updatedAt = new Date().toISOString()
-  }
-  $beforeInsert() {
-    this.createdAt = new Date().toISOString()
-    this.updatedAt = new Date().toISOString()
-  }
   /**
    * Solving the violates foreign key constraint using cascade strategy
    * using static hooks
@@ -310,6 +303,8 @@ module.exports = class Page extends Model {
       path: opts.path,
       publishEndDate: opts.publishEndDate || '',
       publishStartDate: opts.publishStartDate || '',
+      createdAt: opts.createdAt || new Date().toISOString(),
+      updatedAt: opts.updatedAt || new Date().toISOString(),
       title: opts.title,
       toc: '[]',
       extra: JSON.stringify({
@@ -431,6 +426,8 @@ module.exports = class Page extends Model {
       publishEndDate: opts.publishEndDate || '',
       publishStartDate: opts.publishStartDate || '',
       title: opts.title,
+      createdAt: opts.createdAt || ogPage.createdAt || new Date().toISOString(),
+      updatedAt: opts.updatedAt || new Date().toISOString(),
       extra: JSON.stringify({
         ...ogPage.extra,
         js: scriptJs,
diff --git a/server/modules/storage/disk/common.js b/server/modules/storage/disk/common.js
index 9ed0b986..a25822de 100644
--- a/server/modules/storage/disk/common.js
+++ b/server/modules/storage/disk/common.js
@@ -93,7 +93,9 @@ module.exports = {
         isPrivate: false,
         content: pageData.content,
         user: user,
-        skipStorage: true
+        skipStorage: true,
+        createdAt: _.get(pageData, 'dateCreated', currentPage.createdAt).toISOString() || '',
+        updatedAt: _.get(pageData, 'date', currentPage.updatedAt).toISOString() || ''
       })
     } else {
       // Not in the DB, can mark as new
@@ -110,7 +112,9 @@ module.exports = {
         content: pageData.content,
         user: user,
         editor: pageEditor,
-        skipStorage: true
+        skipStorage: true,
+        createdAt: _.get(pageData, 'dateCreated', '').toISOString() || '',
+        updatedAt: _.get(pageData, 'date', '').toISOString() || '',
       })
     }
   },
-- 
2.43.0