<commit_before><?php
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015 Spring Signage Ltd
 * (LayoutStructureStep.php)
 */


namespace Xibo\Upgrade;


use Xibo\Entity\Permission;
use Xibo\Factory\LayoutFactory;
use Xibo\Helper\Config;
use Xibo\Helper\Install;
use Xibo\Storage\PDOConnect;

class LayoutStructureStep implements Step
{
    public static function doStep()
    {
        // Create the new structure
        $dbh = PDOConnect::init();

        // Run the SQL to create the necessary tables
        $statements = Install::remove_remarks(self::$dbStructure);
        $statements = Install::split_sql_file($statements, ';');

        foreach ($statements as $sql) {
            $dbh->exec($sql);
        }

        // Build a keyed array of existing widget permissions
        $mediaPermissions = [];
        foreach (PDOConnect::select('SELECT groupId, layoutId, regionId, mediaId, `view`, `edit`, `del` FROM `lklayoutmediagroup`', []) as $row) {
            $permission = new Permission();
            $permission->entityId = 6; // Widget
            $permission->groupId = $row['groupId'];
            $permission->objectId = $row['layoutId'];
            $permission->objectIdString = $row['regionId'];
            $permission->view = $row['view'];
            $permission->edit = $row['edit'];
            $permission->delete = $row['del'];

            $mediaPermissions[$row['mediaId']] = $permission;
        }

        // Build a keyed array of existing region permissions
        $regionPermissions = [];
        foreach (PDOConnect::select('SELECT groupId, layoutId, regionId, `view`, `edit`, `del` FROM `lklayoutregiongroup`', []) as $row) {
            $permission = new Permission();
            $permission->entityId = 7; // Widget
            $permission->groupId = $row['groupId'];
            $permission->objectId = $row['layoutId'];
            $permission->view = $row['view'];
            $permission->edit = $row['edit'];
            $permission->delete = $row['del'];

            $regionPermissions[$row['regionId']] = $permission;
        }

        // Get the library location to store backups of existing XLF
        $libraryLocation = Config::GetSetting('LIBRARY_LOCATION');

        // We need to go through each layout, save the XLF as a backup in the library and then upgrade it.
        foreach (PDOConnect::select('SELECT layoutId, xml FROM `layout` WHERE IFNULL(xml, \'\') <> \'\'', []) as $oldLayout) {
            // Save off a copy of the XML in the library
            file_put_contents($libraryLocation . 'oldXlf_' . $oldLayout['layoutId'], $oldLayout['xml']);

            // Create a new layout from the XML
            $layout = LayoutFactory::loadByXlf($oldLayout['xml'], LayoutFactory::getById($oldLayout['layoutId']));

            // Save the layout
            $layout->save(['notify' => false]);

            // Now that we have new ID's we need to cross reference them with the old IDs and recreate the permissions
            foreach ($layout->regions as $region) {
                /* @var \Xibo\Entity\Region $region */
                if (array_key_exists($region->tempId, $regionPermissions)) {
                    $permission = $regionPermissions[$region->tempId];
                    /* @var \Xibo\Entity\Permission $permission */
                    // Double check we are for the same layout
                    if ($permission->objectId == $layout->layoutId) {
                        $permission = clone $permission;
                        $permission->objectId = $region->regionId;
                        $permission->save();
                    }
                }<commit_msg>Change the format for archived XLF files (so they are easy to pick out)<commit_after><?php
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015 Spring Signage Ltd
 * (LayoutStructureStep.php)
 */


namespace Xibo\Upgrade;


use Xibo\Entity\Permission;
use Xibo\Factory\LayoutFactory;
use Xibo\Helper\Config;
use Xibo\Helper\Install;
use Xibo\Storage\PDOConnect;

class LayoutStructureStep implements Step
{
    public static function doStep()
    {
        // Create the new structure
        $dbh = PDOConnect::init();

        // Run the SQL to create the necessary tables
        $statements = Install::remove_remarks(self::$dbStructure);
        $statements = Install::split_sql_file($statements, ';');

        foreach ($statements as $sql) {
            $dbh->exec($sql);
        }

        // Build a keyed array of existing widget permissions
        $mediaPermissions = [];
        foreach (PDOConnect::select('SELECT groupId, layoutId, regionId, mediaId, `view`, `edit`, `del` FROM `lklayoutmediagroup`', []) as $row) {
            $permission = new Permission();
            $permission->entityId = 6; // Widget
            $permission->groupId = $row['groupId'];
            $permission->objectId = $row['layoutId'];
            $permission->objectIdString = $row['regionId'];
            $permission->view = $row['view'];
            $permission->edit = $row['edit'];
            $permission->delete = $row['del'];

            $mediaPermissions[$row['mediaId']] = $permission;
        }

        // Build a keyed array of existing region permissions
        $regionPermissions = [];
        foreach (PDOConnect::select('SELECT groupId, layoutId, regionId, `view`, `edit`, `del` FROM `lklayoutregiongroup`', []) as $row) {
            $permission = new Permission();
            $permission->entityId = 7; // Widget
            $permission->groupId = $row['groupId'];
            $permission->objectId = $row['layoutId'];
            $permission->view = $row['view'];
            $permission->edit = $row['edit'];
            $permission->delete = $row['del'];

            $regionPermissions[$row['regionId']] = $permission;
        }

        // Get the library location to store backups of existing XLF
        $libraryLocation = Config::GetSetting('LIBRARY_LOCATION');

        // We need to go through each layout, save the XLF as a backup in the library and then upgrade it.
        foreach (PDOConnect::select('SELECT layoutId, xml FROM `layout` WHERE IFNULL(xml, \'\') <> \'\'', []) as $oldLayout) {
            // Save off a copy of the XML in the library
            file_put_contents($libraryLocation . 'archive_' . $oldLayout['layoutId'] . '.xlf', $oldLayout['xml']);

            // Create a new layout from the XML
            $layout = LayoutFactory::loadByXlf($oldLayout['xml'], LayoutFactory::getById($oldLayout['layoutId']));

            // Save the layout
            $layout->save(['notify' => false]);

            // Now that we have new ID's we need to cross reference them with the old IDs and recreate the permissions
            foreach ($layout->regions as $region) {
                /* @var \Xibo\Entity\Region $region */
                if (array_key_exists($region->tempId, $regionPermissions)) {
                    $permission = $regionPermissions[$region->tempId];
                    /* @var \Xibo\Entity\Permission $permission */
                    // Double check we are for the same layout
                    if ($permission->objectId == $layout->layoutId) {
                        $permission = clone $permission;
                        $permission->objectId = $region->regionId;
                        $permission->save();
                    }
                }
{'fraction_non_alphanumeric': 0.13500404203718674, 'fraction_numerical': 0.0016168148746968471, 'mean_word_length': 2.25284837861525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5903778', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1898, 'n_words': 532}