protected function preparePost(ChefContext $context)
 {
     $result = $context->getResult();
     $app = $context->getApp();
     $log = $context->getLog();
     // Create the posts directory if it doesn't exist.
     if ($app->getPostsDir() == false) {
         $postsDir = $app->getRootDir() . PieCrustDefaults::CONTENT_POSTS_DIR;
         $log->info("Creating posts directory: {$postsDir}");
         mkdir($postsDir, 0777, true);
         $app->setPostsDir($postsDir);
     }
     // Create the relative path of the new post by using the
     // path format of the website's post file-system.
     $slug = $result->command->args['slug'];
     $replacements = array('%day%' => date('d'), '%month%' => date('m'), '%year%' => date('Y'), '%slug%' => $slug);
     $fs = FileSystem::create($app);
     $pathFormat = $fs->getPostPathFormat();
     $path = str_replace(array_keys($replacements), array_values($replacements), $pathFormat);
     // Figure out which blog to create this post for (if the website
     // is hosting several blogs).
     $blogKey = $result->command->options['blog'];
     $blogKeys = $app->getConfig()->getValue('site/blogs');
     if ($blogKey == null) {
         $blogKey = $blogKeys[0];
     } else {
         if (!in_array($blogKey, $blogKeys)) {
             throw new PieCrustException("Specified blog '{$blogKey}' is not one of the known blogs in this website: " . implode(', ', $blogKeys));
         }
     }
     // Get the blog subdir for the post.
     $blogSubDir = $blogKey . '/';
     if ($blogKey == PieCrustDefaults::DEFAULT_BLOG_KEY) {
         $blogSubDir = '';
     }
     // Create the full path.
     $fullPath = $app->getPostsDir() . $blogSubDir . $path;
     $relativePath = PieCrustHelper::getRelativePath($app, $fullPath);
     if (file_exists($fullPath)) {
         throw new PieCrustException("Post already exists: {$relativePath}");
     }
     $log->info("Creating new post: {$relativePath}");
     // Create the title and time of post.
     $title = preg_replace('/[\\-_]+/', ' ', $slug);
     $title = ucwords($title);
     $time = date('H:i:s');
     // Write the contents.
     if (!is_dir(dirname($fullPath))) {
         mkdir(dirname($fullPath), 0777, true);
     }
     $f = fopen($fullPath, 'w');
     fwrite($f, "---\n");
     fwrite($f, "title: {$title}\n");
     fwrite($f, "time: {$time}\n");
     fwrite($f, "---\n");
     fwrite($f, "My new blog post!\n");
     fclose($f);
 }
 public function initializeForTheme(IPieCrust $pieCrust)
 {
     parent::initialize($pieCrust);
     $themeDir = $pieCrust->getThemeDir();
     if (!$themeDir) {
         throw new PieCrustException("The given website doesn't have a theme.");
     }
     $this->pagesDir = $themeDir . PieCrustDefaults::CONTENT_PAGES_DIR;
     $this->postsDir = $themeDir . PieCrustDefaults::CONTENT_POSTS_DIR;
 }
 public function testImportWordpress()
 {
     $fs = MockFileSystem::create()->withPagesDir()->withPostsDir();
     $app = new PieCrust(array('root' => $fs->getAppRoot()));
     $importer = new PieCrustImporter($app);
     $sampleXml = PIECRUST_UNITTESTS_TEST_DATA_DIR . 'import/wordpress.test-data.2011-01-17.xml';
     $importer->import('wordpress', $sampleXml, array());
     // Re-create the app to load from the new values.
     $app = new PieCrust(array('root' => $fs->getAppRoot()));
     // Check the content.
     $pcFs = FileSystem::create($app);
     $pageFiles = $pcFs->getPageFiles();
     $this->assertCount(11, $pageFiles);
     $postFiles = $pcFs->getPostFiles();
     $this->assertCount(22, $postFiles);
 }
 public function initialize(\PieCrust\IPieCrust $pieCrust)
 {
     parent::initialize($pieCrust);
     if (DIRECTORY_SEPARATOR == '\\') {
         $homePath = getenv('USERPROFILE');
     } else {
         $homePath = getenv('HOME');
     }
     $defaultConfig = array('dir' => $homePath . DIRECTORY_SEPARATOR . 'Dropbox' . DIRECTORY_SEPARATOR . 'Documents', 'posts' => '%slug%.%ext%');
     $config = $pieCrust->getConfig()->getValue('dropbox');
     if (!$config) {
         $config = array();
     }
     $this->config = array_merge($defaultConfig, $config);
     if (substr($this->config['dir'], 0, 1) == '~') {
         $this->config['dir'] = $homePath . substr($this->config['dir'], 1);
     }
     $this->config['dir'] = rtrim($this->config['dir'], "/\\") . '/';
 }
 protected function ensurePostInfosCached($blogKey)
 {
     if ($this->postInfos == null) {
         $this->postInfos = array();
     }
     if (!isset($this->postInfos[$blogKey])) {
         $fs = FileSystem::create($this->pieCrust, $blogKey);
         $postInfos = $fs->getPostFiles();
         $this->postInfos[$blogKey] = $postInfos;
     }
 }
 public function run(ChefContext $context)
 {
     $logger = $context->getLog();
     $pieCrust = $context->getApp();
     $result = $context->getResult();
     // Get some options.
     $exact = $result->command->options['exact'];
     $fullPath = $result->command->options['full_path'];
     // If no type filters are given, return all types.
     $returnAllTypes = ($result->command->options['pages'] == false and $result->command->options['posts'] == false and $result->command->options['templates'] == false);
     // Validate the argument.
     $pattern = $result->command->args['pattern'];
     if ($exact) {
         // Check we have a path to match, and get its absolute value.
         if (!$pattern) {
             throw new PieCrustException("You need to specify a path when using the `--exact` option.");
         }
         $pattern = PathHelper::getAbsolutePath($pattern);
     } else {
         // If a pattern was given, get the Regex'd version.
         if ($pattern) {
             $pattern = PathHelper::globToRegex($pattern);
         }
     }
     // Get the pages and posts.
     $pages = array();
     if ($returnAllTypes or $result->command->options['pages']) {
         $pages = PageHelper::getPages($pieCrust);
     }
     if ($returnAllTypes or $result->command->options['posts']) {
         $blogKeys = $pieCrust->getConfig()->getValue('site/blogs');
         if ($result->command->options['blog']) {
             $blogKeys = array($result->command->options['blog']);
         }
         foreach ($blogKeys as $blogKey) {
             $pages = array_merge($pages, PageHelper::getPosts($pieCrust, $blogKey));
         }
     }
     // Get some other stuff.
     $returnComponents = $result->command->options['page_components'];
     // Get a regex for the posts file-naming convention.
     $fs = FileSystem::create($pieCrust);
     $pathComponentsRegex = preg_quote($fs->getPostPathFormat(), '/');
     $pathComponentsRegex = str_replace(array('%year%', '%month%', '%day%', '%slug%'), array('(\\d{4})', '(\\d{2})', '(\\d{2})', '(.+)'), $pathComponentsRegex);
     $pathComponentsRegex = '/' . $pathComponentsRegex . '/';
     // Print the matching pages.
     foreach ($pages as $page) {
         if ($result->command->options['no_special']) {
             // Skip special pages.
             if ($page->getUri() == PieCrustDefaults::CATEGORY_PAGE_NAME or $page->getUri() == PieCrustDefaults::TAG_PAGE_NAME) {
                 continue;
             }
         }
         if ($exact) {
             // Match the path exactly, or pass.
             if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $page->getPath())) {
                 continue;
             }
         } else {
             if ($pattern) {
                 // Match the regex, or pass.
                 if (!preg_match($pattern, $page->getUri())) {
                     continue;
                 }
             }
         }
         $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $page->getPath());
         if (!$fullPath) {
             $path = PieCrustHelper::getRelativePath($pieCrust, $path);
         }
         if ($returnComponents) {
             $components = array('path' => $path, 'type' => 'page', 'uri' => $page->getUri(), 'slug' => $page->getUri());
             if (PageHelper::isPost($page)) {
                 $matches = array();
                 if (preg_match($pathComponentsRegex, str_replace('\\', '/', $path), $matches) !== 1) {
                     throw new PieCrustException("Can't extract path components from path: {$path}");
                 }
                 $components['type'] = 'post';
                 $components['year'] = $matches[1];
                 $components['month'] = $matches[2];
                 $components['day'] = $matches[3];
                 $components['slug'] = $matches[4];
             }
             $str = '';
             foreach ($components as $k => $v) {
                 $str .= $k . ': ' . $v . PHP_EOL;
             }
             $logger->info($str);
         } else {
             $logger->info($path);
         }
     }
     // Get the template files and print them.
     if ($returnAllTypes or $result->command->options['templates']) {
         $templatesDirs = $pieCrust->getTemplatesDirs();
         foreach ($templatesDirs as $dir) {
             $dirIt = new \RecursiveDirectoryIterator($dir);
             $it = new \RecursiveIteratorIterator($dirIt);
             foreach ($it as $path) {
                 if ($it->isDot()) {
                     continue;
                 }
                 $relativePath = PieCrustHelper::getRelativePath($pieCrust, $path->getPathname());
                 if ($exact) {
                     // Match the path exactly, or pass.
                     if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $path->getPathname())) {
                         continue;
                     }
                 } else {
                     if ($pattern) {
                         // Match the regex, or pass.
                         if (!preg_match($pattern, $relativePath)) {
                             continue;
                         }
                     }
                 }
                 // Get the path to print.
                 $finalPath = $relativePath;
                 if ($fullPath) {
                     $finalPath = $path->getPathname();
                 }
                 $finalPath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $finalPath);
                 // Print the information!
                 if ($returnComponents) {
                     $logger->info("path: {$finalPath}");
                     $logger->info("type: template");
                 } else {
                     $logger->info($finalPath);
                 }
             }
         }
     }
     return 0;
 }
 public function __construct($pagesDir, $postsDir)
 {
     FileSystem::__construct($pagesDir, $postsDir);
 }
 private static function tryParsePostUri(IPieCrust $pieCrust, $blogKey, $uri, array &$pageInfo)
 {
     $postsDir = $pieCrust->getPostsDir();
     if ($postsDir === false) {
         return false;
     }
     $matches = array();
     $postsPattern = UriBuilder::buildPostUriPattern($pieCrust->getConfig()->getValueUnchecked($blogKey . '/post_url'));
     if (preg_match($postsPattern, $uri, $matches)) {
         $fs = FileSystem::create($pieCrust, $blogKey);
         $pathInfo = $fs->getPostPathInfo($matches);
         $date = mktime(0, 0, 0, intval($pathInfo['month']), intval($pathInfo['day']), intval($pathInfo['year']));
         $pageInfo['type'] = IPage::TYPE_POST;
         $pageInfo['blogKey'] = $blogKey;
         $pageInfo['date'] = $date;
         $pageInfo['path'] = $pathInfo['path'];
         return true;
     }
     return false;
 }
 public function testGetPageFiles()
 {
     $fs = MockFileSystem::create()->withPage('test1')->withPage('testxml.xml')->withPage('foo/test2')->withPage('foo/testtxt.txt')->withPage('foo/bar/test3')->withPage('foo/test-stuff')->withPage('bar/blah')->withPage('_tag')->withPage('_category')->withPage('otherblog/_tag')->withPage('otherblog/_category')->withPageAsset('bar/blah', 'something.txt')->withAsset('_content/pages/.whatever', 'fake')->withAsset('_content/pages/.DS_Store', 'fake')->withAsset('_content/pages/.svn/blah', 'fake')->withAsset('_content/pages/Thumbs.db', 'fake')->withAsset('_content/pages/foo/.DS_Store', 'fake')->withAsset('_content/pages/foo/Thumbs.db', 'fake')->withAsset('_content/pages/foo/.svn/blah', 'fake');
     $pc = new MockPieCrust();
     $pc->setPagesDir($fs->url('kitchen/_content/pages'));
     $pc->getConfig()->setValue('site/posts_fs', 'flat');
     $pcFs = FileSystem::create($pc);
     $pageFiles = $pcFs->getPageFiles();
     foreach ($pageFiles as &$pf) {
         // Fix slash/backslash problems on Windows that make
         // the test fail (PHP won't care about it so it's
         // functionally the same AFAIK).
         $pf['path'] = str_replace('\\', '/', $pf['path']);
         $pf['relative_path'] = str_replace('\\', '/', $pf['relative_path']);
     }
     $expected = array(array('path' => $fs->url('kitchen/_content/pages/test1.html'), 'relative_path' => 'test1.html'), array('path' => $fs->url('kitchen/_content/pages/testxml.xml'), 'relative_path' => 'testxml.xml'), array('path' => $fs->url('kitchen/_content/pages/foo/test2.html'), 'relative_path' => 'foo/test2.html'), array('path' => $fs->url('kitchen/_content/pages/foo/testtxt.txt'), 'relative_path' => 'foo/testtxt.txt'), array('path' => $fs->url('kitchen/_content/pages/foo/bar/test3.html'), 'relative_path' => 'foo/bar/test3.html'), array('path' => $fs->url('kitchen/_content/pages/foo/test-stuff.html'), 'relative_path' => 'foo/test-stuff.html'), array('path' => $fs->url('kitchen/_content/pages/bar/blah.html'), 'relative_path' => 'bar/blah.html'));
     $this->assertEquals($expected, $pageFiles);
 }