Skip to content

Instantly share code, notes, and snippets.

@avoronkin
Created January 21, 2011 10:48
Show Gist options
  • Save avoronkin/789526 to your computer and use it in GitHub Desktop.
Save avoronkin/789526 to your computer and use it in GitHub Desktop.
<?php
$parent = 156; //parent to put the new pages under
$published = 1; //published? 0 or 1
$hidemenu = 0; //hidemenu? 0 or 1
$template = 11; // template that you like to use for page
$newsdateId = 7; //newsdate tv id
$rssfeed = 'http://www.BLABLABLA.co.uk/index.php?id=68'; //put your feed here
$doc = new DOMDocument();
$doc->load($rssfeed);
if ($doc) {
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array(
'pagetitle' => addslashes($node->getElementsByTagName('pagetitle')->item(0)->nodeValue),
'longtitle' => addslashes($node->getElementsByTagName('longtitle')->item(0)->nodeValue),
'description' => addslashes($node->getElementsByTagName('description')->item(0)->nodeValue),
'alias' => trim($node->getElementsByTagName('alias')->item(0)->nodeValue),
'introtext' => addslashes($node->getElementsByTagName('introtext')->item(0)->nodeValue),
'menutitle' => addslashes($node->getElementsByTagName('menutitle')->item(0)->nodeValue),
'createdon' => addslashes($node->getElementsByTagName('createdon')->item(0)->nodeValue),
'pub_date' => addslashes($node->getElementsByTagName('pub_date')->item(0)->nodeValue),
'content' => addslashes($node->getElementsByTagName('content')->item(0)->nodeValue),
'parent' => $parent,
'published' => $published,
'hidemenu' => $hidemenu,
'template' => $template
);
if (!empty($newsdate)) {
$newsdate = $modx->toDateFormat(addslashes($node->getElementsByTagName('newsdate')->item(0)->nodeValue));
} else {
$newsdate = $modx->toDateFormat($itemRSS['pub_date']);
}
$tvs = array(
$newsdateId => $newsdate
);
// check if item is already in database
if (dimCheckDate($itemRSS['createdon']) == 0) {
// insert item into database
insert_Item($itemRSS, $tvs);
}
}
// an atempt to clear the cache I think that it does not realy works I need to clear cache by hand to see the pages online
$modx->clearCache();
return;
} else {
return '';
}
//function to put the data into the database
function insert_Item($itemArray, $tvs) {
global $modx;
$table_name = $modx->getFullTableName('site_content');
$modx->db->insert($itemArray, $table_name);
$doc_id = $modx->db->getInsertId();
echo $doc_id;
foreach ($tvs as $tv_id => $tv) {
$insert = array(
'tmplvarid' => $tv_id
, 'contentid' => $doc_id
, 'value' => $tv
);
$modx->db->insert($insert, $modx->getFullTableName('site_tmplvar_contentvalues'));
}
}
//check date, so that no double entry's are made in the database
function dimCheckDate($pubDate) {
global $modx;
$res = $modx->db->select("createdon", $modx->getFullTableName('site_content'), "createdon='" . $pubDate . "'");
if (count($modx->db->makeArray($res)) > 0) {
return 1;
} else {
return 0;
}
}
// function to get text inside tags to create a introtext or somthing from the first part (in my case it wass in <strong> tags
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment