Here is the script I use to convert the Sermon Index database into this repo(API) there are some helper static methods used, and the code for them can be found in the JCB helper class. There are also some Joomla API static helper classes.... but yea any contribution to improve this is welcome. I honestly just took the easies path to crank out the API :) I am very sure there are smarter ways.
We start with checking if we have all the sermons in memory... and then:
if (ComponentbuilderHelper::checkArray($items))
{
jimport('joomla.filesystem.folder');
// repo path
$repo_path = '/projects/sermonindex_api/';
// remove all the old files
ComponentbuilderHelper::removeFolder($repo_path, array('README.md', 'LICENSE.txt', '.git', 'images'));
// create the core folders again
JFolder::create($repo_path . 'scripture');
JFolder::create($repo_path . 'speaker');
JFolder::create($repo_path . 'topic');
// Get a db connection.
$db = JFactory::getDbo();
// function to get all scriptural keys
$getAllScripture = function ($id) use($db) {
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__sermonindex_scripture_to_sermon as a
$query->select('a.scriptureId');
$query->from($db->quoteName('#__sermonindex_scripture_to_sermon', 'a'));
// Get where bid is equal to
$query->where('a.sermonId = ' . (int) $id);
$db->setQuery($query);
$db->execute();
// check if there was data returned
if ($db->getNumRows())
{
return $db->loadColumn();
}
return false;
};
// function to get scriptural keys
$getScripture = function ($id) use($db) {
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__sermonindex_scripture as a
$query->select($db->quoteName(
array('a.book','a.scripture'),
array('book','chapter')));
$query->from($db->quoteName('#__sermonindex_scripture', 'a'));
// Get where bid is equal to
$query->where('a.id = ' . (int) $id);
$db->setQuery($query);
$db->execute();
// check if there was data returned
if ($db->getNumRows())
{
return $db->loadObject();
}
return false;
};
// speaker list
$per_speaker = array();
// topic bucket
$per_topic = array();
$index_topic = array();
$topic_name = array();
// scripture buckets
$per_book = array();
$per_beok = array();
$per_chapter = array();
foreach ($items as $nr => &$item)
{
// prep name
$item->name = trim($item->name);
// set the speaker safe name
$name = ComponentbuilderHelper::safeString($item->name);
// do we have an image path
if (ComponentbuilderHelper::checkString($item->image) && strpos($item->image, 'http') !== false)
{
// get image extension
$ext = pathinfo($item->image, PATHINFO_EXTENSION);
// set the preacher image name
$image_name = $name . '.' . $ext;
// check if the image exist
if (!file_exists($repo_path . 'images/' . $image_name))
{
// get the preacher image
if (($image = ComponentbuilderHelper::getFileContents($item->image, false)) !== false)
{
// store the speaker image to folder
ComponentbuilderHelper::writeFile($repo_path . 'images/' . $image_name, $image);
// update with full path
$item->image = '/images/' . $image_name;
}
else
{
$item->image = '';
}
}
else
{
// update with full path
$item->image = '/images/' . $image_name;
}
}
else
{
$item->image = '';
}
$item->description = trim(strip_tags($item->description));
unset($item->id);
unset($item->slug);
if (ComponentbuilderHelper::checkArray($item->audio_sermons))
{
usort($item->audio_sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
foreach ($item->audio_sermons as $i => $sermon)
{
if (($text = ComponentbuilderHelper::getVar('text', $sermon->id, 'id', 'description', '=', 'sermonindex')) !== false && $text !== 'nil')
{
$sermon->description = trim($text);
}
else
{
$sermon->description = '';
}
// set the sermon ID
$sermonID = $sermon->id;
// remove some stuff
unset($sermon->id);
unset($sermon->archive_url);
// set the short URL
$sermon->url = 'http://sermonindex.net/' . $sermon->short_url;
$sermon->download = 'http://sermonindex.net/' . $sermon->short_url . '-download';
unset($sermon->short_url);
// prep sermon in temp bucket
$tmp = $sermon;
$tmp->preacher_name = $item->name;
$tmp->preacher_image = $item->image;
$tmp->preacher_description = $item->description;
// this sermons scripture bucket
$scripture = array();
$tmp_per_scripture = array();
// now sort by scripture
if (($scriptureIDs = $getAllScripture($sermonID)) !== false)
{
foreach ($scriptureIDs as $scriptureID)
{
if (($keys = $getScripture($scriptureID)) !== false)
{
if (isset($keys->book) && ComponentbuilderHelper::checkString($keys->book))
{
if (!isset($tmp_per_scripture[$keys->book]))
{
$tmp_per_scripture[$keys->book] = array();
$scripture[$keys->book] = array();
}
// load the chapter
if (isset($keys->chapter) && ComponentbuilderHelper::checkString($keys->chapter))
{
$cnr = trim(str_replace($keys->book, '', $keys->chapter));
// check if verse is added
if (strpos($cnr, ':') !== false)
{
$cnr = explode(':', $cnr)[0];
}
// check if verse is added
if (strpos($cnr, '-') !== false)
{
$cnr = explode('-', $cnr);
}
// continue only if we have a number
if (is_numeric($cnr) && $cnr > 0)
{
$cnr = array($cnr);
}
// load chapters
if (ComponentbuilderHelper::checkArray($cnr))
{
foreach ($cnr as $chnr)
{
if (!isset($tmp_per_scripture[$keys->book][$chnr]))
{
$tmp_per_scripture[$keys->book][$chnr] = array();
if (!in_array($chnr, $scripture[$keys->book]))
{
$scripture[$keys->book][] = $chnr;
}
}
// per scripture
$tmp_per_scripture[$keys->book][$chnr] = true;
}
}
}
}
}
}
}
// build scripture object
if (ComponentbuilderHelper::checkArray($scripture))
{
$sermon->_scripture = array();
foreach ($scripture as $b => $cs)
{
foreach ($cs as $_c)
{
if (!empty($_c))
{
$sermon->_scripture[] = array('book' => $b, 'chapter' => $_c);
}
}
}
$tmp->_scripture = $sermon->_scripture;
}
else
{
// now set the scripture as an object
$sermon->_scripture = array();
$tmp->_scripture = array();
}
// no load the global per scripture
if (ComponentbuilderHelper::checkArray($tmp_per_scripture))
{
foreach ($tmp_per_scripture as $book_ => $chapters_)
{
if (ComponentbuilderHelper::checkString($book_))
{
if (!isset($per_book[$book_]))
{
$per_book[$book_] = array();
}
// set per book
$per_book[$book_][] = $tmp;
// set per chapter
if (ComponentbuilderHelper::checkArray($chapters_))
{
if (!isset($per_chapter[$book_]))
{
$per_chapter[$book_] = array();
}
foreach($chapters_ as $chapter_ => $hmm)
{
if (!isset($per_chapter[$book_][$chapter_]))
{
$per_chapter[$book_][$chapter_] = array();
}
// per scripture
$per_chapter[$book_][$chapter_][] = $tmp;
}
}
}
}
}
// build the topic array
$key = ComponentbuilderHelper::safeString($tmp->topic);
if (!isset($per_topic[$key]))
{
$per_topic[$key] = array();
$topic_name[$key] = $tmp->topic;
}
$per_topic[$key][] = $tmp;
}
// store the preacher
ComponentbuilderHelper::writeFile($repo_path . 'speaker/' . $name . '.json', json_encode($item, JSON_PRETTY_PRINT));
// set the speaker to index
$per_speaker[$name] = '/speaker/' . $name . '.json';
}
// unset the memory
unset($items[$nr]);
}
// sort the speakers
ksort($per_speaker);
// build the topic index
ComponentbuilderHelper::writeFile($repo_path . 'speaker/_sermonindex.json', json_encode($per_speaker, JSON_PRETTY_PRINT));
// now set the topics
foreach ($per_topic as $key => $_sermons)
{
// sort sermons
usort($_sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set name
$_topic_name = trim($topic_name[$key]);
if (!ComponentbuilderHelper::checkString($_topic_name))
{
$_topic_name = 'unsorted';
$key = $_topic_name;
}
// set the topic
$topic = new stdClass();
$topic->name = $_topic_name;
$topic->description = '';
$topic->audio_sermons = $_sermons;
// store the topic
ComponentbuilderHelper::writeFile($repo_path . 'topic/' . $key . '.json', json_encode($topic, JSON_PRETTY_PRINT));
// unset the memory
unset($topic_name[$key]);
// set the topic index
$index_topic[$key] = '/topic/' . $key . '.json';
}
// sort the topcis
ksort($index_topic);
// build the topic index
ComponentbuilderHelper::writeFile($repo_path . 'topic/_sermonindex.json', json_encode($index_topic, JSON_PRETTY_PRINT));
// now set the topics
foreach ($per_book as $boek => $_chapters)
{
// sort sermons
usort($_chapters, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set the book
$book = new stdClass();
$book->name = $boek;
$book->audio_sermons = $_chapters;
// get book key
$key = ComponentbuilderHelper::safeString($boek, 'L', '_', false, false);
// store the topic
ComponentbuilderHelper::writeFile($repo_path . 'scripture/' . $key . '.json', json_encode($book, JSON_PRETTY_PRINT));
// unset the memory
unset($book);
unset($per_book[$boek]);
// set the book index
$per_beok[$key] = '/scripture/' . $key . '.json';
// create the folder if it does not exist
if (!JFolder::exists($repo_path . 'scripture/' . $key))
{
JFolder::create($repo_path . 'scripture/' . $key);
}
// now set per chapter
if (isset($per_chapter[$boek]))
{
foreach ($per_chapter[$boek] as $number => $__sermons)
{
// sort sermons
usort($__sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set the book
$chapter = new stdClass();
$chapter->name = $boek . ' ' . $number;
$chapter->book = $boek;
$chapter->chapter = $number;
$chapter->audio_sermons = $__sermons;
// store the topic
ComponentbuilderHelper::writeFile($repo_path . 'scripture/' . $key . '/' . $number . '.json', json_encode($chapter, JSON_PRETTY_PRINT));
// unset the memory
unset($chapter);
unset($per_chapter[$boek]);
// set the topic index
$per_chapter[$key][$number] = '/scripture/' . $key . '/' . $number . '.json';
}
// sort the chapters
ksort($per_chapter[$key]);
// build the scripture index
ComponentbuilderHelper::writeFile($repo_path . 'scripture/' . $key . '/_sermonindex.json', json_encode($per_chapter[$key], JSON_PRETTY_PRINT));
// unset the memory
unset($per_chapter[$key]);
}
}
if (ComponentbuilderHelper::checkArray($per_beok))
{
// sort the books
ksort($per_beok);
// build the scripture index
ComponentbuilderHelper::writeFile($repo_path . 'scripture/_sermonindex.json', json_encode($per_beok, JSON_PRETTY_PRINT));
}
}
jexit('Done!'); // we end here
So every time we change things in the DB, I just run this script, and minutes later the whole API is updated.
Here is the script I use to convert the Sermon Index database into this repo(API) there are some helper static methods used, and the code for them can be found in the JCB helper class. There are also some Joomla API static helper classes.... but yea any contribution to improve this is welcome. I honestly just took the easies path to crank out the API :) I am very sure there are smarter ways.
We start with checking if we have all the sermons in memory... and then:
So every time we change things in the DB, I just run this script, and minutes later the whole API is updated.