/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/repairer.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2013, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
$ds = DIRECTORY_SEPARATOR;
require_once "classes{$ds}pathparser.php";
require_once "classes{$ds}pagereader.php";
 
/**
 * Pages data repair
 *
 * @package Showkase
 */
class Repairer
{
    /**
     * @var object instance of site singleton
     */
    private $config;
    
    /**
     * @var array data from page.xml files
     */
    private $pagesData = array();
    
    /**
     * @var integer maximum page ref found in page.xml files
     */
    private $maxRef = 0;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->config = SkConfig::getInstance();
    }
    
    /**
     * Scan directories for web pages
     *
     * @return void
     */
    public function scanPagesData()
    {
        $pathParser = new PathParser;
        $dirs = glob($this->config->getAbsoluteContentPath().'*');
        foreach ($dirs as $key=>$dirPath) {
            $pagePath = $pathParser->findRelativePath(getcwd(), $dirPath);
            $pagePath = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
            if (!is_dir($dirPath)) continue;
            if ($dirPath == getcwd()) continue;
            $fileName = basename($dirPath);
            if ($fileName[0] == '_') continue;
            if (file_exists($dirPath.DIRECTORY_SEPARATOR.'page.xml')) {
                $xmlData = PageReader::readPageVars($dirPath.DIRECTORY_SEPARATOR.'page.xml');
                if (!isset($xmlData['ss_pageType']) || !isset($xmlData['ss_pageRef'])) continue;
                $pageRef = intval($xmlData['ss_pageRef']);
                $this->maxRef = max($this->maxRef, $pageRef);
                $this->pagesData[$key]['pageType'] = $xmlData['ss_pageType'];
                $this->pagesData[$key]['pagePath'] = $pagePath;
                $this->pagesData[$key]['pageRef'] = $pageRef;
            }
        }
    }
    
    /**
     * Scan directories for files
     *
     * @return array scan data
     */
    function scanFilesData()
    {
        $pathParser = new PathParser;
        $paths = glob($this->config->getAbsoluteContentPath().'*');
        foreach ($paths as $key=>$path) {
            $pagePath = $pathParser->findRelativePath(getcwd(), $path);
            $fileName = basename($path);
            switch (true) {
                case ($path == getcwd()) :
                    break;
                case ($fileName[0] == '_') :
                    // this case must go first
                    $scanData[$key]['pageType'] = 'System';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
                    break;
                case (!is_dir($path)) :
                    $scanData[$key]['pageType'] = 'file';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
                    break;
                case (file_exists($path.DIRECTORY_SEPARATOR.'page.xml')) :
                    // paths to files have been eliminated in the previous condition $path is a directory
                    $xmlData = PageReader::readPageVars($path.DIRECTORY_SEPARATOR.'page.xml');
                    if (!isset($xmlData['ss_pageType']) || !isset($xmlData['ss_pageRef'])) continue;
                    $scanData[$key]['pageType'] = $xmlData['ss_pageType'].' page';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);  
                    break;
                case (is_dir($path.DIRECTORY_SEPARATOR.'jbcore')) :
                    $scanData[$key]['pageType'] = 'importable juicebox';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
                    break;
                case (is_dir($path.DIRECTORY_SEPARATOR.'svcore')) :
                    $scanData[$key]['pageType'] = 'importable simpleviewer';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
                    break;
                default :
                    $scanData[$key]['pageType'] = 'unknown';
                    $scanData[$key]['pagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $pagePath);
            }
        }
        return $scanData;   
    }
    
    /**
     * rebuild pages data file from scanned data
     * handles multiple page refs
     */
    public function rebuildPagesData(PageSet $pageSet, PageFactory $pageFactory)
    {
        if (count($this->pagesData) == 0) {
            Board::addMessage('No page records to rebuild', 'notice');
            return;
        }
        $pageSet->wipe();
        $pageRefs = array();
        foreach ($this->pagesData as $key=>$pageData) {
            $ref = $pageData['pageRef'];
            if (in_array($pageData['pageRef'], $pageRefs)) {
                $this->maxRef ++;
                $ref = $this->maxRef;
            }
            $page = $pageFactory->make($pageData['pageType'], $ref, $pageData['pagePath']);
            $page->saveLayer();
            $pageSet->addPagesDataRecord($page);
            $pageRefs[] = $ref;
        }
        $pageSet->savePagesData();
        $pageSet->setPagesPref('publishing', 'unpublished');
        $pageSet->savePagesPrefs();
        Board::addMessage('Page records rebuilt', 'notice');
    }
    
}