<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_Xml_Import_Route extends vB_Xml_Import { protected function import($onlyGuid = []) { $routeTable = $this->db->fetchTableStructure('routenew'); $routeTableColumns = array_diff($routeTable['structure'], ['arguments', 'contentid', $routeTable['key']]); if (empty($this->parsedXML['route'])) { $this->parsedXML['route'] = []; } if (is_string($onlyGuid)) { $onlyGuid = [$onlyGuid]; } $routes = vB_XML_Parser::getList($this->parsedXML, 'route'); $redirects = []; foreach ($routes AS $route) { $route['arguments'] = $route['arguments'] ?? ''; if (!empty($onlyGuid) AND !in_array($route['guid'], $onlyGuid, true)) { continue; } $values = []; foreach($routeTableColumns AS $col) { if (isset($route[$col])) { $values[$col] = $route[$col]; } } if (isset($values['redirect301'])) { $redirects[$route['guid']] = $values['redirect301']; unset($values['redirect301']); } if (!isset($route['class'])) { $values['class'] = ''; } $condition = array('guid' => $route['guid']); $existing = $this->db->getRow('routenew', $condition); if ($existing AND !empty($existing['routeid'])) { $routeid = $existing['routeid']; } else { $class = (!empty($route['class']) AND class_exists($route['class'])) ? $route['class'] : vB5_Route::DEFAULT_CLASS; $isempty = (trim($route['arguments']) == ''); if($isempty) { $arguments = []; } else { $arguments = vb_unserialize($route['arguments']); if($arguments === false) { throw new Exception ('Invalid arguments: ' . $route['arguments']); } } $newarguments = [$class, 'importArguments']($arguments); if(!$isempty OR count($newarguments)) { $values['arguments'] = serialize($newarguments); } else { $values['arguments'] = ''; } $values['contentid'] = [$class, 'importContentId']($newarguments); $testRegex = str_replace('{{DEDUPE-INSERT-MARKER}}', '', $values['regex']); $dupeCheck = $this->db->getRow('routenew', array('regex' => $testRegex)); if (!empty($dupeCheck)) { if (strpos($values['regex'], '{{DEDUPE-INSERT-MARKER}}') !== false) { $i = 1; while(true) { $testRegex = str_replace('{{DEDUPE-INSERT-MARKER}}', '-' . $i, $values['regex']); $dupeCheck = $this->db->getRow('routenew', ['regex' => $testRegex]); if (empty($dupeCheck)) { $values['regex'] = $testRegex; $values['prefix'] = str_replace('{{DEDUPE-INSERT-MARKER}}', '-' . $i, $values['prefix']); break; } if ($i > 1000) { throw new Exception('Unable to rename route regex for ' . $values['guid'] . '. Possible infinite loop.'); } ++$i; } } else { } } $values['regex'] = str_replace('{{DEDUPE-INSERT-MARKER}}', '', $values['regex']); $values['prefix'] = str_replace('{{DEDUPE-INSERT-MARKER}}', '', $values['prefix']); $routeid = $this->db->insertIgnore('routenew', $values); if (is_array($routeid)) { $routeid = array_pop($routeid); } } vB_Xml_Import::setImportedId(vB_Xml_Import::TYPE_ROUTE, $route['guid'], $routeid); } if (count($redirects)) { $map = array(); $routes = $this->db->select('routenew', array('guid' => $redirects), array('routeid', 'guid')); foreach ($routes AS $route) { $map[$route['guid']] = $route['routeid']; } foreach($redirects AS $source => $dest) { if (isset($map[$dest])) { $this->db->update('routenew', array('redirect301' => $map[$dest]), array('guid' => $source)); } else { throw new Exception("Could not find redirect route '$dest' for route '$source'"); } } } } } 