function assetTreeRemoveUnwantedPage(aohs\AssetOperationHandlerService $service, p\Child $child, array $params = NULL, array &$results = NULL)
{
    if ($child->getType() != a\Page::TYPE) {
        return;
    }
    $page_path = $child->getPathPath();
    // junk page names
    if (u\StringUtility::endsWith($page_path, "/pdf") || u\StringUtility::endsWith($page_path, "/word") || u\StringUtility::endsWith($page_path, "/externallink") || u\StringUtility::endsWith($page_path, "/keytiny") || u\StringUtility::endsWith($page_path, "/index-2")) {
        $service->delete($child->toStdClass());
    }
}
function processFolder($dir, &$array)
{
    global $host;
    if (is_dir($host . $dir . "/") && ($handle = opendir($host . $dir . "/"))) {
        while (false !== ($file = readdir($handle))) {
            if ($file == '.' || $file == '..') {
                continue;
            } else {
                if (is_file($host . $dir . "/" . $file)) {
                    $name = $dir . "/" . $file;
                    if (u\StringUtility::endsWith($name, ".php")) {
                        $array[] = $name;
                    }
                } else {
                    if (is_dir($host . $dir . "/" . $file . "/")) {
                        processFolder($dir . "/" . $file, $array);
                    }
                }
            }
        }
    }
}
        echo "Tis true", BR;
    } else {
        echo "Tis false", BR;
    }
    if (u\StringUtility::stringToBool(0)) {
        echo "Tis true", BR;
    } else {
        echo "Tis false", BR;
    }
    if (u\StringUtility::stringToBool("")) {
        echo "Tis true", BR;
    } else {
        echo "Tis false", BR;
    }
    echo u\StringUtility::boolToString(true), BR;
    echo u\StringUtility::startsWith("Hello", "He") ? "yes" : "no", BR;
    echo u\StringUtility::startsWith("Hello", "e") ? "yes" : "no", BR;
    echo u\StringUtility::removeSiteNameFromPath("site://cascade-admin/web-services/api/utility-classes/debug-utility"), BR;
    echo u\StringUtility::getParentPathFromPath("/web-services/api/utility-classes/debug-utility"), BR;
    echo u\StringUtility::getNameFromPath("/web-services/api/utility-classes/debug-utility"), BR;
    echo u\StringUtility::getMethodName("structuredData"), BR;
    u\DebugUtility::dump(u\StringUtility::getExplodedStringArray(";", "this;0;that;3;these"));
    echo u\StringUtility::getFullyQualifiedIdentifierWithoutPositions("this;0;that;3;these"), BR;
    echo u\StringUtility::endsWith("Hello", "lo") ? "yes" : "no", BR;
    echo u\StringUtility::endsWith("Hello", "l") ? "yes" : "no", BR;
    echo u\ReflectionUtility::getClassDocumentation("cascade_ws_utility\\StringUtility", true);
} catch (\Exception $e) {
    echo S_PRE . $e . E_PRE;
} catch (\Error $er) {
    echo S_PRE . $er . E_PRE;
}
function assetTreeSwitchDataDefinitionForBlock(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL)
{
    global $common_assets_site_name;
    $type = $child->getType();
    if ($type != a\DataBlock::TYPE) {
        return;
    }
    if (!isset($params["cascade"])) {
        throw new \Exception("The Cascade object is not supplied");
    } else {
        $cascade = $params["cascade"];
    }
    $block = $child->getAsset($service);
    $b_name = $block->getName();
    if ($b_name == "_footer-contact") {
        $dd_name = "Footer Contact";
    } elseif ($b_name == "_left-column") {
        $dd_name = "Left Column";
    } elseif ($b_name == "_right-column") {
        $dd_name = "Right Column";
    } elseif ($b_name == "_site-info") {
        $dd_name = "Site Info";
    } else {
        return;
    }
    $dd = $cascade->getAsset(a\DataDefinition::TYPE, $dd_name, $common_assets_site_name);
    $st = new p\StructuredData($dd->getStructuredData(), $service, $dd->getId());
    $identifiers = $block->getIdentifiers();
    foreach ($identifiers as $identifier) {
        // create the correct number of instances for multiple field
        if (u\StringUtility::endsWith($identifier, ";0") && $st->hasIdentifier($identifier)) {
            $st->createNInstancesForMultipleField($block->getNumberOfSiblings($identifier), $identifier);
        }
        // map the data
        if ($st->hasNode($identifier) && $block->isTextNode($identifier)) {
            $st->setText($identifier, $block->getText($identifier));
        } elseif ($st->hasNode($identifier) && $block->isAssetNode($identifier)) {
            $asset_node_type = $block->getAssetNodeType($identifier);
            if ($asset_node_type == "page" && $block->getPageId($identifier) != null) {
                $st->setPage($identifier, $cascade->getPage($block->getPageId($identifier)));
            } elseif ($asset_node_type == "file" && $block->getFileId($identifier) != null) {
                $st->setFile($identifier, $cascade->getFile($block->getFileId($identifier)));
            } elseif ($asset_node_type == "block" && $block->getBlockId($identifier) != null) {
                $st->setBlock($identifier, a\Block::getBlock($service, $block->getBlockId($identifier)));
            } elseif ($asset_node_type == "page,file,symlink" && $block->getLinkableId($identifier) != null) {
                $st->setLinkable($identifier, a\Linkable::getLinkable($service, $block->getLinkableId($identifier)));
            }
        }
    }
    $block->setStructuredData($st);
}
 public static function assetTreeReportRelativeLink(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL)
 {
     if (!isset($params['cache'])) {
         throw new e\ReportException(S_SPAN . c\M::NULL_CACHE . E_SPAN);
     }
     $cache = $params['cache'];
     $type = $child->getType();
     if ($type != DataDefinitionBlock::TYPE && $type != Page::TYPE && $type != File::TYPE) {
         return;
     }
     $asset = $cache->retrieveAsset($child);
     // .css and .js only
     // example: href="/com/index.php
     $pattern1 = "/href=[\"']\\/(\\S)+\\.php[\"']/";
     // example: /com/index.php
     $pattern2 = "/^\\/(\\S)+\\.php\$/";
     if ($type == File::TYPE) {
         $filename = $asset->getName();
         if (u\StringUtility::endsWith($filename, '.css') || u\StringUtility::endsWith($filename, '.js')) {
             $pattern3 = "/url\\(\\//";
             $pattern4 = "/url\\(\"\\//";
             $pattern5 = "/url\\('\\//";
             $matches = array();
             preg_match($pattern3, $asset->getData(), $matches);
             if (isset($matches[0])) {
                 $results[$type][] = $child->getPathPath();
                 return;
             }
             $matches = array();
             preg_match($pattern4, $asset->getData(), $matches);
             if (isset($matches[0])) {
                 $results[$type][] = $child->getPathPath();
                 return;
             }
             $matches = array();
             preg_match($pattern5, $asset->getData(), $matches);
             if (isset($matches[0])) {
                 $results[$type][] = $child->getPathPath();
                 return;
             }
         }
     } else {
         if ($asset->hasStructuredData()) {
             $identifiers = $asset->getIdentifiers();
             $count = count($identifiers);
             if ($count > 0) {
                 foreach ($identifiers as $identifier) {
                     if ($asset->isWYSIWYG($identifier)) {
                         $matches = array();
                         preg_match($pattern1, $asset->getText($identifier), $matches);
                         if (isset($matches[0])) {
                             $results[$type][] = $child->getPathPath();
                             return;
                         }
                     } else {
                         $matches = array();
                         preg_match($pattern2, $asset->getText($identifier), $matches);
                         if (isset($matches[0])) {
                             $results[$type][] = $child->getPathPath();
                             return;
                         }
                     }
                 }
             }
         } else {
             $matches = array();
             preg_match($pattern1, $asset->getXhtml(), $matches);
             if (isset($matches[0])) {
                 $results[$type][] = $child->getPathPath();
                 return;
             }
         }
     }
 }
function assetTreeSwitchPageContentType(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL)
{
    if ($child->getType() != a\Page::TYPE) {
        return;
    }
    // make sure that there is a content type passed in
    if (!isset($params['ct'])) {
        throw new \Exception("No content type is supplied");
    }
    // make sure that there is a base page passed in
    if (!isset($params['bp'])) {
        throw new \Exception("No base page is supplied");
    }
    // pages to skip
    if (isset($params['skip'])) {
        $skip = $params['skip'];
    }
    if (in_array($child->getPathPath(), $skip)) {
        return;
    }
    $ct = $params['ct'];
    $bp = $params['bp'];
    // retrieve page to be processed
    $p = $child->getAsset($service);
    // get number of content-group
    $identifier = "content-group;0";
    $num_of_groups = 1;
    // get the number of instances of the multiple node
    if ($p->hasIdentifier($identifier)) {
        $num_of_groups = $p->getNumberOfSiblings($identifier);
    }
    // get the current structured data with data
    $sd_old = $p->getStructuredData();
    if (!isset($sd_old)) {
        return;
    }
    // create enough instances of the multiple field
    if ($num_of_groups > 1) {
        $bp->createNInstancesForMultipleField($num_of_groups, $identifier);
    }
    // get the blank structured data with needed nodes
    $sd_new = clone $bp->getStructuredData();
    // roll back the base page
    if ($num_of_groups > 1) {
        $bp->createNInstancesForMultipleField(1, $identifier);
    }
    // switch the content type
    $p->setContentType($ct, false);
    // fix the data
    if (u\StringUtility::endsWith($child->getPathPath(), 'index')) {
        $sd_new->setText("right-column", "yes");
    } else {
        $sd_new->setText("right-column", "no");
    }
    $sd_new->setText("left-column", "yes");
    $sd_new->setText("left-column-group;left-setup", "default");
    // the H1
    $sd_new->setText("main-content-title", $sd_old->getText("main-content-title"));
    // main WYSIWYG
    $sd_new->setText("main-content-content", $sd_old->getText("main-content-content"));
    // multiple groups with choosers
    for ($i = 0; $i < $num_of_groups; $i++) {
        $identifier = "content-group;" . $i . ";content-group-chooser";
        // null the id for the upcoming round
        $block_id = NULL;
        // read the block
        if ($sd_old->hasIdentifier($identifier)) {
            $block_id = $sd_old->getBlockId($identifier);
        }
        // if there is a block attached
        if (isset($block_id) && $block_id != "") {
            $b = a\Block::getBlock($service, $block_id);
            $sd_new->setBlock($identifier, $b);
            $identifier = "content-group;" . $i . ";content-group-size";
            $size_str = strtolower($sd_old->getText($identifier));
            // fix the possible value for the new data definition
            if ($size_str != 'full') {
                $size_str = 'half';
            }
            $sd_new->setText($identifier, $size_str);
            $identifier = "content-group;" . $i . ";content-group-block-floating";
            $sd_new->setText($identifier, strtolower($sd_old->getText($identifier)));
        }
        // WYSIWYGs in group
        $identifier = "content-group;" . $i . ";content-group-content";
        $sd_new->setText($identifier, $sd_old->getText($identifier));
    }
    // set the data
    $p->setStructuredData($sd_new);
}
// to prevent time-out
set_time_limit(10000);
// to prevent using up memory when traversing a large site
ini_set('memory_limit', '2048M');
use cascade_ws_AOHS as aohs;
use cascade_ws_constants as c;
use cascade_ws_asset as a;
use cascade_ws_property as p;
use cascade_ws_utility as u;
use cascade_ws_exception as e;
try {
    $formats_folder = $cascade->getAsset(a\Folder::TYPE, "formats", $site_name);
    $velocity_folder = $cascade->getAsset(a\Folder::TYPE, "formats/library/velocity", $site_name);
    $library_format_names = array("chanw_database_utilities", "chanw_global_deque", "chanw_global_queue", "chanw_global_stack", "chanw_global_utility_objects", "chanw_global_values", "chanw_global_values_code", "chanw_global_velocity_code", "chanw_html_builder", "chanw_html_builder_with_deque", "chanw_library_import", "chanw_object_creator", "chanw_process_index_block", "chanw_process_xml", "chanw_reflect_utilities", "chanw_sorted_pages", "chanw_structured_data_worker", "upstate_database");
    foreach ($library_format_names as $library_format_name) {
        if (u\StringUtility::endsWith($library_format_name, "_code")) {
            $asset = $cascade->getAsset(a\XmlBlock::TYPE, $velocity_folder->getPath() . "/" . $library_format_name, $site_name);
            $block = true;
        } else {
            $asset = $cascade->getAsset(a\ScriptFormat::TYPE, $velocity_folder->getPath() . "/" . $library_format_name, $site_name);
            $block = false;
        }
        $source_path = $source . $library_format_name . ".xml";
        $source_content = file_get_contents($source_path);
        if ($source_content != "") {
            if ($block) {
                $source_content = str_replace("_common_assets", $site_name, $source_content);
                $asset->setXml($source_content)->edit();
            } else {
                $source_content = str_replace("<code>", "", $source_content);
                $source_content = str_replace("</code>", "", $source_content);
 public function isIdentifierOfFirstNode($identifier)
 {
     if ($this->isMultiple($identifier)) {
         return u\StringUtility::endsWith($identifier, ";0");
     }
     return false;
 }
function assetTreeSwitchPageContentType(aohs\AssetOperationHandlerService $service, p\Child $child, array $params = NULL, array &$results = NULL)
{
    if ($child->getType() != a\Page::TYPE) {
        return;
    }
    // make sure that there is a content type passed in
    if (!isset($params['ct'])) {
        throw new \Exception("No content type is supplied");
    }
    // make sure that there is a base page passed in
    if (!isset($params['bp'])) {
        throw new \Exception("No base page is supplied");
    }
    // make sure that there are global blocks passed in
    if (!isset($params['google-tag']) || !isset($params['site-storage']) || !isset($params['link-script']) || !isset($params['page-title']) || !isset($params['search-form'])) {
        throw new \Exception("No global blocks are supplied");
    }
    // pages to skip
    if (isset($params['skip'])) {
        $skip = $params['skip'];
    }
    // skip whatever that is supposed to be skipped
    if (isset($skip) && is_array($skip) && in_array($child->getPathPath(), $skip)) {
        return;
    }
    // retrieve all required assets
    $ct = $params['ct'];
    $bp = $params['bp'];
    $google_tag = $params['google-tag'];
    $site_storage = $params['site-storage'];
    $link_script = $params['link-script'];
    $page_title = $params['page-title'];
    $search_form = $params['search-form'];
    // can run into phantom nodes
    try {
        // retrieve page to be processed
        $p = $child->getAsset($service);
        // get number of content-group
        $identifier = "content-group;0";
        $num_of_groups = 1;
        // get the number of instances of the multiple node
        if ($p->hasIdentifier($identifier)) {
            $num_of_groups = $p->getNumberOfSiblings($identifier);
        }
        // get the current structured data with data
        $sd_old = $p->getStructuredData();
        if (!isset($sd_old)) {
            return;
        }
        // create enough instances of the multiple field
        if ($num_of_groups > 1) {
            $bp->createNInstancesForMultipleField($num_of_groups, $identifier);
        }
        // get the blank structured data with needed nodes
        $sd_new = clone $bp->getStructuredData();
        // roll back the base page
        if ($num_of_groups > 1) {
            $bp->createNInstancesForMultipleField(1, $identifier);
        }
        // switch the content type
        $p->setContentType($ct, false);
        // fix the data
        if (u\StringUtility::endsWith($p->getPath(), 'index')) {
            $sd_new->setText("right-column", "yes");
        } else {
            $sd_new->setText("right-column", "no");
        }
        $sd_new->setText("left-column", "yes");
        $sd_new->setText("left-column-group;left-setup", "default");
        // the H1
        echo "Title: ", $p->getMetadata()->getTitle(), BR;
        $title = $p->getMetadata()->getTitle() != "" ? $p->getMetadata()->getTitle() : "Title to be set";
        $sd_new->setText("main-content-title", $p->getMetadata()->getTitle());
        // main WYSIWYG
        $sd_new->setText("main-content-content", $sd_old->getText("main-content-content"));
        // multiple groups with choosers
        for ($i = 0; $i < $num_of_groups; $i++) {
            $identifier = "content-group;" . $i . ";content-group-chooser";
            // null the id for the upcoming round
            $block_id = NULL;
            // read the block
            if ($sd_old->hasIdentifier($identifier)) {
                $block_id = $sd_old->getBlockId($identifier);
            }
            // if there is a block attached
            if (isset($block_id) && $block_id != "") {
                $b = a\Block::getBlock($service, $block_id);
                $sd_new->setBlock($identifier, $b);
                $identifier = "content-group;" . $i . ";content-group-size";
                $size_str = strtolower($sd_old->getText($identifier));
                // fix the possible value for the new data definition
                if ($size_str != 'full') {
                    $size_str = 'half';
                }
                $sd_new->setText($identifier, $size_str);
                $identifier = "content-group;" . $i . ";content-group-block-floating";
                $sd_new->setText($identifier, strtolower($sd_old->getText($identifier)));
            }
            // WYSIWYGs in group
            $identifier = "content-group;" . $i . ";content-group-content";
            $sd_new->setText($identifier, $sd_old->getText($identifier));
        }
        // set the data
        $p->setStructuredData($sd_new);
    } catch (e\NoSuchFieldException $e) {
        $page_id = $child->getId();
        $phantom_page = new a\PagePhantom($service, $service->createId(a\PagePhantom::TYPE, $page_id));
        $dd = $phantom_page->getDataDefinition();
        $healthy_sd = new p\StructuredData($dd->getStructuredData(), $service, $dd->getId());
        $phantom_sd = $phantom_page->getStructuredDataPhantom();
        $healthy_sd = $healthy_sd->removePhantomNodes($phantom_sd);
        $phantom_page->setStructuredData($healthy_sd);
    }
    // attach the five global blocks
    $p->setBlock("site-config-group;google-tag-manager", $google_tag)->setBlock("site-config-group;site-storage", $site_storage)->setBlock("site-config-group;global-link-script", $link_script)->setBlock("site-config-group;page-title", $page_title)->setBlock("site-config-group;search-form", $search_form)->setBlock("site-config-group;global-script")->edit();
}
use cascade_ws_exception as e;
use cascade_ws_AOHS as aohs;
$start_time = time();
// Warning: this program can take a very long time to run!!!
// My run lasts for more than 6 minutes.
try {
    // to prevent time-out
    set_time_limit(10000);
    // to prevent using up memory when traversing a large site
    ini_set('memory_limit', '2048M');
    $sites = $cascade->getSites();
    //$sites   = array( new p\Child( $cascade->getAsset( a\Site::TYPE, '22q' )->getIdentifier() ) );
    $results = array();
    foreach ($sites as $site) {
        $site_name = $site->getPathPath();
        if (u\StringUtility::endsWith($site_name, "-dev") || u\StringUtility::endsWith($site_name, "-test") || u\StringUtility::endsWith($site_name, "-old") || u\StringUtility::startsWith($site_name, "_") || u\StringUtility::startsWith($site_name, "test")) {
            continue;
        }
        $cascade->getAsset(a\Folder::TYPE, '/', $site_name)->getAssetTree()->traverse(array(a\Page::TYPE => array("assetTreeCountAsset")), array($site_name), $results);
    }
    u\DebugUtility::dump($results);
    $end_time = time();
    echo "\nTotal time taken: " . ($end_time - $start_time) . " seconds\n";
} catch (\Exception $e) {
    echo S_PRE . $e . E_PRE;
    $end_time = time();
    echo "\nTotal time taken: " . ($end_time - $start_time) . " seconds\n";
}
function assetTreeCountAsset(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL)
{
    $type = $child->getType();