= ? AND rd.resource < ?", [ "i",$fref, "i",$resourcestart, "i",($resourcestart + $chunksize), ] ); if (count($rows) == 0) { // Nothing for this batch of resources $resourcestart = $resourcestart + $chunksize; continue; } // Process in smaller chunks for inserts $rowbatches = array_chunk($rows, 2000); // Get current nodes for this batch of resources $batchresources = array_column($rows, "resource"); $max = max($batchresources); $min = min($batchresources); $resnodes = ps_query("SELECT rn.resource, rn.node FROM resource_node rn LEFT JOIN node n ON n.ref=rn.node WHERE rn.resource >= '" . $min . "' AND rn.resource <= '" . $max . "' AND n.resource_type_field = ?", ["i",$fref]); $resnodearr = []; foreach ($resnodes as $resnode) { $resnodearr[$resnode["resource"]][] = $resnode["node"]; } for ($n = 0; $n < count($rowbatches); $n++) { if (isset($endtime) && time() > $endtime) { logScript("Time limit reached, exiting\n"); break 3; } db_begin_transaction("populate_nodes_from_data"); foreach ($rowbatches[$n] as $rowdata) { if (trim($rowdata["value"]) != "") { $datahash = md5($rowdata["value"]) . md5('!' . $rowdata["value"]); if (isset($allfieldnodes[$datahash])) { $newnode = $allfieldnodes[$datahash]; } else { // Not using set_node() here as that will reindex node. // The existing data from resource_keyword can be used instead to speed things up $addnodequery = "INSERT INTO `node` (`resource_type_field`, `name`, `parent`, `order_by`) VALUES (?, ?, NULL, 0)"; $parameters = array ( "i",$fref, "s",$rowdata["value"], ); ps_query($addnodequery, $parameters); $newnode = sql_insert_id(); $copykeywordquery = "INSERT INTO node_keyword (node, keyword, position) SELECT ?, keyword, position FROM resource_keyword WHERE resource = ? AND resource_type_field = ?"; $copykeywordparams = ["i",$newnode,"i",$rowdata["resource"],"i", $fref]; ps_query($copykeywordquery, $copykeywordparams); $allfieldnodes[md5($rowdata["value"]) . md5('!' . $rowdata["value"])] = $newnode; } if (!isset($resnodearr[$rowdata["resource"]]) || !in_array($newnode, $resnodearr[$rowdata["resource"]])) { logScript("Updating resource " . $rowdata["resource"] . ", field #" . $fref . " (" . $fname . ") with node " . $newnode . " (" . str_replace("\n", " ", mb_strcut($rowdata["value"], 0, 30)) . "...)"); // Not using add_resource_nodes() here to speed things up - action doesn't need to be logged ps_query("INSERT INTO resource_node(resource, node) VALUES (?,?)", ["i",$rowdata["resource"],"i",$newnode]); $resnodearr[$rowdata["resource"]][] = $newnode; } else { logScript("Skipping, correct node already set for resource - " . $rowdata["resource"] . ", field #" . $fref . " (" . $fname . ") node# " . $newnode . " (" . str_replace("\n", " ", mb_strcut($rowdata["value"], 0, 30)) . "...)"); } } $processed++; } db_end_transaction("populate_nodes_from_data"); $out = " - processed " . $processed . "/" . $totalrows . " records for field # " . $fref; logScript(str_pad($out, 100, ' ')); ob_flush(); } $resourcestart = $resourcestart + $chunksize; } $out = sprintf(" - Completed $processed records in %01.2f seconds.\n", microtime(true) - $global_start_time); logScript(str_pad($out, 100, ' ')); } echo "Finished\n\n";