Skip to content

Instantly share code, notes, and snippets.

@AdamNaj AdamNaj/Import.ps1
Created Nov 7, 2018

Embed
What would you like to do?
Wordpress XML (to SXA) import script (messy output)
$xmlPath = "$AppPath\sugcon.xml"
$pageTemplate = "Project/Community/Page"
$siteHome = "master:\content\Community\SUGCON\Home"
$sitepath = "master:\content\Community\SUGCON"
[string[]]$content = get-content $xmlPath
[xml]$xml = $content
$pages = $xml.rss.channel.item | %{ [PSCustomObject]@{
creator=&{$_.creator."#cdata-section" };
guid=&{$_.guid."#text" };
is_sticky = &{$_.is_sticky};
link = &{$_.link};
menu_order = &{$_.menu_order};
post_meta = &{@($_.postmeta | %{[System.Web.HttpUtility]::UrlEncode($_.meta_key."#cdata-section") + "=" + [System.Web.HttpUtility]::UrlEncode($_.meta_value."#cdata-section") }) -join "&"};
post_date = &{$_.post_date."#cdata-section" };
post_date_gmt = &{$_.post_date_gmt."#cdata-section" };
post_id = &{$_.post_id};
post_name = &{$_.post_name."#cdata-section" };
post_parent = &{$_.post_parent};
post_type = &{$_.post_type."#cdata-section" };
pubDate = &{$_.pubDate};
status = &{$_.status."#cdata-section"};
title = &{$_.title};
content = "$(&{$_.encoded.'#cdata-section'})";
children = @()
}}
#fix names
$pages | ? { $_.post_name -eq "" -or $_.post_name -eq $null } |
% {
$_.post_name = "$($_.post_id)"
Write-Host "fixing name of post: $($_.post_id)"
}
# build tree of pages
$pages | ? {
$_.post_parent -ne 0
} | % {
$post_parent = $_.post_parent;
$parent = $null
$parent = $pages | ? {$_.post_id -eq $post_parent }
if($parent -eq $null){
"no parent"
} else {
$parent.children += $_
}
}
#cleanup pages that are rooted in other pages
$pages = $pages | ? { $_.post_parent -eq 0 }
function Create-Pages {
param(
[PSCustomObject[]] $pages,
[Item] $parentItem)
$pages | % {
$newPage = $_;
$NewItemName = "$($parentItem.ProviderPath)\$($newPage.post_name)"
Write-Host "Creating $NewItemName"
$newPageItem = New-Item -Path $NewItemName -ItemType $pageTemplate
$newPageItem.creator = $newPage.creator
$newPageItem.guid = $newPage.guid
$newPageItem.is_sticky = $newPage.is_sticky
$newPageItem.link = $newPage.link
$newPageItem.menu_order = $newPage.menu_order
$newPageItem.post_meta = $newPage.post_meta
$newPageItem.post_date = $newPage.post_date
$newPageItem.post_id = $newPage.post_id
$newPageItem.post_parent = $newPage.post_parent
$newPageItem.pubDate = $newPage.pubDate
$newPageItem.title = $newPage.title
$newPageItem.content = "$($newPage.content)"
if($newPage.children.count -gt 0){
Create-Pages $newPage.children $newPageItem
}
}
}
"Removing Pages under $siteHome"
#gci $siteHome | ? {$_.Templatename -eq "Page"} | %{ Remove-Item -Path $_.ProviderPath -Force -recurse }
gci $siteHome | ? {$_.Templatename -eq "Page"} | Remove-Item -recurse
"Removed children of $siteHome"
Create-Pages $pages (Get-Item $siteHome)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.