-
Notifications
You must be signed in to change notification settings - Fork 9
CMG-824 | Delta Migration Support for Wordpress CMS #1023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chetan-contentstack
wants to merge
7
commits into
feature/delta-mig
Choose a base branch
from
feature/cmg-842-delta-wordpress
base: feature/delta-mig
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e750a8
Implement extractEntries functionality for WordPress migration; updat…
7425777
Refactor extractEntries logic to improve null safety and consistency;…
4dceab4
Enhance null safety in extractEntries by adding optional chaining to …
988e815
Update saveEntry function to enhance field UID matching logic for imp…
f4f58a9
Update lodash and jsdom versions in package.json and package-lock.jso…
9d207dc
Merge branch 'feature/delta-mig' into feature/cmg-842-delta-wordpress
d8cb5e5
fix(extractEntries): align source entry UID generation with WordPress…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import extractContentTypes from './libs/contentTypes'; | ||
| //import contentTypeMaker from './libs/contentTypeMapper'; | ||
| import extractLocale from './libs/extractLocale'; | ||
| import extractEntries from './libs/extractEntries'; | ||
|
|
||
| export { | ||
| extractContentTypes, | ||
| //contentTypeMaker, | ||
| extractLocale | ||
| } | ||
| extractContentTypes, | ||
| //contentTypeMaker, | ||
| extractLocale, | ||
| extractEntries | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| "use strict"; | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const config = require("../config/index.json"); | ||
|
|
||
| const { contentTypes: contentTypesConfig } = config?.modules ?? {}; | ||
| const contentTypeFolderPath = path.resolve(config?.data, contentTypesConfig?.dirName); | ||
| const EXCLUDED_POST_TYPES = new Set(['attachment', 'wp_global_styles', 'wp_navigation']); | ||
|
|
||
| const normalizeArray = (value) => { | ||
| if (!value) return []; | ||
| return Array.isArray(value) ? value : [value]; | ||
| }; | ||
|
|
||
| const idCorrector = (id) => { | ||
| const normalized = id?.replace(/[-{}]/g, ''); | ||
| return normalized ? normalized.toLowerCase() : id; | ||
| }; | ||
|
|
||
| const getEntryName = (item) => { | ||
| if (typeof item?.title === 'string' && item.title.trim()) return item.title.trim(); | ||
| if (item?.title?.text) return String(item.title.text).trim(); | ||
| if (typeof item?.['wp:post_name'] === 'string' && item['wp:post_name'].trim()) { | ||
| return item['wp:post_name'].trim(); | ||
| } | ||
| return 'Untitled Entry'; | ||
| }; | ||
|
|
||
| /** Align with api wordpress.service entry uid: idCorrector(`posts_${wp:post_id}`). */ | ||
| const getSourceEntryUid = (item) => { | ||
| const postId = item?.['wp:post_id']; | ||
| if (postId != null && String(postId).trim() !== '') { | ||
| return idCorrector(`posts_${postId}`); | ||
| } | ||
| const candidate = | ||
| item?.guid?.text ?? item?.guid ?? item?.link ?? getEntryName(item); | ||
| return idCorrector(String(candidate || '')); | ||
| }; | ||
|
|
||
| const getEntryLanguage = (item, channelLanguage) => { | ||
| const postMeta = normalizeArray(item?.['wp:postmeta']); | ||
| const languageMeta = postMeta.find((meta) => { | ||
| const key = String(meta?.['wp:meta_key'] || '').toLowerCase(); | ||
| return key === 'language' || key === '_language' || key === 'locale' || key === '_locale'; | ||
| }); | ||
|
|
||
| const metaLanguage = languageMeta?.['wp:meta_value']; | ||
| if (typeof metaLanguage === 'string' && metaLanguage.trim()) { | ||
| return metaLanguage.trim(); | ||
| } | ||
|
|
||
| if (typeof channelLanguage === 'string' && channelLanguage.trim()) { | ||
| return channelLanguage.trim(); | ||
| } | ||
|
|
||
| return 'en-us'; | ||
| }; | ||
|
|
||
| const extractEntries = async (filePath, contentTypeData = []) => { | ||
| try { | ||
| const rawData = await fs.promises.readFile(filePath, 'utf8'); | ||
| const jsonData = JSON.parse(rawData); | ||
| const items = normalizeArray(jsonData?.rss?.channel?.item); | ||
| const channelLanguage = jsonData?.rss?.channel?.language; | ||
|
|
||
| const groupedByType = items?.reduce((acc, item) => { | ||
| const postType = item?.['wp:post_type'] || 'unknown'; | ||
| if (EXCLUDED_POST_TYPES.has(postType)) return acc; | ||
| if (!acc[postType]) acc[postType] = []; | ||
| acc[postType].push(item); | ||
| return acc; | ||
| }, {}); | ||
|
|
||
| const updatedTypes = contentTypeData.map((ct) => ({ ...ct })); | ||
chetan-contentstack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for (const [type, entries] of Object.entries(groupedByType)) { | ||
| const entryMapping = normalizeArray(entries) | ||
| .map((item) => { | ||
| const otherCmsEntryUid = getSourceEntryUid(item); | ||
| if (!otherCmsEntryUid) return null; | ||
| return { | ||
| contentTypeUid: type, | ||
| entryName: getEntryName(item), | ||
| otherCmsEntryUid: `posts_${otherCmsEntryUid}`, | ||
| otherCmsCTName: type, | ||
| language: getEntryLanguage(item, channelLanguage), | ||
| isUpdate: false | ||
| }; | ||
| }) | ||
| .filter(Boolean); | ||
|
|
||
| const contentTypeFilePath = path.join(contentTypeFolderPath, `${type.toLowerCase()}.json`); | ||
| if (fs.existsSync(contentTypeFilePath)) { | ||
| const ctFile = JSON.parse(await fs.promises.readFile(contentTypeFilePath, 'utf8')); | ||
| ctFile.entryMapping = entryMapping; | ||
| await fs.promises.writeFile(contentTypeFilePath, JSON.stringify(ctFile, null, 4), 'utf8'); | ||
| } | ||
|
|
||
| const index = updatedTypes.findIndex( | ||
| (ct) => | ||
| ct?.otherCmsUid?.toLowerCase?.() === type.toLowerCase() || | ||
| ct?.contentstackUid?.toLowerCase?.() === type.toLowerCase() | ||
| ); | ||
| if (index >= 0) { | ||
| updatedTypes[index] = { ...updatedTypes[index], entryMapping }; | ||
| } | ||
| } | ||
|
|
||
| return updatedTypes; | ||
| } catch (error) { | ||
| console.error('Error while extracting WordPress entries:', error?.message || error); | ||
| return contentTypeData; | ||
| } | ||
| }; | ||
|
|
||
| module.exports = extractEntries; | ||
| module.exports.default = extractEntries; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import config from '../config/index.json'; | ||
|
|
||
| const { contentTypes: contentTypesConfig } = config?.modules; | ||
| const contentTypeFolderPath = path.resolve(config?.data, contentTypesConfig?.dirName); | ||
|
|
||
| const EXCLUDED_POST_TYPES = new Set(['attachment', 'wp_global_styles', 'wp_navigation']); | ||
|
|
||
| const normalizeArray = <T>(value: T | T[] | undefined): T[] => { | ||
| if (!value) return []; | ||
| return Array.isArray(value) ? value : [value]; | ||
| }; | ||
|
|
||
| const idCorrector = (id: string) => { | ||
| const normalized = id?.replace(/[-{}]/g, ''); | ||
| return normalized ? normalized.toLowerCase() : id; | ||
| }; | ||
|
|
||
| const getEntryName = (item: any): string => { | ||
| if (typeof item?.title === 'string' && item.title.trim()) { | ||
| return item.title.trim(); | ||
| } | ||
| if (item?.title?.text) { | ||
| return String(item.title.text).trim(); | ||
| } | ||
| if (typeof item?.['wp:post_name'] === 'string' && item['wp:post_name'].trim()) { | ||
| return item['wp:post_name'].trim(); | ||
| } | ||
| return 'Untitled Entry'; | ||
| }; | ||
|
|
||
| /** | ||
| * Must match api WordPress entry keys: idCorrector(`posts_${wp:post_id}`) in wordpress.service.ts. | ||
| * Raw post id only caused otherCmsEntryUid to diverge from uid-map / entry JSON keys → entry mapper showed "-". | ||
| */ | ||
| const getSourceEntryUid = (item: any): string => { | ||
| const postId = item?.['wp:post_id']; | ||
| if (postId != null && String(postId).trim() !== '') { | ||
| return idCorrector(`posts_${postId}`); | ||
| } | ||
| const candidate = | ||
| item?.guid?.text ?? item?.guid ?? item?.link ?? getEntryName(item); | ||
| return idCorrector(String(candidate || '')); | ||
| }; | ||
|
|
||
| const getEntryLanguage = (item: any, channelLanguage?: string): string => { | ||
| const postMeta = normalizeArray(item?.['wp:postmeta']); | ||
| const languageMeta = postMeta.find((meta: any) => { | ||
| const key = String(meta?.['wp:meta_key'] || '').toLowerCase(); | ||
| return key === 'language' || key === '_language' || key === 'locale' || key === '_locale'; | ||
| }); | ||
|
|
||
| const metaLanguage = languageMeta?.['wp:meta_value']; | ||
| if (typeof metaLanguage === 'string' && metaLanguage.trim()) { | ||
| return metaLanguage.trim(); | ||
| } | ||
|
|
||
| if (typeof channelLanguage === 'string' && channelLanguage.trim()) { | ||
| return channelLanguage.trim(); | ||
| } | ||
|
|
||
| return 'en-us'; | ||
| }; | ||
|
|
||
| const extractEntries = async (filePath: string, contentTypeData: any[] = []) => { | ||
| try { | ||
| const rawData = await fs.promises.readFile(filePath, 'utf8'); | ||
| const jsonData = JSON.parse(rawData); | ||
| const items = normalizeArray(jsonData?.rss?.channel?.item); | ||
| const channelLanguage = jsonData?.rss?.channel?.language; | ||
|
|
||
| const groupedByType = items?.reduce((acc: Record<string, any[]>, item: any) => { | ||
| const postType = item?.['wp:post_type'] || 'unknown'; | ||
| if (EXCLUDED_POST_TYPES.has(postType)) return acc; | ||
| if (!acc[postType]) acc[postType] = []; | ||
| acc[postType].push(item); | ||
| return acc; | ||
| }, {}); | ||
|
|
||
| const updatedTypes = contentTypeData?.map((ct) => ({ ...ct })); | ||
|
|
||
| for (const [type, entries] of Object.entries(groupedByType)) { | ||
| const entryMapping = normalizeArray(entries) | ||
| .map((item: any) => { | ||
| const otherCmsEntryUid = getSourceEntryUid(item); | ||
| if (!otherCmsEntryUid) return null; | ||
| return { | ||
| contentTypeUid: type, | ||
| entryName: getEntryName(item), | ||
| otherCmsEntryUid: `posts_${otherCmsEntryUid}`, | ||
| otherCmsCTName: type, | ||
| language: getEntryLanguage(item, channelLanguage), | ||
| isUpdate: false | ||
| }; | ||
| }) | ||
| .filter(Boolean); | ||
|
|
||
| const contentTypeFilePath = path.join(contentTypeFolderPath, `${type.toLowerCase()}.json`); | ||
| if (fs.existsSync(contentTypeFilePath)) { | ||
| const ctFile = JSON.parse(await fs.promises.readFile(contentTypeFilePath, 'utf8')); | ||
| ctFile.entryMapping = entryMapping; | ||
| await fs.promises.writeFile(contentTypeFilePath, JSON.stringify(ctFile, null, 4), 'utf8'); | ||
| } | ||
|
|
||
| const index = updatedTypes.findIndex( | ||
| (ct: any) => | ||
| ct?.otherCmsUid?.toLowerCase?.() === type.toLowerCase() || | ||
| ct?.contentstackUid?.toLowerCase?.() === type.toLowerCase() | ||
| ); | ||
| if (index >= 0) { | ||
| updatedTypes[index] = { | ||
| ...updatedTypes[index], | ||
| entryMapping | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return updatedTypes; | ||
| } catch (error: any) { | ||
| console.error('Error while extracting WordPress entries:', error?.message || error); | ||
| return contentTypeData; | ||
| } | ||
| }; | ||
|
|
||
| export default extractEntries; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.