<?php
/**
- * get data
+ * return entries of a folder
+ * @param path: the path to search. defaults to the CONTENT_DIR
+ * @param args array
*/
-function get_data( $sources = array(), $params = array())
+function get_entries( $path = "", $args = array())
{
- // if no set sources, grab everything in the content folder
- if (sizeof($sources) == 0)
- $sources = get_content_folders();
-
- $result = array();
- $result_total = 0;
- foreach ($sources as $dir) {
-
- if (is_array($dir) && isset($dir['basename']))
- $dir = $dir['basename'];
-
- $contents = glob(LOCAL_ROOT . CONTENT_DIR . DIRECTORY_SEPARATOR . $dir . '/*');
-
- if (sizeof($contents) <= 0) continue;
-
- foreach ($contents as $f) {
- if (is_file($f)) {
-
- if ($params['name']) {
- if (basename($f) != $params['name'])
- continue;
- }
-
- $parsed = parse_file($f);
-
- if ($parsed['draft'])
- continue;
-
- $result[] = $parsed;
- $result_total++;
- }
+ $recursive = isset($args['recursive']) ? $args['recursive'] : 1;
+ $order_by = empty($args['order_by']) ? null : $args['order_by'];
+ $order = empty($args['order']) ? SORT_DESC : $args['order'];
+
+ $path = LOCAL_ROOT . CONTENT_DIR . $path;
+ if ($recursive) {
+ $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME);
+ $dir_iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
+ }
+ else {
+ $dir_iterator = new DirectoryIterator($path);
+ }
+ $entries = array();
+ foreach ($dir_iterator as $file => $info) {
+ if (!$info->isDir()) {
+ $entries[] = parse_entry($info);
}
}
-
- // order by date
- foreach ($result as $key => $row) {
- $time[$key] = $row['timestamp'];
+
+ switch ($order_by)
+ {
+ default :
+ foreach ($entries as $key => $row)
+ $time[$key] = $row['timestamp'];
+ if ($time)
+ array_multisort($time, $order, $entries);
}
- if ($time)
- array_multisort($time, SORT_DESC, $result);
-
- return array($result, $result_total);
+
+ return array($entries, sizeof($entries));
}
/**
- * parse data file
+ * get all pages
*/
-function parse_file($f)
+function get_pages()
{
- $pathparts = explode("/", dirname($f));
-
- $file_contents = explode("\n", file_get_contents($f, FILE_USE_INCLUDE_PATH));
- $cc = "";
- $content = "";
- $conf = true;
- foreach ( $file_contents as $fc ) {
- if ($fc == CONFIG_DELIMITER) {
- $conf = false;
- continue;
- }
- if ($conf) $cc .= $fc . "\n";
- else $content .= $fc . "\n";
+ $path = LOCAL_ROOT . PAGE_DIR;
+ $dir_iterator = new DirectoryIterator($path);
+ $pages = array();
+ foreach($dir_iterator as $page) {
+ if ($page->isDir()) continue;
+ $arr = parse_entry($page, 1);
+ $arr['is_page'] = 1;
+ $pages[] = $arr;
}
-
- $config = parse_ini_string($cc);
-
- $res = $config;
-
- $res['url'] = $res['is_page'] == 1 ? get_base_dir() . '/' . basename($f) . '/' : get_base_dir() . '/' . $pathparts[sizeof($pathparts)-1] . '/' . basename($f);
- $res['timestamp'] = date('U', strtotime( $config['date'] ? $config['date'] : filemtime($f)));
- $res['cat'] = $pathparts[sizeof($pathparts)-1];
- $res['content'] = Markdown($content);
- $res['tags'] = explode(' ', $config['tags']);
-
- return $res;
+ return $pages;
}
/**
- * get content folders
+ * returns directories of a folder
+ * @param path the path to search. defaults to the CONTENT_DIR
+ * @param args array
*/
-function get_content_folders()
+function get_dirs( $path = "", $args = array())
{
- $folders = glob(LOCAL_ROOT . CONTENT_DIR . DIRECTORY_SEPARATOR . '/*', GLOB_ONLYDIR);
- $content_folders = array();
- foreach($folders as $folder)
- $content_folders[] = array(
- 'basename' => basename($folder),
- 'title' => basename($folder),
- 'url' => get_base_dir() . '/' . basename($folder) . '/'
- );
- return $content_folders;
+ $recursive = isset($args['recursive']) ? $args['recursive'] : 1;
+ $path = LOCAL_ROOT . CONTENT_DIR . $path;
+
+ if ($recursive) {
+ $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME);
+ $dir_iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
+ }
+ else {
+ $dir_iterator = new DirectoryIterator($path);
+ }
+ $dirs = array();
+ foreach ($dir_iterator as $dir => $info) {
+ if ($info->isDir() && $info->getFilename() != '.' && $info->getFilename() != '..') {
+ $dirs[] = str_replace($path, "", $info->getRealPath());
+ }
+ }
+ return $dirs;
}
/**
- * get pages
+ * @param splFileInfo SPLFileInfo Object
+ * @param page default is false
*/
-function get_pages()
+function parse_entry($fileInfo, $page = 0)
{
- $page_files = glob(LOCAL_ROOT . PAGE_DIR . DIRECTORY_SEPARATOR . '/*');
- $pages = array();
- foreach($page_files as $page) {
- $arr = parse_file($page);
- $arr['is_page'] = 1;
- $pages[] = $arr;
+ $config = "";
+ $content = "";
+ $passed_config = false;
+ $file_contents = file($fileInfo->getRealPath(), FILE_USE_INCLUDE_PATH);
+ foreach ( $file_contents as $line ) {
+ if (trim($line) == CONFIG_DELIMITER) {
+ $passed_config = true;
+ continue;
+ }
+ if (!$passed_config) {
+ $config .= $line;
+ continue;
+ }
+ $content .= $line;
}
- return $pages;
+
+ $file = array();
+ $file['config'] = parse_ini_string($config);
+
+ $file['title'] = $file['config']['title'];
+ $file['timestamp'] = $file['config']['date'] ? date('U', strtotime( $file['config']['date'])) : $fileInfo->getCTime();
+ $file['tags'] = $file['config']['tags'] ? explode(" ", $file['config']['tags']) : null;
+ $file['content'] = Markdown($content);
+ $file['cat'] = $page? null : substr(clean_slashes(str_replace(LOCAL_ROOT . CONTENT_DIR, "", $fileInfo->getPath())),1);
+ $file['path'] = $fileInfo->getRealPath();
+ $file['url'] = WEB_ROOT . ($page ? '' : $file['cat'] . '/' ) . $fileInfo->getFilename();
+
+ return $file;
}
+
?>