/** * Hook to check for any new files in the system and register pages (or deregister them as necessary). * * Only runs if the config option is enabled to do so. */ public static function _AutoRegisterFiles() { if (!\ConfigHandler::Get('/markdownbrowser/autoregister')) { echo 'Skipping autoregistration of markdown files, configuration option for this feature is disabled.' . "\n"; return true; } $dir = \ConfigHandler::Get('/markdownbrowser/basedir'); $markdownFiles = []; if (!$dir) { echo 'Skipping autoregistration of markdown files, no markdown directory configured.' . "\n"; return true; } // Make sure it's readable! $dir = \Core\Filestore\Factory::Directory($dir); $dirbase = $dir->getPath(); $dirlen = strlen($dirbase); if (!$dir->exists()) { echo 'Skipping autoregistration of markdown files, ' . $dir->getPath() . ' does not exist.' . "\n"; return true; } $all = []; $files = $dir->ls('md', true); foreach ($files as $file) { /** @var \Core\Filestore\File $file */ $fileBase = substr($file->getFilename(), $dirlen); if (strpos($fileBase, 'index.md') !== false) { $fileRel = substr($fileBase, 0, -8); } else { $fileRel = substr($fileBase, 0, -3); } if (preg_match('/[A-Z]/', $fileRel) !== 0) { $warning = t('STRING_MARKDOWNBROWSER_WARNING_FILE_HAS_CAPITALS'); } elseif (strpos($fileRel, ' ')) { $warning = t('STRING_MARKDOWNBROWSER_WARNING_FILE_HAS_SPACES'); } else { $warning = ''; } if ($warning == '') { $url = '/markdownbrowser/view/' . $fileRel; $all[$url] = ['file' => $file, 'page' => null]; } else { echo $warning . ' - ' . $fileRel . "\n"; } } // Now that the files are loaded into memory, load any page that may already exist. // This will be used to ignore entries that already have a page, to create ones without, // and to remove pages that no longer have a corresponding file. $pages = PageModel::Find(['baseurl LIKE /markdownbrowser/view%']); foreach ($pages as $p) { $url = $p->get('baseurl'); if (!isset($all[$url])) { $all[$url] = ['file' => null, 'page' => null]; } $all[$url]['page'] = $p; } // Now $all contains everything I need to process on! :) foreach ($all as $url => &$dat) { /** @var PageModel|null $page */ $page = $dat['page']; /** @var \Core\Filestore\File|null $file */ $file = $dat['file']; if ($page && !$file) { // There is a page but no file, DELETE! $page->delete(); echo 'Deleted page for non-existent file: ' . $url . "\n"; } elseif (!$page && $file) { // There is a file but no page, create. $contents = $file->getContents(); // Convert these contents from markdown to HTML. $processor = new \Core\MarkdownProcessor(); $html = $processor->transform($contents); // Pre-populate this page with information from the rendered markdown document. // If this page exists, then it'll be updated and kept in sync. // Else, it'll still be set with what's in the document and kept in sync. $page = PageModel::Construct($url); $page->set('title', $processor->getMeta('title')); $page->set('body', $html); $page->set('baseurl', $url); $page->set('rewriteurl', $url); $page->set('editurl', str_replace('/markdownbrowser/view', '/markdownbrowser/update', $url)); $page->set('component', 'markdown-browser'); $page->set('selectable', 1); $page->set('published', $file->getMTime()); $page->set('updated', $file->getMTime()); $page->set('created', $file->getMTime()); $page->save(); echo 'Created page for new file: ' . $url . "\n"; } } return true; }