Er, the comments section at
http://www.concrete5.org/help/building_with_concrete5/developers/mi... did not support code tags, and also it ate lots of my post (i.e., the actual PHP script - I sincerely hope it *did not* actually execute it!), so here's the last part from the line "Here goes" (for the beginning of the post, see the Migrating to Concrete article):
The code I used:
<?php
defined('C5_EXECUTE') or die("Access Denied.");
ini_set("auto_detect_line_endings", "1");
$indata = file("/PATH/TO/flatfile.txt", (int) FILE_IGNORE_EMPTY_LINES | (int) FILE_TEXT);
if(!$indata) die("couldn't open input file");
$logfile = fopen("/PATH/TO/log_file.txt", "w");
if(!$logfile) die("couldn't open log file");
// The page to store the imported pages under - it is best to make sure
// the page doesn't have any existing sub-pages
define('PAGE_TO_STORE_UNDER', '/news');
// The page type to use for the new pages
define('PAGE_TYPE', 'news_pages');
// Uncomment this to make pages invisible in menu
//define('EXCLUDE_FROM_NAV', TRUE);
function cleanpath($dirty)
<?php
defined('C5_EXECUTE') or die("Access Denied.");
ini_set("auto_detect_line_endings", "1");
$indata = file("/PATH/TO/flatfile.txt", (int) FILE_IGNORE_EMPTY_LINES | (int) FILE_TEXT);
if(!$indata) die("couldn't open input file");
$logfile = fopen("/PATH/TO/log_file.txt", "w");
if(!$logfile) die("couldn't open log file");
// The page to store the imported pages under - it is best to make sure
// the page doesn't have any existing sub-pages
define('PAGE_TO_STORE_UNDER', '/news');
// The page type to use for the new pages
define('PAGE_TYPE', 'news_pages');
// Uncomment this to make pages invisible in menu
//define('EXCLUDE_FROM_NAV', TRUE);
function cleanpath($dirty)
{
$c = preg_replace('/[^ a-z0-9_-]+/', "", strtolower($dirty));
return(str_replace(' ', '_', $c));
}
function fprint_r($v, $k, $logfile) {
fwrite($logfile, $k . ' => ' . $v . "<br>\r\n");
}
function doadd($pagename, $pagelocation, $date, $block, $logfile) {
Loader::model('collection_types');
$ptsu = Page::getByPath(PAGE_TO_STORE_UNDER);
$data = array();
$data['cName'] = $pagename;
$data['cPath'] = substr(PAGE_TO_STORE_UNDER . '/' . $pagelocation, 0, 71);
$data['cvIsApproved'] = 1;
$data['cDatePublic'] = trim($date) . ':00';
$ct = CollectionType::getByHandle(PAGE_TYPE);
$ptsu->add($ct, $data);
array_walk_recursive($data, 'fprint_r', $logfile);
fwrite($logfile, 'block: ' . $block . "***********************************\r\n");
$np = Page::getByPath($data['cPath']);
if(defined(EXCLUDE_FROM_NAV) $np->setAttribute('exclude_nav', 1);
$bt = BlockType::getByHandle(t('content'));
$data = array();
$data['content'] = $block;
$np->addBlock($bt, t('Content'), $data);
}
$curline = substr(current($indata), 3);
$firstline = TRUE;
while($curline !== FALSE){
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}\:[0-9]{2}/', trim($curline)) == 1) {
if(!$firstline)
doadd($pagename, cleanpath($pagename), $date, $block, $logfile);
$firstline = FALSE;
unset($block);
$date = $curline;
$pagename = next($indata);
$curline = next($indata);
}
if(isset($block))
$block = $block . "\r\n" . $curline;
else
$block = $curline;
$curline = next($indata);
}
fclose($logfile);
?>
Note that I a) back-translated this to English (was originally in Swedish) and b) changed my path specs to defines to spare you from editing all over the file. So - if there's a syntax error, just correct it ;) The procedure in and of itself worked for me, at least.
Also, to import from somewhere else, like another database, I guess the relevant parts to change are a) the '$indata =' line at the top (remove it or use it to specify your db), and b) the code at the bottom, starting from '$curline = substr...'.