$message, 'progress' => $progress); $output['url'] = $url; echo "id: " . json_encode(time()) . PHP_EOL; echo "data: " . json_encode($output) . PHP_EOL; echo PHP_EOL; // Added to force flush as finding a way to do this seems to have varied results echo str_pad('', 4096) . PHP_EOL; ob_flush(); flush(); } if (getval("submit", "") != "" || $command_line) { ob_start(); $valid_fields = ps_array("SELECT ref value FROM resource_type_field WHERE type IN (" . ps_param_insert(count($FIXED_LIST_FIELD_TYPES)) . ")", ps_param_fill($FIXED_LIST_FIELD_TYPES, 'i')); $messages = array(); if ($showprogress) { header('Content-Type: text/event-stream'); } if (!in_array($migrate_field, $valid_fields)) { $messages[] = "Invalid field specified. Only fixed type field types can be specified"; } $migrated = 0; $lastcompletion = 0; $completion = 0; $now = date(time()); // Set up logging if ($command_line) { $logfile = get_temp_dir(false, '') . "/migrate-data_" . md5($now . $scramble_key) . ".txt"; $logurl = $baseurl . "/pages/download.php?tempfile=migrate-data_" . $now . ".txt"; echo "Script started at " . date("Y-m-d H:i", time()) . PHP_EOL; echo "Migrating data from text field '" . $field_info["title"] . "' ID #" . $migrate_field . PHP_EOL; } else { $logfile = get_temp_dir(false, '') . "/migrate-data_" . $userref . "_" . md5($username . $now . $scramble_key) . ".txt"; $logurl = $baseurl . "/pages/download.php?tempfile=migrate-data_" . $userref . "_" . $now . ".txt"; } $fp = fopen($logfile, 'a'); fwrite($fp, "
Script started at " . date("Y-m-d H:i", time()) . PHP_EOL);
    fwrite($fp, "Migrating data from text field '" . $field_info["title"] . "' ID #" . $migrate_field . PHP_EOL);
    fclose($fp);

    $chunksize = 1000;
    $nodeinfo = ps_query("SELECT MAX(ref) maxref, MIN(ref) minref, count(*) count FROM node WHERE resource_type_field = ?", array("i",$migrate_field), 0);

    $total = $nodeinfo[0]["count"];
    $minref = $nodeinfo[0]["minref"];
    $maxref = $nodeinfo[0]["maxref"];
    $newnoderef = $maxref + 1;
    $deletenodes = [];

    // Get existing nodes
    $existing_nodes = get_nodes($migrate_field, null, true);

    while ($migrated < $total && ($maxrows == 0 || $migrated < $maxrows)) {
        $nodedata = ps_query(
            "SELECT n.ref, n.`name`,
                GROUP_CONCAT(rn.resource)  AS resources
                FROM node n 
                LEFT JOIN resource_node  rn ON n.ref=rn.node
                WHERE resource_type_field = ?
                AND ref >= ?
                GROUP BY n.ref
                ORDER BY n.ref ASC
                LIMIT ?",
            ['i', $migrate_field, 'i', $minref, 'i', $chunksize]
        );

        // Process each data row
        foreach ($nodedata as $node) {
            $deletenodes[] = $node["ref"];
            if (trim($node['name']) == '' || strpos($node['name'], $splitvalue) === false || ($maxrows != 0 && $migrated >= $maxrows)) {
                $minref = $node["ref"];
                $migrated++;
                continue;
            }

            $logtext = "";
            $nodes_to_add = [];
            $resources = explode(",", $node["resources"]);
            $nodename = $node["name"];
            $logtext .= ($dryrun ? "TESTING: " : "") . "Checking data for node id #" . $node["ref"] . ". Value: '" . $nodename . "'" . PHP_EOL;

            $arr_newvals = explode($splitvalue, $nodename);

            foreach ($arr_newvals as $newvalue) {
                // Skip if this value is empty (e.g if users left a separator at the end of the value by mistake)
                $newvalue = trim($newvalue);
                if ($newvalue == '') {
                    continue;
                }
                $nodeidx = array_search($newvalue, array_column($existing_nodes, "name"));

                if ($nodeidx !== false) {
                    $logtext .= ($dryrun ? "TESTING: " : "") . " - Found matching field node option. ref:" . $existing_nodes[$nodeidx]["ref"] . PHP_EOL;
                    $nodes_to_add[] = $existing_nodes[$nodeidx]["ref"];
                } else {
                    if (!$dryrun) {
                        $newnode = set_node(null, $migrate_field, $newvalue, null, '');
                        $newnodecounter = count($existing_nodes);
                        $logtext .= " - New option added for '" . escape($newvalue) . "' - ref: " . $newnode . PHP_EOL;
                        $nodes_to_add[] = $newnode;
                        $existing_nodes[$newnodecounter]["ref"] = $newnode;
                        $existing_nodes[$newnodecounter]["name"] = $newvalue;
                    } else {
                        $newnode = $newnoderef;
                        $logtext .= " - Added node for '" . escape($newvalue) . "' - ref: " . $newnode . PHP_EOL;
                        $newnodecounter = count($existing_nodes);
                        $nodes_to_add[] = $newnode;
                        $existing_nodes[$newnodecounter]["ref"] = $newnoderef;
                        $existing_nodes[$newnodecounter]["name"] = $newvalue;
                        $newnoderef++;
                    }
                }
            }

            if (count($nodes_to_add) > 0) {
                $logtext .= ($dryrun ? "TESTING: " : "") . "Adding nodes to resource IDs " . $node["resources"] . ": (" . implode(",", $nodes_to_add) . ")" . PHP_EOL;
                if (!$dryrun) {
                    add_resource_nodes_multi($resources, $nodes_to_add);
                    delete_resource_nodes_multi($resources, [$node["ref"]]);
                }
            }

            if ($deletedata) {
                $logtext = ($dryrun ? "TESTING: " : "") . "Deleting unused node# " . $node["ref"] . PHP_EOL;
                if (!$dryrun) {
                    delete_node($node["ref"]);
                }
            }

            $migrated++;
            $minref = $node["ref"];

            $completion = ($maxrows == 0) ? floor($migrated / $total * 100) : floor($migrated / $maxrows * 100);
            if ($showprogress && $lastcompletion != $completion) {
                send_event_update("Node " . $migrated . "/" . $total . PHP_EOL, $completion, $logurl);
                $lastcompletion = $completion;
            }

            // Update log
            $fp = fopen($logfile, 'a');
            fwrite($fp, $logtext);
            fclose($fp);
            if ($command_line) {
                echo $logtext . PHP_EOL;
            }

            if (connection_aborted() != 0) {
                $logtext = ($dryrun ? "TESTING: " : "") . " Connection aborted" . PHP_EOL;
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logtext);
                fclose($fp);
                if ($command_line) {
                    echo $logtext . PHP_EOL;
                }
                exit();
            }
        }

        if (connection_aborted() != 0) {
            $logtext = ($dryrun ? "TESTING: " : "") . " Connection aborted" . PHP_EOL;
            $fp = fopen($logfile, 'a');
            fwrite($fp, $logtext);
            fclose($fp);
            if ($command_line) {
                echo $logtext . PHP_EOL;
            }
            exit();
        }
    }

    $logtext = "Completed at " . date("Y-m-d H:i", time()) . ". " . $total . " rows migrated" . PHP_EOL;
    // Update log
    $fp = fopen($logfile, 'a');
    fwrite($fp, $logtext);
    fclose($fp);
    if ($command_line) {
        echo $logtext . PHP_EOL;
    }

    $completemessage = ($dryrun ? "TESTING: " : "") . "Completed at " . date("Y-m-d H:i", time()) . ". " . $migrated . " rows migrated out of " . $total . "
"; if ($command_line) { echo "DONE" . PHP_EOL; } else { // Send a message to the user message_add($userref, $lang["admin_resource_type_field_migrate_data"] . ": " . $completemessage, $logurl); // Always send the completion event if ($showprogress) { send_event_update($completemessage . PHP_EOL, "100", $logurl); } else { echo json_encode(array("message" => $completemessage,"url" => $logurl)); } } exit(); } include_once "../../include/header.php"; ?>

'); return false; }; start_task(this); return false;">
0%
" onclick="document.getElementById('submitinput').value='true';">